10#include <ecl/calibration/eclBhabhaTAlgorithm.h>
13#include <ecl/dataobjects/ECLElementNumbers.h>
14#include <ecl/dbobjects/ECLCrystalCalib.h>
15#include <ecl/dbobjects/ECLReferenceCrystalPerCrateCalib.h>
16#include <ecl/digitization/EclConfiguration.h>
17#include <ecl/mapper/ECLChannelMapper.h>
20#include <framework/database/DBObjPtr.h>
21#include <framework/database/DBStore.h>
22#include <framework/dataobjects/EventMetaData.h>
23#include <framework/datastore/DataStore.h>
24#include <framework/datastore/StoreObjPtr.h>
40 cellIDHi(ECLElementNumbers::c_NCrystals),
41 meanCleanRebinFactor(1),
42 meanCleanCutMinFactor(0),
45 savePrevCrysPayload(false),
46 readPrevCrysPayload(false),
48 debugFilenameBase(
"eclBhabhaTAlgorithm"),
49 collectorName(
"ECLBhabhaTCollector"),
50 refCrysPerCrate{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
51 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
52 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
53 -1, -1, -1, -1, -1, -1, -1}
55 setDescription(
"Calculate time offsets from bhabha events by fitting gaussian function to the (t - T0) difference.");
67 B2INFO(
"eclBhabhaTAlgorithm parameters:");
76 B2INFO(
"refCrysPerCrate = {");
77 for (
int crateTest = 0; crateTest < 52 - 1; crateTest++) {
85 auto TimevsCrysPrevCrateCalibPrevCrystCalib = getObjectPtr<TH2F>(
"TimevsCrysPrevCrateCalibPrevCrystCalib");
86 auto TimevsCratePrevCrateCalibPrevCrystCalib = getObjectPtr<TH2F>(
"TimevsCratePrevCrateCalibPrevCrystCalib");
87 auto TimevsCrysNoCalibrations = getObjectPtr<TH2F>(
"TimevsCrysNoCalibrations");
88 auto TimevsCrysPrevCrateCalibNoCrystCalib = getObjectPtr<TH2F>(
"TimevsCrysPrevCrateCalibNoCrystCalib");
89 auto TimevsCrateNoCrateCalibPrevCrystCalib = getObjectPtr<TH2F>(
"TimevsCrateNoCrateCalibPrevCrystCalib");
92 auto cutflow = getObjectPtr<TH1F>(
"cutflow");
97 unique_ptr<TH1F> tsNew_MINUS_tsOld__cid(
new TH1F(
"TsNew_MINUS_TsOld__cid",
100 unique_ptr<TH1F> tcrateNew_MINUS_tcrateOld__crateID(
new TH1F(
"tcrateNew_MINUS_tcrateOld__crateID",
101 ";crate id; tcrate(new | bhabha) - tcrate(previous iteration | merged) [ns]",
103 unique_ptr<TH1F> tsNew_MINUS_tsCustomPrev__cid(
new TH1F(
"TsNew_MINUS_TsCustomPrev__cid",
104 ";cell id; ts(new|bhabha) - ts(old = 'before 1st iter'|merged) [ns]",
106 unique_ptr<TH1F> tsNew_MINUS_tsOldBhabha__cid(
new TH1F(
"TsNew_MINUS_TsOldBhabha__cid",
112 unique_ptr<TH1F> tsNew_MINUS_tsOld(
new TH1F(
"TsNew_MINUS_TsOld",
113 ";ts(new | bhabha) - ts(previous iteration | merged) [ns];Number of crystals",
114 201, -10.05, 10.05));
115 unique_ptr<TH1F> tcrateNew_MINUS_tcrateOld(
new TH1F(
"tcrateNew_MINUS_tcrateOld",
116 ";tcrate(new) - tcrate(previous iteration) [ns];Number of crates",
117 201, -10.05, 10.05));
118 unique_ptr<TH1F> tsNew_MINUS_tsCustomPrev(
new TH1F(
"TsNew_MINUS_TsCustomPrev",
119 ";ts(new | bhabha) - ts(old = 'before 1st iter' | merged) [ns];Number of crystals",
120 285, -69.5801, 69.5801));
121 unique_ptr<TH1F> tsNew_MINUS_tsOldBhabha(
new TH1F(
"TsNew_MINUS_TsOldBhabha",
122 ";ts(new | bhabha) - ts(previous iteration | bhabha) [ns];Number of crystals",
123 201, -10.05, 10.05));
128 unique_ptr<TH1F> tcrateNew_MINUS_tcrateOld_allRuns(
new TH1F(
"tcrateNew_MINUS_tcrateOld_allRuns",
129 "Crate time constant changes over all runs : tcrate(new) uncertainty < 0.1ns;tcrate(new) - tcrate(previous iteration) [ns];Number of crates",
130 201, -10.05, 10.05));
132 unique_ptr<TH1F> tcrateNew_MINUS_tcrateOld_allRuns_allCrates(
new TH1F(
"tcrateNew_MINUS_tcrateOld_allRuns_allCrates",
133 "Crate time constant changes over all runs : all crates;tcrate(new) - tcrate(previous iteration) [ns];Number of crates",
134 201, -10.05, 10.05));
136 unique_ptr<TH1I> num_tcrates_perRun(
new TH1I(
"num_tcrates_perRun",
137 "Number of good tcrates in each run;Run number;Number of good tcrates",
140 unique_ptr<TH2F> tcrateNew_MINUS_tcrateOld__vs__runNum(
new TH2F(
"tcrateNew_MINUS_tcrateOld__vs__runNum",
141 "Crate time constant changes vs run number : tcrate(new) uncertainty < 0.1ns;Run number;tcrate(new) - tcrate(previous iteration) [ns]",
142 6000, 0, 6000, 21, -10.5, 10.5));
148 if (!TimevsCrysNoCalibrations)
return c_Failure;
153 TFile* histExtraCrateInfofile = 0;
156 unique_ptr<TTree> tree_crystal(
new TTree(
"tree_crystal",
"Debug data from bhabha time calibration algorithm for crystals"));
158 unique_ptr<TTree> tree_crate(
new TTree(
"tree_crate",
"Debug data from bhabha time calibration algorithm for crates"));
162 vector<float> t_offsets;
164 vector<float> t_offsets_unc;
165 vector<float> t_offsets_prev;
168 int minNumEntries = 40;
169 int minNumEntriesCrateConvergence = 1000;
175 int crystalCalibSaved = 0;
179 bool minRunNumBool =
false;
180 bool maxRunNumBool =
false;
186 int expNumber = expRun.first;
187 int runNumber = expRun.second;
188 if (!minRunNumBool) {
189 minExpNum = expNumber;
190 minRunNum = runNumber;
191 minRunNumBool =
true;
193 if (!maxRunNumBool) {
194 maxExpNum = expNumber;
195 maxRunNum = runNumber;
196 maxRunNumBool =
true;
198 if (((minRunNum > runNumber) && (minExpNum >= expNumber)) ||
199 (minExpNum > expNumber)) {
200 minExpNum = expNumber;
201 minRunNum = runNumber;
203 if (((maxRunNum < runNumber) && (maxExpNum <= expNumber)) ||
204 (maxExpNum < expNumber))
207 maxExpNum = expNumber;
208 maxRunNum = runNumber;
213 string runNumsString = string(
"_") + to_string(minExpNum) +
"_" + to_string(minRunNum) + string(
"-") +
214 to_string(maxExpNum) +
"_" + to_string(maxRunNum);
216 string extraCratedebugFilename =
debugFilenameBase + string(
"_cratesAllRuns.root");
223 int eventNumberForCrates = 1;
231 evtPtr.
construct(eventNumberForCrates, minRunNum, minExpNum);
240 B2INFO(
"Uploading payload for exp " << minExpNum <<
", run " << minRunNum <<
", event " << eventNumberForCrates);
243 crystalMapper->initFromDB();
248 B2INFO(
"Reading payloads: ECLCrystalTimeOffset and ECLCrateTimeOffset");
253 vector<float> currentValuesCrys = crystalTimeObject->getCalibVector();
254 vector<float> currentUncCrys = crystalTimeObject->getCalibUncVector();
255 vector<float> currentValuesCrate = crateTimeObject->getCalibVector();
256 vector<float> currentUncCrate = crateTimeObject->getCalibUncVector();
259 B2INFO(
"Values read from database. Write out for their values for comparison against those from tcol");
261 B2INFO(
"ts: cellID " << ic + 1 <<
" " << currentValuesCrys[ic] <<
" +/- " << currentUncCrys[ic]);
262 B2INFO(
"tcrate: cellID " << ic + 1 <<
" " << currentValuesCrate[ic] <<
" +/- " << currentUncCrate[ic]);
271 prevValuesCrys = customPrevCrystalTimeObject->getCalibVector();
274 B2INFO(
"Previous values read from database. Write out for their values for comparison against those from tcol");
276 B2INFO(
"ts custom previous payload: cellID " << ic + 1 <<
" " << prevValuesCrys[ic]);
282 B2INFO(
"Reading payloads: ECLCrystalTimeOffsetBhabha");
286 vector<float> currentBhabhaValuesCrys = crystalBhabhaTimeObject->getCalibVector();
287 vector<float> currentBhabhaUncCrys = crystalBhabhaTimeObject->getCalibUncVector();
291 B2INFO(
"ts bhabha: cellID " << ic + 1 <<
" " << currentBhabhaValuesCrys[ic] <<
" +/- " << currentBhabhaUncCrys[ic]);
297 auto refCrysIDzeroingCrate = getObjectPtr<TH1F>(
"refCrysIDzeroingCrate");
301 B2INFO(
"Extract reference crystals from collector histogram.");
302 vector <short> crystalIDreferenceUntested;
304 if (refCrysIDzeroingCrate->GetBinContent(bin) > 0.5) {
305 crystalIDreferenceUntested.push_back(bin);
311 B2INFO(
"Reference crystals to define as having ts=0. Base 1 counting for both crates and crystals");
312 for (
long unsigned int crysRefCounter = 0; crysRefCounter < crystalIDreferenceUntested.size(); crysRefCounter++) {
313 int crys_id = crystalIDreferenceUntested[crysRefCounter] ;
314 int crate_id_from_crystal = crystalMapper->getCrateID(crys_id);
315 B2INFO(
" crystal " << crys_id <<
" is a reference for crate " << crate_id_from_crystal);
321 B2INFO(
"Checking number of reference crystals");
322 B2INFO(
"Number of reference crystals = " << crystalIDreferenceUntested.size());
325 if (crystalIDreferenceUntested.size() != 52) {
326 B2FATAL(
"The number of reference crystals does not equal 52, which is one per crate");
329 B2INFO(
"Number of reference crystals is 52 as required");
335 vector <short> crateIDsNumRefCrystalsUntested(52, 0);
336 vector <short> crystalIDReferenceForZeroTs(52, 0);
338 for (
long unsigned int crysRefCounter = 0; crysRefCounter < crystalIDreferenceUntested.size(); crysRefCounter++) {
339 int crys_id = crystalIDreferenceUntested[crysRefCounter] ;
340 int crate_id_from_crystal = crystalMapper->getCrateID(crys_id);
341 crateIDsNumRefCrystalsUntested[crate_id_from_crystal - 1]++;
342 crystalIDReferenceForZeroTs[crate_id_from_crystal - 1] = crys_id;
344 B2INFO(
"crystalIDReferenceForZeroTs = {");
345 for (
int crateTest = 0; crateTest < 52 - 1; crateTest++) {
346 B2INFO(crystalIDReferenceForZeroTs[crateTest] <<
",");
348 B2INFO(crystalIDReferenceForZeroTs[52 - 1] <<
"}");
352 for (
int crateTest = 0; crateTest < 52; crateTest++) {
353 if (crateIDsNumRefCrystalsUntested[crateTest] != 1) {
354 B2FATAL(
"Crate " << crateTest + 1 <<
" (base 1) has " << crateIDsNumRefCrystalsUntested[crateTest] <<
" reference crystals");
358 B2INFO(
"All reference crystals are reasonably mapped one crystal to one crate for all crates");
362 B2INFO(
"Extract reference crystals from algorithm steering script if provided. If user inputs custom values via steering script for this algorithm, they are only applied after all the tests are performed on the values from the histogram and override the histogram valuees. User can adjust just a single crystal if desired. Use -1 to indicate that a crystal is not to be modified. Position of crystal in list determines the crate to which the crystal is meant to be associated.");
368 bool userSetRefCrysPerCrate = false ;
369 for (
int crateTest = 0; crateTest < 52; crateTest++) {
372 B2INFO(
"Crate " << crateTest + 1 <<
" (base 1) new reference crystal = " << crystalIDReferenceForZeroTs[crateTest]);
373 userSetRefCrysPerCrate = true ;
376 if (userSetRefCrysPerCrate) {
377 B2INFO(
"User changed reference crystals via steering script");
380 fill(crateIDsNumRefCrystalsUntested.begin(), crateIDsNumRefCrystalsUntested.end(), 0);
381 for (
long unsigned int crysRefCounter = 0; crysRefCounter < 52; crysRefCounter++) {
382 int crys_id = crystalIDReferenceForZeroTs[crysRefCounter] ;
383 int crate_id_from_crystal = crystalMapper->getCrateID(crys_id);
384 crateIDsNumRefCrystalsUntested[crate_id_from_crystal - 1]++;
386 for (
int crateTest = 0; crateTest < 52; crateTest++) {
387 if (crateIDsNumRefCrystalsUntested[crateTest] != 1) {
388 B2FATAL(
"Crate " << crateTest + 1 <<
" (base 1) has " << crateIDsNumRefCrystalsUntested[crateTest] <<
" reference crystals");
392 B2INFO(
"All reference crystals are reasonably mapped one crystal to one crate for all crates after changes made by user steering script.");
399 B2INFO(
"Created reference crystal per crate payload: ECLReferenceCrystalPerCrateCalib");
401 B2INFO(
"User did not change reference crystals via steering script");
408 B2INFO(
"Debug output rootfile: " << debugFilename);
409 histfile =
new TFile(debugFilename.c_str(),
"recreate");
412 TimevsCrysPrevCrateCalibPrevCrystCalib ->Write();
413 TimevsCratePrevCrateCalibPrevCrystCalib->Write();
414 TimevsCrysNoCalibrations ->Write();
415 TimevsCrysPrevCrateCalibNoCrystCalib ->Write();
416 TimevsCrateNoCrateCalibPrevCrystCalib ->Write();
422 tree_crystal->Branch(
"cid", &tree_cid)->SetTitle(
"Cell ID, 1..8736");
423 tree_crystal->Branch(
"ts", &mean)->SetTitle(
"Time offset mean, ts, ns");
424 tree_crystal->Branch(
"tsUnc", &mean_unc)->SetTitle(
"Error of time ts mean, ns.");
425 tree_crystal->Branch(
"tsSigma", &sigma)->SetTitle(
"Sigma of time ts distribution, ns");
426 tree_crystal->Branch(
"crystalCalibSaved",
427 &crystalCalibSaved)->SetTitle(
"0=crystal skipped, 1=crystal calib saved (num entries based)");
428 tree_crystal->Branch(
"tsPrev", &tsPrev)->SetTitle(
"Previous crystal time offset, ts, ns");
429 tree_crystal->SetAutoSave(10);
433 double hist_tmin = TimevsCrysNoCalibrations->GetYaxis()->GetXmin();
434 double hist_tmax = TimevsCrysNoCalibrations->GetYaxis()->GetXmax();
436 double time_fit_min = hist_tmax;
437 double time_fit_max = hist_tmin;
439 B2INFO(
"hist_tmin = " << hist_tmin);
440 B2INFO(
"hist_tmax = " << hist_tmax);
448 auto databaseCounter = getObjectPtr<TH1I>(
"databaseCounter");
449 float numTimesFilled = databaseCounter->GetBinContent(1);
450 B2INFO(
"Number of times database histograms were merged = " << numTimesFilled);
453 auto TsDatabase = getObjectPtr<TH1F>(
"TsDatabase");
454 auto TsDatabaseUnc = getObjectPtr<TH1F>(
"TsDatabaseUnc");
456 t_offsets.push_back(TsDatabase->GetBinContent(i) / numTimesFilled);
457 t_offsets_prev.push_back(TsDatabase->GetBinContent(i) / numTimesFilled);
459 B2INFO(
"t_offsets_prev (last iter) at crysID " << i <<
" = " << t_offsets_prev[i - 1]);
461 t_offsets_unc.push_back(TsDatabaseUnc->GetBinContent(i) / numTimesFilled);
469 TH1D* h_crysHits = TimevsCrysPrevCrateCalibNoCrystCalib->ProjectionX(
"h_crysHits");
470 h_crysHits->SetTitle(
"Hits per crystal;Crystal id");
472 histfile->WriteTObject(h_crysHits,
"h_crysHits");
477 crystalCalibSaved = 0;
479 double database_mean = 0;
480 double database_mean_unc = 0;
482 B2INFO(
"Crystal id = " << crys_id);
491 TH1D* h_time = TimevsCrysPrevCrateCalibNoCrystCalib->ProjectionY((
string(
"h_time_psi__") + to_string(crys_id)).c_str(),
493 TH1D* h_timeMask = (TH1D*)h_time->Clone();
494 TH1D* h_timeMasked = (TH1D*)h_time->Clone((
string(
"h_time_psi_masked__") + to_string(crys_id)).c_str());
495 TH1D* h_timeRebin = (TH1D*)h_time->Clone();
502 h_timeMask->Scale(0.0);
504 time_fit_min = hist_tmax;
505 time_fit_max = hist_tmin;
508 double histRebin_max = h_timeRebin->GetMaximum();
510 bool maskedOutNonZeroBin =
false;
512 for (
int bin = 1; bin <= h_timeRebin->GetNbinsX(); bin++) {
515 if (nonRebinnedBinNumber < h_time->GetNbinsX()) {
517 h_timeMask->SetBinContent(nonRebinnedBinNumber, 1);
520 double x_lower = h_timeRebin->GetXaxis()->GetBinLowEdge(bin);
521 double x_upper = h_timeRebin->GetXaxis()->GetBinUpEdge(bin);
522 if (x_lower < time_fit_min) {
523 time_fit_min = x_lower;
525 if (x_upper > time_fit_max) {
526 time_fit_max = x_upper;
530 if (h_time->GetBinContent(nonRebinnedBinNumber) > 0) {
531 B2DEBUG(22,
"Setting bin " << nonRebinnedBinNumber <<
" from " << h_timeMasked->GetBinContent(nonRebinnedBinNumber) <<
" to 0");
532 maskedOutNonZeroBin =
true;
534 h_timeMasked->SetBinContent(nonRebinnedBinNumber, 0);
539 B2INFO(
"Bins with non-zero values have been masked out: " << maskedOutNonZeroBin);
540 h_timeMasked->ResetStats();
541 h_timeMask->ResetStats();
546 double default_meanMasked = h_timeMasked->GetMean();
548 B2INFO(
"default_meanMasked = " << default_meanMasked);
552 double default_mean = h_time->GetMean();
553 double default_mean_unc = h_time->GetMeanError();
554 double default_sigma = h_time->GetStdDev();
556 B2INFO(
"Fitting crystal between " << time_fit_min <<
" and " << time_fit_max);
559 TF1* gaus =
new TF1(
"func",
"gaus(0)", time_fit_min, time_fit_max);
560 gaus->SetParNames(
"numCrystalHitsNormalization",
"mean",
"sigma");
567 double hist_max = h_time->GetMaximum();
570 double stddev = h_time->GetStdDev();
575 gaus->SetParameter(0, hist_max / 2.);
576 gaus->SetParameter(1, mean);
577 gaus->SetParameter(2, sigma);
584 h_timeMasked->Fit(gaus,
"LQR");
586 double fit_mean = gaus->GetParameter(1);
587 double fit_mean_unc = gaus->GetParError(1);
588 double fit_sigma = gaus->GetParameter(2);
590 double meanDiff = fit_mean - default_mean;
591 double meanUncDiff = fit_mean_unc - default_mean_unc;
592 double sigmaDiff = fit_sigma - default_sigma;
594 bool good_fit =
false;
596 if ((fabs(meanDiff) > 10) ||
597 (fabs(meanUncDiff) > 10) ||
598 (fabs(sigmaDiff) > 10) ||
599 (fit_mean_unc > 0.09) ||
601 (fit_mean < time_fit_min) ||
602 (fit_mean > time_fit_max)) {
603 B2INFO(
"Crystal id = " << crys_id);
604 B2INFO(
"fit mean, default mean = " << fit_mean <<
", " << default_mean);
605 B2INFO(
"fit mean unc, default mean unc = " << fit_mean_unc <<
", " << default_mean_unc);
606 B2INFO(
"fit sigma, default sigma = " << fit_sigma <<
", " << default_sigma);
608 B2INFO(
"crystal fit mean - hist mean = " << meanDiff);
609 B2INFO(
"fit mean unc. - hist mean unc. = " << meanUncDiff);
610 B2INFO(
"fit sigma - hist sigma = " << sigmaDiff);
612 B2INFO(
"fit_mean = " << fit_mean);
613 B2INFO(
"time_fit_min = " << time_fit_min);
614 B2INFO(
"time_fit_max = " << time_fit_max);
616 if (fabs(meanDiff) > 10) B2INFO(
"fit mean diff too large");
617 if (fabs(meanUncDiff) > 10) B2INFO(
"fit mean unc diff too large");
618 if (fabs(sigmaDiff) > 10) B2INFO(
"fit mean sigma diff too large");
619 if (fit_mean_unc > 0.09) B2INFO(
"fit mean unc too large");
620 if (fit_sigma < 0.1) B2INFO(
"fit sigma too small");
629 sigma = default_sigma;
632 int numEntries = h_time->GetEntries();
635 if ((numEntries >= minNumEntries) && good_fit) {
636 crystalCalibSaved = 1;
637 database_mean = fit_mean;
638 database_mean_unc = fit_mean_unc;
640 database_mean = default_mean;
641 database_mean_unc = -fabs(default_mean_unc);
644 if (numEntries < minNumEntries) B2INFO(
"Number of entries less than minimum");
645 if (numEntries == 0) B2INFO(
"Number of entries == 0");
651 t_offsets[crys_id - 1] = database_mean / TICKS_TO_NS;
652 t_offsets_unc[crys_id - 1] = database_mean_unc / TICKS_TO_NS;
655 histfile->WriteTObject(h_time, (
string(
"h_time_psi") + to_string(crys_id)).c_str());
656 histfile->WriteTObject(h_timeMasked, (
string(
"h_time_psi_masked") + to_string(crys_id)).c_str());
658 mean = database_mean;
659 mean_unc = database_mean_unc;
661 tsPrev = t_offsets_prev[crys_id - 1] * TICKS_TO_NS;
664 tree_crystal->Fill();
670 vector <double> tsRefCID ;
671 B2INFO(
"crystal times before shift");
672 for (
int crate_id = 1; crate_id <= 52; crate_id++) {
673 tsRefCID.push_back(t_offsets[ crystalIDReferenceForZeroTs[crate_id - 1] - 1 ]);
674 B2INFO(
"crystal time [crystal = " << crystalIDReferenceForZeroTs[crate_id - 1] <<
", crate = " << crate_id <<
" (base 1)] = " <<
675 t_offsets[ crystalIDReferenceForZeroTs[crate_id - 1] - 1 ] <<
" ticks");
678 B2INFO(
"crystal times after shift wrt reference crystal");
680 int crate_id_from_crystal = crystalMapper->getCrateID(crys_id);
681 B2INFO(
"crystal time before shift [crystal = " << crys_id <<
", crate = " << crate_id_from_crystal <<
" (base 1)] = " <<
682 t_offsets[crys_id - 1] <<
" +- " << t_offsets_unc[crys_id - 1] <<
" ticks");
687 if (t_offsets[crys_id - 1] == 0 && t_offsets_unc[crys_id - 1] == 0) {
688 B2INFO(
"crystal time after shift [crystal = " << crys_id <<
", crate = " << crate_id_from_crystal <<
" (base 1)] = " <<
689 t_offsets[crys_id - 1] <<
" ticks. No change because ts=0 and ts_unc=0 (no entries).");
691 t_offsets[crys_id - 1] = t_offsets[crys_id - 1] - tsRefCID[crate_id_from_crystal - 1];
692 B2INFO(
"crystal time after shift [crystal = " << crys_id <<
", crate = " << crate_id_from_crystal <<
" (base 1)] = " <<
693 t_offsets[crys_id - 1] <<
" ticks");
698 double tsDiff_ns = (t_offsets[crys_id - 1] - t_offsets_prev[crys_id - 1]) * TICKS_TO_NS;
699 double tsDiffBhabha_ns = -999;
701 tsDiffBhabha_ns = (t_offsets[crys_id - 1] - currentBhabhaValuesCrys[crys_id - 1]) * TICKS_TO_NS;
704 B2INFO(
"Crystal " << crys_id <<
": ts new bhabha - old merged = (" <<
705 t_offsets[crys_id - 1] <<
" - " << t_offsets_prev[crys_id - 1] <<
706 ") ticks * " << TICKS_TO_NS <<
" ns/tick = " << tsDiff_ns <<
" ns");
707 B2INFO(
"Crystal " << crys_id <<
": ts new bhabha - old bhabha = (" <<
708 t_offsets[crys_id - 1] <<
" - " << currentBhabhaValuesCrys[crys_id - 1] <<
709 ") ticks * " << TICKS_TO_NS <<
" ns/tick = " << tsDiffBhabha_ns <<
" ns");
711 tsNew_MINUS_tsOld__cid->SetBinContent(crys_id, tsDiff_ns);
712 tsNew_MINUS_tsOld__cid->SetBinError(crys_id, 0);
713 tsNew_MINUS_tsOld__cid->ResetStats();
715 tsNew_MINUS_tsOld->Fill(tsDiff_ns);
718 tsNew_MINUS_tsOldBhabha__cid->SetBinContent(crys_id, tsDiffBhabha_ns);
719 tsNew_MINUS_tsOldBhabha__cid->SetBinError(crys_id, 0);
720 tsNew_MINUS_tsOldBhabha__cid->ResetStats();
722 tsNew_MINUS_tsOldBhabha->Fill(tsDiffBhabha_ns);
728 double tsDiffCustomOld_ns = -999;
730 tsDiffCustomOld_ns = (t_offsets[crys_id - 1] - prevValuesCrys[crys_id - 1]) * TICKS_TO_NS;
731 B2INFO(
"Crystal " << crys_id <<
": ts new bhabha - 'before 1st iter' merged = (" <<
732 t_offsets[crys_id - 1] <<
" - " << prevValuesCrys[crys_id - 1] <<
733 ") ticks * " << TICKS_TO_NS <<
" ns/tick = " << tsDiffCustomOld_ns <<
" ns");
735 tsNew_MINUS_tsCustomPrev__cid->SetBinContent(crys_id, tsDiffCustomOld_ns);
736 tsNew_MINUS_tsCustomPrev__cid->SetBinError(crys_id, 0);
737 tsNew_MINUS_tsCustomPrev__cid->ResetStats();
739 tsNew_MINUS_tsCustomPrev->Fill(tsDiffCustomOld_ns);
744 histfile->WriteTObject(tsNew_MINUS_tsOld__cid.get(),
"tsNew_MINUS_tsOld__cid");
745 histfile->WriteTObject(tsNew_MINUS_tsOld.get(),
"tsNew_MINUS_tsOld");
747 histfile->WriteTObject(tsNew_MINUS_tsCustomPrev__cid.get(),
"tsNew_MINUS_tsCustomPrev__cid");
748 histfile->WriteTObject(tsNew_MINUS_tsCustomPrev.get(),
"tsNew_MINUS_tsCustomPrev");
750 histfile->WriteTObject(tsNew_MINUS_tsOldBhabha__cid.get(),
"tsNew_MINUS_tsOldBhabha__cid");
751 histfile->WriteTObject(tsNew_MINUS_tsOldBhabha.get(),
"tsNew_MINUS_tsOldBhabha");
761 crysTCalib_prev->
setCalibVector(currentValuesCrys, currentUncCrys);
764 crysBhabhaTCalib_prev->
setCalibVector(currentBhabhaValuesCrys, currentBhabhaUncCrys);
770 saveCalibration(crysTCalib_prev,
"ECLCrystalTimeOffsetPreviousValues");
771 B2INFO(
"Previous overall crystal payload made");
773 saveCalibration(crysBhabhaTCalib_prev,
"ECLCrystalTimeOffsetBhabhaPreviousValues");
774 B2INFO(
"Previous bhabha crystal payload made");
787 B2DEBUG(22,
"crystal payload made");
791 B2DEBUG(22,
"end of crystal start of crate corrections .....");
797 hist_tmin = TimevsCrateNoCrateCalibPrevCrystCalib->GetYaxis()->GetXmin();
798 hist_tmax = TimevsCrateNoCrateCalibPrevCrystCalib->GetYaxis()->GetXmax();
800 B2DEBUG(22,
"Found min/max of X axis of TimevsCrateNoCrateCalibPrevCrystCalib");
804 auto TcrateDatabase = getObjectPtr<TH1F>(
"TcrateDatabase");
807 B2DEBUG(22,
"Retrieved Ts and Tcrate histograms from tcol root file");
810 vector<float> tcrate_mean_new(52, 0.0);
811 vector<float> tcrate_mean_unc_new(52, 0.0);
812 vector<float> tcrate_sigma_new(52, 0.0);
813 vector<float> tcrate_mean_prev(52, 0.0);
815 vector<bool> tcrate_new_was_set(52,
false);
816 vector<bool> tcrate_new_goodQuality(52,
false);
818 B2DEBUG(22,
"crate vectors set");
825 int crate_id_from_crystal = crystalMapper->getCrateID(crys_id);
826 tcrate_mean_prev[crate_id_from_crystal - 1] = TcrateDatabase->GetBinContent(crys_id) / numTimesFilled;
830 B2INFO(
"Print out previous crate time calibration constants to make sure they match from the two different sources.");
831 for (
int crate_id = 1; crate_id <= 52; crate_id++) {
832 B2INFO(
"tcrate_mean_prev[crate " << crate_id <<
" (base 1)] = " << tcrate_mean_prev[crate_id - 1]);
834 int thisRefCellID = crystalIDReferenceForZeroTs[crate_id - 1];
835 B2INFO(
"tcrate from payload: ref cellID " << thisRefCellID <<
" " << currentValuesCrate[thisRefCellID - 1] <<
" +/- " <<
836 currentUncCrate[thisRefCellID - 1]);
843 TFile* histExtraCrateInfofile_dummy = 0;
844 B2INFO(
"Debug output rootfile used for crate iterations: " << extraCratedebugFilename);
845 histExtraCrateInfofile_dummy =
new TFile(extraCratedebugFilename.c_str(),
"UPDATE");
849 TKey* key = histExtraCrateInfofile_dummy->FindKey(
"tcrateNew_MINUS_tcrateOld_allRuns");
851 TH1F* h = (TH1F*)histExtraCrateInfofile_dummy->Get(
"tcrateNew_MINUS_tcrateOld_allRuns");
852 tcrateNew_MINUS_tcrateOld_allRuns->Add(h);
855 key = histExtraCrateInfofile_dummy->FindKey(
"tcrateNew_MINUS_tcrateOld_allRuns_allCrates");
857 TH1F* h = (TH1F*)histExtraCrateInfofile_dummy->Get(
"tcrateNew_MINUS_tcrateOld_allRuns_allCrates");
858 tcrateNew_MINUS_tcrateOld_allRuns_allCrates->Add(h);
861 key = histExtraCrateInfofile_dummy->FindKey(
"num_tcrates_perRun");
863 TH1F* h = (TH1F*)histExtraCrateInfofile_dummy->Get(
"num_tcrates_perRun");
864 num_tcrates_perRun->Add(h);
867 key = histExtraCrateInfofile_dummy->FindKey(
"tcrateNew_MINUS_tcrateOld__vs__runNum");
869 TH1F* h = (TH1F*)histExtraCrateInfofile_dummy->Get(
"tcrateNew_MINUS_tcrateOld__vs__runNum");
870 tcrateNew_MINUS_tcrateOld__vs__runNum->Add(h);
873 histExtraCrateInfofile_dummy->Close();
877 histExtraCrateInfofile =
new TFile(extraCratedebugFilename.c_str(),
"recreate");
884 B2DEBUG(22,
"Start of crate id = " << crate_id);
886 TH1D* h_time_crate = TimevsCrateNoCrateCalibPrevCrystCalib->ProjectionY(
"h_time_psi_crate", crate_id, crate_id);
887 TH1D* h_time_crate_mask = (TH1D*)h_time_crate->Clone();
888 TH1D* h_time_crate_masked = (TH1D*)h_time_crate->Clone();
889 TH1D* h_time_crate_rebin = (TH1D*)h_time_crate->Clone();
896 h_time_crate_mask->Scale(0.0);
898 time_fit_min = hist_tmax;
899 time_fit_max = hist_tmin;
902 double histRebin_max = h_time_crate_rebin->GetMaximum();
904 bool maskedOutNonZeroBin =
false;
906 for (
int bin = 1; bin <= h_time_crate_rebin->GetNbinsX(); bin++) {
909 if (nonRebinnedBinNumber < h_time_crate->GetNbinsX()) {
911 h_time_crate_mask->SetBinContent(nonRebinnedBinNumber, 1);
914 double x_lower = h_time_crate_rebin->GetXaxis()->GetBinLowEdge(bin);
915 double x_upper = h_time_crate_rebin->GetXaxis()->GetBinUpEdge(bin);
916 if (x_lower < time_fit_min) {
917 time_fit_min = x_lower;
919 if (x_upper > time_fit_max) {
920 time_fit_max = x_upper;
923 if (h_time_crate->GetBinContent(nonRebinnedBinNumber) > 0) {
924 B2DEBUG(22,
"Setting bin " << nonRebinnedBinNumber <<
" from " << h_time_crate_masked->GetBinContent(
925 nonRebinnedBinNumber) <<
" to 0");
926 maskedOutNonZeroBin =
true;
928 h_time_crate_masked->SetBinContent(nonRebinnedBinNumber, 0);
933 B2INFO(
"Bins with non-zero values have been masked out: " << maskedOutNonZeroBin);
934 h_time_crate_masked->ResetStats();
935 h_time_crate_mask->ResetStats();
941 B2DEBUG(22,
"crate loop - projected h_time_psi_crate");
944 double default_mean_crate = h_time_crate_masked->GetMean();
945 double default_mean_crate_unc = h_time_crate_masked->GetMeanError();
946 double default_sigma_crate = h_time_crate_masked->GetStdDev();
947 B2INFO(
"Fitting crate between " << time_fit_min <<
" and " << time_fit_max);
948 TF1* gaus =
new TF1(
"func",
"gaus(0)", time_fit_min, time_fit_max);
949 gaus->SetParNames(
"numCrateHisNormalization",
"mean",
"sigma");
950 double hist_max = h_time_crate->GetMaximum();
951 double stddev = h_time_crate->GetStdDev();
952 double sigma_crate = stddev;
953 double mean_crate = default_mean_crate;
954 gaus->SetParameter(0, hist_max / 2.);
955 gaus->SetParameter(1, mean_crate);
956 gaus->SetParameter(2, sigma_crate);
958 h_time_crate_masked->Fit(gaus,
"LQR");
960 double fit_mean_crate = gaus->GetParameter(1);
961 double fit_mean_crate_unc = gaus->GetParError(1);
962 double fit_sigma_crate = gaus->GetParameter(2);
964 double meanDiff = fit_mean_crate - default_mean_crate;
965 double meanUncDiff = fit_mean_crate_unc - default_mean_crate_unc;
966 double sigmaDiff = fit_sigma_crate - default_sigma_crate;
968 bool good_fit =
false;
970 B2DEBUG(22,
"Crate id = " << crate_id <<
" with crate mean = " << default_mean_crate <<
" +- " << fit_mean_crate_unc);
972 if ((fabs(meanDiff) > 7) ||
973 (fabs(meanUncDiff) > 7) ||
974 (fabs(sigmaDiff) > 7) ||
975 (fit_mean_crate_unc > 3) ||
976 (fit_sigma_crate < 0.1) ||
977 (fit_mean_crate < time_fit_min) ||
978 (fit_mean_crate > time_fit_max)) {
979 B2INFO(
"Crate id = " << crate_id);
980 B2INFO(
"fit mean, default mean = " << fit_mean_crate <<
", " << default_mean_crate);
981 B2INFO(
"fit sigma, default sigma = " << fit_sigma_crate <<
", " << default_sigma_crate);
983 B2INFO(
"crate fit mean - hist mean = " << meanDiff);
984 B2INFO(
"fit mean unc. - hist mean unc. = " << meanUncDiff);
985 B2INFO(
"fit sigma - hist sigma = " << sigmaDiff);
986 B2INFO(
"fit_mean_crate = " << fit_mean_crate);
987 B2INFO(
"time_fit_min = " << time_fit_min);
988 B2INFO(
"time_fit_max = " << time_fit_max);
993 int numEntries = h_time_crate->GetEntries();
994 B2INFO(
"Number entries = " << numEntries);
995 double database_mean_crate = 0;
996 double database_mean_crate_unc = 0;
997 if ((numEntries >= minNumEntries) && good_fit) {
998 database_mean_crate = fit_mean_crate;
999 database_mean_crate_unc = fit_mean_crate_unc;
1001 if ((numEntries >= minNumEntriesCrateConvergence) && (fit_mean_crate_unc < 0.1)) {
1002 tcrate_new_goodQuality[crate_id - 1] =
true;
1005 database_mean_crate = default_mean_crate;
1006 database_mean_crate_unc = default_mean_crate_unc;
1008 if (numEntries == 0) {
1009 database_mean_crate = 0;
1010 database_mean_crate_unc = 0;
1013 tcrate_mean_new[crate_id - 1] = database_mean_crate;
1014 tcrate_mean_unc_new[crate_id - 1] = database_mean_crate_unc;
1015 tcrate_sigma_new[crate_id - 1] = fit_sigma_crate;
1016 tcrate_new_was_set[crate_id - 1] =
true;
1019 histfile->WriteTObject(h_time_crate, (
string(
"h_time_psi_crate") + to_string(crate_id)).c_str());
1020 histfile->WriteTObject(h_time_crate_masked, (
string(
"h_time_psi_crate_masked") + to_string(crate_id)).c_str());
1021 histfile->WriteTObject(h_time_crate_rebin, (
string(
"h_time_psi_crate_rebinned") + to_string(crate_id)).c_str());
1026 B2DEBUG(22,
"crate histograms made");
1031 vector<float> t_offsets_crate;
1033 vector<float> t_offsets_crate_unc;
1035 t_offsets_crate.push_back(0);
1036 t_offsets_crate_unc.push_back(0);
1041 int crate_id_from_crystal = crystalMapper->getCrateID(crys_id);
1042 if (tcrate_new_was_set[crate_id_from_crystal - 1]) {
1043 t_offsets_crate[crys_id - 1] = tcrate_mean_new[crate_id_from_crystal - 1] / TICKS_TO_NS;
1044 t_offsets_crate_unc[crys_id - 1] = tcrate_mean_unc_new[crate_id_from_crystal - 1] / TICKS_TO_NS;
1047 t_offsets_crate[crys_id - 1] = tcrate_mean_prev[crate_id_from_crystal - 1];
1048 B2INFO(
"used old crate mean but zeroed uncertainty since not saved in root file");
1057 double tCrateDiff_ns = tcrate_mean_new[crate_id - 1] - (tcrate_mean_prev[crate_id - 1] * TICKS_TO_NS);
1058 B2INFO(
"Crate " << crate_id <<
": tcrate new - previous iteration = "
1059 << tcrate_mean_new[crate_id - 1]
1060 <<
" - " << tcrate_mean_prev[crate_id - 1] * TICKS_TO_NS
1061 <<
" = " << tCrateDiff_ns <<
" ns");
1062 tcrateNew_MINUS_tcrateOld__crateID->SetBinContent(crate_id, tCrateDiff_ns);
1063 tcrateNew_MINUS_tcrateOld__crateID->SetBinError(crate_id, 0);
1064 tcrateNew_MINUS_tcrateOld__crateID->ResetStats();
1066 tcrateNew_MINUS_tcrateOld->Fill(tCrateDiff_ns);
1069 tcrateNew_MINUS_tcrateOld_allRuns_allCrates->Fill(tCrateDiff_ns);
1070 if (tcrate_new_goodQuality[crate_id - 1]) {
1071 tcrateNew_MINUS_tcrateOld_allRuns->Fill(tCrateDiff_ns);
1072 num_tcrates_perRun->Fill(minRunNum);
1073 tcrateNew_MINUS_tcrateOld__vs__runNum->Fill(minRunNum, tCrateDiff_ns);
1078 histfile->WriteTObject(tcrateNew_MINUS_tcrateOld__crateID.get(),
"tcrateNew_MINUS_tcrateOld__crateID");
1079 histfile->WriteTObject(tcrateNew_MINUS_tcrateOld.get(),
"tcrateNew_MINUS_tcrateOld");
1082 histExtraCrateInfofile->WriteTObject(tcrateNew_MINUS_tcrateOld_allRuns.get(),
"tcrateNew_MINUS_tcrateOld_allRuns");
1083 histExtraCrateInfofile->WriteTObject(tcrateNew_MINUS_tcrateOld_allRuns_allCrates.get(),
1084 "tcrateNew_MINUS_tcrateOld_allRuns_allCrates");
1085 histExtraCrateInfofile->WriteTObject(num_tcrates_perRun.get(),
"num_tcrates_perRun");
1086 histExtraCrateInfofile->WriteTObject(tcrateNew_MINUS_tcrateOld__vs__runNum.get(),
"tcrateNew_MINUS_tcrateOld__vs__runNum");
1091 BhabhaTCrateCalib->
setCalibVector(t_offsets_crate, t_offsets_crate_unc);
1098 B2DEBUG(22,
"crate payload made");
1100 histExtraCrateInfofile->Close();
1106 double tree_tcrate_mean;
1107 double tree_tcrate_mean_unc;
1108 double tree_tcrate_sigma;
1109 double tree_tcrate_meanPrev;
1111 tree_crate->Branch(
"runNum", &tree_runNum)->SetTitle(
"Run number, 0..infinity and beyond!");
1112 tree_crate->Branch(
"crateid", &tree_crateid)->SetTitle(
"Crate id, 1..52");
1113 tree_crate->Branch(
"tcrate", &tree_tcrate_mean)->SetTitle(
"Crate time offset mean, tcrate, ns");
1114 tree_crate->Branch(
"tcratePrev", &tree_tcrate_meanPrev)->SetTitle(
"Previous crate time offset mean, tcrate, ns");
1115 tree_crate->Branch(
"tcrate_unc", &tree_tcrate_mean_unc)->SetTitle(
"Error of time tcrate mean, ns.");
1116 tree_crate->Branch(
"tcrate_sigma", &tree_tcrate_sigma)->SetTitle(
"Sigma of time tcrate distribution, ns");
1117 tree_crate->SetAutoSave(10);
1122 B2INFO(
"run num, exp num: " << expRun.second <<
", " << expRun.first);
1123 int runNumber = expRun.second;
1125 for (
int crate_id = 1; crate_id <= 52; crate_id++) {
1126 if (tcrate_new_was_set[crate_id - 1]) {
1127 tree_runNum = runNumber;
1128 tree_crateid = crate_id;
1129 tree_tcrate_mean = tcrate_mean_new[crate_id - 1];
1130 tree_tcrate_mean_unc = tcrate_mean_unc_new[crate_id - 1];
1131 tree_tcrate_sigma = tcrate_sigma_new[crate_id - 1];
1132 tree_tcrate_meanPrev = tcrate_mean_prev[crate_id - 1] * TICKS_TO_NS;
1138 B2DEBUG(22,
"end of crate corrections .....");
1140 tree_crystal->Write();
1141 tree_crate->Write();
1145 B2INFO(
"Finished talgorithm");
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_Failure
Failed =3 in Python.
Class for accessing objects in the database.
Singleton class to cache database objects.
static DataStore & Instance()
Instance of singleton Store.
void setInitializeActive(bool active)
Setter for m_initializeActive.
General DB object to store one calibration number per ECL crystal.
void setCalibVector(const std::vector< float > &CalibConst, const std::vector< float > &CalibConstUnc)
Set vector of constants with uncertainties.
General DB object to store one reference crystal per per ECL crate for calibration purposes.
void setCalibVector(const std::vector< short > &refCrystals)
Set vector of constants with uncertainties.
This class provides access to ECL channel map that is either a) Loaded from the database (see ecl/dbo...
static double getRF()
See m_rf.
int crateIDHi
Fit crates with crateID0 in the inclusive range [crateIDLo,crateIDHi].
int cellIDHi
Fit crystals with cellID0 in the inclusive range [cellIDLo,cellIDHi].
int cellIDLo
Fit crystals with cellID0 in the inclusive range [cellIDLo,cellIDHi].
bool debugOutput
Save every histogram and fitted function to debugFilename.
double meanCleanRebinFactor
Rebinning factor for mean calculation.
eclBhabhaTAlgorithm()
..Constructor
int crateIDLo
Fit crates with crateID0 in the inclusive range [crateIDLo,crateIDHi].
double meanCleanCutMinFactor
After rebinning, create a mask for bins that have values less than meanCleanCutMinFactor times the ma...
bool savePrevCrysPayload
Save the previous crystal payload values for comparison.
bool readPrevCrysPayload
Read the previous crystal payload values for comparison.
EResult calibrate() override
..Run algorithm on events
int refCrysPerCrate[52]
List of crystals, one per crate, used as reference time for crystal time calibration.
std::string debugFilenameBase
Name of file with debug output, eclBhabhaTAlgorithm.root by default.
bool registerInDataStore(DataStore::EStoreFlags storeFlags=DataStore::c_WriteOut)
Register the object/array in the DataStore.
Type-safe access to single objects in the data store.
bool construct(Args &&... params)
Construct an object of type T in this StoreObjPtr, using the provided constructor arguments.
static DBStore & Instance()
Instance of a singleton DBStore.
void updateEvent()
Updates all intra-run dependent objects.
void update()
Updates all objects that are outside their interval of validity.
const int c_NCrystals
Number of crystals.
Abstract base class for different kinds of events.