9#include <cdc/calibration/CDCdEdx/CDCDedx1DCellAlgorithm.h>
49 setDescription(
"A calibration algorithm for the CDC dE/dx entrance angle cleanup correction");
61 B2FATAL(
"There is no valid previous payload for CDCDedx1DCell");
67 std::vector<double>* dedxhit = 0, *enta = 0;
68 std::vector<int>* layer = 0;
69 double pt = 0, costh = 0;
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);
91 std::array<std::vector<TH1D*>,
m_kNGroups> hdedxhit;
92 std::array<TH1D*, m_kNGroups> hdedxlay{};
93 std::array<TH1D*, m_kNGroups> hentalay{};
95 TH2D* hptcosth =
new TH2D(
"hptcosth",
"pt vs costh dist;pt;costh", 1000, -8.0, 8.0, 1000, -1.0, 1.0);
100 for (
int i = 0; i < ttree->GetEntries(); ++i) {
104 if (std::abs(costh) >
m_cosMax)
continue;
110 if (std::abs(pt) >
m_ptMax)
continue;
113 int rand = gRandom->Integer(100);
114 if (rand < 10) hptcosth->Fill(pt, costh);
116 for (
unsigned int j = 0; j < dedxhit->size(); ++j) {
118 if (dedxhit->at(j) == 0)
continue;
120 double entaval = enta->at(j);
123 if (ibin < 0 || ibin >=
m_eaBin)
continue;
125 int lay = layer->at(j);
126 int mL = (lay < 8) ? 0 : ((lay < 14) ? 1 : 2);
128 hdedxlay[mL]->Fill(dedxhit->at(j));
129 if (rand < 10) hentalay[mL]->Fill(entaval);
133 hdedxhit[mL][jbinea]->Fill(dedxhit->at(j));
136 std::vector<std::vector<double>> tempconst(
m_kNGroups);
139 int minlay = 0, maxlay = 0;
143 hdedxlay[il]->SetTitle(Form(
"%s;%d;%d", hdedxlay[il]->GetTitle(), minlay, maxlay));
154 TH1D* htemp = (TH1D*)hdedxhit[il][jea]->Clone(Form(
"h_%s_b%d_c",
m_label[il].data(), jea));
156 int minbin = 1, maxbin = 1;
167 tempconst[il].push_back(dedxmean);
169 hdedxhit[il][iea]->SetTitle(Form(
"%s, #mu_{truc} = %0.5f;%d;%d", hdedxhit[il][iea]->GetTitle(), dedxmean, minbin, maxbin));
173 std::vector<double>layerconst;
176 for (
int iea = 0; iea <
m_eaBin; iea++) {
179 layerconst.push_back(tempconst[il].at(jea));
216 delete hdedxhit[il][iea];
239 if (cruns == 0) B2INFO(
"CDCDedxBadWires: start exp " << expRun.first <<
" and run " << expRun.second <<
"");
244 int estart = erStart.first;
245 int rstart = erStart.second;
248 int eend = erEnd.first;
249 int rend = erEnd.second;
253 m_runExp = Form(
"Range (%d:%d,%d:%d)", estart, rstart, eend, rend);
255 else m_suffix = Form(
"e%d_r%dr%d", estart, rstart, rend);
262 std::map<int, std::vector<double>> bounds;
263 std::map<int, std::vector<int>> steps;
266 const std::array<int, 2> nDev{8, 4};
269 bounds[0] = {0, 108, 123, 133, 158, 183, 193, 208, 316};
270 steps[0] = {9, 3, 2, 1, 1, 2, 3, 9};
273 bounds[1] = {0, 38, 158, 278, 316};
274 steps[1] = {2, 1, 1, 2};
277 const std::array<int, m_kNGroups> configIndex = {0, 0, 1};
281 int icfg = configIndex[il];
283 std::vector<double> scaledBounds = bounds[icfg];
284 for (
int ibin = 0; ibin <= nDev[icfg]; ibin++) {
285 scaledBounds[ibin] = scaledBounds[ibin] *
m_binSplit;
288 int ieaprime = -1, temp = -99, ibin = 0;
293 for (
int iea = 0; iea <
m_eaBin; iea++) {
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++;
305 if (ieaprime != temp) {
308 double binvalue = pastbin + binwidth;
310 if (std::abs(binvalue) < 1e-5) binvalue = 0;
325 std::array<TH1D*, m_kNGroups>& hdedxlay,
326 std::array<TH1D*, m_kNGroups>& hentalay)
330 std::string title = Form(
"dedxhit dist (%s): %s ; dedxhit;entries",
m_label[il].data(),
m_runExp.data());
332 hdedxlay[il]->SetTitle(title.c_str());
337 title = Form(
"entaRS dist (variable bins): %s: (%s); entaRS (#alpha);entries",
m_label[il].data(),
m_runExp.data());
339 title = Form(
"entaRS dist (sym. bins): %s: (%s); entaRS (#alpha);entries",
m_label[il].data(),
m_runExp.data());
341 hentalay[il] =
new TH1D(Form(
"hentalay%s",
m_label[il].data()),
"",
m_eaBinLocal[il], nvarBins);
342 hentalay[il]->SetTitle(title.c_str());
348 double width = max - min;
351 B2INFO(
"bin: " << iea <<
" ], min:" << min <<
" , max: " << max <<
" , width: " << width);
353 title = Form(
"%s: entaRS = (%0.03f to %0.03f)",
m_label[il].data(), min, max);
355 hdedxhit[il].push_back(
new TH1D(Form(
"hdedxhit_%s_bin%d",
m_label[il].data(), iea),
358 hdedxhit[il][iea]->SetTitle(title.c_str());
368 double sum = hist->Integral();
369 if (sum <= 0 || hist->GetNbinsX() <= 0) {
370 binlow = 1; binhigh = 1;
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);
395 if (hist->Integral() < 100)
return 1.0;
397 if (binlow <= 0 || binhigh > hist->GetNbinsX())
return 1.0;
399 double binweights = 0., sumofbc = 0.;
400 for (
int ibin = binlow; ibin <= binhigh; ibin++) {
401 double bcdedx = hist->GetBinContent(ibin);
403 binweights += (bcdedx * hist->GetBinCenter(ibin));
407 if (sumofbc > 0)
return binweights / sumofbc;
415 B2INFO(
"dE/dx one cell calibration: Generating payloads");
417 for (
unsigned int il = 0; il <
m_kNGroups; il++) {
423 B2ERROR(
"merging failed because of unmatch bins (old " <<
m_eaBin <<
" new " << nbins <<
")");
425 for (
unsigned int iea = 0; iea < nbins; iea++) {
431 double binsize = TMath::Pi() /
m_onedcors[il].size();
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;
439 for (
unsigned int iea = binLow; iea < binHigh; ++iea) {
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);
450 double negLow = -0.5, negHigh = -0.2;
451 double posLow = 0.2, posHigh = 0.5;
454 negLow = -0.75; negHigh = -0.25;
455 posLow = 0.25; posHigh = 0.75;
458 auto [avgNewNeg, avgPrevNeg] = computeAverages(negLow, negHigh);
459 auto [avgNewPos, avgPrevPos] = computeAverages(posLow, posHigh);
461 double avgNew = (avgNewNeg + avgNewPos) / 2.0;
462 double avgPrev = (avgPrevNeg + avgPrevPos) / 2.0;
463 double scaleFactor = avgPrev / avgNew;
465 for (
int iea = 0; iea <
m_eaBin; iea++) {
477 B2INFO(
"dE/dx Calibration done for CDCDedx1DCell");
478 std::vector<unsigned int> layerToGroup(56);
480 for (
unsigned int layer = 0; layer < 56; layer++) {
481 if (layer < 8) layerToGroup[layer] = 0;
482 else if (layer < 14) layerToGroup[layer] = 1;
483 else layerToGroup[layer] = 2;
492 const std::array<int, 2> nDev,
493 std::map<
int, std::vector<int>> steps)
495 TCanvas cmfactor(
"cmfactor",
"Merging factors", 800, 400);
496 cmfactor.Divide(2, 1);
498 std::array<TH1I*, 2> hists{};
500 for (
int icfg = 0; icfg < 2; icfg++) {
501 Double_t* nvarBins = bounds[icfg].data();
503 hists[icfg] =
new TH1I(Form(
"hist_cfg%d", icfg),
"", nDev[icfg], nvarBins);
506 hists[icfg]->SetTitle(
"Merging factor for SL0/SL1 bins;binindex;merge-factors");
508 hists[icfg]->SetTitle(
"Merging factor for SL2-8 bins;binindex;merge-factors");
510 for (
int ibin = 0; ibin < nDev[icfg]; ibin++) {
511 hists[icfg]->SetBinContent(ibin + 1, steps[icfg][ibin]);
514 cmfactor.cd(icfg + 1);
515 hists[icfg]->SetFillColor(kYellow);
516 hists[icfg]->Draw(
"hist");
519 cmfactor.SaveAs(Form(
"cdcdedx_1dcell_mergefactor_%s.pdf",
m_suffix.data()));
520 cmfactor.SaveAs(Form(
"cdcdedx_1dcell_mergefactor_%s.root",
m_suffix.data()));
522 for (
int icfg = 0; icfg < 2; icfg++) {
531 TCanvas ctmp(
"tmp",
"tmp", 1200, 1200);
533 std::stringstream psname;
535 psname << Form(
"cdcdedx_1dcell_dedxhit_%s.pdf[",
m_suffix.data());
536 ctmp.Print(psname.str().c_str());
538 psname << Form(
"cdcdedx_1dcell_dedxhit_%s.pdf",
m_suffix.data());
544 int minbin = std::stoi(hdedxhit[il][jea]->GetXaxis()->GetTitle());
545 int maxbin = std::stoi(hdedxhit[il][jea]->GetYaxis()->GetTitle());
547 ctmp.cd(jea % 16 + 1);
548 hdedxhit[il][jea]->SetFillColor(4 + il);
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");
558 ctmp.Print(psname.str().c_str());
566 psname << Form(
"cdcdedx_1dcell_dedxhit_%s.pdf]",
m_suffix.data());
567 ctmp.Print(psname.str().c_str());
574 TCanvas cdedxlayer(
"layerdedxhit",
"Inner and Outer Layer dedxhit dist", 900, 400);
575 cdedxlayer.Divide(3, 1);
578 int minlay = 0, maxlay = 0;
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));
587 cdedxlayer.cd(il + 1);
588 hdedxlay[il]->SetFillColor(kYellow);
589 hdedxlay[il]->Draw(
"histo");
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");
599 cdedxlayer.SaveAs(Form(
"cdcdedx_1dcell_dedxlayer_%s.pdf",
m_suffix.data()));
600 cdedxlayer.SaveAs(Form(
"cdcdedx_1dcell_dedxlayer_%s.root",
m_suffix.data()));
607 TCanvas ceadist(
"ceadist",
"Enta distributions", 1600, 800);
608 ceadist.Divide(3, 1);
614 hentalay[il]->SetFillColor(kYellow);
615 hentalay[il]->Draw(
"hist");
618 TCanvas cptcos(
"cptcos",
"pt vs costh dist.", 400, 400);
620 hptcosth->Draw(
"colz");
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()));
631 const std::string pdfName =
632 Form(
"cdcdedx_1dcell_relconst_%s.pdf",
m_suffix.data());
634 const std::string rootName =
635 Form(
"cdcdedx_1dcell_relconst_%s.root",
m_suffix.data());
637 TFile rootFile(rootName.c_str(),
"RECREATE");
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()));
646 TH1D* hconstvar =
nullptr;
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()));
657 hconstvar->SetBinContent(iea + 1, tempconst.at(il).at(iea));
660 for (
int jea = 0; jea <
m_eaBin; jea++) hconst->SetBinContent(jea + 1,
m_onedcors.at(il).at(jea));
662 gStyle->SetOptStat(
"ne");
663 TCanvas cconst(
"cconst",
"calibration Constants", 800, 400);
666 cconst.SetWindowSize(1000, 800);
670 hconst->SetFillColor(kYellow);
671 hconst->Draw(
"histo");
674 hconstvar->SetFillColor(kBlue);
675 hconstvar->Draw(
"hist");
681 cconst.Print(pdfName.c_str());
682 }
else if (il == 0) {
683 cconst.Print((pdfName +
"(").c_str());
685 cconst.Print((pdfName +
")").c_str());
687 cconst.Print(pdfName.c_str());
708 for (
unsigned int il = 0; il <
m_kNGroups; il++) {
711 std::string title = Form(
"final calibration const dist (%s): %s; entaRS (#alpha); entries",
m_label[il].data(),
m_runExp.data());
713 hnewconst[il]->SetTitle(Form(
"%s", title.data()));
715 title = Form(
"old calibration const dist (%s): %s; entaRS (#alpha); entries",
m_label[il].data(),
m_runExp.data());
717 holdconst[il]->SetTitle(Form(
"%s", title.data()));
719 for (
unsigned int iea = 0; iea < nbins; iea++) {
721 holdconst[il]->SetBinContent(iea + 1, prev);
722 hnewconst[il]->SetBinContent(iea + 1,
m_onedcors[il][iea]);
724 min[il] = hnewconst[il]->GetMinimum();
725 max[il] = hnewconst[il]->GetMaximum();
729 double globalMin = min[0];
730 double globalMax = max[0];
732 if (min[il] < globalMin) globalMin = min[il];
733 if (max[il] > globalMax) globalMax = max[il];
736 gStyle->SetOptStat(
"ne");
737 TCanvas cfconst(
"cfconst",
"Final calibration constants", 1600, 600);
738 cfconst.Divide(3, 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");
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");
754 cfconst.SaveAs(Form(
"cdcdedx_1dcell_fconsts_%s.pdf",
m_suffix.data()));
755 cfconst.SaveAs(Form(
"cdcdedx_1dcell_fconsts_%s.root",
m_suffix.data()));
758 delete hnewconst[il];
759 delete holdconst[il];
767 TCanvas cstats(
"cstats",
"cstats", 1000, 500);
768 cstats.SetBatch(kTRUE);
774 hestats->SetName(Form(
"hestats_%s",
m_suffix.data()));
775 hestats->SetStats(0);
776 hestats->DrawCopy(
"");
782 htstats->SetName(Form(
"htstats_%s",
m_suffix.data()));
783 htstats->SetStats(0);
784 htstats->DrawCopy(
"");
786 cstats.Print(Form(
"cdcdedx_1dcell_stats_%s.pdf",
m_suffix.data()));
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::vector< int > m_eaBinLocal
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.