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");
88 auto TimevsCrysPrevCrateCalibNoCrystCalib =
getObjectPtr<TH2F>(
"TimevsCrysPrevCrateCalibNoCrystCalib");
89 auto TimevsCrateNoCrateCalibPrevCrystCalib =
getObjectPtr<TH2F>(
"TimevsCrateNoCrateCalibPrevCrystCalib");
98 unique_ptr<TH1F> tsNew_MINUS_tsOld__cid(
new TH1F(
"TsNew_MINUS_TsOld__cid",
101 unique_ptr<TH1F> tcrateNew_MINUS_tcrateOld__crateID(
new TH1F(
"tcrateNew_MINUS_tcrateOld__crateID",
102 ";crate id; tcrate(new | bhabha) - tcrate(previous iteration | merged) [ns]",
104 unique_ptr<TH1F> tsNew_MINUS_tsCustomPrev__cid(
new TH1F(
"TsNew_MINUS_TsCustomPrev__cid",
105 ";cell id; ts(new|bhabha) - ts(old = 'before 1st iter'|merged) [ns]",
107 unique_ptr<TH1F> tsNew_MINUS_tsOldBhabha__cid(
new TH1F(
"TsNew_MINUS_TsOldBhabha__cid",
113 unique_ptr<TH1F> tsNew_MINUS_tsOld(
new TH1F(
"TsNew_MINUS_TsOld",
114 ";ts(new | bhabha) - ts(previous iteration | merged) [ns];Number of crystals",
115 201, -10.05, 10.05));
116 unique_ptr<TH1F> tcrateNew_MINUS_tcrateOld(
new TH1F(
"tcrateNew_MINUS_tcrateOld",
117 ";tcrate(new) - tcrate(previous iteration) [ns];Number of crates",
118 201, -10.05, 10.05));
119 unique_ptr<TH1F> tsNew_MINUS_tsCustomPrev(
new TH1F(
"TsNew_MINUS_TsCustomPrev",
120 ";ts(new | bhabha) - ts(old = 'before 1st iter' | merged) [ns];Number of crystals",
121 285, -69.5801, 69.5801));
122 unique_ptr<TH1F> tsNew_MINUS_tsOldBhabha(
new TH1F(
"TsNew_MINUS_TsOldBhabha",
123 ";ts(new | bhabha) - ts(previous iteration | bhabha) [ns];Number of crystals",
124 201, -10.05, 10.05));
129 unique_ptr<TH1F> tcrateNew_MINUS_tcrateOld_allRuns(
new TH1F(
"tcrateNew_MINUS_tcrateOld_allRuns",
130 "Crate time constant changes over all runs : tcrate(new) uncertainty < 0.1ns;tcrate(new) - tcrate(previous iteration) [ns];Number of crates",
131 201, -10.05, 10.05));
133 unique_ptr<TH1F> tcrateNew_MINUS_tcrateOld_allRuns_allCrates(
new TH1F(
"tcrateNew_MINUS_tcrateOld_allRuns_allCrates",
134 "Crate time constant changes over all runs : all crates;tcrate(new) - tcrate(previous iteration) [ns];Number of crates",
135 201, -10.05, 10.05));
137 unique_ptr<TH1I> num_tcrates_perRun(
new TH1I(
"num_tcrates_perRun",
138 "Number of good tcrates in each run;Run number;Number of good tcrates",
141 unique_ptr<TH2F> tcrateNew_MINUS_tcrateOld__vs__runNum(
new TH2F(
"tcrateNew_MINUS_tcrateOld__vs__runNum",
142 "Crate time constant changes vs run number : tcrate(new) uncertainty < 0.1ns;Run number;tcrate(new) - tcrate(previous iteration) [ns]",
143 6000, 0, 6000, 21, -10.5, 10.5));
149 if (!TimevsCrysNoCalibrations)
return c_Failure;
154 TFile* histExtraCrateInfofile = 0;
157 unique_ptr<TTree> tree_crystal(
new TTree(
"tree_crystal",
"Debug data from bhabha time calibration algorithm for crystals"));
159 unique_ptr<TTree> tree_crate(
new TTree(
"tree_crate",
"Debug data from bhabha time calibration algorithm for crates"));
163 vector<float> t_offsets;
165 vector<float> t_offsets_unc;
166 vector<float> t_offsets_prev;
169 int minNumEntries = 40;
170 int minNumEntriesCrateConvergence = 1000;
176 int crystalCalibSaved = 0;
180 bool minRunNumBool =
false;
181 bool maxRunNumBool =
false;
187 int expNumber = expRun.first;
188 int runNumber = expRun.second;
189 if (!minRunNumBool) {
190 minExpNum = expNumber;
191 minRunNum = runNumber;
192 minRunNumBool =
true;
194 if (!maxRunNumBool) {
195 maxExpNum = expNumber;
196 maxRunNum = runNumber;
197 maxRunNumBool =
true;
199 if (((minRunNum > runNumber) && (minExpNum >= expNumber)) ||
200 (minExpNum > expNumber)) {
201 minExpNum = expNumber;
202 minRunNum = runNumber;
204 if (((maxRunNum < runNumber) && (maxExpNum <= expNumber)) ||
205 (maxExpNum < expNumber))
208 maxExpNum = expNumber;
209 maxRunNum = runNumber;
214 string runNumsString = string(
"_") + to_string(minExpNum) +
"_" + to_string(minRunNum) + string(
"-") +
215 to_string(maxExpNum) +
"_" + to_string(maxRunNum);
217 string extraCratedebugFilename =
debugFilenameBase + string(
"_cratesAllRuns.root");
224 int eventNumberForCrates = 1;
232 evtPtr.
construct(eventNumberForCrates, minRunNum, minExpNum);
241 B2INFO(
"Uploading payload for exp " << minExpNum <<
", run " << minRunNum <<
", event " << eventNumberForCrates);
244 crystalMapper->initFromDB();
249 B2INFO(
"Reading payloads: ECLCrystalTimeOffset and ECLCrateTimeOffset");
254 vector<float> currentValuesCrys = crystalTimeObject->getCalibVector();
255 vector<float> currentUncCrys = crystalTimeObject->getCalibUncVector();
256 vector<float> currentValuesCrate = crateTimeObject->getCalibVector();
257 vector<float> currentUncCrate = crateTimeObject->getCalibUncVector();
260 B2INFO(
"Values read from database. Write out for their values for comparison against those from tcol");
262 B2INFO(
"ts: cellID " << ic + 1 <<
" " << currentValuesCrys[ic] <<
" +/- " << currentUncCrys[ic]);
263 B2INFO(
"tcrate: cellID " << ic + 1 <<
" " << currentValuesCrate[ic] <<
" +/- " << currentUncCrate[ic]);
272 prevValuesCrys = customPrevCrystalTimeObject->getCalibVector();
275 B2INFO(
"Previous values read from database. Write out for their values for comparison against those from tcol");
277 B2INFO(
"ts custom previous payload: cellID " << ic + 1 <<
" " << prevValuesCrys[ic]);
283 B2INFO(
"Reading payloads: ECLCrystalTimeOffsetBhabha");
287 vector<float> currentBhabhaValuesCrys = crystalBhabhaTimeObject->getCalibVector();
288 vector<float> currentBhabhaUncCrys = crystalBhabhaTimeObject->getCalibUncVector();
292 B2INFO(
"ts bhabha: cellID " << ic + 1 <<
" " << currentBhabhaValuesCrys[ic] <<
" +/- " << currentBhabhaUncCrys[ic]);
302 B2INFO(
"Extract reference crystals from collector histogram.");
303 vector <short> crystalIDreferenceUntested;
305 if (refCrysIDzeroingCrate->GetBinContent(bin) > 0.5) {
306 crystalIDreferenceUntested.push_back(bin);
312 B2INFO(
"Reference crystals to define as having ts=0. Base 1 counting for both crates and crystals");
313 for (
long unsigned int crysRefCounter = 0; crysRefCounter < crystalIDreferenceUntested.size(); crysRefCounter++) {
314 int crys_id = crystalIDreferenceUntested[crysRefCounter] ;
315 int crate_id_from_crystal = crystalMapper->getCrateID(crys_id);
316 B2INFO(
" crystal " << crys_id <<
" is a reference for crate " << crate_id_from_crystal);
322 B2INFO(
"Checking number of reference crystals");
323 B2INFO(
"Number of reference crystals = " << crystalIDreferenceUntested.size());
326 if (crystalIDreferenceUntested.size() != 52) {
327 B2FATAL(
"The number of reference crystals does not equal 52, which is one per crate");
330 B2INFO(
"Number of reference crystals is 52 as required");
336 vector <short> crateIDsNumRefCrystalsUntested(52, 0);
337 vector <short> crystalIDReferenceForZeroTs(52, 0);
339 for (
long unsigned int crysRefCounter = 0; crysRefCounter < crystalIDreferenceUntested.size(); crysRefCounter++) {
340 int crys_id = crystalIDreferenceUntested[crysRefCounter] ;
341 int crate_id_from_crystal = crystalMapper->getCrateID(crys_id);
342 crateIDsNumRefCrystalsUntested[crate_id_from_crystal - 1]++;
343 crystalIDReferenceForZeroTs[crate_id_from_crystal - 1] = crys_id;
345 B2INFO(
"crystalIDReferenceForZeroTs = {");
346 for (
int crateTest = 0; crateTest < 52 - 1; crateTest++) {
347 B2INFO(crystalIDReferenceForZeroTs[crateTest] <<
",");
349 B2INFO(crystalIDReferenceForZeroTs[52 - 1] <<
"}");
353 for (
int crateTest = 0; crateTest < 52; crateTest++) {
354 if (crateIDsNumRefCrystalsUntested[crateTest] != 1) {
355 B2FATAL(
"Crate " << crateTest + 1 <<
" (base 1) has " << crateIDsNumRefCrystalsUntested[crateTest] <<
" reference crystals");
359 B2INFO(
"All reference crystals are reasonably mapped one crystal to one crate for all crates");
363 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.");
369 bool userSetRefCrysPerCrate = false ;
370 for (
int crateTest = 0; crateTest < 52; crateTest++) {
373 B2INFO(
"Crate " << crateTest + 1 <<
" (base 1) new reference crystal = " << crystalIDReferenceForZeroTs[crateTest]);
374 userSetRefCrysPerCrate = true ;
377 if (userSetRefCrysPerCrate) {
378 B2INFO(
"User changed reference crystals via steering script");
381 fill(crateIDsNumRefCrystalsUntested.begin(), crateIDsNumRefCrystalsUntested.end(), 0);
382 for (
long unsigned int crysRefCounter = 0; crysRefCounter < 52; crysRefCounter++) {
383 int crys_id = crystalIDReferenceForZeroTs[crysRefCounter] ;
384 int crate_id_from_crystal = crystalMapper->getCrateID(crys_id);
385 crateIDsNumRefCrystalsUntested[crate_id_from_crystal - 1]++;
387 for (
int crateTest = 0; crateTest < 52; crateTest++) {
388 if (crateIDsNumRefCrystalsUntested[crateTest] != 1) {
389 B2FATAL(
"Crate " << crateTest + 1 <<
" (base 1) has " << crateIDsNumRefCrystalsUntested[crateTest] <<
" reference crystals");
393 B2INFO(
"All reference crystals are reasonably mapped one crystal to one crate for all crates after changes made by user steering script.");
400 B2INFO(
"Created reference crystal per crate payload: ECLReferenceCrystalPerCrateCalib");
402 B2INFO(
"User did not change reference crystals via steering script");
409 B2INFO(
"Debug output rootfile: " << debugFilename);
410 histfile =
new TFile(debugFilename.c_str(),
"recreate");
413 TimevsCrysPrevCrateCalibPrevCrystCalib ->Write();
414 TimevsCratePrevCrateCalibPrevCrystCalib->Write();
415 TimevsCrysNoCalibrations ->Write();
416 TimevsCrysPrevCrateCalibNoCrystCalib ->Write();
417 TimevsCrateNoCrateCalibPrevCrystCalib ->Write();
424 tree_crystal->Branch(
"cid", &tree_cid)->SetTitle(
"Cell ID, 1..8736");
425 tree_crystal->Branch(
"ts", &mean)->SetTitle(
"Time offset mean, ts, ns");
426 tree_crystal->Branch(
"tsUnc", &mean_unc)->SetTitle(
"Error of time ts mean, ns.");
427 tree_crystal->Branch(
"tsSigma", &sigma)->SetTitle(
"Sigma of time ts distribution, ns");
428 tree_crystal->Branch(
"crystalCalibSaved",
429 &crystalCalibSaved)->SetTitle(
"0=crystal skipped, 1=crystal calib saved (num entries based)");
430 tree_crystal->Branch(
"tsPrev", &tsPrev)->SetTitle(
"Previous crystal time offset, ts, ns");
431 tree_crystal->SetAutoSave(10);
435 double hist_tmin = TimevsCrysNoCalibrations->GetYaxis()->GetXmin();
436 double hist_tmax = TimevsCrysNoCalibrations->GetYaxis()->GetXmax();
438 double time_fit_min = hist_tmax;
439 double time_fit_max = hist_tmin;
441 B2INFO(
"hist_tmin = " << hist_tmin);
442 B2INFO(
"hist_tmax = " << hist_tmax);
451 float numTimesFilled = databaseCounter->GetBinContent(1);
452 B2INFO(
"Number of times database histograms were merged = " << numTimesFilled);
458 t_offsets.push_back(TsDatabase->GetBinContent(i) / numTimesFilled);
459 t_offsets_prev.push_back(TsDatabase->GetBinContent(i) / numTimesFilled);
461 B2INFO(
"t_offsets_prev (last iter) at crysID " << i <<
" = " << t_offsets_prev[i - 1]);
463 t_offsets_unc.push_back(TsDatabaseUnc->GetBinContent(i) / numTimesFilled);
471 TH1D* h_crysHits = TimevsCrysPrevCrateCalibNoCrystCalib->ProjectionX(
"h_crysHits");
472 h_crysHits->SetTitle(
"Hits per crystal;Crystal id");
474 histfile->WriteTObject(h_crysHits,
"h_crysHits");
479 crystalCalibSaved = 0;
481 double database_mean = 0;
482 double database_mean_unc = 0;
484 B2INFO(
"Crystal id = " << crys_id);
492 TH1D* h_time = TimevsCrysPrevCrateCalibNoCrystCalib->ProjectionY((
string(
"h_time_psi__") + to_string(crys_id)).c_str(),
494 TH1D* h_timeMask =
static_cast<TH1D*
>(h_time->Clone());
495 TH1D* h_timeMasked =
static_cast<TH1D*
>(h_time->Clone((
string(
"h_time_psi_masked__") + to_string(crys_id)).c_str()));
496 TH1D* h_timeRebin =
static_cast<TH1D*
>(h_time->Clone());
503 h_timeMask->Scale(0.0);
505 time_fit_min = hist_tmax;
506 time_fit_max = hist_tmin;
509 double histRebin_max = h_timeRebin->GetMaximum();
511 bool maskedOutNonZeroBin =
false;
513 for (
int bin = 1; bin <= h_timeRebin->GetNbinsX(); bin++) {
516 if (nonRebinnedBinNumber < h_time->GetNbinsX()) {
518 h_timeMask->SetBinContent(nonRebinnedBinNumber, 1);
521 double x_lower = h_timeRebin->GetXaxis()->GetBinLowEdge(bin);
522 double x_upper = h_timeRebin->GetXaxis()->GetBinUpEdge(bin);
523 if (x_lower < time_fit_min) {
524 time_fit_min = x_lower;
526 if (x_upper > time_fit_max) {
527 time_fit_max = x_upper;
531 if (h_time->GetBinContent(nonRebinnedBinNumber) > 0) {
532 B2DEBUG(22,
"Setting bin " << nonRebinnedBinNumber <<
" from " << h_timeMasked->GetBinContent(nonRebinnedBinNumber) <<
" to 0");
533 maskedOutNonZeroBin =
true;
535 h_timeMasked->SetBinContent(nonRebinnedBinNumber, 0);
540 B2INFO(
"Bins with non-zero values have been masked out: " << maskedOutNonZeroBin);
541 h_timeMasked->ResetStats();
542 h_timeMask->ResetStats();
547 double default_meanMasked = h_timeMasked->GetMean();
549 B2INFO(
"default_meanMasked = " << default_meanMasked);
553 double default_mean = h_time->GetMean();
554 double default_mean_unc = h_time->GetMeanError();
555 double default_sigma = h_time->GetStdDev();
557 B2INFO(
"Fitting crystal between " << time_fit_min <<
" and " << time_fit_max);
560 TF1* gaus =
new TF1(
"func",
"gaus(0)", time_fit_min, time_fit_max);
561 gaus->SetParNames(
"numCrystalHitsNormalization",
"mean",
"sigma");
568 double hist_max = h_time->GetMaximum();
571 double stddev = h_time->GetStdDev();
576 gaus->SetParameter(0, hist_max / 2.);
577 gaus->SetParameter(1, mean);
578 gaus->SetParameter(2, sigma);
585 h_timeMasked->Fit(gaus,
"LQR");
587 double fit_mean = gaus->GetParameter(1);
588 double fit_mean_unc = gaus->GetParError(1);
589 double fit_sigma = gaus->GetParameter(2);
591 double meanDiff = fit_mean - default_mean;
592 double meanUncDiff = fit_mean_unc - default_mean_unc;
593 double sigmaDiff = fit_sigma - default_sigma;
595 bool good_fit =
false;
597 if ((fabs(meanDiff) > 10) ||
598 (fabs(meanUncDiff) > 10) ||
599 (fabs(sigmaDiff) > 10) ||
600 (fit_mean_unc > 0.09) ||
602 (fit_mean < time_fit_min) ||
603 (fit_mean > time_fit_max)) {
604 B2INFO(
"Crystal id = " << crys_id);
605 B2INFO(
"fit mean, default mean = " << fit_mean <<
", " << default_mean);
606 B2INFO(
"fit mean unc, default mean unc = " << fit_mean_unc <<
", " << default_mean_unc);
607 B2INFO(
"fit sigma, default sigma = " << fit_sigma <<
", " << default_sigma);
609 B2INFO(
"crystal fit mean - hist mean = " << meanDiff);
610 B2INFO(
"fit mean unc. - hist mean unc. = " << meanUncDiff);
611 B2INFO(
"fit sigma - hist sigma = " << sigmaDiff);
613 B2INFO(
"fit_mean = " << fit_mean);
614 B2INFO(
"time_fit_min = " << time_fit_min);
615 B2INFO(
"time_fit_max = " << time_fit_max);
617 if (fabs(meanDiff) > 10) B2INFO(
"fit mean diff too large");
618 if (fabs(meanUncDiff) > 10) B2INFO(
"fit mean unc diff too large");
619 if (fabs(sigmaDiff) > 10) B2INFO(
"fit mean sigma diff too large");
620 if (fit_mean_unc > 0.09) B2INFO(
"fit mean unc too large");
621 if (fit_sigma < 0.1) B2INFO(
"fit sigma too small");
630 sigma = default_sigma;
633 int numEntries = h_time->GetEntries();
636 if ((numEntries >= minNumEntries) && good_fit) {
637 crystalCalibSaved = 1;
638 database_mean = fit_mean;
639 database_mean_unc = fit_mean_unc;
641 database_mean = default_mean;
642 database_mean_unc = -fabs(default_mean_unc);
645 if (numEntries < minNumEntries) B2INFO(
"Number of entries less than minimum");
646 if (numEntries == 0) B2INFO(
"Number of entries == 0");
652 t_offsets[crys_id - 1] = database_mean / TICKS_TO_NS;
653 t_offsets_unc[crys_id - 1] = database_mean_unc / TICKS_TO_NS;
656 histfile->WriteTObject(h_time, (
string(
"h_time_psi") + to_string(crys_id)).c_str());
657 histfile->WriteTObject(h_timeMasked, (
string(
"h_time_psi_masked") + to_string(crys_id)).c_str());
659 mean = database_mean;
660 mean_unc = database_mean_unc;
662 tsPrev = t_offsets_prev[crys_id - 1] * TICKS_TO_NS;
665 tree_crystal->Fill();
671 vector <double> tsRefCID ;
672 B2INFO(
"crystal times before shift");
673 for (
int crate_id = 1; crate_id <= 52; crate_id++) {
674 tsRefCID.push_back(t_offsets[ crystalIDReferenceForZeroTs[crate_id - 1] - 1 ]);
675 B2INFO(
"crystal time [crystal = " << crystalIDReferenceForZeroTs[crate_id - 1] <<
", crate = " << crate_id <<
" (base 1)] = " <<
676 t_offsets[ crystalIDReferenceForZeroTs[crate_id - 1] - 1 ] <<
" ticks");
679 B2INFO(
"crystal times after shift wrt reference crystal");
681 int crate_id_from_crystal = crystalMapper->getCrateID(crys_id);
682 B2INFO(
"crystal time before shift [crystal = " << crys_id <<
", crate = " << crate_id_from_crystal <<
" (base 1)] = " <<
683 t_offsets[crys_id - 1] <<
" +- " << t_offsets_unc[crys_id - 1] <<
" ticks");
688 if (t_offsets[crys_id - 1] == 0 && t_offsets_unc[crys_id - 1] == 0) {
689 B2INFO(
"crystal time after shift [crystal = " << crys_id <<
", crate = " << crate_id_from_crystal <<
" (base 1)] = " <<
690 t_offsets[crys_id - 1] <<
" ticks. No change because ts=0 and ts_unc=0 (no entries).");
692 t_offsets[crys_id - 1] = t_offsets[crys_id - 1] - tsRefCID[crate_id_from_crystal - 1];
693 B2INFO(
"crystal time after shift [crystal = " << crys_id <<
", crate = " << crate_id_from_crystal <<
" (base 1)] = " <<
694 t_offsets[crys_id - 1] <<
" ticks");
699 double tsDiff_ns = (t_offsets[crys_id - 1] - t_offsets_prev[crys_id - 1]) * TICKS_TO_NS;
700 double tsDiffBhabha_ns = -999;
702 tsDiffBhabha_ns = (t_offsets[crys_id - 1] - currentBhabhaValuesCrys[crys_id - 1]) * TICKS_TO_NS;
705 B2INFO(
"Crystal " << crys_id <<
": ts new bhabha - old merged = (" <<
706 t_offsets[crys_id - 1] <<
" - " << t_offsets_prev[crys_id - 1] <<
707 ") ticks * " << TICKS_TO_NS <<
" ns/tick = " << tsDiff_ns <<
" ns");
708 B2INFO(
"Crystal " << crys_id <<
": ts new bhabha - old bhabha = (" <<
709 t_offsets[crys_id - 1] <<
" - " << currentBhabhaValuesCrys[crys_id - 1] <<
710 ") ticks * " << TICKS_TO_NS <<
" ns/tick = " << tsDiffBhabha_ns <<
" ns");
712 tsNew_MINUS_tsOld__cid->SetBinContent(crys_id, tsDiff_ns);
713 tsNew_MINUS_tsOld__cid->SetBinError(crys_id, 0);
714 tsNew_MINUS_tsOld__cid->ResetStats();
716 tsNew_MINUS_tsOld->Fill(tsDiff_ns);
719 tsNew_MINUS_tsOldBhabha__cid->SetBinContent(crys_id, tsDiffBhabha_ns);
720 tsNew_MINUS_tsOldBhabha__cid->SetBinError(crys_id, 0);
721 tsNew_MINUS_tsOldBhabha__cid->ResetStats();
723 tsNew_MINUS_tsOldBhabha->Fill(tsDiffBhabha_ns);
729 double tsDiffCustomOld_ns = -999;
731 tsDiffCustomOld_ns = (t_offsets[crys_id - 1] - prevValuesCrys[crys_id - 1]) * TICKS_TO_NS;
732 B2INFO(
"Crystal " << crys_id <<
": ts new bhabha - 'before 1st iter' merged = (" <<
733 t_offsets[crys_id - 1] <<
" - " << prevValuesCrys[crys_id - 1] <<
734 ") ticks * " << TICKS_TO_NS <<
" ns/tick = " << tsDiffCustomOld_ns <<
" ns");
736 tsNew_MINUS_tsCustomPrev__cid->SetBinContent(crys_id, tsDiffCustomOld_ns);
737 tsNew_MINUS_tsCustomPrev__cid->SetBinError(crys_id, 0);
738 tsNew_MINUS_tsCustomPrev__cid->ResetStats();
740 tsNew_MINUS_tsCustomPrev->Fill(tsDiffCustomOld_ns);
745 histfile->WriteTObject(tsNew_MINUS_tsOld__cid.get(),
"tsNew_MINUS_tsOld__cid");
746 histfile->WriteTObject(tsNew_MINUS_tsOld.get(),
"tsNew_MINUS_tsOld");
748 histfile->WriteTObject(tsNew_MINUS_tsCustomPrev__cid.get(),
"tsNew_MINUS_tsCustomPrev__cid");
749 histfile->WriteTObject(tsNew_MINUS_tsCustomPrev.get(),
"tsNew_MINUS_tsCustomPrev");
751 histfile->WriteTObject(tsNew_MINUS_tsOldBhabha__cid.get(),
"tsNew_MINUS_tsOldBhabha__cid");
752 histfile->WriteTObject(tsNew_MINUS_tsOldBhabha.get(),
"tsNew_MINUS_tsOldBhabha");
762 crysTCalib_prev->
setCalibVector(currentValuesCrys, currentUncCrys);
765 crysBhabhaTCalib_prev->
setCalibVector(currentBhabhaValuesCrys, currentBhabhaUncCrys);
771 saveCalibration(crysTCalib_prev,
"ECLCrystalTimeOffsetPreviousValues");
772 B2INFO(
"Previous overall crystal payload made");
774 saveCalibration(crysBhabhaTCalib_prev,
"ECLCrystalTimeOffsetBhabhaPreviousValues");
775 B2INFO(
"Previous bhabha crystal payload made");
788 B2DEBUG(22,
"crystal payload made");
792 B2DEBUG(22,
"end of crystal start of crate corrections .....");
798 hist_tmin = TimevsCrateNoCrateCalibPrevCrystCalib->GetYaxis()->GetXmin();
799 hist_tmax = TimevsCrateNoCrateCalibPrevCrystCalib->GetYaxis()->GetXmax();
801 B2DEBUG(22,
"Found min/max of X axis of TimevsCrateNoCrateCalibPrevCrystCalib");
808 B2DEBUG(22,
"Retrieved Ts and Tcrate histograms from tcol root file");
811 vector<float> tcrate_mean_new(52, 0.0);
812 vector<float> tcrate_mean_unc_new(52, 0.0);
813 vector<float> tcrate_sigma_new(52, 0.0);
814 vector<float> tcrate_mean_prev(52, 0.0);
816 vector<bool> tcrate_new_was_set(52,
false);
817 vector<bool> tcrate_new_goodQuality(52,
false);
819 B2DEBUG(22,
"crate vectors set");
826 int crate_id_from_crystal = crystalMapper->getCrateID(crys_id);
827 tcrate_mean_prev[crate_id_from_crystal - 1] = TcrateDatabase->GetBinContent(crys_id) / numTimesFilled;
831 B2INFO(
"Print out previous crate time calibration constants to make sure they match from the two different sources.");
832 for (
int crate_id = 1; crate_id <= 52; crate_id++) {
833 B2INFO(
"tcrate_mean_prev[crate " << crate_id <<
" (base 1)] = " << tcrate_mean_prev[crate_id - 1]);
835 int thisRefCellID = crystalIDReferenceForZeroTs[crate_id - 1];
836 B2INFO(
"tcrate from payload: ref cellID " << thisRefCellID <<
" " << currentValuesCrate[thisRefCellID - 1] <<
" +/- " <<
837 currentUncCrate[thisRefCellID - 1]);
844 TFile* histExtraCrateInfofile_dummy = 0;
845 B2INFO(
"Debug output rootfile used for crate iterations: " << extraCratedebugFilename);
846 histExtraCrateInfofile_dummy =
new TFile(extraCratedebugFilename.c_str(),
"UPDATE");
850 const TKey* key = histExtraCrateInfofile_dummy->FindKey(
"tcrateNew_MINUS_tcrateOld_allRuns");
852 TH1F* h =
static_cast<TH1F*
>(histExtraCrateInfofile_dummy->Get(
"tcrateNew_MINUS_tcrateOld_allRuns"));
853 tcrateNew_MINUS_tcrateOld_allRuns->Add(h);
856 key = histExtraCrateInfofile_dummy->FindKey(
"tcrateNew_MINUS_tcrateOld_allRuns_allCrates");
858 TH1F* h =
static_cast<TH1F*
>(histExtraCrateInfofile_dummy->Get(
"tcrateNew_MINUS_tcrateOld_allRuns_allCrates"));
859 tcrateNew_MINUS_tcrateOld_allRuns_allCrates->Add(h);
862 key = histExtraCrateInfofile_dummy->FindKey(
"num_tcrates_perRun");
864 TH1F* h =
static_cast<TH1F*
>(histExtraCrateInfofile_dummy->Get(
"num_tcrates_perRun"));
865 num_tcrates_perRun->Add(h);
868 key = histExtraCrateInfofile_dummy->FindKey(
"tcrateNew_MINUS_tcrateOld__vs__runNum");
870 TH1F* h =
static_cast<TH1F*
>(histExtraCrateInfofile_dummy->Get(
"tcrateNew_MINUS_tcrateOld__vs__runNum"));
871 tcrateNew_MINUS_tcrateOld__vs__runNum->Add(h);
874 histExtraCrateInfofile_dummy->Close();
878 histExtraCrateInfofile =
new TFile(extraCratedebugFilename.c_str(),
"recreate");
885 B2DEBUG(22,
"Start of crate id = " << crate_id);
887 TH1D* h_time_crate = TimevsCrateNoCrateCalibPrevCrystCalib->ProjectionY(
"h_time_psi_crate", crate_id, crate_id);
888 TH1D* h_time_crate_mask =
static_cast<TH1D*
>(h_time_crate->Clone());
889 TH1D* h_time_crate_masked =
static_cast<TH1D*
>(h_time_crate->Clone());
890 TH1D* h_time_crate_rebin =
static_cast<TH1D*
>(h_time_crate->Clone());
897 h_time_crate_mask->Scale(0.0);
899 time_fit_min = hist_tmax;
900 time_fit_max = hist_tmin;
903 double histRebin_max = h_time_crate_rebin->GetMaximum();
905 bool maskedOutNonZeroBin =
false;
907 for (
int bin = 1; bin <= h_time_crate_rebin->GetNbinsX(); bin++) {
910 if (nonRebinnedBinNumber < h_time_crate->GetNbinsX()) {
912 h_time_crate_mask->SetBinContent(nonRebinnedBinNumber, 1);
915 double x_lower = h_time_crate_rebin->GetXaxis()->GetBinLowEdge(bin);
916 double x_upper = h_time_crate_rebin->GetXaxis()->GetBinUpEdge(bin);
917 if (x_lower < time_fit_min) {
918 time_fit_min = x_lower;
920 if (x_upper > time_fit_max) {
921 time_fit_max = x_upper;
924 if (h_time_crate->GetBinContent(nonRebinnedBinNumber) > 0) {
925 B2DEBUG(22,
"Setting bin " << nonRebinnedBinNumber <<
" from " << h_time_crate_masked->GetBinContent(
926 nonRebinnedBinNumber) <<
" to 0");
927 maskedOutNonZeroBin =
true;
929 h_time_crate_masked->SetBinContent(nonRebinnedBinNumber, 0);
934 B2INFO(
"Bins with non-zero values have been masked out: " << maskedOutNonZeroBin);
935 h_time_crate_masked->ResetStats();
936 h_time_crate_mask->ResetStats();
942 B2DEBUG(22,
"crate loop - projected h_time_psi_crate");
945 double default_mean_crate = h_time_crate_masked->GetMean();
946 double default_mean_crate_unc = h_time_crate_masked->GetMeanError();
947 double default_sigma_crate = h_time_crate_masked->GetStdDev();
948 B2INFO(
"Fitting crate between " << time_fit_min <<
" and " << time_fit_max);
949 TF1* gaus =
new TF1(
"func",
"gaus(0)", time_fit_min, time_fit_max);
950 gaus->SetParNames(
"numCrateHisNormalization",
"mean",
"sigma");
951 double hist_max = h_time_crate->GetMaximum();
952 double stddev = h_time_crate->GetStdDev();
953 double sigma_crate = stddev;
954 double mean_crate = default_mean_crate;
955 gaus->SetParameter(0, hist_max / 2.);
956 gaus->SetParameter(1, mean_crate);
957 gaus->SetParameter(2, sigma_crate);
959 h_time_crate_masked->Fit(gaus,
"LQR");
961 double fit_mean_crate = gaus->GetParameter(1);
962 double fit_mean_crate_unc = gaus->GetParError(1);
963 double fit_sigma_crate = gaus->GetParameter(2);
965 double meanDiff = fit_mean_crate - default_mean_crate;
966 double meanUncDiff = fit_mean_crate_unc - default_mean_crate_unc;
967 double sigmaDiff = fit_sigma_crate - default_sigma_crate;
969 bool good_fit =
false;
971 B2DEBUG(22,
"Crate id = " << crate_id <<
" with crate mean = " << default_mean_crate <<
" +- " << fit_mean_crate_unc);
973 if ((fabs(meanDiff) > 7) ||
974 (fabs(meanUncDiff) > 7) ||
975 (fabs(sigmaDiff) > 7) ||
976 (fit_mean_crate_unc > 3) ||
977 (fit_sigma_crate < 0.1) ||
978 (fit_mean_crate < time_fit_min) ||
979 (fit_mean_crate > time_fit_max)) {
980 B2INFO(
"Crate id = " << crate_id);
981 B2INFO(
"fit mean, default mean = " << fit_mean_crate <<
", " << default_mean_crate);
982 B2INFO(
"fit sigma, default sigma = " << fit_sigma_crate <<
", " << default_sigma_crate);
984 B2INFO(
"crate fit mean - hist mean = " << meanDiff);
985 B2INFO(
"fit mean unc. - hist mean unc. = " << meanUncDiff);
986 B2INFO(
"fit sigma - hist sigma = " << sigmaDiff);
987 B2INFO(
"fit_mean_crate = " << fit_mean_crate);
988 B2INFO(
"time_fit_min = " << time_fit_min);
989 B2INFO(
"time_fit_max = " << time_fit_max);
994 int numEntries = h_time_crate->GetEntries();
995 B2INFO(
"Number entries = " << numEntries);
996 double database_mean_crate = 0;
997 double database_mean_crate_unc = 0;
998 tcrate_new_was_set[crate_id - 1] =
false;
999 tcrate_new_goodQuality[crate_id - 1] =
false;
1000 if ((numEntries >= minNumEntries) && good_fit) {
1001 database_mean_crate = fit_mean_crate;
1002 database_mean_crate_unc = fit_mean_crate_unc;
1003 tcrate_new_was_set[crate_id - 1] =
true;
1005 if ((numEntries >= minNumEntriesCrateConvergence) && (fit_mean_crate_unc < 0.1)) {
1006 tcrate_new_goodQuality[crate_id - 1] =
true;
1010 tcrate_mean_new[crate_id - 1] = database_mean_crate;
1011 tcrate_mean_unc_new[crate_id - 1] = database_mean_crate_unc;
1012 tcrate_sigma_new[crate_id - 1] = fit_sigma_crate;
1014 histfile->WriteTObject(h_time_crate, (
string(
"h_time_psi_crate") + to_string(crate_id)).c_str());
1015 histfile->WriteTObject(h_time_crate_masked, (
string(
"h_time_psi_crate_masked") + to_string(crate_id)).c_str());
1016 histfile->WriteTObject(h_time_crate_rebin, (
string(
"h_time_psi_crate_rebinned") + to_string(crate_id)).c_str());
1021 B2DEBUG(22,
"crate histograms made");
1026 vector<float> t_offsets_crate;
1028 vector<float> t_offsets_crate_unc;
1030 t_offsets_crate.push_back(0);
1031 t_offsets_crate_unc.push_back(0);
1036 int crate_id_from_crystal = crystalMapper->getCrateID(crys_id);
1037 if (tcrate_new_was_set[crate_id_from_crystal - 1]) {
1038 t_offsets_crate[crys_id - 1] = tcrate_mean_new[crate_id_from_crystal - 1] / TICKS_TO_NS;
1039 t_offsets_crate_unc[crys_id - 1] = tcrate_mean_unc_new[crate_id_from_crystal - 1] / TICKS_TO_NS;
1042 t_offsets_crate[crys_id - 1] = tcrate_mean_prev[crate_id_from_crystal - 1];
1043 B2INFO(
"used old crate mean but zeroed uncertainty since not saved in root file");
1052 double tCrateDiff_ns = tcrate_mean_new[crate_id - 1] - (tcrate_mean_prev[crate_id - 1] * TICKS_TO_NS);
1053 B2INFO(
"Crate " << crate_id <<
": tcrate new - previous iteration = "
1054 << tcrate_mean_new[crate_id - 1]
1055 <<
" - " << tcrate_mean_prev[crate_id - 1] * TICKS_TO_NS
1056 <<
" = " << tCrateDiff_ns <<
" ns");
1057 tcrateNew_MINUS_tcrateOld__crateID->SetBinContent(crate_id, tCrateDiff_ns);
1058 tcrateNew_MINUS_tcrateOld__crateID->SetBinError(crate_id, 0);
1059 tcrateNew_MINUS_tcrateOld__crateID->ResetStats();
1061 tcrateNew_MINUS_tcrateOld->Fill(tCrateDiff_ns);
1064 tcrateNew_MINUS_tcrateOld_allRuns_allCrates->Fill(tCrateDiff_ns);
1065 if (tcrate_new_goodQuality[crate_id - 1]) {
1066 tcrateNew_MINUS_tcrateOld_allRuns->Fill(tCrateDiff_ns);
1067 num_tcrates_perRun->Fill(minRunNum);
1068 tcrateNew_MINUS_tcrateOld__vs__runNum->Fill(minRunNum, tCrateDiff_ns);
1073 histfile->WriteTObject(tcrateNew_MINUS_tcrateOld__crateID.get(),
"tcrateNew_MINUS_tcrateOld__crateID");
1074 histfile->WriteTObject(tcrateNew_MINUS_tcrateOld.get(),
"tcrateNew_MINUS_tcrateOld");
1077 histExtraCrateInfofile->WriteTObject(tcrateNew_MINUS_tcrateOld_allRuns.get(),
"tcrateNew_MINUS_tcrateOld_allRuns");
1078 histExtraCrateInfofile->WriteTObject(tcrateNew_MINUS_tcrateOld_allRuns_allCrates.get(),
1079 "tcrateNew_MINUS_tcrateOld_allRuns_allCrates");
1080 histExtraCrateInfofile->WriteTObject(num_tcrates_perRun.get(),
"num_tcrates_perRun");
1081 histExtraCrateInfofile->WriteTObject(tcrateNew_MINUS_tcrateOld__vs__runNum.get(),
"tcrateNew_MINUS_tcrateOld__vs__runNum");
1086 BhabhaTCrateCalib->
setCalibVector(t_offsets_crate, t_offsets_crate_unc);
1093 B2DEBUG(22,
"crate payload made");
1095 histExtraCrateInfofile->Close();
1101 double tree_tcrate_mean;
1102 double tree_tcrate_mean_unc;
1103 double tree_tcrate_sigma;
1104 double tree_tcrate_meanPrev;
1106 tree_crate->Branch(
"runNum", &tree_runNum)->SetTitle(
"Run number, 0..infinity and beyond!");
1107 tree_crate->Branch(
"crateid", &tree_crateid)->SetTitle(
"Crate id, 1..52");
1108 tree_crate->Branch(
"tcrate", &tree_tcrate_mean)->SetTitle(
"Crate time offset mean, tcrate, ns");
1109 tree_crate->Branch(
"tcratePrev", &tree_tcrate_meanPrev)->SetTitle(
"Previous crate time offset mean, tcrate, ns");
1110 tree_crate->Branch(
"tcrate_unc", &tree_tcrate_mean_unc)->SetTitle(
"Error of time tcrate mean, ns.");
1111 tree_crate->Branch(
"tcrate_sigma", &tree_tcrate_sigma)->SetTitle(
"Sigma of time tcrate distribution, ns");
1112 tree_crate->SetAutoSave(10);
1117 B2INFO(
"run num, exp num: " << expRun.second <<
", " << expRun.first);
1118 int runNumber = expRun.second;
1120 for (
int crate_id = 1; crate_id <= 52; crate_id++) {
1121 if (tcrate_new_was_set[crate_id - 1]) {
1122 tree_runNum = runNumber;
1123 tree_crateid = crate_id;
1124 tree_tcrate_mean = tcrate_mean_new[crate_id - 1];
1125 tree_tcrate_mean_unc = tcrate_mean_unc_new[crate_id - 1];
1126 tree_tcrate_sigma = tcrate_sigma_new[crate_id - 1];
1127 tree_tcrate_meanPrev = tcrate_mean_prev[crate_id - 1] * TICKS_TO_NS;
1133 B2DEBUG(22,
"end of crate corrections .....");
1135 tree_crystal->Write();
1136 tree_crate->Write();
1140 B2INFO(
"Finished talgorithm");