BmnRoot 26.10.0-233-gb9bbe585
Software framework of the BM@N experiment
Loading...
Searching...
No Matches
postrun_check.C
Go to the documentation of this file.
1// file name of the raw file without the directory
3{
4 TString file_path = pRawFile->GetFilePath();
5 return gSystem->BaseName(file_path.Data());
6}
7
8// add the error of the given type to the errors of the run, with the name of the raw file for the raw file errors
9void AddRunError(map<int, vector<TString>>& run_errors, int error_type, TString file_name = "")
10{
11 vector<TString>& files = run_errors[error_type];
12 if ((file_name != "") && (find(files.begin(), files.end(), file_name) == files.end()))
13 files.push_back(file_name);
14}
15
16// list of the file names separated by commas
17TString FileList(const vector<TString>& files)
18{
19 TString file_list = "";
20 for (size_t i = 0; i < files.size(); i++)
21 file_list += (i > 0 ? ", " : "") + files[i];
22 return file_list;
23}
24
25// matching the event numbers and the event count of the raw files with the run metadata, as well as the time intervals:
26// - the event count, start and end time of the run exist, and the times of the run and its raw files are within
27// the period;
28// - the events of a run are written in parallel streams (one stream in Run 7, 2 streams in Run 8, 8 streams in Run 9),
29// every stream gets every N-th event (N - the stream count), and the stream data are split into parts (p0, p1, ...):
30// the first event of the run is 1, the first parts of the streams start with the events 1, 2, ..., N, the gap
31// between the end event of a part and the start event of the next part of the stream equals N, all streams have
32// the same number of parts, and the parts with the same index start with consecutive events in all streams;
33// - the events of a raw file follow with the step N: end event = start event + N * (event count - 1);
34// - the sum of the event counts of the raw files equals the event count of the run;
35// - the start and end times of the run equal the earliest start time and the latest end time of the raw files
36// After the summary, the runs with errors are listed with the error types and the raw files
37int check_event_and_time(int period_number, int start_run, int end_run)
38{
39 // get the given period
40 UniRunPeriod* pCurrentPeriod = UniRunPeriod::GetRunPeriod(period_number);
41 if (pCurrentPeriod == nullptr) {
42 cout << "The function encountered with errors: no experimental period was found with number " << period_number
43 << endl;
44 return -1;
45 }
46
47 TDatime period_start_time = pCurrentPeriod->GetStartDatetime();
48 TDatime* period_end_time = pCurrentPeriod->GetEndDatetime();
49 delete pCurrentPeriod;
50 if (period_end_time == nullptr) {
51 cout << "ERROR: there is no end time for Period " << period_number << ". Exiting..." << endl;
52 return -2;
53 }
54
55 UniqueRunNumber* run_numbers;
56 int run_count = UniRun::GetRunNumbers(run_numbers, period_number, start_run, period_number, end_run);
57 if (run_count <= 0) {
58 delete period_end_time;
59 return -1;
60 }
61
62 // error types and their descriptions in the summary and in the list of the runs with errors
63 enum
64 {
65 kNoEventCount,
66 kOutStartRun,
67 kNoEndTime,
68 kOutEndRun,
69 kRawReadError,
70 kNoRawFiles,
71 kOutStartRaw,
72 kOutEndRaw,
73 kFirstEvent,
74 kWrongEndEvent,
75 kEventGap,
76 kStreamStart,
77 kStreamEnd,
78 kEventSum,
79 kTimeInterval,
80 kErrorTypeCount
81 };
82 const char* error_names[kErrorTypeCount] = {"no event count of the run",
83 "start time of the run is out of the period",
84 "no end time of the run",
85 "end time of the run is out of the period",
86 "raw files were not read (database error)",
87 "no raw files",
88 "start time of a raw file is out of the period",
89 "end time of a raw file is out of the period",
90 "first event is not 1",
91 "wrong end event",
92 "wrong event gap",
93 "wrong start event",
94 "stream ends earlier",
95 "event sum of raw files differs from the run",
96 "run time differs from the raw file time"};
97 int error_run_count[kErrorTypeCount] = {}; // counts of the runs with the errors by the types
98 // runs with errors: index of the run in run_numbers and description of its errors
99 vector<pair<int, TString>> error_runs;
100 int not_found_run = 0, success_runs = 0, raw_file_count = 0;
101
102 // cycle by all runs for the given period
103 for (int i = 0; i < run_count; i++) {
104 // get the current run
105 UniRun* pCurrentRun = UniRun::GetRun(run_numbers[i].period_number, run_numbers[i].run_number);
106 if (pCurrentRun == nullptr) {
107 cout << "The function encountered with errors: no experimental run was found ("
108 << run_numbers[i].period_number << ":" << run_numbers[i].run_number << "). This run will be skipped!"
109 << endl;
110 not_found_run++;
111 error_runs.push_back({i, "run not found in UniConDa (not checked)"});
112 continue;
113 }
114 cout << endl << "Period:Run " << run_numbers[i].period_number << ":" << run_numbers[i].run_number << endl;
115
116 // errors of the current run by the types (with the raw files for the raw file errors)
117 map<int, vector<TString>> run_errors;
118
119 int* run_event_count = pCurrentRun->GetEventCount();
120 if (run_event_count == nullptr) {
121 AddRunError(run_errors, kNoEventCount);
122 cout << "ERROR: there is no event count in the run" << endl;
123 }
124
125 TDatime run_start_time = pCurrentRun->GetStartDatetime();
126 if ((run_start_time < period_start_time) || (run_start_time > (*period_end_time))) {
127 AddRunError(run_errors, kOutStartRun);
128 cout << "ERROR: the start time of the run is out of range: " << run_start_time.AsSQLString() << endl;
129 }
130 TDatime* run_end_time = pCurrentRun->GetEndDatetime();
131 if (run_end_time == nullptr) {
132 AddRunError(run_errors, kNoEndTime);
133 cout << "ERROR: there is no end time of the run" << endl;
134 } else {
135 if (((*run_end_time) < period_start_time) || ((*run_end_time) > (*period_end_time))) {
136 AddRunError(run_errors, kOutEndRun);
137 cout << "ERROR: the end time of the run is out of range: " << run_end_time->AsSQLString() << endl;
138 }
139 }
140
141 // get all raw files for the current run (ordered by the start event)
142 TObjArray* raw_files = pCurrentRun->GetRawFiles();
143 delete pCurrentRun;
144 int file_count = (raw_files == nullptr) ? 0 : raw_files->GetEntriesFast();
145 raw_file_count += file_count;
146 if (raw_files == nullptr) {
147 AddRunError(run_errors, kRawReadError);
148 cout << "ERROR: the raw files of the run were not read" << endl;
149 } else if (file_count == 0) {
150 AddRunError(run_errors, kNoRawFiles);
151 cout << "ERROR: there are no raw files in the run" << endl;
152 } else {
153 // the stream count: the first parts of all streams start with consecutive events
154 int first_event = ((UniRawFile*)raw_files->At(0))->GetStartEvent();
155 int stream_count = 1;
156 while ((stream_count < file_count)
157 && (((UniRawFile*)raw_files->At(stream_count))->GetStartEvent() == first_event + stream_count))
158 stream_count++;
159 cout << "Raw files: " << file_count << " in " << stream_count << " stream(s)" << endl;
160
161 // check the first event
162 if (first_event != 1) {
163 AddRunError(run_errors, kFirstEvent);
164 cout << "ERROR: the first event of the run is " << first_event << " instead of 1" << endl;
165 }
166
167 // check the times and the event numbers of the raw files and distribute the raw files by the streams:
168 // the stream of a raw file is defined by its start event
169 vector<vector<UniRawFile*>> streams(stream_count);
170 int sum_event_count = 0;
171 TDatime min_start_time, max_end_time;
172 for (int j = 0; j < file_count; j++) {
173 UniRawFile* pRawFile = (UniRawFile*)raw_files->At(j);
174 TDatime file_start_time = pRawFile->GetStartDatetime(), file_end_time = pRawFile->GetEndDatetime();
175 if ((j == 0) || (file_start_time < min_start_time))
176 min_start_time = file_start_time;
177 if ((j == 0) || (file_end_time > max_end_time))
178 max_end_time = file_end_time;
179
180 if ((file_start_time < period_start_time) || (file_start_time > (*period_end_time))) {
181 AddRunError(run_errors, kOutStartRaw, RawFileName(pRawFile));
182 cout << "ERROR: the start time of the raw file " << RawFileName(pRawFile)
183 << " is out of range: " << file_start_time.AsSQLString() << endl;
184 }
185 if ((file_end_time < period_start_time) || (file_end_time > (*period_end_time))) {
186 AddRunError(run_errors, kOutEndRaw, RawFileName(pRawFile));
187 cout << "ERROR: the end time of the raw file " << RawFileName(pRawFile)
188 << " is out of range: " << file_end_time.AsSQLString() << endl;
189 }
190
191 // the events of the raw file follow with the step 'stream count'
192 int file_event_count = pRawFile->GetEventCount();
193 if (file_event_count > 0) {
194 long expected_end_event = pRawFile->GetStartEvent() + (long)stream_count * (file_event_count - 1);
195 if (pRawFile->GetEndEvent() != expected_end_event) {
196 AddRunError(run_errors, kWrongEndEvent, RawFileName(pRawFile));
197 cout << "ERROR: the raw file " << RawFileName(pRawFile) << " ends with event "
198 << pRawFile->GetEndEvent() << " instead of " << expected_end_event << " (start event "
199 << pRawFile->GetStartEvent() << ", " << file_event_count << " events with step "
200 << stream_count << ")";
201 // the real step of the events, if it is integer and greater than the stream count,
202 // means that some streams are absent in the database
203 int event_range = pRawFile->GetEndEvent() - pRawFile->GetStartEvent();
204 if ((file_event_count > 1) && (event_range % (file_event_count - 1) == 0)
205 && (event_range / (file_event_count - 1) > stream_count))
206 cout << ", the events have step " << event_range / (file_event_count - 1)
207 << ": probably, not all streams are in the database";
208 cout << endl;
209 }
210 }
211
212 sum_event_count += file_event_count;
213 streams[(pRawFile->GetStartEvent() - first_event) % stream_count].push_back(pRawFile);
214 }
215
216 // check the event numbers in the streams (every stream has at least one raw file by the stream count)
217 size_t max_part_count = 0;
218 for (auto& stream : streams)
219 max_part_count = max(max_part_count, stream.size());
220 for (int k = 0; k < stream_count; k++) {
221 // the next part of the stream starts with the event 'stream count' after the end event of the
222 // previous part
223 for (size_t p = 1; p < streams[k].size(); p++) {
224 int event_gap = streams[k][p]->GetStartEvent() - streams[k][p - 1]->GetEndEvent();
225 if (event_gap != stream_count) {
226 AddRunError(run_errors, kEventGap, RawFileName(streams[k][p]));
227 cout << "ERROR: event gap between the parts of the stream is " << event_gap << " instead of "
228 << stream_count << " (" << RawFileName(streams[k][p - 1]) << " ends with event "
229 << streams[k][p - 1]->GetEndEvent() << ", " << RawFileName(streams[k][p])
230 << " starts with event " << streams[k][p]->GetStartEvent() << ")" << endl;
231 }
232 }
233
234 // the stream with fewer parts ends earlier than the other streams
235 if (streams[k].size() < max_part_count) {
236 AddRunError(run_errors, kStreamEnd, RawFileName(streams[k].back()));
237 cout << "ERROR: the stream ends after " << streams[k].size() << " of " << max_part_count
238 << " parts: the last raw file " << RawFileName(streams[k].back()) << " ends with event "
239 << streams[k].back()->GetEndEvent() << endl;
240 }
241 }
242
243 // the parts with the same index start with consecutive events in all streams: the start event of a part
244 // is compared with the same part of the first stream having it
245 for (size_t p = 0; p < max_part_count; p++) {
246 int ref_stream = -1;
247 for (int k = 0; k < stream_count; k++) {
248 if (p >= streams[k].size())
249 continue;
250 if (ref_stream < 0) {
251 ref_stream = k;
252 continue;
253 }
254 UniRawFile* pRefFile = streams[ref_stream][p];
255 int expected_event = pRefFile->GetStartEvent() + (k - ref_stream);
256 if (streams[k][p]->GetStartEvent() != expected_event) {
257 AddRunError(run_errors, kStreamStart, RawFileName(streams[k][p]));
258 cout << "ERROR: the raw file " << RawFileName(streams[k][p]) << " starts with event "
259 << streams[k][p]->GetStartEvent() << " instead of " << expected_event << " ("
260 << RawFileName(pRefFile) << " starts with event " << pRefFile->GetStartEvent() << ")"
261 << endl;
262 }
263 }
264 }
265
266 if ((run_event_count != nullptr) && ((*run_event_count) != sum_event_count)) {
267 AddRunError(run_errors, kEventSum);
268 cout << "ERROR: raw event sum is different from run event count (" << sum_event_count << " <> "
269 << *run_event_count << ")" << endl;
270 }
271 if ((run_end_time != nullptr) && ((run_start_time != min_start_time) || ((*run_end_time) != max_end_time)))
272 {
273 AddRunError(run_errors, kTimeInterval);
274 cout << "ERROR: run time interval (" << run_start_time.AsSQLString() << " - "
275 << run_end_time->AsSQLString() << ") is different from raw file time ("
276 << min_start_time.AsSQLString() << " - " << max_end_time.AsSQLString() << ")" << endl;
277 }
278 }
279
280 if (run_errors.empty()) {
281 success_runs++;
282 cout << "Event numbers and time intervals were successfully matched" << endl;
283 } else {
284 // description of the run errors: the error types and the raw files
285 TString run_description = "";
286 for (auto& run_error : run_errors) {
287 error_run_count[run_error.first]++;
288 if (run_description != "")
289 run_description += "; ";
290 run_description += error_names[run_error.first];
291 if (!run_error.second.empty())
292 run_description += ": " + FileList(run_error.second);
293 }
294 error_runs.push_back({i, run_description});
295 }
296
297 delete run_event_count;
298 delete run_end_time;
299 if (raw_files != nullptr)
300 raw_files->Delete();
301 delete raw_files;
302 } // for (int i = 0; i < run_count; i++)
303
304 // summary of the checks: only the error types with non-zero counts are shown, a run can have several errors
305 int checked_runs = run_count - not_found_run;
306 cout << endl
307 << "Summary for " << run_count << " runs (" << period_number << ":" << run_numbers[0].run_number << " - "
308 << period_number << ":" << run_numbers[run_count - 1].run_number << "):" << endl;
309 cout << TString::Format(" %-50s %6d", "checked runs", checked_runs) << endl;
310 cout << TString::Format(" %-50s %6d", "checked raw files", raw_file_count) << endl;
311 cout << TString::Format(" %-50s %6d", "successfully matched runs", success_runs) << endl;
312 cout << TString::Format(" %-50s %6d", "runs with errors", checked_runs - success_runs) << endl;
313 for (int t = 0; t < kErrorTypeCount; t++)
314 if (error_run_count[t] > 0)
315 cout << TString::Format(" %-48s %6d", error_names[t], error_run_count[t]) << endl;
316 if (not_found_run > 0)
317 cout << TString::Format(" %-50s %6d", "runs not found in UniConDa (not checked)", not_found_run) << endl;
318
319 // list of the runs with errors
320 if (!error_runs.empty()) {
321 cout << endl << "Runs with errors:" << endl;
322 for (auto& error_run : error_runs) {
323 const UniqueRunNumber& run = run_numbers[error_run.first];
324 TString run_name = TString::Format("%d:%d", run.period_number, run.run_number);
325 cout << TString::Format(" %-9s %s", run_name.Data(), error_run.second.Data()) << endl;
326 }
327 }
328 cout << endl;
329
330 delete[] run_numbers;
331 delete period_end_time;
332 return 0;
333}
334
335/**
336 * Main post run checking procedure to match run metadata and parameters of the raw files
337 **/
338int postrun_check(int period_number = 9, int start_run = 1, int end_run = 100000)
339{
340 check_event_and_time(period_number, start_run, end_run);
341
342 return 0;
343}
int i
Definition P4_F32vec4.h:22
friend F32vec4 max(const F32vec4 &a, const F32vec4 &b)
Definition P4_F32vec4.h:31
R__EXTERN TSystem * gSystem
const char * AsSQLString() const
Int_t GetEntriesFast() const
void Delete(Option_t *option="") override
TObject * At(Int_t idx) const override
const char * Data() const
static TString Format(const char *fmt,...)
/home/soul/bmnroot/database/uni_db1/UniRawFile.h Class for the table: raw_file
Definition UniRawFile.h:19
int GetEventCount()
get event count of the current raw file
Definition UniRawFile.h:112
TString GetFilePath()
get file path of the current raw file
Definition UniRawFile.h:110
TDatime GetStartDatetime()
get start datetime of the current raw file
Definition UniRawFile.h:106
TDatime GetEndDatetime()
get end datetime of the current raw file
Definition UniRawFile.h:108
int GetEndEvent()
get end event of the current raw file
Definition UniRawFile.h:104
int GetStartEvent()
get start event of the current raw file
Definition UniRawFile.h:102
UniRunPeriod.h Class for the table: run_period.
TDatime * GetEndDatetime()
get end datetime of the current run period
static UniRunPeriod * GetRunPeriod(int period_number)
get run period from the database
TDatime GetStartDatetime()
get start datetime of the current run period
UniRun.h Class for the table: run_.
Definition UniRun.h:21
static UniRun * GetRun(int period_number, int run_number)
get run from the database
Definition UniRun.cxx:161
TObjArray * GetRawFiles()
get array of raw files (UniRawFile*) corresponding to the run (return nullptr if an error occured)
Definition UniRun.cxx:798
TDatime * GetEndDatetime()
get end datetime of the current run
Definition UniRun.h:101
TDatime GetStartDatetime()
get start datetime of the current run
Definition UniRun.h:99
static int GetRunNumbers(UniqueRunNumber *&run_numbers, int start_period, int start_run=0, int end_period=-1, int end_run=-1)
get numbers of runs stored in the Database for a selected range
Definition UniRun.cxx:804
int * GetEventCount()
get event count of the current run
Definition UniRun.h:129
int check_event_and_time(int period_number, int start_run, int end_run)
TString FileList(const vector< TString > &files)
int postrun_check(int period_number=9, int start_run=1, int end_run=100000)
Main post run checking procedure to match run metadata and parameters of the raw files.
void AddRunError(map< int, vector< TString > > &run_errors, int error_type, TString file_name="")
TString RawFileName(UniRawFile *pRawFile)