BmnRoot 26.10.0-18-g4e3414f9
Software framework of the BM@N experiment
Loading...
Searching...
No Matches
show_run_stats.C
Go to the documentation of this file.
1// limits to define out-of-range values and to exclude them while calculating the averages
2#define MIN_RUN_DUR_SEC 6
3#define MAX_RUN_DUR_HOUR 3
4#define MIN_EVENT_RATE 100
5#define MAX_EVENT_RATE 3600
6
8int fill_run_diff_hist(TH1D*& hist_run_diff, int period, int start_run, int end_run);
9
10/**
11 * Function for creating statistics histograms on run duration and average stream (event/sec)
12 * for runs stored in the UniConDa Database for a specified Run/Period (or run interval)
13 */
14void show_run_stats(int period = 8, int start_run = 6587, int end_run = -1, bool show_only_wrong = false)
15{
16 UniqueRunNumber* run_numbers;
17 int run_count = UniRun::GetRunNumbers(run_numbers, period, start_run, period, end_run);
18 if (run_count <= 0)
19 return;
20
21 // bool isTerminal = isatty(fileno(stdout));
22 const string red("\033[0;91m"), orange("\033[0;33m"), cyan("\033[0;96m"), reset("\033[0m");
23
24 TH1D* hist_run_duration = new TH1D("hist_run_duration", TString::Format("Run duration for Period %d", period),
26 TAxis* xaxis_run_duration = hist_run_duration->GetXaxis();
27 xaxis_run_duration->SetTitle("Run Duration, min");
28
29 TH1D* hist_stream_ev = new TH1D("hist_stream_ev", TString::Format("Event count per second for Period %d", period),
31 TAxis* xaxis_stream_ev = hist_stream_ev->GetXaxis();
32 xaxis_stream_ev->SetTitle("Event stream, event/sec");
33
34 int correct_run_count = 0;
35 int64_t sum_run_sec = 0, sum_event_count = 0;
36 map<int, int> wrong_time;
37 for (int i = 0; i < run_count; i++) {
38 // get the current run
39 UniRun* pRun = UniRun::GetRun(run_numbers[i].period_number, run_numbers[i].run_number);
40 if (pRun == nullptr) {
41 cout << "The function encountered with errors: no experimental run was found ("
42 << run_numbers[i].period_number << ":" << run_numbers[i].run_number << "). This run will be skipped!"
43 << endl;
44 continue;
45 }
46
47 bool is_run_wrong = false;
48 // get run duration and event count
49 TDatime run_start_time = pRun->GetStartDatetime();
50 TDatime* run_end_time = pRun->GetEndDatetime();
51 if (run_end_time == nullptr) {
52 cout << "The function encountered with errors: end time of the run was not defined for "
53 << run_numbers[i].period_number << ":" << run_numbers[i].run_number << endl;
54 delete pRun;
55 continue;
56 }
57 UInt_t run_duration_sec = 0, end_seconds = (*run_end_time).Convert(), start_seconds = run_start_time.Convert();
58 if (end_seconds < start_seconds) {
59 is_run_wrong = true;
60 run_duration_sec = start_seconds - end_seconds;
61 wrong_time.insert(pair<int, int>(run_numbers[i].run_number, -1 * run_duration_sec));
62 } else
63 run_duration_sec = end_seconds - start_seconds;
64
65 int* p_event_count = pRun->GetEventCount();
66 delete pRun;
67 if (p_event_count == nullptr) {
68 cout << "The function encountered with errors: event count was not defined for "
69 << run_numbers[i].period_number << ":" << run_numbers[i].run_number << endl;
70 continue;
71 }
72 int event_count = *p_event_count;
73
74 double events_per_second = event_count / (double)run_duration_sec;
75
76 // do not take into account runs with duration < MIN_RUN_DUR_SEC or > MAX_RUN_DUR_HOUR
77 if ((run_duration_sec < MIN_RUN_DUR_SEC) || (run_duration_sec / 3600 > MAX_RUN_DUR_HOUR)) {
78 is_run_wrong = true;
79 wrong_time.insert(pair<int, int>(run_numbers[i].run_number, run_duration_sec));
80 } else {
81 sum_run_sec += run_duration_sec;
82 sum_event_count += event_count;
83 correct_run_count++;
84 }
85
86 if (is_run_wrong)
87 cout << red;
88 if (!(show_only_wrong && !is_run_wrong)) {
89 cout << "Current run " << run_numbers[i].period_number << "-" << run_numbers[i].run_number << ":" << endl;
90 cout << "Duration: " << run_duration_sec << " sec" << endl;
91 cout << "Events/second: " << events_per_second << endl << endl;
92 }
93 if (is_run_wrong)
94 cout << reset;
95
96 hist_run_duration->Fill(run_duration_sec / 60.0);
97 hist_stream_ev->Fill(events_per_second);
98 }
99
100 double average_run_duration = sum_run_sec / (double)correct_run_count;
101 double average_event_stream = sum_event_count / (double)sum_run_sec;
102 cout << cyan << "-------------------------------------------------------" << endl;
103 cout << "Average run duration for period " << period << ": " << round(average_run_duration) << " sec ("
104 << TString::Format("%.2f", average_run_duration / 60) << " min)" << endl;
105 cout << "Average event stream for period " << period << ": " << round(average_event_stream) << " event/sec" << endl;
106 cout << "-------------------------------------------------------" << endl << reset;
107
108 if (wrong_time.size() > 0) {
109 cout << endl << "Wrong run duration:" << endl;
110 for (auto it = wrong_time.begin(); it != wrong_time.end(); ++it)
111 cout << "Run " << period << "-" << (*it).first << ": " << (*it).second << " seconds" << endl;
112 cout << red;
113 cout << "Total wrong run duration: " << wrong_time.size() << endl;
114 cout << reset;
115 }
116 cout << endl;
117
118 delete run_numbers;
119
120 TCanvas* canvas_run = new TCanvas("canvas_run", "canvas_run", 1200, 640);
121 canvas_run->Divide(2, 1);
122 canvas_run->cd(1);
123 hist_run_duration->Draw();
124 canvas_run->cd(2);
125 hist_stream_ev->Draw();
126
127 TCanvas* canvas_run_diff = new TCanvas("canvas_run_diff", "canvas_run_diff");
128 TH1D* hist_run_diff =
129 new TH1D("hist_run_diff", TString::Format("Run time gap for Period %d", period), 1000, 0, 3 * 3600);
130 TAxis* xaxis_run_diff = hist_run_diff->GetXaxis();
131 xaxis_run_diff->SetTitle("Run Time Gaps, sec");
132 fill_run_diff_hist(hist_run_diff, period, start_run, end_run);
133 canvas_run_diff->cd();
134 hist_run_diff->Draw();
135
136 cout << "Macro finished successfully" << endl;
137}
138
139int fill_run_diff_hist(TH1D*& hist_run_diff, int period, int start_run, int end_run)
140{
142 if (connDb == nullptr)
143 return -1;
144
145 TSQLServer* db_server = connDb->GetSQLServer();
146
147 TString sql = TString::Format("select r1.run_number, EXTRACT(EPOCH FROM (r2.start_datetime - r1.end_datetime)) "
148 "from run_ r1 cross join lateral (select start_datetime from run_ rin where "
149 "r1.period_number = rin.period_number and r1.run_number+1 = rin.run_number) r2 "
150 "where period_number = %d AND run_number >= %d AND (run_number <= %d OR -1 = %d)",
151 period, start_run, end_run, end_run);
152 TSQLStatement* stmt = db_server->Statement(sql);
153
154 // get time differences for the closest runs (between runs with difference in run_numbers being equal to 1)
155 if (!stmt->Process()) {
156 cout << "ERROR: getting time differences for the closest runs from the database has been failed" << endl;
157 delete stmt;
158 delete connDb;
159 return -2;
160 }
161
162 // store result of statement in buffer
163 stmt->StoreResult();
164 while (stmt->NextResultRow()) {
166 if (connRun == nullptr) {
167 cout << "ERROR: the connection to the UniConDa Database failed" << endl;
168 delete stmt;
169 delete connDb;
170 return -3;
171 }
172
173 int diff_sec = stmt->GetInt(1);
174 if (diff_sec > 0)
175 hist_run_diff->Fill(diff_sec);
176 }
177
178 delete stmt;
179 delete connDb;
180
181 return 0;
182}
int i
Definition P4_F32vec4.h:22
unsigned int UInt_t
TVirtualPad * cd(Int_t subpadnumber=0) override
UInt_t Convert(Bool_t toGMT=kFALSE) const
TAxis * GetXaxis()
void Draw(Option_t *option="") override
virtual Int_t Fill(const char *name, Double_t w)
virtual void SetTitle(const char *title="")
void Divide(Int_t nx=1, Int_t ny=1, Float_t xmargin=0.01, Float_t ymargin=0.01, Int_t color=0) override
virtual TSQLStatement * Statement(const char *, Int_t=100)
virtual Bool_t NextResultRow()=0
virtual Int_t GetInt(Int_t)
virtual Bool_t Process()=0
virtual Bool_t StoreResult()=0
static TString Format(const char *fmt,...)
static UniConnection * Open()
TSQLServer * GetSQLServer()
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
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
#define MIN_EVENT_RATE
#define MAX_EVENT_RATE
#define MAX_RUN_DUR_HOUR
#define MIN_RUN_DUR_SEC
RVec< PromoteType< T > > round(const RVec< T > &v)
void show_run_stats(int period=8, int start_run=6587, int end_run=-1, bool show_only_wrong=false)
Function for creating statistics histograms on run duration and average stream (event/sec) for runs s...
int fill_run_diff_hist(TH1D *&hist_run_diff, int period, int start_run, int end_run)