Belle II Software development
HadronBgPrep.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8
9#include <cdc/calibration/CDCdEdx/HadronBgPrep.h>
10#include <framework/utilities/MathHelpers.h>
11using namespace Belle2;
12
14{
15 m_bgBins = 15;
16 m_bgMin = 0.1;
17 m_bgMax = 40;
18 m_cosBins = 18;
19 m_cosMin = -1.0;
20 m_cosMax = 1.0;
21 m_injBins = 20;
22 m_injMin = 0;
23 m_injMax = 80000;
24 m_nhitBins = 10;
25 m_nhitMin = 7;
26 m_nhitMax = 39;
27 m_cut = 0.5;
28}
29HadronBgPrep::HadronBgPrep(int bgbins, double lowerbg, double upperbg, int cosbins, double lowercos, double uppercos, int injbins,
30 double lowerinj, double upperinj, int nhitbins, double lowernhit, double uppernhit, double cut)
31{
32 m_bgBins = bgbins;
33 m_bgMin = lowerbg;
34 m_bgMax = upperbg;
35 m_cosBins = cosbins;
36 m_cosMin = lowercos;
37 m_cosMax = uppercos;
38 m_injBins = injbins;
39 m_injMin = lowerinj;
40 m_injMax = upperinj;
41 m_nhitBins = nhitbins;
42 m_nhitMin = lowernhit;
43 m_nhitMax = uppernhit;
44 m_cut = cut;
45}
46
47void HadronBgPrep::prepareSample(std::shared_ptr<TTree> hadron, TFile*& outfile, const std::string& suffix,
48 const std::string& bgcurvefile,
49 const std::string& bgsigmafile, const std::string& pdg, bool ismakePlots)
50{
51
52 std::vector<TH1F*> hdedx_bg, hchi_bg, hionzsigma_bg;
53 std::map<int, std::vector<TH1F*>> hchi_inj, hchicos_allbg, hchicos_1by3bg, hchicos_2by3bg, hchicos_3by3bg;
54
55 //define histograms
56 defineHisto(hdedx_bg, "dedx", "bg", pdg.data());
57 defineHisto(hchi_bg, "chi", "bg", pdg.data());
58 defineHisto(hionzsigma_bg, "ionzsigma", "bg", pdg.data());
59 for (int i = 0; i < 2; ++i) {
60
61 defineHisto(hchi_inj[i], "chi", Form("inj_%d", i), pdg.data());
62
63 std::string charge = "pos";
64 if (i == 1) charge = "neg";
65
66 defineHisto(hchicos_allbg[i], "chi", Form("%s_allbg_cos", charge.data()), pdg.data());
67 defineHisto(hchicos_1by3bg[i], "chi", Form("%s_1b3bg_cos", charge.data()), pdg.data());
68 defineHisto(hchicos_2by3bg[i], "chi", Form("%s_2b3bg_cos", charge.data()), pdg.data());
69 defineHisto(hchicos_3by3bg[i], "chi", Form("%s_3b3bg_cos", charge.data()), pdg.data());
70 }
71
72
73 // Create some containers to calculate averages
74 for (int i = 0; i < m_bgBins; ++i) {
75 m_sumcos.push_back(0.);
76 m_sumbg.push_back(0.);
77 m_sumres_square.push_back(0.);
78 m_sumsize.push_back(0.);
79 m_means.push_back(0.);
80 m_errors.push_back(0.);
81 }
82
83 for (int i = 0; i < m_injBins; ++i) {
84 m_injsize.push_back(0.);
85 m_suminj.push_back(0.);
86 }
87
88 // --------------------------------------------------
89 // LOOP OVER EVENTS AND FILL CONTAINERS
90 // --------------------------------------------------
91 // Fill the histograms to be fitted
92
93 double dedxnosat; // dE/dx w/o HS correction (use to get new parameters)
94 double p; // track momentum
95 double costh; // cosine of track polar angle
96 double timereso;
97 int nhit; // number of hits on this track
98 double injtime; //injection time
99 double isher;
100 int charge;
101
102 hadron->SetBranchAddress("dedxnosat", &dedxnosat); //must be without any HS correction
103 hadron->SetBranchAddress("p", &p);
104 hadron->SetBranchAddress("costh", &costh);
105 hadron->SetBranchAddress("timereso", &timereso);
106 hadron->SetBranchAddress("nhits", &nhit);
107 hadron->SetBranchAddress("injtime", &injtime);
108 hadron->SetBranchAddress("injring", &isher);
109 hadron->SetBranchAddress("charge", &charge);
110
111 double mass = getParticleMass(pdg);
112 if (mass == 0.0) B2FATAL("Mass of particle " << pdg.data() << " is zero");
113
114 const double cosstep = (m_cosMax - m_cosMin) / m_cosBins;
115 const double tstep = (m_injMax - m_injMin) / m_injBins;
116
117 CDCDedxMeanPred mbg;
118 mbg.setParameters(bgcurvefile);
120 sbg.setParameters(bgsigmafile);
121
122 CDCDedxHadSat had;
123 had.setParameters("sat-pars.fit.txt");
124
125 unsigned int entries = hadron->GetEntries();
126
127 for (unsigned int index = 0; index < entries; ++index) {
128
129 hadron->GetEntry(index);
130
131 int chg = (charge < 0) ? 1 : 0;
132 double bg = fabs(p) / mass;
133
134 // clean up bad events and restrict the momentum range
135 if (nhit < 0 || nhit > 100)continue;
136 if (injtime <= 0 || isher < 0)continue;
137
138 if (dedxnosat <= 0)continue;
139 if (costh != costh)continue;
140 if (bg < m_bgMin || bg > m_bgMax)continue;
141
142 if (fabs(p) > 7.0)continue;
143
144 if (pdg == "proton") if ((dedxnosat - 0.45) * abs(p) * abs(p) < m_cut)continue;
145
146 int bgBin = static_cast<int>((bg - m_bgMin) / (m_bgMax - m_bgMin) * m_bgBins);
147 bgBin = std::min(m_bgBins - 1, std::max(0, bgBin));
148
149 if (bgBin >= m_bgBins) {
150 B2WARNING("bgBin out of range: " << bgBin
151 << " (valid range: 0-" << (m_bgBins - 1) << ")");
152 }
153
154 double dedx_new = had.D2I(costh, had.I2D(costh, 1.0) * dedxnosat);
155
156 hdedx_bg[bgBin]->Fill(dedx_new);
157
158 //get predicted value of dEdx mean
159 double dedx_cur = mbg.getMean(bg);
160 double dedx_res = sbg.getSigma(dedx_new, nhit, costh, timereso);
161
162 if (dedx_res == 0) {
163 B2INFO("RESOLUTION IS ZERO!!!");
164 continue;
165 }
166
167 //Modified chi_new values
168 double chi_new = (dedx_new - dedx_cur) / dedx_res;
169
170 hchi_bg[bgBin]->Fill(chi_new);
171
172 double ionz_res = sbg.cosPrediction(costh) * sbg.nhitPrediction(nhit) * timereso;
173
174 if (ionz_res == 0) {
175 B2INFO("RESOLUTION costh * nhit * timereso IS ZERO!!!");
176 continue;
177 }
178
179 hionzsigma_bg[bgBin]->Fill((dedx_new - dedx_cur) / ionz_res);
180
181 m_sumcos[bgBin] += costh;
182 m_sumbg[bgBin] += bg;
183 m_sumres_square[bgBin] += square(dedx_res);
184 m_sumsize[bgBin] += 1;
185
186 // make histograms of dE/dx vs. cos(theta) for validation
187 int icos = static_cast<int>((costh + 1) / cosstep);
188 if (icos >= m_cosBins) {
189 B2WARNING("cosBin (icos) out of range: " << icos
190 << " (valid range: 0-" << (m_cosBins - 1) << ")");
191 }
192 icos = std::min(m_cosBins - 1, icos);
193
194 hchicos_allbg[chg][icos]->Fill(chi_new);
195
196 if (bgBin <= int(m_bgBins / 3)) hchicos_1by3bg[chg][icos]->Fill(chi_new);
197 else if (bgBin <= int(2 * m_bgBins / 3)) hchicos_2by3bg[chg][icos]->Fill(chi_new);
198 else hchicos_3by3bg[chg][icos]->Fill(chi_new);
199
200 if (injtime > m_injMax) injtime = m_injMax - 10.0;
201 int wr = 0;
202 if (isher > 0.5) wr = 1;
203 int injBin = static_cast<int>((injtime - m_injMin) / tstep);
204 if (injBin >= m_injBins) {
205 B2WARNING("injBin out of range: " << injBin
206 << " (valid range: 0-" << (m_injBins - 1) << ")");
207 }
208 injBin = std::min(m_injBins - 1, std::max(0, injBin));
209 hchi_inj[wr][injBin]->Fill(chi_new);
210
211 m_suminj[injBin] += injtime;
212 m_injsize[injBin] += 1;
213
214 } // end of event loop
215
216 // --------------------------------------------------
217 // FIT IN BINS OF BETA-GAMMA AND COS(THETA)
218 // --------------------------------------------------
219 // fit the histograms with Gaussian functions
220 // and extract the means and errors
221
222 setPars(outfile, pdg, hdedx_bg, hchi_bg, hionzsigma_bg, hchi_inj);
223
224 // Plot the histograms
225 if (ismakePlots) {
226
227 plotDist(hdedx_bg, Form("fits_dedx_inbg_%s_%s", suffix.data(), pdg.data()), m_bgBins);
228 plotDist(hchi_bg, Form("fits_chi_inbg_%s_%s", suffix.data(), pdg.data()), m_bgBins);
229 plotDist(hionzsigma_bg, Form("fits_ionzreso_inbg_%s_%s", suffix.data(), pdg.data()), m_bgBins);
230 plotDist(hchi_inj, Form("fits_chi_ininj_%s_%s", suffix.data(), pdg.data()), m_injBins);
231 printCanvasCos(hchicos_allbg, hchicos_1by3bg, hchicos_2by3bg, hchicos_3by3bg, pdg, suffix);
232 }
233
234 // delete histograms
235 clearVars();
236 deleteHistos(hdedx_bg);
237 deleteHistos(hchi_bg);
238 deleteHistos(hionzsigma_bg);
239 for (int i = 0; i < 2; ++i) {
240 deleteHistos(hchi_inj[i]);
241 deleteHistos(hchicos_allbg[i]);
242 deleteHistos(hchicos_1by3bg[i]);
243 deleteHistos(hchicos_2by3bg[i]);
244 deleteHistos(hchicos_3by3bg[i]);
245 }
246
247}
248
249//------------------------------------
250void HadronBgPrep::defineHisto(std::vector<TH1F*>& htemp, const std::string& svar, const std::string& stype, const std::string& pdg)
251{
252 int nbdEdx = 200, nbchi = 200;
253 double dedxMax = 20.0;
254
255 if (pdg == "pion") {
256 nbdEdx = 400, dedxMax = 4.0;
257 } else if (pdg == "kaon") {
258 nbdEdx = 500, dedxMax = 5.0;
259 } else if (pdg == "proton") {
260 nbdEdx = 1500, dedxMax = 30.0;
261 nbchi = 350;
262 } else if (pdg == "muon") {
263 nbdEdx = 300, dedxMax = 3.0;
264 } else if (pdg == "electron") {
265 nbdEdx = 200, dedxMax = 2.0;
266 }
267
268 int bins;
269 double min, max;
270
271 if (stype == "bg") bins = m_bgBins, min = m_bgMin, max = m_bgMax ;
272 else if (stype == "inj_0" || stype == "inj_1") bins = m_injBins, min = m_injMin, max = m_injMax ;
273 else if (stype == "nhit") bins = m_nhitBins, min = m_nhitMin, max = m_nhitMax ;
274 else bins = m_cosBins, min = m_cosMin, max = m_cosMax ;
275
276 double step = (max - min) / bins;
277 if (stype == "nhit") step = (max - min + 1) / bins;
278
279 for (int j = 0; j < bins; ++j) {
280
281 double start = min + j * step;
282 double end = start + step;
283 if (stype == "nhit") end = int((start + step) * 0.99999);
284
285 // initialize the histograms
286 std::string histname = Form("%s_%s_%s_%d", pdg.data(), svar.data(), stype.data(), j);
287 std::string title = Form("%s_%s_%s (%.02f, %.02f)", pdg.data(), svar.data(), stype.data(), start, end);
288
289 if (svar == "dedx")
290 htemp.push_back(new TH1F(histname.data(), title.data(), nbdEdx, 0, dedxMax));
291 else if (svar == "chi")
292 htemp.push_back(new TH1F(histname.data(), title.data(), nbchi, -10.0, 10.0));
293 else
294 htemp.push_back(new TH1F(histname.data(), title.data(), 300, -3.0, 3.0));
295
296 htemp[j]->GetXaxis()->SetTitle(Form("%s", svar.data()));
297 htemp[j]->GetYaxis()->SetTitle("Entries");
298 }
299}
300
301//------------------------------------
302void HadronBgPrep::plotDist(std::map<int, std::vector<TH1F*>>& hist, const std::string& sname, int bins)
303{
304
305 TCanvas ctmp(Form("cdcdedx_%s", sname.data()), "", 1200, 600);
306 ctmp.Divide(2, 1);
307 ctmp.SetBatch(kTRUE);
308
309 std::stringstream psname;
310 psname << Form("plots/HadronPrep/%s.pdf[", sname.data());
311 ctmp.Print(psname.str().c_str());
312 psname.str("");
313 psname << Form("plots/HadronPrep/%s.pdf", sname.data());
314 for (int j = 0; j < bins; ++j) {
315
316 for (int i = 0 ; i < 2; ++i) {
317 ctmp.cd(i + 1);
318 hist[i][j]->SetFillColorAlpha(i + 5, 0.25);
319 hist[i][j]->Draw();
320 }
321 ctmp.Print(psname.str().c_str());
322 }
323 psname.str("");
324 psname << Form("plots/HadronPrep/%s.pdf]", sname.data());
325 ctmp.Print(psname.str().c_str());
326}
327
328//------------------------------------
329void HadronBgPrep::plotDist(std::vector<TH1F*>& hist, const std::string& sname, int nbins)
330{
331
332 TCanvas ctmp(Form("cdcdedx_%s", sname.data()), "", 1200, 1200);
333 ctmp.Divide(2, 2);
334 ctmp.SetBatch(kTRUE);
335
336 std::stringstream psname;
337 psname << Form("plots/HadronPrep/%s.pdf[", sname.data());
338 ctmp.Print(psname.str().c_str());
339 psname.str("");
340 psname << Form("plots/HadronPrep/%s.pdf", sname.data());
341
342 for (int i = 0 ; i < nbins; ++i) {
343 ctmp.cd(i % 4 + 1);
344 hist[i]->SetFillColor(kYellow - 9);
345 hist[i]->Draw();
346
347 if ((i + 1) % 4 == 0 || (i + 1) == nbins) {
348 ctmp.Print(psname.str().c_str());
349 ctmp.Clear("D");
350 }
351 }
352 psname.str("");
353 psname << Form("plots/HadronPrep/%s.pdf]", sname.data());
354 ctmp.Print(psname.str().c_str());
355}
356
357//----------------------------------------
358void HadronBgPrep::setPars(TFile*& outfile, std::string pdg, std::vector<TH1F*>& hdedx_bg, std::vector<TH1F*>& hchi_bg,
359 std::vector<TH1F*>& hionzsigma_bg, std::map<int, std::vector<TH1F*>>& hchi_inj)
360{
361 outfile->cd();
362
363 TTree* satTree = new TTree(Form("%s", pdg.data()), "dE/dx m_means and m_errors");
364 double satbg; // beta-gamma value for this bin
365 double satcosth; // cos(theta) value for this bin
366 double satdedx; // mean dE/dx value for this bin
367 double satdedxerr; // error on ^
368 double satdedxwidth;// width of ^ distribution
369
370 double satchi; // mean chi value for this bin
371 double satchierr; // error on ^
372 double satchiwidth; // width of ^ distribution
373 double satchiwidth_err;
374 double sationzres; // width of dedx reso
375
376 double satbg_avg; // average beta-gamma value for this sample
377 double satcosth_avg; // average cos(theta) value for this sample
378 double satdedxres_avg; // average dE/dx error squared for this sample
379
380 satTree->Branch("bg", &satbg, "bg/D");
381 satTree->Branch("costh", &satcosth, "costh/D");
382
383 //dEdx related variables
384 satTree->Branch("dedx", &satdedx, "dedx/D");
385 satTree->Branch("dedxerr", &satdedxerr, "dedxerr/D");
386 satTree->Branch("dedxwidth", &satdedxwidth, "dedxwidth/D");
387
388 // Chi related variables
389 satTree->Branch("chimean", &satchi, "chimean/D");
390 satTree->Branch("chimean_err", &satchierr, "chimean_err/D");
391 satTree->Branch("chisigma", &satchiwidth, "chisigma/D");
392 satTree->Branch("chisigma_err", &satchiwidth_err, "chisigma_err/D");
393
394 satTree->Branch("ionzres", &sationzres, "ionzres/D");
395
396 // Other variables
397 satTree->Branch("bg_avg", &satbg_avg, "bg_avg/D");
398 satTree->Branch("costh_avg", &satcosth_avg, "costh_avg/D");
399 satTree->Branch("dedxres_avg", &satdedxres_avg, "dedxres_avg/D");
400
401 double bgstep = (m_bgMax - m_bgMin) / m_bgBins;
402
403 for (int i = 0; i < m_bgBins; ++i) {
404
405 // fill some details for this bin
406 satbg = m_bgMin + 0.5 * bgstep + i * bgstep;
407 satcosth = 0.0;
408
409 satbg_avg = (m_sumsize[i] > 0) ? m_sumbg[i] / m_sumsize[i] : 0.0;
410 satcosth_avg = (m_sumsize[i] > 0) ? m_sumcos[i] / m_sumsize[i] : 0.0;
411 satdedxres_avg = (m_sumsize[i] > 0) ? m_sumres_square[i] / m_sumsize[i] : 0.0;
412
413 //1. -------------------------
414 // fit the dE/dx distribution in bins of beta-gamma
415 gstatus bgstat;
416 fit(hdedx_bg[i], pdg.data(), bgstat);
417 TF1* f = hdedx_bg[i]->GetFunction("gaus");
418 if (bgstat == OK && f) {
419 const auto mean = f->GetParameter(1);
420 satdedx = mean;
421 m_means[i] = mean;
422 const auto err = f->GetParError(1);
423 satdedxerr = err;
424 m_errors[i] = err;
425 satdedxwidth = f->GetParameter(2);
426 } else { satdedx = 0.0; satdedxerr = 0.0; satdedxwidth = 0.0;}
427
428 //2. -------------------------
429 // fit the chi distribution in bins of beta-gamma
430 gstatus chistat;
431 fit(hchi_bg[i], pdg.data(), chistat);
432 f = hchi_bg[i]->GetFunction("gaus");
433 if (chistat == OK && f) {
434 satchi = f->GetParameter(1);
435 satchierr = f->GetParError(1);
436 satchiwidth = f->GetParameter(2);
437 satchiwidth_err = f->GetParError(2);
438 } else { satchi = 0.0; satchierr = 0.0; satchiwidth = 0.0; satchiwidth_err = 0.0;}
439
440
441 //3. -------------------------
442 // fit the chi distribution in bins of beta-gamma
443 gstatus ionstat;
444 fit(hionzsigma_bg[i], pdg.data(), ionstat);
445 if (ionstat == OK) {
446 sationzres = hionzsigma_bg[i]->GetFunction("gaus")->GetParameter(2);
447 } else sationzres = 0.0;
448
449 // fill the tree for this bin
450 satTree->Fill();
451 }
452 // write out the data to file
453 satTree->Write();
454
455 // --------------------------------------------------
456 // FIT IN BINS OF INJECTION TIME FOR VALIDATION
457 // --------------------------------------------------
458
459 std::string svar = "ler";
460 for (int ir = 0; ir < 2; ir++) {
461
462 if (ir == 1) svar = "her";
463
464 TTree* injTree = new TTree(Form("%s_%s", pdg.data(), svar.data()), "chi m_means and m_errors");
465 double inj_avg;
466 double mean;
467 double mean_err;
468 double sigma;
469 double sigma_err;
470
471 injTree->Branch("inj_avg", &inj_avg, "inj_avg/D");
472 injTree->Branch("chimean", &mean, "chimean/D");
473 injTree->Branch("chimean_err", &mean_err, "chimean_err/D");
474 injTree->Branch("chisigma", &sigma, "chisigma/D");
475 injTree->Branch("chisigma_err", &sigma_err, "chisigma_err/D");
476
477 for (int i = 0; i < m_injBins; ++i) {
478
479 inj_avg = (m_injsize[i] > 0) ? m_suminj[i] / m_injsize[i] : 0.0;
480
481
482 // fit the dE/dx distribution in bins of injection time'
483 gstatus injstat;
484 fit(hchi_inj[ir][i], pdg.data(), injstat);
485 if (injstat == OK) {
486 mean = hchi_inj[ir][i]->GetFunction("gaus")->GetParameter(1);
487 mean_err = hchi_inj[ir][i]->GetFunction("gaus")->GetParError(1);
488 sigma = hchi_inj[ir][i]->GetFunction("gaus")->GetParameter(2);
489 sigma_err = hchi_inj[ir][i]->GetFunction("gaus")->GetParError(2);
490 } else {
491 mean = 0.0; mean_err = 0.0; sigma = 0.0; sigma_err = 0.0;
492 }
493 injTree->Fill();
494 }
495
496 injTree->Write();
497 }
498 outfile->Write();
499}
500
501//----------------------------------------
502void HadronBgPrep::fit(TH1F*& hist, const std::string& pdg, gstatus& status)
503{
504
505 if (pdg == "pion") fitGaussianWRange(hist, status, 1.0);
506 else fitGaussianWRange(hist, status, 2.0);
507
508 hist->SetFillColorAlpha(kAzure + 1, 0.30);
509
510 if (status == OK) {
511 double mean = hist->GetFunction("gaus")->GetParameter(1);
512 double meanerr = hist->GetFunction("gaus")->GetParError(1);
513 double width = hist->GetFunction("gaus")->GetParameter(2);
514
515 std::string title = Form("#mu_{fit}: %0.03f #pm %0.03f, #sigma_{fit}: %0.03f", mean, meanerr, width);
516 hist->SetTitle(Form("%s, %s", hist->GetTitle(), title.data()));
517 }
518
519}
520
521//----------------------------------------
522void HadronBgPrep::fitGaussianWRange(TH1F*& temphist, gstatus& status, double sigmaR)
523{
524 double histmean = temphist->GetMean();
525 double histrms = temphist->GetRMS();
526 temphist->GetXaxis()->SetRangeUser(histmean - 5.0 * histrms, histmean + 5.0 * histrms);
527
528 int fs = temphist->Fit("gaus", "Q");
529 if (fs != 0) {
530 B2INFO(Form("\tFit (round 1) for hist (%s) failed (status = %d)", temphist->GetName(), fs));
531 status = Failed;
532 return;
533 } else {
534 double mean = temphist->GetFunction("gaus")->GetParameter(1);
535 double width = temphist->GetFunction("gaus")->GetParameter(2);
536 temphist->GetXaxis()->SetRangeUser(mean - 5.0 * width, mean + 5.0 * width);
537 fs = temphist->Fit("gaus", "QR", "", mean - sigmaR * width, mean + sigmaR * width);
538 if (fs != 0) {
539 B2INFO(Form("\tFit (round 2) for hist (%s) failed (status = %d)", temphist->GetName(), fs));
540 status = Failed;
541 return;
542 } else {
543 temphist->GetXaxis()->SetRangeUser(mean - 5.0 * width, mean + 5.0 * width);
544 B2INFO(Form("\tFit for hist (%s) sucessfull (status = %d)", temphist->GetName(), fs));
545 status = OK;
546 }
547 }
548}
549
550
551// --------------------------------------------------
552// FIT IN BINS OF COS(THETA) FOR VALIDATION
553// --------------------------------------------------
554void HadronBgPrep::printCanvasCos(std::map<int, std::vector<TH1F*>>& hchicos_allbg,
555 std::map<int, std::vector<TH1F*>>& hchicos_1by3bg, std::map<int, std::vector<TH1F*>>& hchicos_2by3bg,
556 std::map<int, std::vector<TH1F*>>& hchicos_3by3bg, const std::string& pdg, const std::string& suffix)
557{
558
559 std::vector<std::vector<double>> chicos(2, std::vector<double>(m_cosBins));
560 std::vector<std::vector<double>> sigmacos(2, std::vector<double>(m_cosBins));
561 std::vector<std::vector<double>> chicoserr(2, std::vector<double>(m_cosBins));
562 std::vector<std::vector<double>> sigmacoserr(2, std::vector<double>(m_cosBins));
563
564 std::vector<std::vector<double>> chicos_1b3bg(2, std::vector<double>(m_cosBins));
565 std::vector<std::vector<double>> sigmacos_1b3bg(2, std::vector<double>(m_cosBins));
566 std::vector<std::vector<double>> chicos_1b3bgerr(2, std::vector<double>(m_cosBins));
567 std::vector<std::vector<double>> sigmacos_1b3bgerr(2, std::vector<double>(m_cosBins));
568
569 std::vector<std::vector<double>> chicos_2b3bg(2, std::vector<double>(m_cosBins));
570 std::vector<std::vector<double>> sigmacos_2b3bg(2, std::vector<double>(m_cosBins));
571 std::vector<std::vector<double>> chicos_2b3bgerr(2, std::vector<double>(m_cosBins));
572 std::vector<std::vector<double>> sigmacos_2b3bgerr(2, std::vector<double>(m_cosBins));
573
574 std::vector<std::vector<double>> chicos2(2, std::vector<double>(m_cosBins));
575 std::vector<std::vector<double>> sigmacos2(2, std::vector<double>(m_cosBins));
576 std::vector<std::vector<double>> chicos2err(2, std::vector<double>(m_cosBins));
577 std::vector<std::vector<double>> sigmacos2err(2, std::vector<double>(m_cosBins));
578
579 for (int c = 0; c < 2; ++c) {
580 for (int i = 0; i < m_cosBins; ++i) {
581 if (hchicos_allbg[c][i]->Integral() > 100) {
582 gstatus allbgstat;
583 fit(hchicos_allbg[c][i], pdg.data(), allbgstat);
584 if (allbgstat == OK) {
585 chicos[c][i] = hchicos_allbg[c][i]->GetFunction("gaus")->GetParameter(1);
586 chicoserr[c][i] = hchicos_allbg[c][i]->GetFunction("gaus")->GetParError(1);
587 sigmacos[c][i] = hchicos_allbg[c][i]->GetFunction("gaus")->GetParameter(2);
588 sigmacoserr[c][i] = hchicos_allbg[c][i]->GetFunction("gaus")->GetParError(2);
589 } else { chicos[c][i] = 0.0; chicoserr[c][i] = 0.0; sigmacos[c][i] = 0.0; sigmacoserr[c][i] = 0.0;}
590 }
591
592 if (hchicos_1by3bg[c][i]->Integral() > 100) {
593 gstatus all1bgstat;
594
595 fit(hchicos_1by3bg[c][i], pdg.data(), all1bgstat);
596 if (all1bgstat == OK) {
597 chicos_1b3bg[c][i] = hchicos_1by3bg[c][i]->GetFunction("gaus")->GetParameter(1);
598 chicos_1b3bgerr[c][i] = hchicos_1by3bg[c][i]->GetFunction("gaus")->GetParError(1);
599 sigmacos_1b3bg[c][i] = hchicos_1by3bg[c][i]->GetFunction("gaus")->GetParameter(2);
600 sigmacos_1b3bgerr[c][i] = hchicos_1by3bg[c][i]->GetFunction("gaus")->GetParError(2);
601 } else { chicos_1b3bg[c][i] = 0.0; chicos_1b3bgerr[c][i] = 0.0; sigmacos_1b3bg[c][i] = 0.0; sigmacos_1b3bgerr[c][i] = 0.0;}
602
603 }
604
605 if (hchicos_2by3bg[c][i]->Integral() > 100) {
606 gstatus all2bgstat;
607 fit(hchicos_2by3bg[c][i], pdg.data(), all2bgstat);
608 if (all2bgstat == OK) {
609 chicos_2b3bg[c][i] = hchicos_2by3bg[c][i]->GetFunction("gaus")->GetParameter(1);
610 chicos_2b3bgerr[c][i] = hchicos_2by3bg[c][i]->GetFunction("gaus")->GetParError(1);
611 sigmacos_2b3bg[c][i] = hchicos_2by3bg[c][i]->GetFunction("gaus")->GetParameter(2);
612 sigmacos_2b3bgerr[c][i] = hchicos_2by3bg[c][i]->GetFunction("gaus")->GetParError(2);
613 } else { chicos_2b3bg[c][i] = 0.0; chicos_2b3bgerr[c][i] = 0.0; sigmacos_2b3bg[c][i] = 0.0; sigmacos_2b3bgerr[c][i] = 0.0;}
614
615 }
616
617 if (hchicos_3by3bg[c][i]->Integral() > 100) {
618 gstatus all3bgstat;
619 fit(hchicos_3by3bg[c][i], pdg.data(), all3bgstat);
620 if (all3bgstat == OK) {
621 chicos2[c][i] = hchicos_3by3bg[c][i]->GetFunction("gaus")->GetParameter(1);
622 chicos2err[c][i] = hchicos_3by3bg[c][i]->GetFunction("gaus")->GetParError(1);
623 sigmacos2[c][i] = hchicos_3by3bg[c][i]->GetFunction("gaus")->GetParameter(2);
624 sigmacos2err[c][i] = hchicos_3by3bg[c][i]->GetFunction("gaus")->GetParError(2);
625 } else { chicos2[c][i] = 0.0; chicos2err[c][i] = 0.0; sigmacos2[c][i] = 0.0; sigmacos2err[c][i] = 0.0;}
626
627 }
628 }
629 }
630
631 plotDist(hchicos_allbg, Form("fits_chi_vscos_allbg_%s_%s", pdg.data(), suffix.data()), m_cosBins);
632 plotDist(hchicos_1by3bg, Form("fits_chi_vscos_1by3bg_%s_%s", pdg.data(), suffix.data()), m_cosBins);
633 plotDist(hchicos_2by3bg, Form("fits_chi_vscos_2by3bg_%s_%s", pdg.data(), suffix.data()), m_cosBins);
634 plotDist(hchicos_3by3bg, Form("fits_chi_vscos_3by3bg_%s_%s", pdg.data(), suffix.data()), m_cosBins);
635
636 const double cosstep = 2.0 / m_cosBins;
637 std::vector<double> cosArray(m_cosBins), cosArrayErr(m_cosBins);
638 for (int i = 0; i < m_cosBins; ++i) {
639 cosArray[i] = -1.0 + (i * cosstep + cosstep / 2.0); //finding bin centre
640 cosArrayErr[i] = 0.0;
641 }
642
643 TGraphErrors grchicos(m_cosBins, cosArray.data(), chicos[0].data(), cosArrayErr.data(), chicoserr[0].data());
644 TGraphErrors grchicos_1b3bg(m_cosBins, cosArray.data(), chicos_1b3bg[0].data(), cosArrayErr.data(), chicos_1b3bgerr[0].data());
645 TGraphErrors grchicos_2b3bg(m_cosBins, cosArray.data(), chicos_2b3bg[0].data(), cosArrayErr.data(), chicos_2b3bgerr[0].data());
646 TGraphErrors grchicos2(m_cosBins, cosArray.data(), chicos2[0].data(), cosArrayErr.data(), chicos2err[0].data());
647
648 TGraphErrors grchicosn(m_cosBins, cosArray.data(), chicos[1].data(), cosArrayErr.data(), chicoserr[1].data());
649 TGraphErrors grchicos_1b3bgn(m_cosBins, cosArray.data(), chicos_1b3bg[1].data(), cosArrayErr.data(), chicos_1b3bgerr[1].data());
650 TGraphErrors grchicos_2b3bgn(m_cosBins, cosArray.data(), chicos_2b3bg[1].data(), cosArrayErr.data(), chicos_2b3bgerr[1].data());
651 TGraphErrors grchicos2n(m_cosBins, cosArray.data(), chicos2[1].data(), cosArrayErr.data(), chicos2err[1].data());
652
653 TGraphErrors grsigmacos(m_cosBins, cosArray.data(), sigmacos[0].data(), cosArrayErr.data(), sigmacoserr[0].data());
654 TGraphErrors grsigmacos_1b3bg(m_cosBins, cosArray.data(), sigmacos_1b3bg[0].data(), cosArrayErr.data(),
655 sigmacos_1b3bgerr[0].data());
656 TGraphErrors grsigmacos_2b3bg(m_cosBins, cosArray.data(), sigmacos_2b3bg[0].data(), cosArrayErr.data(),
657 sigmacos_2b3bgerr[0].data());
658 TGraphErrors grsigmacos2(m_cosBins, cosArray.data(), sigmacos2[0].data(), cosArrayErr.data(), sigmacos2err[0].data());
659
660 TGraphErrors grsigmacosn(m_cosBins, cosArray.data(), sigmacos[1].data(), cosArrayErr.data(), sigmacoserr[1].data());
661 TGraphErrors grsigmacos_1b3bgn(m_cosBins, cosArray.data(), sigmacos_1b3bg[1].data(), cosArrayErr.data(),
662 sigmacos_1b3bgerr[1].data());
663 TGraphErrors grsigmacos_2b3bgn(m_cosBins, cosArray.data(), sigmacos_2b3bg[1].data(), cosArrayErr.data(),
664 sigmacos_2b3bgerr[1].data());
665 TGraphErrors grsigmacos2n(m_cosBins, cosArray.data(), sigmacos2[1].data(), cosArrayErr.data(), sigmacos2err[1].data());
666
667 TLine line0(-1, 0, 1, 0);
668 line0.SetLineStyle(kDashed);
669 line0.SetLineColor(kRed);
670
671 TLine line1(-1, 1, 1, 1);
672 line1.SetLineStyle(kDashed);
673 line1.SetLineColor(kRed);
674
675 TString ptype("");
676 double mass = 0.0;
677 if (pdg == "pion") {ptype += "#pi"; mass = Const::pion.getMass();}
678 else if (pdg == "kaon") {ptype += "K"; mass = Const::kaon.getMass();}
679 else if (pdg == "proton") { ptype += "p"; mass = Const::proton.getMass();}
680 else if (pdg == "muon") {ptype += "#mu"; mass = Const::muon.getMass();}
681 else if (pdg == "electron") {ptype += "e"; mass = Const::electron.getMass();}
682 if (mass == 0.0) B2FATAL("Mass of particle " << pdg.data() << " is zero");
683
684 int bglow = 0, bghigh = 0;
685 double bgstep = (m_bgMax - m_bgMin) / m_bgBins;
686
687 TLegend lchi(0.7, 0.75, 0.8, 0.85);
688 lchi.SetBorderSize(0);
689 lchi.SetFillColor(kYellow);
690 lchi.AddEntry(&grchicos, ptype + "^{+}", "pc");
691 lchi.AddEntry(&grchicosn, ptype + "^{-}", "pc");
692
693 TCanvas cchi(Form("cchi_%s", suffix.data()), "cchi", 700, 600);
694 cchi.Divide(2, 2);
695 cchi.cd(1);
696 FormatGraph(grchicos, 0, Form("all (%s) bg bins, p =(%0.02f, %0.02f)", pdg.data(), m_bgMin * mass, m_bgMax * mass));
697 FormatGraph(grchicosn, 1);
698 grchicos.Draw("AP");
699 grchicosn.Draw("P,same");
700 line0.Draw("same");
701 lchi.Draw("same");
702
703 cchi.cd(2);
704 bghigh = int(m_bgBins / 3);
705 FormatGraph(grchicos_1b3bg, 0, Form("first 1/3 bg bins, p =(%0.02f, %0.02f)", m_bgMin * mass, (m_bgMin + bghigh * bgstep) * mass));
706 FormatGraph(grchicos_1b3bgn, 1);
707 grchicos_1b3bg.Draw("AP");
708 grchicos_1b3bgn.Draw("P,same");
709 line0.Draw("same");
710 lchi.Draw("same");
711
712 cchi.cd(3);
713 bglow = int(m_bgBins / 3);
714 bghigh = int(2 * m_bgBins / 3);
715 FormatGraph(grchicos_2b3bg, 0, Form("second 1/3 bg bins, p =(%0.02f, %0.02f)", (m_bgMin + bglow * bgstep) * mass,
716 (m_bgMin + bghigh * bgstep) * mass));
717 FormatGraph(grchicos_2b3bgn, 1);
718 grchicos_2b3bg.Draw("AP");
719 grchicos_2b3bgn.Draw("P,same");
720 line0.Draw("same");
721 lchi.Draw("same");
722
723 cchi.cd(4);
724 bglow = int(2 * m_bgBins / 3);
725 FormatGraph(grchicos2, 0, Form("third 1/3 bg bins, p =(%0.02f, %0.02f)", (m_bgMin + bglow * bgstep) * mass, m_bgMax * mass));
726 FormatGraph(grchicos2n, 1);
727 grchicos2.Draw("AP");
728 grchicos2n.Draw("P,same");
729 line0.Draw("same");
730 lchi.Draw("same");
731 cchi.SaveAs(Form("plots/HadronCal/Monitoring/mean_chi_vscos_inbg_%s_%s.pdf", pdg.data(), suffix.data()));
732
733 cchi.cd(1);
734 FormatGraph(grsigmacos, 2, Form("all (%s) bg bins, p =(%0.02f, %0.02f)", pdg.data(), m_bgMin * mass, m_bgMax * mass));
735 FormatGraph(grsigmacosn, 1);
736 grsigmacos.Draw("AP");
737 grsigmacosn.Draw("P,same");
738 line1.Draw("same");
739 lchi.Draw("same");
740
741 cchi.cd(2);
742 bghigh = int(m_bgBins / 3);
743 FormatGraph(grsigmacos_1b3bg, 2, Form("first 1/3 bg bins, p =(%0.02f, %0.02f)", m_bgMin * mass,
744 (m_bgMin + bghigh * bgstep) * mass));
745 FormatGraph(grsigmacos_1b3bgn, 1);
746 grsigmacos_1b3bg.Draw("AP");
747 grsigmacos_1b3bgn.Draw("P,same");
748 line1.Draw("same");
749 lchi.Draw("same");
750
751 cchi.cd(3);
752 bglow = int(m_bgBins / 3);
753 bghigh = int(2 * m_bgBins / 3);
754 FormatGraph(grsigmacos_2b3bg, 2, Form("second 1/3 bg bins, p =(%0.02f, %0.02f)", (m_bgMin + bglow * bgstep) * mass,
755 (m_bgMin + bghigh * bgstep) * mass));
756 FormatGraph(grsigmacos_2b3bgn, 1);
757 grsigmacos_2b3bg.Draw("AP");
758 grsigmacos_2b3bgn.Draw("P,same");
759 line1.Draw("same");
760 lchi.Draw("same");
761
762 cchi.cd(4);
763 bglow = int(2 * m_bgBins / 3);
764 FormatGraph(grsigmacos2, 2, Form("third 1/3 bg bins, p =(%0.02f, %0.02f)", (m_bgMin + bglow * bgstep) * mass, m_bgMax * mass));
765 FormatGraph(grsigmacos2n, 1);
766 grsigmacos2.Draw("AP");
767 grsigmacos2n.Draw("P,same");
768 line1.Draw("same");
769 lchi.Draw("same");
770 cchi.SaveAs(Form("plots/HadronCal/Monitoring/sigma_chi_vscos_inbg_%s_%s.pdf", pdg.data(), suffix.data()));
771
772}
Class to hold the hadron saturation functions.
double I2D(double cosTheta, double I) const
hadron saturation parameterization part 2
double D2I(double cosTheta, double D) const
hadron saturation parameterization part 1
void setParameters()
set the parameters
Class to hold the prediction of mean as a function of beta-gamma (bg)
double getMean(double bg)
Return the predicted mean value as a function of beta-gamma (bg)
void setParameters(std::string infile)
set the parameters from file
Class to hold the prediction of resolution depending dE/dx, nhit, and cos(theta)
double getSigma(double dedx, double nhit, double cos, double timereso)
Return the predicted resolution depending on dE/dx, nhit, and cos(theta)
double cosPrediction(double cos)
Return sigma from the cos parameterization.
double nhitPrediction(double nhit)
Return sigma from the nhit parameterization.
void setParameters(std::string infile)
set the parameters from file
static const ChargedStable muon
muon particle
Definition Const.h:660
static const ChargedStable pion
charged pion particle
Definition Const.h:661
static const ChargedStable proton
proton particle
Definition Const.h:663
static const ChargedStable kaon
charged kaon particle
Definition Const.h:662
static const ChargedStable electron
electron particle
Definition Const.h:659
void fit(TH1F *&hist, const std::string &pdg, gstatus &status)
function to fit the histograms
void clearVars()
function to clear the variables
void defineHisto(std::vector< TH1F * > &htemp, const std::string &svar, const std::string &stype, const std::string &pdg)
function to define histograms
std::vector< double > m_sumcos
variables to add cos values
std::vector< double > m_suminj
variables to add injection time
int m_nhitBins
bins for nhits
int m_injBins
bins for injection time
int m_cosBins
bins for cosine
double getParticleMass(const std::string &particle)
function to get the particle mass
void deleteHistos(std::vector< TH1F * > &htemp)
function to delete the histograms
double m_cosMax
max range of cosine
void printCanvasCos(std::map< int, std::vector< TH1F * > > &hchicos_allbg, std::map< int, std::vector< TH1F * > > &hchicos_1by3bg, std::map< int, std::vector< TH1F * > > &hchicos_2by3bg, std::map< int, std::vector< TH1F * > > &hchicos_3by3bg, const std::string &particle, const std::string &suffix)
function to draw the dedx vs costh histograms
int m_bgBins
bins for bg
std::vector< double > m_means
mean variable
std::vector< double > m_errors
error variable
std::vector< int > m_sumsize
size of the bg bins
void plotDist(std::map< int, std::vector< TH1F * > > &hist, const std::string &suffix, int bins)
function to plot the map of histograms
HadronBgPrep()
Constructor: Sets the description, the properties and the parameters of the algorithm.
void FormatGraph(TGraphErrors &gr, int flag, const std::string &name="")
function to set graph cosmetics
double m_injMax
max range of injection time
double m_nhitMax
max range of nhits
std::vector< double > m_sumres_square
variables to add square of resolution
double m_injMin
min range of injection time
void setPars(TFile *&outfile, std::string pdg, std::vector< TH1F * > &hdedx_bg, std::vector< TH1F * > &hchi_bg, std::vector< TH1F * > &hionzsigma_bg, std::map< int, std::vector< TH1F * > > &hchi_inj)
function to fill the parameters like mean and reso in the tree
double m_bgMax
max range of bg
std::vector< int > m_injsize
size of the injection bins
double m_bgMin
min range of bg
double m_cosMin
min range of cosine
double m_cut
cut to clean protons
std::vector< double > m_sumbg
variables to add bg values
void fitGaussianWRange(TH1F *&temphist, gstatus &status, double sigmaR)
function to perform gauss fit for input histogram
double m_nhitMin
min range of nhits
void prepareSample(std::shared_ptr< TTree > hadron, TFile *&outfile, const std::string &suffix, const std::string &bgcurvefile, const std::string &bgsigmafile, const std::string &pdg, bool ismakePlots)
function to prepare sample for monitoring plots, bg curve fitting and sigma vs ionz fitting
constexpr T square(const T &x)
Calculate the square of the input.
Definition MathHelpers.h:21
Abstract base class for different kinds of events.