9#include <reconstruction/calibration/CDCdEdx/CDCDedxCosineAlgorithm.h>
37 setDescription(
"A calibration algorithm for CDC dE/dx electron cos(theta) dependence");
46 B2INFO(
"Preparing dE/dx calibration for CDC dE/dx electron saturation");
49 auto ttree = getObjectPtr<TTree>(
"tree");
52 double dedx, costh;
int charge;
53 ttree->SetBranchAddress(
"dedx", &dedx);
54 ttree->SetBranchAddress(
"costh", &costh);
55 ttree->SetBranchAddress(
"charge", &charge);
67 for (
unsigned int i = 0; i <
fCosbins; ++i) {
69 double coslow = i * binW +
fCosMin, coshigh = coslow + binW;
72 hdEdx_elCosbin[i]->SetTitle(Form(
"dE/dx dist (e-) in costh (%0.02f, %0.02f), run start: %d", coslow, coshigh,
fStartRun));
73 hdEdx_elCosbin[i]->GetXaxis()->SetTitle(
"dE/dx (no had sat, for e-)");
74 hdEdx_elCosbin[i]->GetYaxis()->SetTitle(
"Entries");
77 hdEdx_poCosbin[i]->SetTitle(Form(
"dE/dx dist (e+) in costh (%0.02f, %0.02f), run start: %d", coslow, coshigh,
fStartRun));
78 hdEdx_poCosbin[i]->GetXaxis()->SetTitle(
"dE/dx (no had sat, for e+)");
79 hdEdx_poCosbin[i]->GetYaxis()->SetTitle(
"Entries");
82 hdEdx_epCosbin[i]->SetTitle(Form(
"dE/dx dist (e-,e+) in costh (%0.02f, %0.02f), run start: %d", coslow, coshigh,
fStartRun));
83 hdEdx_epCosbin[i]->GetXaxis()->SetTitle(
"dE/dx (no had sat, for e-,e+)");
84 hdEdx_epCosbin[i]->GetYaxis()->SetTitle(
"Entries");
88 TH1D* hCosth_el =
new TH1D(Form(
"hCosth_el_fRun%d",
fStartRun),
90 TH1D* hCosth_po =
new TH1D(Form(
"hCosth_po_fRun%d",
fStartRun), Form(
"cos(#theta) dist (e+), start run: %d; cos(#theta); Entries",
92 TH1D* hCosth_ep =
new TH1D(Form(
"hCosth_ep_fRun%d",
fStartRun),
95 for (
int i = 0; i < ttree->GetEntries(); ++i) {
100 if (dedx <= 0 || charge == 0)
continue;
103 if (costh < TMath::Cos(150 * TMath::DegToRad()) || costh > TMath::Cos(17 * TMath::DegToRad()))
continue;
105 int bin = int((costh -
fCosMin) / binW);
106 if (bin < 0 || bin >=
int(
fCosbins))
continue;
110 hCosth_el->Fill(costh);
111 hdEdx_elCosbin[bin]->Fill(dedx);
112 }
else if (charge > 0) {
113 hCosth_po->Fill(costh);
114 hdEdx_poCosbin[bin]->Fill(dedx);
117 hCosth_ep->Fill(costh);
118 hdEdx_epCosbin[bin]->Fill(dedx);
123 TH1D* hdEdxMeanvsCos_po =
new TH1D(Form(
"hdEdxMeanvsCos_po_fRun%d",
fStartRun),
125 TH1D* hdEdxSigmavsCos_po =
new TH1D(Form(
"hdEdxSigmavsCos_po_fRun%d",
fStartRun),
128 TH1D* hdEdxMeanvsCos_el =
new TH1D(Form(
"hdEdxMeanvsCos_el_fRun%d",
fStartRun),
130 TH1D* hdEdxSigmavsCos_el =
new TH1D(Form(
"hdEdxSigmavsCos_el_fRun%d",
fStartRun),
133 TH1D* hdEdxMeanvsCos_ep =
new TH1D(Form(
"hdEdxMeanvsCos_ep_fRun%d",
fStartRun),
135 TH1D* hdEdxSigmavsCos_ep =
new TH1D(Form(
"hdEdxSigmavsCos_ep_fRun%d",
fStartRun),
139 TCanvas* ctmp_ep =
new TCanvas(
"ctmp_ep",
"ctmp_ep", 800, 400);
142 ctmp_ep->Divide(2, 2);
143 ctmp_ep->SetCanvasSize(800, 800);
145 std::stringstream psname_ep;
149 psname_ep << Form(
"cdcdedx_coscal_fits_frun%d.pdf[",
fStartRun);
150 ctmp_ep->Print(psname_ep.str().c_str());
152 psname_ep << Form(
"cdcdedx_coscal_fits_frun%d.pdf",
fStartRun);
156 std::vector<double> cosine;
157 for (
unsigned int i = 0; i <
fCosbins; ++i) {
160 TLine* tl =
new TLine();
161 tl->SetLineColor(kBlack);
163 double fdEdxMean = 1.0;
164 double fdEdxMeanErr = 0.0;
170 double fdEdxSigma = 0.0, fdEdxSigmaErr = 0.0;
173 if (status !=
"FitOK") {
175 hdEdx_epCosbin[i]->SetTitle(Form(
"%s, Fit(%s)", hdEdx_epCosbin[i]->GetTitle(), status.Data()));
177 fdEdxMean = hdEdx_epCosbin[i]->GetFunction(
"gaus")->GetParameter(1);
178 fdEdxMeanErr = hdEdx_epCosbin[i]->GetFunction(
"gaus")->GetParError(1);
179 fdEdxSigma = hdEdx_epCosbin[i]->GetFunction(
"gaus")->GetParameter(2);
180 fdEdxSigmaErr = hdEdx_epCosbin[i]->GetFunction(
"gaus")->GetParError(2);
181 hdEdx_epCosbin[i]->SetTitle(Form(
"%s, Fit (%s), #mu_{fit}: %0.04f#pm%0.04f,, #sigma_{fit}: %0.04f", hdEdx_epCosbin[i]->GetTitle(),
182 status.Data(), fdEdxMean, fdEdxMeanErr, fdEdxSigma));
185 hdEdxMeanvsCos_ep->SetBinContent(i + 1, fdEdxMean);
186 hdEdxMeanvsCos_ep->SetBinError(i + 1, fdEdxMeanErr);
187 hdEdxSigmavsCos_ep->SetBinContent(i + 1, fdEdxSigma);
188 hdEdxSigmavsCos_ep->SetBinError(i + 1, fdEdxSigmaErr);
191 ctmp_ep->cd(i % 4 + 1);
192 hdEdx_epCosbin[i]->SetFillColorAlpha(kYellow, 0.25);
193 hdEdx_epCosbin[i]->DrawCopy(
"hist");
195 tl->SetX1(fdEdxMean); tl->SetX2(fdEdxMean);
196 tl->SetY1(0); tl->SetY2(hdEdx_epCosbin[i]->GetMaximum());
197 tl->DrawClone(
"same");
198 if ((i + 1) % 4 == 0 || (i + 1 ==
fCosbins))ctmp_ep->Print(psname_ep.str().c_str());
202 double fdEdxMean_el = 1.0, fdEdxMean_elErr = 0.0;
203 double fdEdxSigma_el = 0.0, fdEdxSigma_elErr = 0.0;
204 double fdEdxMean_po = 1.0, fdEdxMean_poErr = 0.0;
205 double fdEdxSigma_po = 0.0, fdEdxSigma_poErr = 0.0;
206 TString status_el =
"", status_po =
"";
210 if (status_el !=
"FitOK") {
212 hdEdx_elCosbin[i]->SetTitle(Form(
"%s, Fit(%s)", hdEdx_elCosbin[i]->GetTitle(), status_el.Data()));
214 fdEdxMean_el = hdEdx_elCosbin[i]->GetFunction(
"gaus")->GetParameter(1);
215 fdEdxMean_elErr = hdEdx_elCosbin[i]->GetFunction(
"gaus")->GetParError(1);
216 fdEdxSigma_el = hdEdx_elCosbin[i]->GetFunction(
"gaus")->GetParameter(2);
217 fdEdxSigma_elErr = hdEdx_elCosbin[i]->GetFunction(
"gaus")->GetParError(2);
218 hdEdx_elCosbin[i]->SetTitle(Form(
"%s, Fit (%s), #mu_{fit}: %0.04f#pm%0.04f,, #sigma_{fit}: %0.04f", hdEdx_elCosbin[i]->GetTitle(),
219 status_el.Data(), fdEdxMean_el, fdEdxMean_elErr, fdEdxSigma_el));
222 hdEdxMeanvsCos_el->SetBinContent(i + 1, fdEdxMean_el);
223 hdEdxMeanvsCos_el->SetBinError(i + 1, fdEdxMean_elErr);
224 hdEdxSigmavsCos_el->SetBinContent(i + 1, fdEdxSigma_el);
225 hdEdxSigmavsCos_el->SetBinError(i + 1, fdEdxSigma_elErr);
229 if (status_po !=
"FitOK") {
231 hdEdx_poCosbin[i]->SetTitle(Form(
"%s, Fit(%s)", hdEdx_poCosbin[i]->GetTitle(), status_po.Data()));
233 fdEdxMean_po = hdEdx_poCosbin[i]->GetFunction(
"gaus")->GetParameter(1);
234 fdEdxMean_poErr = hdEdx_poCosbin[i]->GetFunction(
"gaus")->GetParError(1);
235 fdEdxSigma_po = hdEdx_poCosbin[i]->GetFunction(
"gaus")->GetParameter(2);
236 fdEdxSigma_poErr = hdEdx_poCosbin[i]->GetFunction(
"gaus")->GetParError(2);
237 hdEdx_poCosbin[i]->SetTitle(Form(
"%s, Fit (%s), #mu_{fit}: %0.04f#pm%0.04f,, #sigma_{fit}: %0.04f", hdEdx_poCosbin[i]->GetTitle(),
238 status_po.Data(), fdEdxMean_po, fdEdxMean_poErr, fdEdxSigma_po));
241 if (status_po !=
"FitOK" && status_el ==
"FitOK") {
242 fdEdxMean_po = fdEdxMean_el;
243 hdEdx_poCosbin[i]->SetTitle(Form(
"%s, mean (manual) = elec left", hdEdx_poCosbin[i]->GetTitle()));
244 }
else if (status_el !=
"FitOK" && status_po ==
"FitOK") {
245 fdEdxMean_el = fdEdxMean_po;
246 hdEdx_elCosbin[i]->SetTitle(Form(
"%s, mean (manual) = posi right", hdEdx_elCosbin[i]->GetTitle()));
247 }
else if (status_el !=
"FitOK" && status_po !=
"FitOK") {
248 fdEdxMean_po = 1.0; fdEdxMean_el = 1.0;
251 hdEdxMeanvsCos_po->SetBinContent(i + 1, fdEdxMean_po);
252 hdEdxMeanvsCos_po->SetBinError(i + 1, fdEdxMean_poErr);
253 hdEdxSigmavsCos_po->SetBinContent(i + 1, fdEdxSigma_po);
254 hdEdxSigmavsCos_po->SetBinError(i + 1, fdEdxSigma_poErr);
260 hdEdx_elCosbin[i]->SetFillColorAlpha(kYellow, 0.25);
261 hdEdx_elCosbin[i]->DrawCopy(
"");
262 tl->SetX1(fdEdxMean_el); tl->SetX2(fdEdxMean_el);
263 tl->SetY1(0); tl->SetY2(hdEdx_elCosbin[i]->GetMaximum());
264 tl->DrawClone(
"same");
267 hdEdx_poCosbin[i]->SetFillColorAlpha(kBlue, 0.25);
268 hdEdx_poCosbin[i]->DrawCopy(
"");
269 tl->SetX1(fdEdxMean_po); tl->SetX2(fdEdxMean_po);
270 tl->SetY1(0); tl->SetY2(hdEdx_poCosbin[i]->GetMaximum());
271 tl->DrawClone(
"same");
272 ctmp_ep->Print(psname_ep.str().c_str());
276 fdEdxMean = 0.5 * (fdEdxMean_po + fdEdxMean_el);
277 if (fdEdxMean <= 0)fdEdxMean = 1.0;
278 fdEdxMeanErr = 0.5 * TMath::Sqrt(fdEdxMean_elErr * fdEdxMean_elErr + fdEdxMean_poErr * fdEdxMean_poErr);
279 hdEdxMeanvsCos_ep->SetBinContent(i + 1, fdEdxMean);
280 hdEdxMeanvsCos_ep->SetBinError(i + 1, fdEdxMeanErr);
283 cosine.push_back(fdEdxMean);
291 psname_ep << Form(
"cdcdedx_coscal_fits_frun%d.pdf]",
fStartRun);
292 ctmp_ep->Print(psname_ep.str().c_str());
295 TCanvas* cstats =
new TCanvas(
"cstats",
"cstats", 1000, 500);
296 cstats->SetBatch(kTRUE);
297 cstats->Divide(2, 1);
299 auto hestats = getObjectPtr<TH1I>(
"hestats");
301 hestats->SetName(Form(
"hestats_fRun%d",
fStartRun));
302 hestats->SetStats(0);
303 hestats->DrawCopy(
"");
306 auto htstats = getObjectPtr<TH1I>(
"htstats");
308 hestats->SetName(Form(
"htstats_fRun%d",
fStartRun));
309 htstats->DrawCopy(
"");
310 hestats->SetStats(0);
312 cstats->Print(Form(
"cdcdedx_coscal_stats_frun%d.pdf",
fStartRun));
315 TCanvas* ctmp_epConst =
new TCanvas(
"ctmp_epConst",
"ctmp_epConst", 800, 400);
316 ctmp_epConst->Divide(2, 1);
318 TCanvas* ctmp_epCosth =
new TCanvas(
"ctmp_epCosth",
"ctmp_epCosth", 600, 500);
324 hdEdxMeanvsCos_el->SetMarkerStyle(20);
325 hdEdxMeanvsCos_el->SetMarkerSize(0.60);
326 hdEdxMeanvsCos_el->SetMarkerColor(kRed);
327 hdEdxMeanvsCos_el->SetStats(0);
328 hdEdxMeanvsCos_el->SetTitle(
"comparison of dedx #mu_{fit}^{rel}: (e-=red, e+=blue, avg=black)");
329 hdEdxMeanvsCos_el->GetYaxis()->SetRangeUser(0.96, 1.04);
330 hdEdxMeanvsCos_el->DrawCopy(
"");
332 hdEdxMeanvsCos_po->SetMarkerStyle(20);
333 hdEdxMeanvsCos_po->SetMarkerSize(0.60);
334 hdEdxMeanvsCos_po->SetMarkerColor(kBlue);
335 hdEdxMeanvsCos_po->SetStats(0);
336 hdEdxMeanvsCos_po->DrawCopy(
"same");
338 hdEdxMeanvsCos_ep->SetMarkerStyle(20);
339 hdEdxMeanvsCos_ep->SetMarkerSize(0.60);
340 hdEdxMeanvsCos_ep->SetMarkerColor(kBlack);
341 hdEdxMeanvsCos_ep->SetStats(0);
342 hdEdxMeanvsCos_ep->DrawCopy(
"same");
346 hdEdxSigmavsCos_el->SetMarkerStyle(4);
347 hdEdxSigmavsCos_el->SetMarkerColor(kRed);
348 hdEdxSigmavsCos_el->SetMarkerSize(0.90);
349 hdEdxSigmavsCos_el->SetTitle(
"comparison of dedx #mu_{fit}^{rel}: (e-=open, e+=closed)");
350 hdEdxSigmavsCos_el->GetYaxis()->SetRangeUser(0.4, 0.12);
351 hdEdxSigmavsCos_el->SetStats(0);
352 hdEdxSigmavsCos_el->DrawCopy(
"");
354 hdEdxSigmavsCos_po->SetMarkerStyle(8);
355 hdEdxSigmavsCos_po->SetMarkerSize(0.80);
356 hdEdxSigmavsCos_po->SetMarkerColor(kBlue);
357 hdEdxSigmavsCos_po->SetStats(0);
358 hdEdxSigmavsCos_po->DrawCopy(
"same");
361 hCosth_el->SetStats(0);
362 hCosth_el->SetLineColor(kRed);
363 hCosth_el->SetFillColorAlpha(kYellow, 0.55);
364 hCosth_el->DrawCopy(
"");
365 hCosth_po->SetStats(0);
366 hCosth_po->SetLineColor(kBlue);
367 hCosth_po->SetFillColorAlpha(kGray, 0.35);
368 hCosth_po->DrawCopy(
"same");
374 hdEdxMeanvsCos_ep->SetMarkerStyle(20);
375 hdEdxMeanvsCos_ep->SetMarkerSize(0.60);
376 hdEdxMeanvsCos_ep->SetMarkerColor(kBlack);
377 hdEdxMeanvsCos_ep->SetStats(0);
378 hdEdxMeanvsCos_ep->SetTitle(
"dedx rel(#mu_{fit}) for e- and e+ combined");
379 hdEdxMeanvsCos_ep->GetYaxis()->SetRangeUser(0.97, 1.04);
380 hdEdxMeanvsCos_ep->DrawCopy(
"");
384 hdEdxSigmavsCos_ep->SetMarkerStyle(20);
385 hdEdxSigmavsCos_ep->SetMarkerColor(kRed);
386 hdEdxSigmavsCos_ep->SetMarkerSize(1.1);
387 hdEdxSigmavsCos_ep->SetTitle(
"dedx rel(#sigma_{fit}) for e- and e+ combined");
388 hdEdxSigmavsCos_ep->GetYaxis()->SetRangeUser(0.4, 0.12);
389 hdEdxSigmavsCos_ep->SetStats(0);
390 hdEdxSigmavsCos_ep->DrawCopy(
"");
393 hCosth_ep->SetStats(0);
394 hCosth_ep->SetLineColor(kGray);
395 hCosth_ep->SetFillColorAlpha(kGray, 0.25);
396 hCosth_ep->DrawCopy(
"same");
399 ctmp_epCosth->SaveAs(Form(
"cdcdedx_coscal_costhdist_frun%d.pdf",
fStartRun));
402 ctmp_epConst->SaveAs(Form(
"cdcdedx_coscal_relmeans_frun%d.pdf",
fStartRun));
403 ctmp_epConst->SaveAs(Form(
"cdcdedx_coscal_relmeans_frun%d.root",
fStartRun));
414 TH1D* hCosCorrOld =
new TH1D(Form(
"hCosCorrOld_fRun%d",
fStartRun),
415 Form(
"cos corr const comparison (red=old, blue=new), start run: %d;cos(#theta);dE/dx #mu_{fit}",
fStartRun),
fCosbins,
fCosMin,
417 TH1D* hCosCorrNew =
new TH1D(Form(
"hCosCorrNew_fRun%d",
fStartRun), Form(
"coss corr, start run: %d;cos(#theta);dE/dx #mu_{fit}",
419 TH1D* hCosCorrRel =
new TH1D(Form(
"hCosCorrRel_fRun%d",
fStartRun),
426 B2INFO(
"Saving new rung for (Exp, Run) : (" << expRun.first <<
"," << expRun.second <<
")");
427 for (
unsigned int ibin = 0; ibin <
m_DBCosineCor->getSize(); ibin++) {
428 hCosCorrOld->SetBinContent(ibin + 1, (
double)
m_DBCosineCor->getMean(ibin));
429 hCosCorrRel->SetBinContent(ibin + 1, cosine.at(ibin));
430 B2INFO(
"Cosine Corr for Bin # " << ibin <<
", Previous = " <<
m_DBCosineCor->getMean(ibin) <<
", Relative = " << cosine.at(
431 ibin) <<
", Merged = " <<
m_DBCosineCor->getMean(ibin)*cosine.at(ibin));
433 hCosCorrNew->SetBinContent(ibin + 1, cosine.at(ibin));
438 TCanvas* ctmp_const =
new TCanvas(
"ctmp_const",
"ctmp_const", 900, 450);
439 ctmp_const->Divide(2, 1);
444 hCosCorrOld->SetStats(0);
445 hCosCorrOld->SetLineColor(kRed);
446 hCosCorrOld->GetYaxis()->SetRangeUser(0.64, 1.20);
447 hCosCorrOld->DrawCopy(
"");
448 hCosCorrNew->SetStats(0);
449 hCosCorrNew->SetLineColor(kBlue);
450 hCosCorrNew->DrawCopy(
"same");
454 hCosCorrRel->SetStats(0);
455 hCosCorrRel->GetYaxis()->SetRangeUser(0.97, 1.03);
456 hCosCorrRel->SetLineColor(kBlack);
457 hCosCorrRel->DrawCopy(
"");
459 ctmp_const->SaveAs(Form(
"cdcdedx_coscal_constants_frun%d.pdf",
fStartRun));
460 ctmp_const->SaveAs(Form(
"cdcdedx_coscal_constants_frun%d.root",
fStartRun));
464 B2INFO(
"dE/dx calibration done for CDC dE/dx _eltron saturation");
471 if (temphist->Integral() < 2000) {
472 B2INFO(Form(
"\tThis hist (%s) have insufficient entries to perform fit (%0.03f)", temphist->GetName(), temphist->Integral()));
476 temphist->GetXaxis()->SetRange(temphist->FindFirstBinAbove(0, 1), temphist->FindLastBinAbove(0, 1));
477 int fs = temphist->Fit(
"gaus",
"QR");
479 B2INFO(Form(
"\tFit (round 1) for hist (%s) failed (status = %d)", temphist->GetName(), fs));
480 status =
"FitFailed";
483 double fdEdxMean = temphist->GetFunction(
"gaus")->GetParameter(1);
484 double width = temphist->GetFunction(
"gaus")->GetParameter(2);
485 temphist->GetXaxis()->SetRangeUser(fdEdxMean - 5.0 * width, fdEdxMean + 5.0 * width);
486 fs = temphist->Fit(
"gaus",
"QR",
"", fdEdxMean -
fSigLim * width, fdEdxMean +
fSigLim * width);
488 B2INFO(Form(
"\tFit (round 2) for hist (%s) failed (status = %d)", temphist->GetName(), fs));
489 status =
"FitFailed";
492 temphist->GetXaxis()->SetRangeUser(fdEdxMean - 5.0 * width, fdEdxMean + 5.0 * width);
493 B2INFO(Form(
"\tFit for hist (%s) successful (status = %d)", temphist->GetName(), fs));
void generateNewPayloads(std::vector< double > cosine)
function to store new payload after full calibration
int fStartRun
boundary start at this run
double fdEdxMax
max dedx range for gain cal
bool isMergePayload
merge payload at the of calibration
double fCosMax
max cosine angle for cal
DBObjPtr< CDCDedxCosineCor > m_DBCosineCor
Electron saturation correction DB object.
double fCosMin
min cosine angle for cal
CDCDedxCosineAlgorithm()
Constructor: Sets the description, the properties and the parameters of the algorithm.
virtual EResult calibrate() override
Cosine algorithm.
double fdEdxMin
min dedx range for gain cal
void FitGaussianWRange(TH1D *&temphist, TString &status)
function to fit histogram in each cosine bin
int fHistbins
number of bins for dedx histogram
bool isMethodSep
if e+e- need to be consider sep
unsigned int fCosbins
number of bins across cosine range
bool isMakePlots
produce plots for status
double fSigLim
gaussian fit sigma limit
dE/dx wire gain calibration constants
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.