9#include <reconstruction/calibration/CDCdEdx/CDCDedxWireGainAlgorithm.h>
31 setDescription(
"A calibration algorithm for CDC dE/dx wire gains");
44 B2FATAL(
"There is no valid payload for BadWires and/or Wirgain");
47 auto ttree = getObjectPtr<TTree>(
"tree");
50 vector<int>* wire = 0;
51 ttree->SetBranchAddress(
"wire", &wire);
53 vector<double>* dedxhit = 0;
54 ttree->SetBranchAddress(
"dedxhit", &dedxhit);
57 map<int, vector<double>> wirededx;
60 array<TH1D*, 2> hdedxL;
61 string label[2] = {
"IL",
"OL"};
62 for (
int il = 0; il < 2; il++)
65 for (
int i = 0; i < ttree->GetEntries(); ++i) {
67 for (
unsigned int j = 0; j < wire->size(); ++j) {
68 int jwire = wire->at(j);
69 double jhitdedx = dedxhit->at(j);
70 wirededx[jwire].push_back(jhitdedx);
72 if (jwire < 1280) hdedxL[0]->Fill(jhitdedx);
73 else hdedxL[1]->Fill(jhitdedx);
79 for (
unsigned int jw = 0; jw < c_nwireCDC; ++jw)
80 if (wirededx[jw].size() <= 100) minstat++;
85 array<unsigned int, 2> minbinL, maxbinL;
86 for (
int il = 0; il < 2; il++) {
88 hdedxL[il]->SetTitle(Form(
"%s(%s);%d;%d", label[il].data(),
m_suffix.data(), minbinL[il], maxbinL[il]));
91 vector<double> vrel_mean;
92 vector<double> vdedx_means;
94 vector<TH1D*> hdedxhit(c_nwireCDC);
96 B2INFO(
"Creating CDCGeometryPar object");
99 vector<double> layermean(c_maxNSenseLayers);
101 int activelayers = 0;
102 double layeravg = 0.0;
105 for (
unsigned int il = 0; il < c_maxNSenseLayers; ++il) {
110 for (
unsigned int iw = 0; iw < cdcgeo.
nWiresInLayer(il); ++iw) {
115 for (
unsigned int ih = 0; ih < wirededx[jwire].size(); ++ih) {
116 hdedxhit[jwire]->Fill(wirededx[jwire][ih]);
119 unsigned int minbin, maxbin;
131 hdedxhit[jwire]->SetTitle(Form(
"dedxhit-dist, wire: %d (%s);%d;%d", jwire,
m_suffix.data(), minbin, maxbin));
134 if (
m_DBBadWires->getBadWireStatus(jwire) == kTRUE) dedxmean = 0.0;
137 vrel_mean.push_back(dedxmean);
141 vdedx_means.push_back(dedxmean * prewg);
142 B2INFO(
"merged-wireGain: [" << jwire <<
"], prewgvious = " << prewg <<
", rel = " << dedxmean <<
", merged = " << vdedx_means.at(
144 }
else vdedx_means.push_back(dedxmean);
147 if (vdedx_means.at(jwire) > 0) {
148 layermean[il] += vdedx_means.at(jwire);
153 if (activewires > 0) layermean[il] /= activewires;
154 else layermean[il] = 1.0;
157 if (il >= 8 && layermean[il] > 0) {
158 layeravg += layermean[il];
165 if (activelayers > 0) layeravg /= activelayers;
167 for (
unsigned int iw = 0; iw < c_nwireCDC; ++iw) {
168 vrel_mean.at(iw) /= layeravg;
169 vdedx_means.at(iw) /= layeravg;
204 if (cruns == 0) B2INFO(
"CDCDedxWireGain: start exp " << expRun.first <<
" and run " << expRun.second <<
"");
209 int estart = erStart.first;
210 int rstart = erStart.second;
213 int rend = erEnd.second;
218 else m_suffix = Form(
"e%d_r%dr%d", estart, rstart, rend);
225 B2INFO(
"dE/dx Calibration done for " << vdedx_means.size() <<
" CDC wires");
235 if (hdedxhit->Integral() < 100)
return 1.0;
237 if (binlow <= 0 || binhigh > hdedxhit->GetNbinsX())
return 1.0;
239 double binweights = 0., sumofbc = 0.;
240 for (
int ibin = binlow; ibin <= binhigh; ibin++) {
241 double bcdedx = hdedxhit->GetBinContent(ibin);
243 binweights += (bcdedx * hdedxhit->GetBinCenter(ibin));
247 if (sumofbc > 0)
return binweights / sumofbc;
256 double sum = hdedxhit->Integral();
257 if (sum <= 0 || hdedxhit->GetNbinsX() <= 0) {
258 binlow = 1; binhigh = 1;
262 binlow = 1.0; binhigh = 1.0;
263 double sumPer5 = 0.0, sumPer75 = 0.0;
264 for (
int ibin = 1; ibin <= hdedxhit->GetNbinsX(); ibin++) {
265 double bcdedx = hdedxhit->GetBinContent(ibin);
282 TCanvas cldedx(
"cldedx",
"IL/OL dedxhit dist", 900, 400);
285 for (
int il = 0; il < 2; il++) {
288 int minbin = stoi(hdedxL[il]->GetXaxis()->GetTitle());
289 int maxbin = stoi(hdedxL[il]->GetYaxis()->GetTitle());
290 double lowedge = hdedxL[il]->GetXaxis()->GetBinLowEdge(minbin);
291 double upedge = hdedxL[il]->GetXaxis()->GetBinUpEdge(maxbin);
293 hdedxL[il]->SetFillColor(kYellow);
294 hdedxL[il]->SetTitle(Form(
"%s, trunc(%0.02f - %0.02f);dedxhit;entries", hdedxL[il]->GetTitle(), lowedge, upedge));
295 hdedxL[il]->Draw(
"histo");
297 TH1D* hdedxLC = (TH1D*)hdedxL[il]->Clone(Form(
"%s_c", hdedxL[il]->GetName()));
298 hdedxLC->GetXaxis()->SetRange(minbin, maxbin);
299 hdedxLC->SetFillColor(kAzure + 1);
300 hdedxLC->Draw(
"same histo");
303 cldedx.SaveAs(Form(
"cdcdedx_wgcal_layerdedx_%s.pdf",
m_suffix.data()));
310 TCanvas ctmp(Form(
"cdcdedx_%s",
m_suffix.data()),
"", 1200, 1200);
312 ctmp.SetBatch(kTRUE);
315 psname << Form(
"cdcdedx_wgcal_%s.pdf[",
m_suffix.data());
316 ctmp.Print(psname.str().c_str());
318 psname << Form(
"cdcdedx_wgcal_%s.pdf",
m_suffix.data());
320 for (
unsigned int iw = 0; iw < hist.size(); iw++) {
322 int minbin = stoi(hist[iw]->GetXaxis()->GetTitle());
323 int maxbin = stoi(hist[iw]->GetYaxis()->GetTitle());
325 hist[iw]->SetFillColor(kYellow - 9);
326 hist[iw]->SetTitle(Form(
"%s, rel. #mu_{trunc} %0.03f;dedxhit;entries", hist[iw]->GetTitle(), vrel_mean.at(iw)));
329 hist[iw]->SetLineColor(kRed);
330 hist[iw]->SetLineWidth(2);
332 ctmp.cd(iw % 16 + 1);
333 hist[iw]->DrawCopy();
335 TH1D* hdedxhitC = (TH1D*)hist[iw]->Clone(Form(
"%sC", hist[iw]->GetName()));
336 hdedxhitC->GetXaxis()->SetRange(minbin, maxbin);
337 hdedxhitC->SetFillColor(kAzure + 1);
338 hdedxhitC->DrawCopy(
"same histo");
340 if (((iw + 1) % 16 == 0) || iw == (hist.size() - 1)) {
341 ctmp.Print(psname.str().c_str());
349 psname << Form(
"cdcdedx_wgcal_%s.pdf]",
m_suffix.data());
350 ctmp.Print(psname.str().c_str());
358 TCanvas cwconst(
"cwconst",
"", 900, 500);
359 TCanvas cwconstvar(
"cwconstvar",
"", 500, 400);
361 array<TH1D*, 2> hconstpw, hconstpwvar;
363 for (
int i = 0; i < 2; i++) {
365 hconstpw[i] =
new TH1D(Form(
"hconstpw_%d_%s", i,
m_suffix.data()),
"", c_nwireCDC, -0.5, 14335.5);
366 hconstpw[i]->SetTitle(Form(
"wiregain const (%s); wire numbers;<dedxhit>",
m_suffix.data()));
368 && i == 0) hconstpw[i]->SetTitle(Form(
"merged wiregain rel-const (%s), avg = %0.03f; wire numbers;<dedxhit>",
m_suffix.data(),
371 hconstpwvar[i] =
new TH1D(Form(
"hconstpwvar_%s",
m_suffix.data()),
"", 400, -0.5, 2.5);
372 hconstpwvar[i]->SetTitle(Form(
"wiregain const (%s); wire gains; nentries",
m_suffix.data()));
374 && i == 0) hconstpwvar[i]->SetTitle(Form(
"merged wiregain rel-const (%s), avg = %0.03f; wire gains; nentries",
m_suffix.data(),
377 for (
unsigned int iw = 0; iw < c_nwireCDC; iw++) {
379 double gain = vdedx_means.at(iw);
380 if (
m_isMerge && i == 1) gain = vrel_mean.at(iw);
381 hconstpw[i]->SetBinContent(iw + 1, gain);
382 hconstpwvar[i]->Fill(gain);
384 if (iw % 500 == 0) hconstpw[i]->GetXaxis()->SetBinLabel(iw + 1, Form(
"w%d", iw + 1));
387 hconstpw[i]->SetLineColor(i * 2 + 2);
388 hconstpw[i]->LabelsOption(
"u",
"X");
389 hconstpw[i]->GetYaxis()->SetRangeUser(-0.1, 3.5);
390 hconstpw[i]->LabelsDeflate();
392 hconstpwvar[i]->SetFillColor(i * 2 + 2);
393 hconstpwvar[i]->Scale(1 / hconstpwvar[i]->GetMaximum());
398 hconstpw[0]->Draw(
"");
399 if (
m_isMerge) hconstpw[1]->Draw(
"same");
402 hconstpwvar[0]->Draw(
"hist");
403 if (
m_isMerge) hconstpwvar[1]->Draw(
"hist same");
405 cwconst.SaveAs(Form(
"cdcdedx_wgcal_wireconst_%s.pdf",
m_suffix.data()));
406 cwconstvar.SaveAs(Form(
"cdcdedx_wgcal_wireconstvar_%s.pdf",
m_suffix.data()));
413 TH1D hlayeravg(Form(
"hlayeravg_%s",
m_suffix.data()),
"", layermean.size(), -0.5, 55.5);
414 hlayeravg.SetTitle(Form(
"layer gain avg (%s); layer numbers;<dedxhit>",
m_suffix.data()));
416 for (
unsigned int il = 0; il < layermean.size(); il++) {
417 hlayeravg.SetBinContent(il + 1, layermean[il]);
418 if (il % 2 == 0 || il == layermean.size() - 1) hlayeravg.GetXaxis()->SetBinLabel(il + 1, Form(
"L%d", il));
421 TCanvas clayeravg(
"clayeravg",
"clayeravg", 800, 500);
422 clayeravg.SetGridy(1);
424 gStyle->SetOptStat(
"ne");
425 hlayeravg.LabelsOption(
"u",
"X");
426 hlayeravg.SetLineColor(kBlue);
427 hlayeravg.GetYaxis()->SetRangeUser(-0.1, 3.5);
428 hlayeravg.SetTitle(Form(
"%s, avg = %0.03f (abs)", hlayeravg.GetTitle(), layeravg));
429 hlayeravg.LabelsDeflate();
431 TLine* tl =
new TLine(-0.5, layeravg, 55.5, layeravg);
432 tl->SetLineColor(kRed);
433 tl->DrawClone(
"same");
434 clayeravg.SaveAs(Form(
"cdcdedx_wgcal_layeravg_%s.pdf",
m_suffix.data()));
444 TCanvas clconst(
"clconst",
"", 800, 500);
445 clconst.Divide(2, 2);
446 clconst.SetBatch(kTRUE);
448 stringstream psnameL;
449 psnameL << Form(
"cdcdedx_wgcal_layerconst_%s.pdf[",
m_suffix.data());
450 clconst.Print(psnameL.str().c_str());
451 psnameL.str(
""); psnameL << Form(
"cdcdedx_wgcal_layerconst_%s.pdf",
m_suffix.data());
455 for (
unsigned int il = 0; il < layermean.size(); ++il) {
458 TH1D hconstpl(Form(
"hconstpwvar_l%d_%s", il,
m_suffix.data()),
"", nwires, jwire, jwire + nwires);
459 hconstpl.SetTitle(Form(
"abs-const, layer: %d (%s); wire numbers;<dedxhit>", il,
m_suffix.data()));
461 for (
unsigned int iw = 0; iw < nwires; ++iw) {
462 hconstpl.SetBinContent(iw + 1, vdedx_means.at(jwire));
464 if (iw % 10 == 0) hconstpl.GetXaxis()->SetBinLabel(iw + 1, Form(
"w%d", jwire));
466 if (iw % 15 == 0) hconstpl.GetXaxis()->SetBinLabel(iw + 1, Form(
"w%d", jwire));
471 double lmean = layermean.at(il) / layeravg;
473 clconst.cd(il % 4 + 1);
474 gStyle->SetOptStat(
"ne");
476 hconstpl.SetTitle(Form(
"%s, avg = %0.03f", hconstpl.GetTitle(), lmean));
478 if (il < 8) hconstpl.GetYaxis()->SetRangeUser(-0.1, 4.0);
479 else hconstpl.GetYaxis()->SetRangeUser(-0.1, 2.0);
480 hconstpl.SetFillColor(kAzure - 1);
481 hconstpl.LabelsOption(
"u",
"X");
482 hconstpl.DrawCopy(
"hist");
484 TLine* tlc =
new TLine(jwire - nwires, lmean, jwire, lmean);
485 tlc->SetLineColor(kRed);
486 tlc->DrawClone(
"same");
488 if ((il + 1) % 4 == 0 || (il + 1) == layermean.size()) {
489 clconst.Print(psnameL.str().c_str());
497 psnameL << Form(
"cdcdedx_wgcal_layerconst_%s.pdf]",
m_suffix.data());
498 clconst.Print(psnameL.str().c_str());
505 TCanvas cstats(
"cstats",
"cstats", 800, 400);
506 cstats.SetBatch(kTRUE);
510 auto hestats = getObjectPtr<TH1I>(
"hestats");
512 hestats->SetName(Form(
"hestats_%s",
m_suffix.data()));
513 hestats->SetStats(0);
514 hestats->DrawCopy(
"");
518 auto htstats = getObjectPtr<TH1I>(
"htstats");
520 htstats->SetName(Form(
"htstats_%s",
m_suffix.data()));
521 htstats->SetStats(0);
522 htstats->DrawCopy(
"");
525 cstats.Print(Form(
"cdcdedx_wgcal_stats_%s.pdf",
m_suffix.data()));
void plotWGPerLayer(const std::vector< double > &vdedx_means, const std::vector< double > &layermean, double layeravg)
function to draw WG per layer
void plotLayerGain(const std::vector< double > &layermean, double layeravg)
function to draw layer gains
void plotLayerDist(std::array< TH1D *, 2 > hdedxL)
function to draw dE/dx for inner/outer layer
bool m_isMerge
merge payload at the time of calibration
double m_truncMax
max trunc range for mean
double m_truncMin
min trunc range for mean
void getTruncatedBins(TH1D *hdedxhit, unsigned int &binlow, unsigned int &binhigh)
function to get bins of truncation from histogram
void plotWireGain(const std::vector< double > &vdedx_means, const std::vector< double > &vrel_mean, double layeravg)
function to draw wire gains
void getExpRunInfo()
function to get extract calibration run/exp
void createPayload(const std::vector< double > &vdedx_tmeans)
function to finally store new payload after full calibration
void plotWireDist(const std::vector< TH1D * > &hist, const std::vector< double > &vrel_mean)
function to draw dE/dx histograms for each wire
bool m_isMakePlots
Save arithmetic and truncated mean for the 'dedx' values.
DBObjPtr< CDCDedxBadWires > m_DBBadWires
Bad wire DB object.
std::string m_suffix
suffix string to separate plots
double getTruncationMean(TH1D *hdedxhit, int binlow, int binhigh)
function to get mean of truncation from histogram
int m_dedxBins
number of bins for dedx histogram
DBObjPtr< CDCGeometry > m_cdcGeo
Geometry of CDC.
CDCDedxWireGainAlgorithm()
Constructor: Sets the description, the properties and the parameters of the algorithm.
DBObjPtr< CDCDedxWireGain > m_DBWireGains
Wire gain DB object.
void plotEventStats()
function to draw statstics
virtual EResult calibrate() override
Wire gain algorithm.
double m_dedxMax
max dedx range for wiregain cal
double m_dedxMin
min dedx range for wiregain cal
bool m_isWireTruc
method of trunc range for mean
dE/dx wire gain calibration constants
The Class for CDC Geometry Parameters.
unsigned nWiresInLayer(int layerId) const
Returns wire numbers in a layer.
static CDCGeometryPar & Instance(const CDCGeometry *=nullptr)
Static method to get a reference to the CDCGeometryPar instance.
Base class for calibration algorithms.
void saveCalibration(TClonesArray *data, const std::string &name)
Store DBArray payload with given name with default IOV.
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.
Abstract base class for different kinds of events.