BmnRoot 26.10.0-18-g4e3414f9
Software framework of the BM@N experiment
Loading...
Searching...
No Matches
write_emd.C
Go to the documentation of this file.
1R__ADD_INCLUDE_PATH($VMCWORKDIR)
2R__LOAD_LIBRARY(libcurl)
3#include "database/event_db/event_db_settings.h"
4
5#include <curl/curl.h>
6#include <services/nlohmann/json.hpp>
7using json = nlohmann::ordered_json;
8// namespace json = nlohmann::json;
9
10int write_emd_2db(TString json_path);
11// function for recursively searching for DST ROOT files and reading them to write their event metadata to the Event
12// Catalogue returns the success code: 0 - without errors, error_code - otherwise
13int write_emd(TString dir_name, TString storage_name, TString software_version, bool isWriteEventDB = false)
14{
15 gSystem->ExpandPathName(dir_name);
16
17 TSystemDirectory dir(dir_name.Data(), dir_name.Data());
18 TList* file_list = dir.GetListOfFiles();
19 if (file_list == nullptr) {
20 cout << "WARNING: There are no files in the directory: " << dir_name << endl;
21 return 0;
22 }
23
24 int total_parsed_events = 0, added_events = 0, total_files = 0, parsed_files = 0, added_files = 0;
25 TSystemFile* cur_file;
26 TIter iterFile(file_list);
27 while ((cur_file = (TSystemFile*)iterFile.Next())) {
28 TString file_name = cur_file->GetName();
29 if ((file_name == ".") || (file_name == ".."))
30 continue;
31
32 TString file_dir = cur_file->GetTitle();
33 if (cur_file->IsDirectory()) // if directory
34 {
35 write_emd(file_dir, storage_name, software_version);
36 } else {
37 total_files++;
38 TString file_path = file_dir;
39 if (file_path[file_path.Length() - 1] != '/')
40 file_path += "/";
41 file_path += file_name;
42 if (file_name.EndsWith(".root")) {
43 // open DST ROOT file, then print and write event metadata to the Event Catalogue
44 TFile dst_file(file_path);
45 TTree* dst_tree = (TTree*)dst_file.Get("bmndata");
46 if (!dst_tree) {
47 cout << "WARNING: the current file has no 'bmndata' tree: " << file_path << endl;
48 continue;
49 }
50 cout << "Current file: " << dst_tree->GetCurrentFile()->GetName() << endl;
51
52 // read Run Header if present
53 DstRunHeader* run_header = (DstRunHeader*)dst_tree->GetCurrentFile()->Get("DstRunHeader");
54 int period_number = -1, run_number = -1, track_number = -1;
55 if (run_header) {
56 period_number = run_header->GetPeriodNumber();
57 run_number = run_header->GetRunNumber();
58 delete run_header;
59 } else {
60 cout << "WARNING: the current file has not DST format: " << file_path << endl;
61 continue;
62 }
63
64 // link Event Header to get event metadata
65 DstEventHeader* fEventHeader = nullptr;
66 dst_tree->SetBranchAddress("DstEventHeader.", &fEventHeader);
67
68 Int_t event_count = dst_tree->GetEntries(); // events = records - 1; (-RunHeader)
69 if (event_count < 1) {
70 cout << endl
71 << "ERROR: the current file (Run " << period_number << ":" << run_number
72 << ") has 0 events: " << file_path << endl;
73 continue;
74 }
75 parsed_files++;
76
77 json emd_json = json::array();
78 // assign TClonesArray to 'BmnGlobalTrack' branch
79 TClonesArray* globTracks = nullptr;
80 dst_tree->SetBranchAddress("BmnGlobalTrack", &globTracks);
81 dst_tree->SetBranchStatus("*", 0);
82 dst_tree->SetBranchStatus("DstEventHeader.", 1);
83 dst_tree->SetBranchStatus("BmnGlobalTrack", 1);
84 // event loop
85 int parsed_events = 0;
86 for (Int_t curEvent = 0; curEvent < dst_tree->GetEntries(); curEvent++) {
87 dst_tree->GetEntry(curEvent);
88 track_number = globTracks->GetEntriesFast();
89
90 // write event metadata
91 json emd_element;
92 emd_element["reference"]["storage_name"] = storage_name.Data();
93 emd_element["reference"]["file_path"] = file_path.Data();
94 emd_element["reference"]["event_number"] = curEvent;
95 emd_element["software_version"] = software_version.Data();
96 emd_element["period_number"] = period_number;
97 emd_element["run_number"] = run_number;
98 emd_element["parameters"]["track_number"] = track_number;
99
100 emd_json.push_back(emd_element);
101 parsed_events++;
102 }
103 total_parsed_events += parsed_events;
104
105 // write JSON to a file
106 size_t lastindex = file_path.Last('.');
107 TString json_name = file_path(0, lastindex);
108 json_name.ReplaceAll('/', '_');
109 json_name.Append(".json");
110 ofstream json_file(json_name.Data());
111 json_file << setw(4) << emd_json << endl;
112 json_file.close();
113 // string json_string = emd_json.dump(); // emd_json.get<string>(); // emd_json.dump();
114 // cout<<"json_string = "<<json_string<<endl;
115
116 if (isWriteEventDB) {
117 gSystem->ExpandPathName(json_name);
119 int ret_code = ec.AddEventInfo(json_name.Data());
120 if (ret_code == 0) {
121 added_files++;
122 added_events += parsed_events;
123 }
124 cout << endl;
125 // remove JSON file
126 gSystem->Unlink(json_name);
127 }
128 } // if (file_name.EndsWith(".root"))
129 } // if not a directory
130 } // while file list
131
132 if (total_parsed_events > 0) {
133 cout << endl << total_parsed_events << " events have been parsed in " << parsed_files << " files";
134 if (isWriteEventDB)
135 cout << " and " << added_events << " events were successfully added from " << added_files << " files";
136 cout << " (dir: " << dir_name << " with " << total_files << " files)" << endl << endl;
137 } else
138 cout << endl << "There are no DST ROOT files in the directory (dir: " << dir_name << ")" << endl << endl;
139
140 return 0;
141}
int Int_t
#define R__LOAD_LIBRARY(LIBRARY)
#define R__ADD_INCLUDE_PATH(PATH)
R__EXTERN TSystem * gSystem
Main Interface Class to work with the Event Catalogue of the experiment Throws runtime_exception if a...
int AddEventInfo(string storage_name, string file_path, int event_number, EventInfo event_metadata)
Function to add metadata for the event stored at the given location.
T * Get(const char *namecycle)
TObject * Next()
const char * GetName() const override
const char * GetTitle() const override
Int_t GetEntriesFast() const
Ssiz_t Length() const
Bool_t EndsWith(const char *pat, ECaseCompare cmp=kExact) const
const char * Data() const
Ssiz_t Last(char c) const
TString & Append(char c, Ssiz_t rep=1)
TString & ReplaceAll(const char *s1, const char *s2)
virtual TList * GetListOfFiles() const
virtual Bool_t IsDirectory(const char *dir=nullptr) const
virtual void SetBranchStatus(const char *bname, bool status=true, UInt_t *found=nullptr)
virtual Int_t GetEntry(Long64_t entry, Int_t getall=0)
TFile * GetCurrentFile() const
virtual Long64_t GetEntries() const
Int_t SetBranchAddress(const char *bname, T **add, TBranch **ptr=nullptr)
int write_emd_2db(TString json_path)
int write_emd(TString dir_name, TString storage_name, TString software_version, bool isWriteEventDB=false)
Definition write_emd.C:13
basic_json< nlohmann::ordered_map > ordered_json
specialization that maintains the insertion order of object keys
Definition json.hpp:3374