Belle II Software development
CDCDedx1DCellAlgorithm.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/CDCDedx1DCellAlgorithm.h>
10
11#include <TCanvas.h>
12#include <TLegend.h>
13#include <TMath.h>
14#include <TPad.h>
15#include <TRandom.h>
16#include <TStyle.h>
17#include <iostream>
18#include <cmath>
19
20using namespace Belle2;
21
22//-----------------------------------------------------------------
23// Implementation
24//-----------------------------------------------------------------
26 CalibrationAlgorithm("CDCDedxElectronCollector"),
27 m_eaMin(-TMath::Pi() / 2),
28 m_eaMax(+TMath::Pi() / 2),
29 m_eaB(316),
30 m_dedxMin(0.0),
31 m_dedxMax(5.0),
32 m_dedxBin(250),
33 m_ptMax(8.0),
34 m_cosMax(1.0),
35 m_truncMin(0.05),
36 m_truncMax(0.75),
37 m_binSplit(3),
38 m_chargeType(0),
39 m_adjustFac(1.00),
40 isFixTrunc(false),
41 isVarBins(true),
42 isRotSymm(false),
43 isMakePlots(true),
44 isPrintLog(false),
45 isMerge(true),
46 m_suffix("")
47{
48 // Set module properties
49 setDescription("A calibration algorithm for the CDC dE/dx entrance angle cleanup correction");
50}
51
52//-----------------------------------------------------------------
53// Run the calibration
54//-----------------------------------------------------------------
56{
57
59
60 if (!m_DBOneDCell.isValid())
61 B2FATAL("There is no valid previous payload for CDCDedx1DCell");
62
63 //reading radiative electron collector TREE
64 auto ttree = getObjectPtr<TTree>("tree");
65 if (!ttree) return c_NotEnoughData;
66
67 std::vector<double>* dedxhit = 0, *enta = 0;
68 std::vector<int>* layer = 0;
69 double pt = 0, costh = 0;
70
71 ttree->SetBranchAddress("dedxhit", &dedxhit);
72 ttree->SetBranchAddress("entaRS", &enta);
73 ttree->SetBranchAddress("layer", &layer);
74 ttree->SetBranchAddress("pt", &pt);
75 ttree->SetBranchAddress("costh", &costh);
76
77 //repair nbins if they are not divisible accordingly
80
81 //Settings of variables bins
83
84 if (isPrintLog) {
85 B2INFO("Superlayer0 bins: " << m_eaBinLocal[0]);
86 B2INFO("Superlayer1 bins: " << m_eaBinLocal[1]);
87 B2INFO("Superlayers2-8 bins: " << m_eaBinLocal[2]);
88 }
89
90 // dedxhit vector to store dE/dx values for each enta bin
91 std::array<std::vector<TH1D*>, m_kNGroups> hdedxhit;
92 std::array<TH1D*, m_kNGroups> hdedxlay{};
93 std::array<TH1D*, m_kNGroups> hentalay{};
94
95 TH2D* hptcosth = new TH2D("hptcosth", "pt vs costh dist;pt;costh", 1000, -8.0, 8.0, 1000, -1.0, 1.0);
96
97 defineHisto(hdedxhit, hdedxlay, hentalay);
98
99 //Star filling histogram defined above
100 for (int i = 0; i < ttree->GetEntries(); ++i) {
101
102 ttree->GetEvent(i);
103
104 if (std::abs(costh) > m_cosMax) continue;
105
106 // remove wide angle bhabha tracks
107 // double mom = pt/sqrt(1-costh*costh);
108 // if(abs(pt)<2.4 && abs(mom)>3.6)continue;
109
110 if (std::abs(pt) > m_ptMax) continue;
111
112 //change to random 10%
113 int rand = gRandom->Integer(100);
114 if (rand < 10) hptcosth->Fill(pt, costh);
115
116 for (unsigned int j = 0; j < dedxhit->size(); ++j) {
117
118 if (dedxhit->at(j) == 0) continue;
119
120 double entaval = enta->at(j);
121 //Mapped bin corresponds to entaval
122 int ibin = std::floor((entaval - m_eaMin) / m_eaBW);
123 if (ibin < 0 || ibin >= m_eaBin) continue;
124
125 int lay = layer->at(j);
126 int mL = (lay < 8) ? 0 : ((lay < 14) ? 1 : 2);
127
128 hdedxlay[mL]->Fill(dedxhit->at(j));
129 if (rand < 10) hentalay[mL]->Fill(entaval);
130
131 int jbinea = ibin;
132 if (isVarBins) jbinea = m_binIndex[mL].at(ibin);
133 hdedxhit[mL][jbinea]->Fill(dedxhit->at(j));
134 }
135 }
136 std::vector<std::vector<double>> tempconst(m_kNGroups);
137 for (int il = 0; il < m_kNGroups; il++) {
138
139 int minlay = 0, maxlay = 0;
140
141 if (isFixTrunc) {
142 getTruncatedBins(hdedxlay[il], minlay, maxlay);
143 hdedxlay[il]->SetTitle(Form("%s;%d;%d", hdedxlay[il]->GetTitle(), minlay, maxlay));
144 }
145 tempconst[il].reserve(m_eaBinLocal[il]);
146
147 for (int iea = 0; iea < m_eaBinLocal[il]; iea++) {
148
149 int jea = iea;
150
151 // rotation symmtery for 1<->3 and 4<->2 but only symmetric bin
152 if (!isVarBins && isRotSymm) jea = rotationalBin(m_eaBinLocal[il], jea);
153
154 TH1D* htemp = (TH1D*)hdedxhit[il][jea]->Clone(Form("h_%s_b%d_c", m_label[il].data(), jea));
155
156 int minbin = 1, maxbin = 1;
157 if (isFixTrunc) {
158 minbin = minlay;
159 maxbin = maxlay;
160 } else {
161 //extract truncation window per bin
162 getTruncatedBins(htemp, minbin, maxbin);
163 }
164
165 double dedxmean;
166 dedxmean = getTruncationMean(htemp, minbin, maxbin);
167 tempconst[il].push_back(dedxmean);
168
169 hdedxhit[il][iea]->SetTitle(Form("%s, #mu_{truc} = %0.5f;%d;%d", hdedxhit[il][iea]->GetTitle(), dedxmean, minbin, maxbin));
170 }
171
172 //Expending constants
173 std::vector<double>layerconst;
174 layerconst.reserve(m_eaBin);
175
176 for (int iea = 0; iea < m_eaBin; iea++) {
177 int jea = iea;
178 if (isVarBins) jea = m_binIndex[il].at(iea);
179 layerconst.push_back(tempconst[il].at(jea));
180 }
181
182 m_onedcors.push_back(layerconst);
183
184 layerconst.clear();
185
186 }
187
188 // plot the rel constants var/sym bins
189 if (isMakePlots) plotRelConst(tempconst);
190
191 //Saving final constants
193
194 if (isMakePlots) {
195
196 //1. dE/dx dist. for entrance angle bins
197 plotdedxHist(hdedxhit);
198
199 //3. Inner and Outer layer dE/dx distributions
200 plotLayerDist(hdedxlay);
201
202 //4. entrance angle distribution sym/var bins
203 plotQaPars(hentalay, hptcosth);
204
205 //6. draw the final constants
207
208 //7. plot statistics related plots here
210 }
211
212 for (int il = 0; il < m_kNGroups; il++) {
213 delete hentalay[il];
214 delete hdedxlay[il];
215 for (int iea = 0; iea < m_eaBinLocal[il]; iea++)
216 delete hdedxhit[il][iea];
217 }
218
219 delete hptcosth;
220 m_eaBinLocal.clear();
221 for (int il = 0; il < m_kNGroups; il++) {
222 m_binValue[il].clear();
223 m_binIndex[il].clear();
224
225 }
226
227 m_suffix.clear();
228 tempconst.clear();
229 m_onedcors.clear();
230 return c_OK;
231}
232
233//--------------------------------------------------
235{
236
237 int cruns = 0;
238 for (auto expRun : getRunList()) {
239 if (cruns == 0) B2INFO("CDCDedxBadWires: start exp " << expRun.first << " and run " << expRun.second << "");
240 cruns++;
241 }
242
243 const auto erStart = getRunList()[0];
244 int estart = erStart.first;
245 int rstart = erStart.second;
246
247 const auto erEnd = getRunList()[cruns - 1];
248 int eend = erEnd.first;
249 int rend = erEnd.second;
250
251 updateDBObjPtrs(1, rstart, estart);
252
253 m_runExp = Form("Range (%d:%d,%d:%d)", estart, rstart, eend, rend);
254 if (m_suffix.length() > 0) m_suffix = Form("%s_e%d_r%dr%d", m_suffix.data(), estart, rstart, rend);
255 else m_suffix = Form("e%d_r%dr%d", estart, rstart, rend);
256}
257
258//--------------------------------------------------
260{
261
262 std::map<int, std::vector<double>> bounds;
263 std::map<int, std::vector<int>> steps;
264
265 // number of intervals for each unique configuration
266 const std::array<int, 2> nDev{8, 4};
267
268 // config 0 -> used for SL = 0,1
269 bounds[0] = {0, 108, 123, 133, 158, 183, 193, 208, 316};
270 steps[0] = {9, 3, 2, 1, 1, 2, 3, 9};
271
272 // config 1 -> used for SL = 2-8
273 bounds[1] = {0, 38, 158, 278, 316};
274 steps[1] = {2, 1, 1, 2};
275
276 // map SL -> configuration
277 const std::array<int, m_kNGroups> configIndex = {0, 0, 1};
278
279 for (int il = 0; il < m_kNGroups; il++) {
280
281 int icfg = configIndex[il];
282
283 std::vector<double> scaledBounds = bounds[icfg];
284 for (int ibin = 0; ibin <= nDev[icfg]; ibin++) {
285 scaledBounds[ibin] = scaledBounds[ibin] * m_binSplit;
286 }
287
288 int ieaprime = -1, temp = -99, ibin = 0;
289
290 double pastbin = m_eaMin;
291 m_binValue[il].push_back(pastbin);
292
293 for (int iea = 0; iea < m_eaBin; iea++) {
294
295 if (isVarBins) {
296 if (iea % int(scaledBounds[ibin + 1]) == 0 && iea > 0) ibin++;
297 int diff = iea - int(scaledBounds[ibin]);
298 if (diff % steps[icfg][ibin] == 0) ieaprime++;
299 } else {
300 ieaprime = iea;
301 }
302
303 m_binIndex[il].push_back(ieaprime);
304
305 if (ieaprime != temp) {
306 double binwidth = m_eaBW;
307 if (isVarBins) binwidth = m_eaBW * steps[icfg][ibin];
308 double binvalue = pastbin + binwidth;
309 pastbin = binvalue;
310 if (std::abs(binvalue) < 1e-5) binvalue = 0;
311 m_binValue[il].push_back(binvalue);
312 }
313 temp = ieaprime;
314 }
315
316 m_eaBinLocal.push_back(int(m_binValue[il].size()) - 1);
317 }
318
319 if (isMakePlots) plotMergeFactor(bounds, nDev, steps);
320}
321
322//--------------------------------------------------
323
324void CDCDedx1DCellAlgorithm::defineHisto(std::array<std::vector<TH1D*>, m_kNGroups>& hdedxhit,
325 std::array<TH1D*, m_kNGroups>& hdedxlay,
326 std::array<TH1D*, m_kNGroups>& hentalay)
327{
328 for (int il = 0; il < m_kNGroups; il++) {
329
330 std::string title = Form("dedxhit dist (%s): %s ; dedxhit;entries", m_label[il].data(), m_runExp.data());
331 hdedxlay[il] = new TH1D(Form("hdedxlay%s", m_label[il].data()), "", m_dedxBin, m_dedxMin, m_dedxMax);
332 hdedxlay[il]->SetTitle(title.c_str());
333
334 Double_t* nvarBins = m_binValue[il].data();
335
336 if (isVarBins)
337 title = Form("entaRS dist (variable bins): %s: (%s); entaRS (#alpha);entries", m_label[il].data(), m_runExp.data());
338 else
339 title = Form("entaRS dist (sym. bins): %s: (%s); entaRS (#alpha);entries", m_label[il].data(), m_runExp.data());
340
341 hentalay[il] = new TH1D(Form("hentalay%s", m_label[il].data()), "", m_eaBinLocal[il], nvarBins);
342 hentalay[il]->SetTitle(title.c_str());
343
344 for (int iea = 0; iea < m_eaBinLocal[il]; iea++) {
345
346 double min = m_binValue[il].at(iea);
347 double max = m_binValue[il].at(iea + 1);
348 double width = max - min;
349
350 if (isPrintLog)
351 B2INFO("bin: " << iea << " ], min:" << min << " , max: " << max << " , width: " << width);
352
353 title = Form("%s: entaRS = (%0.03f to %0.03f)", m_label[il].data(), min, max);
354
355 hdedxhit[il].push_back(new TH1D(Form("hdedxhit_%s_bin%d", m_label[il].data(), iea),
357
358 hdedxhit[il][iea]->SetTitle(title.c_str());
359 }
360 }
361}
362
363//--------------------------------------------------
364void CDCDedx1DCellAlgorithm::getTruncatedBins(TH1D* hist, int& binlow, int& binhigh)
365{
366
367 //calculating truncation average
368 double sum = hist->Integral();
369 if (sum <= 0 || hist->GetNbinsX() <= 0) {
370 binlow = 1; binhigh = 1;
371 return ;
372 }
373
374 binlow = 1.0; binhigh = 1.0;
375 double sumPer5 = 0.0, sumPer75 = 0.0;
376 for (int ibin = 1; ibin <= hist->GetNbinsX(); ibin++) {
377 double bcdedx = hist->GetBinContent(ibin);
378 if (sumPer5 <= m_truncMin * sum) {
379 sumPer5 += bcdedx;
380 binlow = ibin;
381 }
382 if (sumPer75 <= m_truncMax * sum) {
383 sumPer75 += bcdedx;
384 binhigh = ibin;
385 }
386 }
387 return;
388}
389
390//--------------------------
391double CDCDedx1DCellAlgorithm::getTruncationMean(TH1D* hist, int binlow, int binhigh)
392{
393
394 //calculating truncation average
395 if (hist->Integral() < 100) return 1.0;
396
397 if (binlow <= 0 || binhigh > hist->GetNbinsX())return 1.0;
398
399 double binweights = 0., sumofbc = 0.;
400 for (int ibin = binlow; ibin <= binhigh; ibin++) {
401 double bcdedx = hist->GetBinContent(ibin);
402 if (bcdedx > 0) {
403 binweights += (bcdedx * hist->GetBinCenter(ibin));
404 sumofbc += bcdedx;
405 }
406 }
407 if (sumofbc > 0) return binweights / sumofbc;
408 else return 1.0;
409}
410
411//--------------------------------------------------
413{
414
415 B2INFO("dE/dx one cell calibration: Generating payloads");
416
417 for (unsigned int il = 0; il < m_kNGroups; il++) {
418
419 if (isMerge) {
420 unsigned int nbins = m_DBOneDCell->getNBins(getRepresentativeLayer(il));
421
422 if (int(nbins) != m_eaBin)
423 B2ERROR("merging failed because of unmatch bins (old " << m_eaBin << " new " << nbins << ")");
424
425 for (unsigned int iea = 0; iea < nbins; iea++) {
426 double prev = m_DBOneDCell->getMean(getRepresentativeLayer(il), iea);
427 m_onedcors[il][iea] *= prev;
428 }
429 }
430
431 double binsize = TMath::Pi() / m_onedcors[il].size();
432
433 auto computeAverages = [&](double angLow, double angHigh) {
434 unsigned int binLow = std::floor((angLow + TMath::Pi() / 2.0) / binsize);
435 unsigned int binHigh = std::floor((angHigh + TMath::Pi() / 2.0) / binsize);
436 double sum_new = 0.0, sum_prev = 0.0;
437 int count = 0;
438
439 for (unsigned int iea = binLow; iea < binHigh; ++iea) {
440 sum_new += m_onedcors[il][iea];
441 sum_prev += m_DBOneDCell->getMean(getRepresentativeLayer(il), iea);
442 ++count;
443 }
444
445 double avg_new = (count > 0) ? sum_new / count : 1.0;
446 double avg_prev = (count > 0) ? sum_prev / count : 1.0;
447 return std::make_pair(avg_new, avg_prev);
448 };
449
450 double negLow = -0.5, negHigh = -0.2;
451 double posLow = 0.2, posHigh = 0.5;
452
453 if (il == 2) {
454 negLow = -0.75; negHigh = -0.25;
455 posLow = 0.25; posHigh = 0.75;
456 }
457
458 auto [avgNewNeg, avgPrevNeg] = computeAverages(negLow, negHigh);
459 auto [avgNewPos, avgPrevPos] = computeAverages(posLow, posHigh);
460
461 double avgNew = (avgNewNeg + avgNewPos) / 2.0;
462 double avgPrev = (avgPrevNeg + avgPrevPos) / 2.0;
463 double scaleFactor = avgPrev / avgNew;
464
465 for (int iea = 0; iea < m_eaBin; iea++) {
466 m_onedcors[il][iea] *= scaleFactor;
467 }
468
469 if (m_chargeType > 0)
470 for (int ie = 0; ie < m_eaBin / 2; ie++) m_onedcors[il][ie] *= m_adjustFac;
471 if (m_chargeType < 0)
472 for (int ie = m_eaBin / 2; ie < m_eaBin; ie++) m_onedcors[il][ie] *= m_adjustFac;
473
474 }
475
476 //Saving constants
477 B2INFO("dE/dx Calibration done for CDCDedx1DCell");
478 std::vector<unsigned int> layerToGroup(56);
479
480 for (unsigned int layer = 0; layer < 56; layer++) {
481 if (layer < 8) layerToGroup[layer] = 0; // SL0
482 else if (layer < 14) layerToGroup[layer] = 1; // SL1
483 else layerToGroup[layer] = 2; // SL2-8
484 }
485
486 CDCDedx1DCell* gain = new CDCDedx1DCell(0, m_onedcors, layerToGroup);
487 saveCalibration(gain, "CDCDedx1DCell");
488}
489
490//--------------------------------------------------
491void CDCDedx1DCellAlgorithm::plotMergeFactor(std::map<int, std::vector<double>> bounds,
492 const std::array<int, 2> nDev,
493 std::map<int, std::vector<int>> steps)
494{
495 TCanvas cmfactor("cmfactor", "Merging factors", 800, 400);
496 cmfactor.Divide(2, 1);
497
498 std::array<TH1I*, 2> hists{};
499
500 for (int icfg = 0; icfg < 2; icfg++) {
501 Double_t* nvarBins = bounds[icfg].data();
502
503 hists[icfg] = new TH1I(Form("hist_cfg%d", icfg), "", nDev[icfg], nvarBins);
504
505 if (icfg == 0)
506 hists[icfg]->SetTitle("Merging factor for SL0/SL1 bins;binindex;merge-factors");
507 else
508 hists[icfg]->SetTitle("Merging factor for SL2-8 bins;binindex;merge-factors");
509
510 for (int ibin = 0; ibin < nDev[icfg]; ibin++) {
511 hists[icfg]->SetBinContent(ibin + 1, steps[icfg][ibin]);
512 }
513
514 cmfactor.cd(icfg + 1);
515 hists[icfg]->SetFillColor(kYellow);
516 hists[icfg]->Draw("hist");
517 }
518
519 cmfactor.SaveAs(Form("cdcdedx_1dcell_mergefactor_%s.pdf", m_suffix.data()));
520 cmfactor.SaveAs(Form("cdcdedx_1dcell_mergefactor_%s.root", m_suffix.data()));
521
522 for (int icfg = 0; icfg < 2; icfg++) {
523 delete hists[icfg];
524 }
525}
526
527//--------------------------------------------------
528void CDCDedx1DCellAlgorithm::plotdedxHist(std::array<std::vector<TH1D*>, 3>& hdedxhit)
529{
530
531 TCanvas ctmp("tmp", "tmp", 1200, 1200);
532 ctmp.Divide(4, 4);
533 std::stringstream psname;
534
535 psname << Form("cdcdedx_1dcell_dedxhit_%s.pdf[", m_suffix.data());
536 ctmp.Print(psname.str().c_str());
537 psname.str("");
538 psname << Form("cdcdedx_1dcell_dedxhit_%s.pdf", m_suffix.data());
539
540 for (int il = 0; il < m_kNGroups; il++) {
541
542 for (int jea = 0; jea < m_eaBinLocal[il]; jea++) {
543
544 int minbin = std::stoi(hdedxhit[il][jea]->GetXaxis()->GetTitle());
545 int maxbin = std::stoi(hdedxhit[il][jea]->GetYaxis()->GetTitle());
546
547 ctmp.cd(jea % 16 + 1);
548 hdedxhit[il][jea]->SetFillColor(4 + il);
549
550 hdedxhit[il][jea]->SetTitle(Form("%s;dedxhit;entries", hdedxhit[il][jea]->GetTitle()));
551 hdedxhit[il][jea]->DrawClone("hist");
552 TH1D* htempC = (TH1D*)hdedxhit[il][jea]->Clone(Form("%sc2", hdedxhit[il][jea]->GetName()));
553 htempC->GetXaxis()->SetRange(minbin, maxbin);
554 htempC->SetFillColor(kGray);
555 htempC->DrawClone("same hist");
556
557 if (jea % 16 == 15 || (jea == m_eaBinLocal[il] - 1)) {
558 ctmp.Print(psname.str().c_str());
559 gPad->Clear("D");
560 ctmp.Clear("D");
561 }
562 delete htempC;
563 }
564 }
565 psname.str("");
566 psname << Form("cdcdedx_1dcell_dedxhit_%s.pdf]", m_suffix.data());
567 ctmp.Print(psname.str().c_str());
568}
569
570//--------------------------------------------------
571void CDCDedx1DCellAlgorithm::plotLayerDist(std::array<TH1D*, 3>& hdedxlay)
572{
573
574 TCanvas cdedxlayer("layerdedxhit", "Inner and Outer Layer dedxhit dist", 900, 400);
575 cdedxlayer.Divide(3, 1);
576
577 for (int il = 0; il < m_kNGroups; il++) {
578 int minlay = 0, maxlay = 0;
579 if (isFixTrunc) {
580 minlay = std::stoi(hdedxlay[il]->GetXaxis()->GetTitle());
581 maxlay = std::stoi(hdedxlay[il]->GetYaxis()->GetTitle());
582 double lowedge = hdedxlay[il]->GetXaxis()->GetBinLowEdge(minlay);
583 double upedge = hdedxlay[il]->GetXaxis()->GetBinUpEdge(maxlay);
584 hdedxlay[il]->SetTitle(Form("%s, trunc #rightarrow: %0.02f - %0.02f;dedxhit;entries", hdedxlay[il]->GetTitle(), lowedge, upedge));
585 }
586
587 cdedxlayer.cd(il + 1);
588 hdedxlay[il]->SetFillColor(kYellow);
589 hdedxlay[il]->Draw("histo");
590
591 if (isFixTrunc) {
592 TH1D* hdedxlayC = (TH1D*)hdedxlay[il]->Clone(Form("hdedxlayC%d", il));
593 hdedxlayC->GetXaxis()->SetRange(minlay, maxlay);
594 hdedxlayC->SetFillColor(kAzure + 1);
595 hdedxlayC->Draw("same histo");
596 }
597 }
598
599 cdedxlayer.SaveAs(Form("cdcdedx_1dcell_dedxlayer_%s.pdf", m_suffix.data()));
600 cdedxlayer.SaveAs(Form("cdcdedx_1dcell_dedxlayer_%s.root", m_suffix.data()));
601}
602
603//--------------------------------------------------
604void CDCDedx1DCellAlgorithm::plotQaPars(std::array<TH1D*, 3>& hentalay, TH2D* hptcosth)
605{
606
607 TCanvas ceadist("ceadist", "Enta distributions", 1600, 800);
608 ceadist.Divide(3, 1);
609
610 for (int il = 0; il < m_kNGroups; il++) {
611
612 ceadist.cd(il + 1);
613 gPad->SetLogy();
614 hentalay[il]->SetFillColor(kYellow);
615 hentalay[il]->Draw("hist");
616 }
617
618 TCanvas cptcos("cptcos", "pt vs costh dist.", 400, 400);
619 cptcos.cd();
620 hptcosth->Draw("colz");
621
622 cptcos.SaveAs(Form("cdcdedx_ptcosth_%s.pdf", m_suffix.data()));
623 ceadist.SaveAs(Form("cdcdedx_1dcell_enta_%s.pdf", m_suffix.data()));
624 ceadist.SaveAs(Form("cdcdedx_1dcell_enta_%s.root", m_suffix.data()));
625}
626
627//--------------------------------------------------
628void CDCDedx1DCellAlgorithm::plotRelConst(std::vector<std::vector<double>>& tempconst)
629{
630
631 const std::string pdfName =
632 Form("cdcdedx_1dcell_relconst_%s.pdf", m_suffix.data());
633
634 const std::string rootName =
635 Form("cdcdedx_1dcell_relconst_%s.root", m_suffix.data());
636
637 TFile rootFile(rootName.c_str(), "RECREATE");
638
639 for (int il = 0; il < m_kNGroups; il++) {
640
641
642 TH1D* hconst = new TH1D(Form("hconst%s", m_label[il].data()), "", m_eaBin, m_eaMin, m_eaMax);
643 std::string title = Form("calibration const dist: %s: (%s); entaRS (#alpha); entries", m_label[il].data(), m_runExp.data());
644 hconst->SetTitle(Form("%s", title.data()));
645
646 TH1D* hconstvar = nullptr;
647
648 if (isVarBins) {
649 Double_t* nvarBins;
650 nvarBins = &m_binValue[il][0];
651
652 hconstvar = new TH1D(Form("hconstvar%s", m_label[il].data()), "", m_eaBinLocal[il], nvarBins);
653 title = Form("calibration const dist (var bins): %s: (%s); entaRS (#alpha);entries", m_label[il].data(), m_runExp.data());
654 hconstvar->SetTitle(Form("%s", title.data()));
655
656 for (int iea = 0; iea < m_eaBinLocal[il]; iea++)
657 hconstvar->SetBinContent(iea + 1, tempconst.at(il).at(iea));
658 }
659
660 for (int jea = 0; jea < m_eaBin; jea++) hconst->SetBinContent(jea + 1, m_onedcors.at(il).at(jea));
661
662 gStyle->SetOptStat("ne");
663 TCanvas cconst("cconst", "calibration Constants", 800, 400);
664 if (isVarBins) {
665 cconst.Divide(2, 1);
666 cconst.SetWindowSize(1000, 800);
667 }
668
669 cconst.cd(1);
670 hconst->SetFillColor(kYellow);
671 hconst->Draw("histo");
672 if (isVarBins && hconstvar) {
673 cconst.cd(2);
674 hconstvar->SetFillColor(kBlue);
675 hconstvar->Draw("hist");
676 }
677
678 cconst.Update();
679
680 if (m_kNGroups == 1) {
681 cconst.Print(pdfName.c_str());
682 } else if (il == 0) {
683 cconst.Print((pdfName + "(").c_str());
684 } else if (il == m_kNGroups - 1) {
685 cconst.Print((pdfName + ")").c_str());
686 } else {
687 cconst.Print(pdfName.c_str());
688 }
689
690 // Save this canvas in the ROOT file
691 rootFile.cd();
692 cconst.Write();
693
694 delete hconst;
695 delete hconstvar;
696 }
697 rootFile.Close();
698}
699
700//--------------------------------------------------
702{
703
704 //Draw New/Old final constants
705 TH1D* hnewconst[m_kNGroups], *holdconst[m_kNGroups];
706 double min[m_kNGroups], max[m_kNGroups];
707
708 for (unsigned int il = 0; il < m_kNGroups; il++) {
709 unsigned int nbins = m_DBOneDCell->getNBins(getRepresentativeLayer(il));
710
711 std::string title = Form("final calibration const dist (%s): %s; entaRS (#alpha); entries", m_label[il].data(), m_runExp.data());
712 hnewconst[il] = new TH1D(Form("hnewconst_%s", m_label[il].data()), "", m_eaBin, m_eaMin, m_eaMax);
713 hnewconst[il]->SetTitle(Form("%s", title.data()));
714
715 title = Form("old calibration const dist (%s): %s; entaRS (#alpha); entries", m_label[il].data(), m_runExp.data());
716 holdconst[il] = new TH1D(Form("holdconst_%s", m_label[il].data()), "", m_eaBin, m_eaMin, m_eaMax);
717 holdconst[il]->SetTitle(Form("%s", title.data()));
718
719 for (unsigned int iea = 0; iea < nbins; iea++) {
720 double prev = m_DBOneDCell->getMean(getRepresentativeLayer(il), iea);
721 holdconst[il]->SetBinContent(iea + 1, prev);
722 hnewconst[il]->SetBinContent(iea + 1, m_onedcors[il][iea]);
723 }
724 min[il] = hnewconst[il]->GetMinimum();
725 max[il] = hnewconst[il]->GetMaximum();
726 }
727
728 //Ploting final constants
729 double globalMin = min[0];
730 double globalMax = max[0];
731 for (int il = 1; il < m_kNGroups; il++) {
732 if (min[il] < globalMin) globalMin = min[il];
733 if (max[il] > globalMax) globalMax = max[il];
734 }
735
736 gStyle->SetOptStat("ne");
737 TCanvas cfconst("cfconst", "Final calibration constants", 1600, 600);
738 cfconst.Divide(3, 1);
739
740 for (int il = 0; il < m_kNGroups; il++) {
741 cfconst.cd(il + 1);
742 hnewconst[il]->GetYaxis()->SetRangeUser(globalMin * 0.95, globalMax * 1.05);
743 hnewconst[il]->SetLineColor(kBlack);
744 hnewconst[il]->Draw("histo");
745 holdconst[il]->SetLineColor(kRed);
746 holdconst[il]->Draw("histo same");
747
748 auto legend = new TLegend(0.4, 0.75, 0.56, 0.85);
749 legend->AddEntry(holdconst[il], "Old", "lep");
750 legend->AddEntry(hnewconst[il], "New", "lep");
751 legend->Draw();
752 }
753
754 cfconst.SaveAs(Form("cdcdedx_1dcell_fconsts_%s.pdf", m_suffix.data()));
755 cfconst.SaveAs(Form("cdcdedx_1dcell_fconsts_%s.root", m_suffix.data()));
756
757 for (int il = 0; il < m_kNGroups; il++) {
758 delete hnewconst[il];
759 delete holdconst[il];
760 }
761}
762
763//------------------------------------
765{
766
767 TCanvas cstats("cstats", "cstats", 1000, 500);
768 cstats.SetBatch(kTRUE);
769 cstats.Divide(2, 1);
770
771 cstats.cd(1);
772 auto hestats = getObjectPtr<TH1I>("hestats");
773 if (hestats) {
774 hestats->SetName(Form("hestats_%s", m_suffix.data()));
775 hestats->SetStats(0);
776 hestats->DrawCopy("");
777 }
778
779 cstats.cd(2);
780 auto htstats = getObjectPtr<TH1I>("htstats");
781 if (htstats) {
782 htstats->SetName(Form("htstats_%s", m_suffix.data()));
783 htstats->SetStats(0);
784 htstats->DrawCopy("");
785 }
786 cstats.Print(Form("cdcdedx_1dcell_stats_%s.pdf", m_suffix.data()));
787}
void plotLayerDist(std::array< TH1D *, m_kNGroups > &hdedxlay)
function to draw dedx dist.
CDCDedx1DCellAlgorithm()
Constructor: Sets the description, the properties and the parameters of the algorithm.
double m_eaMax
upper edge of entrance angle
void plotMergeFactor(std::map< int, std::vector< double > > bounds, const std::array< int, 2 > nDev, std::map< int, std::vector< int > > steps)
function to plot merging factor
double m_truncMax
upper threshold on truncation
int m_binSplit
multiply nbins by this factor in full range
double m_truncMin
lower threshold on truncation
double m_adjustFac
factor with that one what to adjust baseline
void getTruncatedBins(TH1D *hist, int &binlow, int &binhigh)
function to get bins of truncation from histogram
void CreateBinMapping()
class function to create vectors for bin mapping (Var->symm)
double m_chargeType
charge type for baseline adj
void getExpRunInfo()
function to extract calibration run/exp
unsigned int getRepresentativeLayer(unsigned int il) const
Representative CDC layer for each SL group (used to access group-wise constants): SL0 => 1,...
static constexpr int m_kNGroups
SL grouping: inner (SL0), middle (SL1), outer (SL2–8)
DBObjPtr< CDCDedx1DCell > m_DBOneDCell
One cell correction DB object.
double m_cosMax
a limit on cos theta
bool isPrintLog
print more debug information
std::string m_suffix
add suffix to all plot name
int m_eaB
reset # of bins for entrance angle for each experiment
double getTruncationMean(TH1D *hist, int binlow, int binhigh)
function to get truncated mean
double m_ptMax
a limit on transverse momentum
void plotRelConst(std::vector< std::vector< double > > &tempconst)
function to draw symm/Var layer constant
void plotConstants()
function to draw the old/new final constants
std::array< std::vector< int >, m_kNGroups > m_binIndex
symm/Var bin numbers
bool isFixTrunc
true = fix window for all out/inner layers
bool isVarBins
true: if variable bin size is requested
void plotQaPars(std::array< TH1D *, m_kNGroups > &hentalay, TH2D *hptcosth)
function to draw pt vs costh and entrance angle distribution for Inner/Outer layer
double m_eaBW
binwdith of entrance angle bin
bool isRotSymm
if rotation symmetry requested
std::string m_runExp
add run and exp to title of plot
void defineHisto(std::array< std::vector< TH1D * >, 3 > &hdedxhit, std::array< TH1D *, 3 > &hdedxlay, std::array< TH1D *, 3 > &hentalay)
function to define histograms
void plotEventStats()
function to draw the stats plots
int rotationalBin(int nbin, int ibin)
class function to set rotation symmetry
virtual EResult calibrate() override
1D cell algorithm
std::array< std::vector< double >, m_kNGroups > m_binValue
enta Var bin values
double m_dedxMax
upper edge of dedxhit
void createPayload()
function to generate final constants
bool isMakePlots
produce plots for status
std::vector< std::vector< double > > m_onedcors
final vectors of calibration
bool isMerge
print more debug information
double m_dedxMin
lower edge of dedxhit
std::string m_label[m_kNGroups]
add inner/outer superlayer label
void plotdedxHist(std::array< std::vector< TH1D * >, m_kNGroups > &hdedxhit)
function to draw the dE/dx histogram in enta bins
double m_eaMin
lower edge of entrance angle
dE/dx 1D cell correction calibration constants
void saveCalibration(TClonesArray *data, const std::string &name)
Store DBArray payload with given name with default IOV.
static void updateDBObjPtrs(const unsigned int event, const int run, const int experiment)
Updates any DBObjPtrs by calling update(event) for DBStore.
void setDescription(const std::string &description)
Set algorithm description (in constructor)
const std::vector< Calibration::ExpRun > & getRunList() const
Get the list of runs for which calibration is called.
EResult
The result of calibration.
@ c_OK
Finished successfully =0 in Python.
@ c_NotEnoughData
Needs more data =2 in Python.
CalibrationAlgorithm(const std::string &collectorModuleName)
Constructor - sets the prefix for collected objects (won't be accesses until execute(....
std::shared_ptr< T > getObjectPtr(const std::string &name, const std::vector< Calibration::ExpRun > &requestedRuns)
Get calibration data object by name and list of runs, the Merge function will be called to generate t...
Abstract base class for different kinds of events.