BmnRoot 26.10.0-18-g4e3414f9
Software framework of the BM@N experiment
Loading...
Searching...
No Matches
Spill.C
Go to the documentation of this file.
1#include <TCanvas.h>
2#include <TClonesArray.h>
3#include <TDatime.h>
4#include <TH1D.h>
5#include <map>
6#include <utility>
7#include <vector>
8
9using namespace std;
10/** flux ~= sum [BT * (DAQ_Busy -peds)/ (DAQ_TRigger - peds)]*/
11
12void Draw2Can(TCanvas* c, TH1* h1, TH1* h2 = nullptr)
13{
14 c->cd();
15 h1->Draw();
16 if (h2) {
17 h2->Draw("same");
18 Double_t k = max(h1->GetMaximum(), h2->GetMaximum());
19 h1->SetMaximum(1.05 * k);
20 h1->SetMinimum(0);
21 }
22}
23
24void Spill(int period = 7,
25 TString beam = "C",
26 TString target = "",
27 // TString digi_path = "/ncx/eos/nica/bmn/exp/digi/run7/20.02.0/3590-4707_BMN_Argon/",
28 TString digi_path = "/ncx/scratch1/ilnur/mini_release/",
29 TString txtfile_path = "$VMCWORKDIR/database/uni_db/macros/parse_schemes/spill_run7/SRC_Data.txt",
30 TString scheme_path = "$VMCWORKDIR/database/uni_db/macros/parse_schemes/spill_run7/spill_run7_src_full.xslt")
31{
32 // TString txtfile_path = "$VMCWORKDIR/database/uni_db/macros/parse_schemes/spill_run7/summary_corr_v2.txt",
33 // TString scheme_path =
34 // "$VMCWORKDIR/database/uni_db/macros/parse_schemes/spill_run7/spill_run7_bmn_full.xslt") {
35
36 Int_t nPedestals = 100;
37 Int_t run = 0;
38 Int_t HW = 1920;
39 Int_t HH = 1080;
40 Int_t log_shift = 6;
41 Int_t log_shift_src = -3;
42 map<TDatime, vector<Int_t>> spill_map;
43 map<Int_t, vector<Int_t>> run_map;
44 // get spill info
45 UniParser parser;
46 vector<structParseValue*> parse_values;
47 vector<structParseSchema> vecElements;
48 int res_code = parser.ParseTxt2Struct(txtfile_path, scheme_path, parse_values, vecElements);
49 if (res_code != 0) {
50 cout << endl << "Macro finished with errors: parser error (" << res_code << ")" << endl;
51 exit(-1);
52 }
53
54 if (parse_values.size() < 1) {
55 cout << endl << "Macro finished with errors: there are no lines to parse" << endl;
56 exit(-2);
57 }
58
59 UniqueRunNumber* run_numbers = NULL;
60 TObjArray* pRunArray = NULL;
61 int run_count = 1;
62 // select only with a given condition, e.g. target particle
63 TObjArray arrayConditions;
64 arrayConditions.SetOwner(kTRUE);
65
66 UniSearchCondition* searchCondition =
68 arrayConditions.Add((TObject*)searchCondition);
69 if (target != "") {
70 searchCondition =
72 target); // one can choose any condition: beam, energy...
73 arrayConditions.Add((TObject*)searchCondition);
74 }
75 if (beam != "") {
76 searchCondition =
78 arrayConditions.Add((TObject*)searchCondition);
79 }
80 pRunArray = UniRun::Search(arrayConditions);
81 run_count = pRunArray->GetEntriesFast();
82 if (run_count <= 0) {
83 cout << endl << "Macro finished with errors: no runs exists for the given target: " << target << endl;
84 exit(-4);
85 }
86 arrayConditions.Delete();
87
88 // Create histograms
89 printf("len %d\n", pRunArray->GetEntries());
90 Int_t first_run_id = (reinterpret_cast<UniRun*>(pRunArray->First()))->GetRunNumber(); // 4635;
91 Int_t last_run_id = (reinterpret_cast<UniRun*>(pRunArray->Last()))->GetRunNumber(); // 4680;
92 printf("range %d - %d\n", first_run_id, last_run_id);
93 Int_t nbins = (last_run_id + 1 - first_run_id);
94 TCanvas* can = new TCanvas("runs_btnb", "Run vs BT\\& not Busy", HW, HH);
95 TH1* hBT = new TH1I("hBT", "BT", nbins, first_run_id, last_run_id + 1);
96 TH1* hBTnB = new TH1I("hBTnB", "BT & notBusy", nbins, first_run_id, last_run_id + 1);
97 TH1* hBTAc = new TH1D("hBTAc", "BT Accepted", nbins, first_run_id, last_run_id + 1);
98 // TH1* hAcc = new TH1I("hAcc", "Accepted", nbins, first_run_id, last_run_id + 1);
99 hBT->SetLineColor(kRed);
100 hBTnB->SetLineColor(kRed);
101 hBTAc->SetLineColor(kRed);
102 // hAcc->SetLineColor(kRed);
103 vector<TH1*> hists;
104 hists.push_back(hBT);
105 hists.push_back(hBTnB);
106 hists.push_back(hBTAc);
107
108 TH1I* hBT_log = new TH1I("hBT_log", "BT", nbins, first_run_id, last_run_id + 1);
109 TH1* hBTnB_log = new TH1I("hBTnB_log", "BT & notBusy", nbins, first_run_id, last_run_id + 1);
110 TH1D* hBTAc_log = new TH1D("hBTAc_log", "BT Accepted", nbins, first_run_id, last_run_id + 1);
111 // TH1* hAcc_log = new TH1I("hAcc_log", "Accepted", nbins, first_run_id, last_run_id + 1);
112 TH1* hBC1H_log = new TH1I("hBC1H_log", "BC1 High", nbins, first_run_id, last_run_id + 1);
113 TH1* hBC1BP_log = new TH1I("hBC1BP_log", "BC1 Before Protection", nbins, first_run_id, last_run_id + 1);
114 TH1* hBC1xBC2_log = new TH1I("hBC1xBC2_log", "BC1 * BC2", nbins, first_run_id, last_run_id + 1);
115 TH1* hBC1nBusy_log = new TH1I("hBC1nBusy_log", "BC1 * !Busy", nbins, first_run_id, last_run_id + 1);
116 TH1* hIT_log = new TH1I("hIT_log", "Interaction Trigger", nbins, first_run_id, last_run_id + 1);
117 TH1* hSRC_log = new TH1I("hSRC_log", "SRC Trigger", nbins, first_run_id, last_run_id + 1);
118 TH1* hTrignBusy_log = new TH1I("hTrignBusy_log", "Trigger * !Busy", nbins, first_run_id, last_run_id + 1);
119 vector<TH1*> hists_log;
120 // hists_log.push_back(hBT_log);
121 // hists_log.push_back(hBTnB_log);
122 // hists_log.push_back(hBTAc_log);
123 hists_log.push_back(hBC1H_log);
124 // hists_log.push_back(hBC1BP_log);
125 // hists_log.push_back(hBC1xBC2_log);
126 // hists_log.push_back(hBC1nBusy_log);
127 hists_log.push_back(hIT_log);
128 hists_log.push_back(hSRC_log);
129 hists_log.push_back(hTrignBusy_log);
130
131 int sum_size = parse_values[0]->arrValues.size();
132
133 for (int ind = 0; ind < parse_values.size(); ind++) {
134 structParseValue* st = parse_values.at(ind);
135 vector<Int_t> vals;
136 for (Int_t iValue = 0; iValue < sum_size; iValue++)
137 vals.push_back(boost::any_cast<Int_t>(st->arrValues[iValue]));
138 st->dtSpillEnd.Set(st->dtSpillEnd.Convert() - log_shift_src);
139 spill_map.insert(pair<TDatime, vector<Int_t>>(st->dtSpillEnd, vals));
140 }
141
142 vector<Long64_t> total_columns;
143 for (int i_total = 0; i_total < sum_size; i_total++)
144 total_columns.push_back(0);
145
146 // cycle for all runs to sum the spill data
147 for (int i = 0; i < run_count; i++) {
148 if (run_numbers != NULL)
149 run = run_numbers[i].run_number;
150 if (pRunArray != NULL)
151 run = ((UniRun*)pRunArray->At(i))->GetRunNumber();
152
153 // get run time
154 UniRun* pRun = UniRun::GetRun(period, run);
155 if (pRun == NULL) {
156 cout << endl
157 << "Macro finished with errors: no experimental run was found " << period << " : " << run << " / "
158 << ((UniRun*)pRunArray->At(i))->GetRunNumber() << endl;
159 if (run_numbers != NULL)
160 delete[] run_numbers;
161 if (pRunArray != NULL)
162 delete pRunArray;
163 exit(-5);
164 }
165
166 TDatime dtStart = pRun->GetStartDatetime();
167 TDatime* dateEnd = pRun->GetEndDatetime();
168 if (dateEnd == NULL) {
169 cout << "Macro finished with errors: no end datetime in the database for this run" << endl;
170 delete pRun;
171 if (run_numbers != NULL)
172 delete[] run_numbers;
173 if (pRunArray != NULL)
174 delete pRunArray;
175 exit(-6);
176 }
177 TDatime dtEnd = *dateEnd;
178 delete pRun;
179
180 vector<Long64_t> sum_columns;
181 for (int i_sum = 0; i_sum < sum_size; i_sum++)
182 sum_columns.push_back(0);
183
184 if (!((run >= first_run_id) && (run <= last_run_id)))
185 continue;
186 // check for presence in ELOG
187 TObjArray* recs = ElogRecord::GetRecords(period, run);
188 if (recs == NULL) {
189 fprintf(stderr, "ELOG Error!\n");
190 continue;
191 } else if (recs->GetEntries() == 0) {
192 fprintf(stderr, "Run %d not found in ELOG!\n", run);
193 // return NULL;
194 continue;
195 }
196 printf("Run %d %s", run, dtStart.AsSQLString());
197 printf(" - %s\n", dtEnd.AsSQLString());
198 Double_t bt_acc = 0;
199 vector<Int_t> preVals;
200 Bool_t isRepeat;
201 preVals.resize(20, 0);
202 for (auto it = spill_map.lower_bound(dtStart); it != spill_map.upper_bound(dtEnd); it++) {
203
204 printf(" spill %s\n", it->first.AsSQLString());
205 vector<Int_t> v = it->second;
206 isRepeat = kTRUE;
207 for (Int_t i = 0; i < v.size() - 1; i++)
208 if ((preVals[i] != v[i]))
209 isRepeat = kFALSE;
210 if (isRepeat)
211 printf("\n\tALARM!\n\n");
212 preVals = v;
213 Long64_t acc = v[13];
214 Long64_t bp = v[17];
215 Long64_t ap = v[18];
216 Long64_t bt = v[6];
217 Long64_t bt_notBusy = v[16];
218 if (acc)
219 bt_acc += bt_notBusy * (acc - nPedestals) / ((Double_t)(acc - nPedestals + bp + ap));
220 // printf("\t %s bt= %05lld bt_notBusy= %05lld acc = %05lld bt_acc = %05.3f\n",
221 // it->first.AsSQLString(), bt, bt_notBusy, acc, bt_acc);
222
223 int count_column = 0;
224 for (vector<structParseSchema>::iterator it = vecElements.begin(); it != vecElements.end(); ++it) {
225 if (count_column >= sum_size)
226 break;
227
228 structParseSchema schema = *it;
229 if (schema.isSkip)
230 continue;
231
232 if (schema.vecRows[0].strStatementType == "int") {
233 if (count_column > 0)
234 cout << ", ";
235 cout << "\tsum(" << schema.vecRows[0].strColumnName << "): " << v[count_column];
236 count_column++;
237 } else {
238 if (schema.vecRows[0].strStatementType == "double") {
239 cout << "ERROR: type of the column is not supported: " << schema.vecRows[0].strStatementType
240 << endl;
241 exit(-7);
242 }
243 }
244 }
245 cout << endl << endl;
246 for (int i_sum = 0; i_sum < sum_size; i_sum++)
247 sum_columns[i_sum] += v[i_sum];
248 }
249 // printf("\tsum BT = %lld\n", sum_columns[6]);
250 Long64_t bt_sum = sum_columns[6];
251 Long64_t bt_notBusy_sum = sum_columns[16];
252 Long64_t acc_sum = sum_columns[13];
253 printf("\tSum bt= %05lld bt_notBusy= %05lld acc = %05lld bt_acc = %05.3f\n\n", bt_sum, bt_notBusy_sum, acc_sum,
254 bt_acc);
255 Int_t iBin = hBT_log->GetXaxis()->FindBin(run);
256 Double_t bc = hBT_log->GetBinCenter(iBin);
257 // printf("bin %d center %f bt_sum %lld bt_notBusy_sum %lld bt_acc %f\n",
258 // iBin, bc, bt_sum, bt_notBusy_sum, bt_acc);
259 hBT_log->SetBinContent(iBin, bt_sum);
260 hBTnB_log->SetBinContent(iBin, bt_notBusy_sum);
261 hBTAc_log->SetBinContent(iBin, bt_acc);
262 // hAcc_log->SetBinContent(iBin, acc_sum);
263 hBC1H_log->SetBinContent(iBin, sum_columns[3]);
264 // hBC1BP_log->SetBinContent(iBin, sum_columns[]);
265 // hBC1xBC2_log->SetBinContent(iBin, sum_columns[]);
266 // hBC1nBusy_log->SetBinContent(iBin, sum_columns[]);
267 hIT_log->SetBinContent(iBin, sum_columns[18]);
268 hSRC_log->SetBinContent(iBin, sum_columns[19]);
269 // hTrignBusy_log->SetBinContent(iBin, sum_columns[7]);
270
271 // TString fileName = Form("%s/bmn_run%d_digi.root", digi_path.Data(), run);
272 // TFile *file = new TFile(fileName, "READ");
273 // if (
274 // (file->IsOpen() == kTRUE) &&
275 // (file->IsZombie() == kFALSE) &&
276 // (file->TestBit(TFile::kRecovered) == kFALSE)) {
277 // printf("%s is ok\n", fileName.Data());
278 // DigiRunHeader * rh = static_cast<DigiRunHeader*> (file->Get("DigiRunHeader"));
279 // Int_t iBin = hBT_log->GetXaxis()->FindBin(run);
280 // hBT->SetBinContent(iBin, rh->GetBT());
281 // hBTnB->SetBinContent(iBin, rh->GetBTnBusy());
282 // hBTAc->SetBinContent(iBin, rh->GetBTAccepted());
283 // printf("\tbt= %u bt_notBusy= %u bt_acc = %05u\n", rh->GetBT(), rh->GetBTnBusy(),
284 // rh->GetBTAccepted());
285 // // hAcc->SetBinContent(iBin, rh->Get());
286 // }
287 }
288
289 // for (Int_t run = first_run_id; run <= last_run_id; run++) {
290 // TString fileName = Form("%s/bmn_run%d_digi.root", digi_path.Data(), run);
291 // TFile *file = new TFile(fileName, "READ");
292 // if (
293 // (file->IsOpen() == kTRUE) &&
294 // (file->IsZombie() == kFALSE) &&
295 // (file->TestBit(TFile::kRecovered) == kFALSE)) {
296 // printf("%s is ok\n", fileName.Data());
297 // DigiRunHeader * rh = static_cast<DigiRunHeader*> (file->Get("DigiRunHeader"));
298 // Int_t iBin = hBT_log->GetXaxis()->FindBin(run);
299 // hBT->SetBinContent(iBin, rh->GetBT());
300 // hBTnB->SetBinContent(iBin, rh->GetBTnBusy());
301 // hBTAc->SetBinContent(iBin, rh->GetBTAccepted());
302 //// printf("\tbt_notBusy= %u bt_acc = %05u\n", rh->GetBTnBusy(), rh->GetBTAccepted());
303 // // hAcc->SetBinContent(iBin, rh->Get());
304 //
305 //
306 //
307 // }
308 //
309 // }
310
311 TString plotName = "spill.pdf";
312 for (Int_t i = 0; i < hists_log.size(); i++) {
313 Draw2Can(can, hists_log[i], (i < hists.size()) ? hists[i] : nullptr);
314 can->SaveAs(Form("%s.png", hists_log[i]->GetName()));
315 }
316
317 // Draw2Can(can, hBT_log, hBT);
318 // can->SaveAs("counter_bt.png");
319 // //can->Print(plotName + "(", "pdf");
320 //
321 // Draw2Can(can, hBTnB_log, hBTnB);
322 // can->SaveAs("counter_bt_nb.png");
323 // //can->Print(plotName, "pdf");
324 //
325 // Draw2Can(can, hBTAc_log, hBTAc);
326 // can->SaveAs("counter_bt_acc.png");
327 // //can->Print(plotName + ")", "pdf");
328
329 // cleaning memory after work
330 if (run_numbers != NULL)
331 delete[] run_numbers;
332 if (pRunArray != NULL)
333 delete pRunArray;
334 for (auto& h : hists)
335 delete h;
336 for (auto& h : hists_log)
337 delete h;
338 // delete hBT;
339 // delete hBTnB;
340 // delete hBTAc;
341 //// delete hAcc;
342 // delete hBT_log;
343 // delete hBTnB_log;
344 // delete hBTAc_log;
345 //// delete hAcc_log;
346
347 cout << "Macro finished successfully" << endl;
348}
TCanvas * can(nullptr)
__m128 v
Definition P4_F32vec4.h:1
int i
Definition P4_F32vec4.h:22
friend F32vec4 max(const F32vec4 &a, const F32vec4 &b)
Definition P4_F32vec4.h:31
bool Bool_t
int Int_t
constexpr Bool_t kFALSE
double Double_t
long long Long64_t
constexpr Bool_t kTRUE
kRed
char * Form(const char *fmt,...)
static TObjArray * GetRecords(int period_number, int run_number, int findNeighbourRun=0)
get array of ElogRecord-s for a given or a neighbour run (the same period) from the database if findN...
virtual void SetLineColor(Color_t lcolor)
virtual Int_t FindBin(const char *label)
TVirtualPad * cd(Int_t subpadnumber=0) override
virtual void SetOwner(Bool_t enable=kTRUE)
const char * AsSQLString() const
void Set()
UInt_t Convert(Bool_t toGMT=kFALSE) const
virtual Double_t GetBinCenter(Int_t bin) const
TAxis * GetXaxis()
virtual Double_t GetMaximum(Double_t maxval=FLT_MAX) const
virtual void SetMaximum(Double_t maximum=-1111)
void Draw(Option_t *option="") override
virtual void SetMinimum(Double_t minimum=-1111)
virtual void SetBinContent(Int_t bin, Double_t content)
Int_t GetEntriesFast() const
TObject * Last() const override
Int_t GetEntries() const override
void Delete(Option_t *option="") override
TObject * At(Int_t idx) const override
TObject * First() const override
void Add(TObject *obj) override
int ParseTxt2Struct(TString txtName, TString schemaPath, vector< structParseValue * > &parse_values, vector< structParseSchema > &vecElements, int iVerbose=1)
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
static TObjArray * Search(UniSearchCondition &search_condition)
get runs corresponding to the specified single condition and set owner for search_condition to kTRUE
Definition UniRun.cxx:1155
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
void Spill(int period=7, TString beam="C", TString target="", TString digi_path="/ncx/scratch1/ilnur/mini_release/", TString txtfile_path="$VMCWORKDIR/database/uni_db/macros/parse_schemes/spill_run7/SRC_Data.txt", TString scheme_path="$VMCWORKDIR/database/uni_db/macros/parse_schemes/spill_run7/spill_run7_src_full.xslt")
Definition Spill.C:24
void Draw2Can(TCanvas *c, TH1 *h1, TH1 *h2=nullptr)
flux ~= sum [BT * (DAQ_Busy -peds)/ (DAQ_TRigger - peds)]
Definition Spill.C:12
Definition json.hpp:4446
vector< structParseRow > vecRows
Definition UniParser.h:71
TDatime dtSpillEnd
Definition UniParser.h:92
vector< boost::any > arrValues
Definition UniParser.h:93