BmnRoot 26.10.0-18-g4e3414f9
Software framework of the BM@N experiment
Loading...
Searching...
No Matches
write_raw_info.C
Go to the documentation of this file.
1R__ADD_INCLUDE_PATH($VMCWORKDIR)
2#include "macro/run/run_raw_bmn.C"
3
4int append_log(TString log_name, TString message);
5
6// Macro to extract information while converting raw files
7// and writing the raw file metadata to the Unified Condition Database
8// uses run_raw_bmn.C macro
9//
10// Parameters:
11// input_raw_file: full path to the raw file
12// period - period number, default: 9
13// isRewriteMeta - rewrite raw file metadata in the Condition Database if exists. default: false
14// error_log_name: full path to the log file with errors, default: "" - no log will be created
15//
16// example:
17// root 'write_raw_info.C("/ceph/bmn/run/run9/beam/run_Top_9086_ev1_p0.data", "", 9, false, "error.log")'
18void write_raw_info(TString input_raw_file,
19 UInt_t period = 9,
20 Bool_t isRewriteMeta = false,
21 TString error_log_file = "")
22{
23 // expand paths of input and output files to substitute env variables
24 gSystem->ExpandPathName(input_raw_file);
25 bool isLog = false;
26 if (error_log_file != "") {
27 isLog = true;
28 gSystem->ExpandPathName(error_log_file);
29 }
30
31 // check input raw file exist and has no zero size
32 if (gSystem->AccessPathName(input_raw_file.Data()) == true) {
33 TString message = TString::Format("ERROR: no input file was found: %s", input_raw_file.Data());
34 cout << endl << message << endl;
35 if (isLog)
36 append_log(error_log_file, message);
37 return;
38 }
39 Long_t raw_id, raw_flags, raw_modtime;
40 Long64_t raw_file_size = -1;
41 gSystem->GetPathInfo(input_raw_file.Data(), &raw_id, &raw_file_size, &raw_flags, &raw_modtime);
42 if (raw_file_size <= 0) {
43 TString message = TString::Format("ERROR: input file has zero size: %s", input_raw_file.Data());
44 cout << endl << message << endl;
45 if (isLog)
46 append_log(error_log_file, message);
47 return;
48 }
49
50 // calculate raw root file name
51 /*Int_t lastSlash = output_digi_file.Last('/');
52 TString digiPath = (lastSlash == TString::kNPOS) ? "" : TString(output_digi_file(0, lastSlash + 1));
53 TString subName = input_raw_file;
54 TPRegexp re_data(".+_(\\d+)([^/]*)\\.data");
55 if (re_data.MatchB(subName))
56 re_data.Substitute(subName, "$2");
57 else
58 subName = "";
59 TString raw_root_file = Form("%sbmn_run%d%s_raw.root", digiPath.Data(), run_number, subName.Data());*/
60
61 // 2: RAW -> RAW_ROOT converting
62 run_raw_bmn(input_raw_file, "", 0, period, 2, kTRUE);
63
64 // if need to rewrite raw file metadata (created while converting RAW -> RAW_ROOT)
65 if (isRewriteMeta) {
66 // get name of the created RAW ROOT file
67 TString raw_root_file = gRawRootFilePath;
68 if (gRawRootFilePath == "") {
69 TString message =
70 TString::Format("ERROR: decoder failed while creating a RAW ROOT file for: %s", input_raw_file.Data());
71 cout << endl << message << endl;
72 if (isLog)
73 append_log(error_log_file, message);
74 return;
75 }
76
77 // check raw root file exist and has no zero size
78 Long_t root_id, root_flags, root_modtime;
79 Long64_t root_file_size = -1;
80 gSystem->GetPathInfo(raw_root_file.Data(), &root_id, &root_file_size, &root_flags, &root_modtime);
81 if (root_file_size <= 0) {
82 TString message = TString::Format("ERROR: output file has zero size: %s", raw_root_file.Data());
83 cout << endl << message << endl;
84 if (isLog)
85 append_log(error_log_file, message);
86 return;
87 }
88
89 // open RAW ROOT file, then print and write to the Unified Condition Database
90 TChain* tree = new TChain("BMN_RAW");
91 tree->Add(raw_root_file);
92
93 // link event header to get information on the raw file
94 BmnEventHeader* fEventHeader = NULL;
95 tree->SetBranchAddress("BmnEventHeader.", &fEventHeader);
96
97 // get event count (== tree records) from the RAW ROOT file
98 Int_t event_count = tree->GetEntries();
99 if (event_count < 1) {
100 cout << endl << "ERROR: File '" << raw_root_file << "' has no events" << endl;
101 return;
102 } else {
103 // get start time and start event from the RAW ROOT file
104 tree->GetEntry(0);
105 // get period and run number from the event header
106 Int_t period_number = fEventHeader->GetPeriodId();
107 Int_t run_number = fEventHeader->GetRunId();
108 // get start and end time from the event header
109 TTimeStamp startTime = fEventHeader->GetEventTimeTS();
110 UInt_t start_event = fEventHeader->GetEventId();
111
112 // get end time and end event from the RAW ROOT file
113 tree->GetEntry(event_count - 1);
114 TTimeStamp endTime = fEventHeader->GetEventTimeTS();
115 UInt_t end_event = fEventHeader->GetEventId();
116
117 cout << "Event number interval: " << start_event << " - " << end_event << endl;
118 cout << "Event time interval: " << startTime.AsString() << " - " << endTime.AsString() << endl;
119 cout << "Event count: " << event_count << endl;
120
121 // searching for the raw file with the period_number, run_number, start_event and end_event
122 TObjArray* arrRawFiles =
123 UniRawFile::GetRawFiles(int period_number, int run_number, int start_event, int end_event);
124 if (arrRawFiles == nullptr) {
125 cout << endl << "ERROR: no raw files found to rewrite the file metadata" << endl;
126 return;
127 }
128 if (arrRawFiles->GetEntries() > 1) {
129 cout << endl << "ERROR: more than one raw files found to rewrite the file metadata" << endl;
130 delete arrRawFiles;
131 return;
132 }
133
134 UniRawFile* pRawFile = (UniRawFile*)arrRawFiles->At(0);
135
136 bool isErrors = false;
137 // setting new metadata for the input raw file
138 TDatime startDate(Int_t(startTime.GetDate(kFALSE)), Int_t(startTime.GetTime(kFALSE)));
139 TDatime endDate(Int_t(endTime.GetDate(kFALSE)), Int_t(endTime.GetTime(kFALSE)));
140 if (pRawFile->SetFilePath(input_raw_file) != 0)
141 isErrors = true;
142 if (pRawFile->SetStartDatetime(startDate) != 0)
143 isErrors = true;
144 if (pRawFile->SetEndDatetime(endDate) != 0)
145 isErrors = true;
146 if (pRawFile->SetStartEvent(start_event) != 0)
147 isErrors = true;
148 if (pRawFile->SetEndEvent(end_event) != 0)
149 isErrors = true;
150 if (pRawFile->SetEventCount(event_count) != 0)
151 isErrors = true;
152 if (pRawFile->SetFileSize(raw_file_size) != 0)
153 isErrors = true;
154 TString strFileHash = UniRawFile::CalculateFileHash(input_raw_file);
155 if (strFileHash != "") {
156 if (pRawFile->SetFileHash(&strFileHash) != 0)
157 isErrors = true;
158 } else
159 cout << "WARNING: a problem occured while file checksum calculation: " << raw_root_file << endl;
160
161 delete arrRawFiles;
162
163 if (isErrors)
164 cout << "The errors occured during raw file " << run_number << ":" << run_number << " " << start_event
165 << "-" << end_event << " info update" << endl;
166 else
167 cout << "Info for raw file " << run_number << ":" << run_number << " " << start_event << "-"
168 << end_event << " updated" << endl;
169 } // calculating and writing raw file info if event_count > 0
170 } // if (isRewriteMeta)
171}
172
173/**
174 * @brief Appends a message to a log file
175 * @param log_name Full path to the log file
176 * @param message Message to be appended to the log file
177 * @return 0 if successful, -1 if the log file could not be opened
178 */
179int append_log(TString log_name, TString message)
180{
181 // open log file
182 ofstream logFile;
183 logFile.open(log_name, ofstream::out | ofstream::app);
184 if (!logFile.is_open()) {
185 cout << "ERROR: log file could not be opened: " << log_name << endl;
186 return -1;
187 }
188
189 logFile << message << endl;
190
191 logFile.close();
192 return 0;
193}
bool Bool_t
int Int_t
long Long_t
unsigned int UInt_t
constexpr Bool_t kFALSE
long long Long64_t
constexpr Bool_t kTRUE
#define R__ADD_INCLUDE_PATH(PATH)
R__EXTERN TSystem * gSystem
UInt_t GetPeriodId()
Get the period ID for this event.
UInt_t GetEventId()
Get the event ID for this event.
TTimeStamp GetEventTimeTS()
Get the time for this event.
Int_t SetBranchAddress(const char *bname, T **add, TBranch **ptr=nullptr)
virtual Int_t Add(const char *name, Long64_t nentries=TTree::kMaxEntries)
Long64_t GetEntries() const override
Int_t GetEntry(Long64_t entry=0, Int_t getall=0) override
Int_t GetEntries() const override
TObject * At(Int_t idx) const override
const char * Data() const
static TString Format(const char *fmt,...)
UInt_t GetTime(Bool_t inUTC=kTRUE, Int_t secOffset=0, UInt_t *hour=nullptr, UInt_t *min=nullptr, UInt_t *sec=nullptr) const
const char * AsString(const Option_t *option="") const
UInt_t GetDate(Bool_t inUTC=kTRUE, Int_t secOffset=0, UInt_t *year=nullptr, UInt_t *month=nullptr, UInt_t *day=nullptr) const
/home/soul/bmnroot/database/uni_db1/UniRawFile.h Class for the table: raw_file
Definition UniRawFile.h:19
int SetFileSize(int64_t file_size)
set file size of the current raw file
int SetEndDatetime(TDatime end_datetime)
set end datetime of the current raw file
int SetFileHash(TString *file_hash)
set file hash of the current raw file
int SetEventCount(int event_count)
set event count of the current raw file
int SetStartDatetime(TDatime start_datetime)
set start datetime of the current raw file
int SetStartEvent(int start_event)
set start event of the current raw file
int SetEndEvent(int end_event)
set end event of the current raw file
int SetFilePath(TString file_path)
set file path of the current raw file
static TObjArray * GetRawFiles(int period_number, int run_number)
get array of raw files (UniRawFile*) corresponding to the period:run number ordered by start_event as...
static TString CalculateFileHash(TString file_path)
calculate and return (in hex string format) cheсksum for a given file use Adler32 algorithm if zlib l...
void write_raw_info(TString input_raw_file, UInt_t period=9, Bool_t isRewriteMeta=false, TString error_log_file="")
int append_log(TString log_name, TString message)
Appends a message to a log file.