10#include <ecl/calibration/eclMergingCrystalTimingAlgorithm.h>
13#include <ecl/dbobjects/ECLCrystalCalib.h>
14#include <ecl/dbobjects/ECLReferenceCrystalPerCrateCalib.h>
15#include <ecl/digitization/EclConfiguration.h>
16#include <ecl/mapper/ECLChannelMapper.h>
19#include <TDirectory.h>
27using namespace Calibration;
32 readPrevCrysPayload(false),
33 m_ECLCrystalTimeOffsetBhabha(
"ECLCrystalTimeOffsetBhabha"),
34 m_ECLCrystalTimeOffsetBhabhaGamma(
"ECLCrystalTimeOffsetBhabhaGamma"),
35 m_ECLCrystalTimeOffsetCosmic(
"ECLCrystalTimeOffsetCosmic"),
36 m_ECLReferenceCrystalPerCrateCalib(
"ECLReferenceCrystalPerCrateCalib"),
37 m_ECLCrateTimeOffset(
"ECLCrateTimeOffset")
40 "Perform time calibration of ecl crystals by combining previous values from the DB for different calibrations."
54 ExpRun chosenRun = runs.front();
55 B2INFO(
"merging using the ExpRun (" << chosenRun.second <<
"," << chosenRun.first <<
")");
64 crystalMapper->initFromDB();
68 bool allObjectsFound =
true;
73 allObjectsFound =
false;
74 B2ERROR(
"No valid DBObject found for 'ECLCrystalTimeOffsetBhabha'");
77 allObjectsFound =
false;
78 B2ERROR(
"No valid DBObject found for 'ECLCrystalTimeOffsetBhabhaGamma'");
81 allObjectsFound =
false;
82 B2ERROR(
"No valid DBObject found for 'ECLCrystalTimeOffsetCosmic'");
87 allObjectsFound =
false;
88 B2ERROR(
"No valid DBObject found for 'ECLReferenceCrystalPerCrateCalib'");
93 allObjectsFound =
false;
94 B2ERROR(
"No valid DBObject found for 'ECLCrateTimeOffset'");
98 if (allObjectsFound) {
99 B2INFO(
"Valid objects found for 'ECLCrystalTimeOffsetBhabha', 'ECLCrystalTimeOffsetBhabhaGamma' and 'ECLCrystalTimeOffsetCosmic')");
100 B2INFO(
"Valid object found for 'ECLReferenceCrystalPerCrateCalib'");
101 B2INFO(
"Valid object found for 'ECLCrateTimeOffset'");
103 B2INFO(
"Exiting with failure");
115 bool minRunNumBool =
false;
116 bool maxRunNumBool =
false;
122 int expNumber = expRun.first;
123 int runNumber = expRun.second;
124 if (!minRunNumBool) {
125 minExpNum = expNumber;
126 minRunNum = runNumber;
127 minRunNumBool =
true;
129 if (!maxRunNumBool) {
130 maxExpNum = expNumber;
131 maxRunNum = runNumber;
132 maxRunNumBool =
true;
134 if (((minRunNum > runNumber) && (minExpNum >= expNumber)) ||
135 (minExpNum > expNumber)) {
136 minExpNum = expNumber;
137 minRunNum = runNumber;
139 if (((maxRunNum < runNumber) && (maxExpNum <= expNumber)) ||
140 (maxExpNum < expNumber))
143 maxExpNum = expNumber;
144 maxRunNum = runNumber;
148 string debugFilenameBase =
"ECLCrystalTimeOffsetMerged";
149 B2INFO(
"debugFilenameBase = " << debugFilenameBase);
150 string runNumsString = string(
"_") + to_string(minExpNum) +
"_" + to_string(minRunNum) + string(
"-") +
151 to_string(maxExpNum) +
"_" + to_string(maxRunNum);
152 string debugFilename = debugFilenameBase + runNumsString + string(
".root");
168 B2INFO(
"Loaded 'ECLCrystalTimeOffset*' calibrations");
173 B2INFO(
"Loaded 'ECLReferenceCrystalPerCrateCalib' calibration");
177 B2INFO(
"Loaded 'ECLCrateTimeOffset' calibration");
191 prevValuesCrys = customPrevCrystalTimeObject->getCalibVector();
192 prevValuesCrysUnc = customPrevCrystalTimeObject->getCalibUncVector();
194 prevBhabhaValuesCrys = customPrevBhabhaCrystalTimeObject->getCalibVector();
195 prevBhabhaValuesCrysUnc = customPrevBhabhaCrystalTimeObject->getCalibUncVector();
198 B2INFO(
"Previous values read from database. Write out for their values for comparison");
200 B2INFO(
"ts custom previous payload: cellID " << ic + 1 <<
" " << prevValuesCrys[ic]);
202 B2INFO(
"Previous bhabha values read from database. Write out for their values for comparison");
204 B2INFO(
"ts custom previous bhabha payload: cellID " << ic + 1 <<
" " << prevBhabhaValuesCrys[ic]);
242 vector<bool> bhabhaGammaCalibGoodQuality(
m_numCrystals,
false);
251 if (bhabhaCalibUnc[ic] != 0
252 && fabs(bhabhaCalibUnc[ic])*TICKS_TO_NS < 2) {
259 bhabhaCalibGoodQuality[ic] = true ;
270 if (bhabhaGammaCalibUnc[ic] != 0
271 && fabs(bhabhaGammaCalibUnc[ic])*TICKS_TO_NS <
273 bhabhaGammaCalibGoodQuality[ic] = true ;
283 vector<float> cosmicCalibShifted = cosmicCalib;
284 vector<float> cosmicCalibShiftedUnc = cosmicCalibUnc;
286 cosmicCalibShifted[ic] -= crateCalib[ic];
287 int crate_id_from_crystal = crystalMapper->getCrateID(ic + 1);
288 crateCalibShort[crate_id_from_crystal - 1] = crateCalib[ic];
291 for (
int icrate = 0; icrate <
m_numCrates; icrate++) {
292 B2INFO(
"crate time calibration for crate " <<
293 icrate + 1 <<
" = " << crateCalibShort[icrate]);
300 vector<float> meanGoodCrystalCalibPerCrate_bhabha(
m_numCrates, 0);
301 vector<float> meanGoodCrystalCalibPerCrate_cosmic(
m_numCrates, 0);
308 vector<short> numGoodCrystalCalibPerCrate_bhabha(
m_numCrates, 1);
313 for (
int ic_base1 = 1; ic_base1 <=
m_numCrystals; ic_base1++) {
314 int crate_id_from_crystal = crystalMapper->getCrateID(ic_base1);
316 if (!((bhabhaCalib[ic_base1 - 1] == 0) || (! bhabhaCalibGoodQuality[ic_base1 - 1]))) {
317 meanGoodCrystalCalibPerCrate_bhabha[crate_id_from_crystal - 1] += bhabhaCalib[ic_base1 - 1];
318 meanGoodCrystalCalibPerCrate_cosmic[crate_id_from_crystal - 1] += cosmicCalibShifted[ic_base1 - 1];
319 numGoodCrystalCalibPerCrate_bhabha[crate_id_from_crystal - 1]++ ;
324 for (
int icrate = 0; icrate <
m_numCrates; icrate++) {
325 meanGoodCrystalCalibPerCrate_bhabha[icrate] /= numGoodCrystalCalibPerCrate_bhabha[icrate] ;
326 meanGoodCrystalCalibPerCrate_cosmic[icrate] /= numGoodCrystalCalibPerCrate_bhabha[icrate] ;
327 B2INFO(
"Mean bhabha crystal calibration for crate " << icrate + 1 <<
" = " << meanGoodCrystalCalibPerCrate_bhabha[icrate]);
328 B2INFO(
"Mean cosmic crystal calibration for crate " << icrate + 1 <<
" = " << meanGoodCrystalCalibPerCrate_cosmic[icrate]);
329 B2INFO(
"Number of good crystal calibrations for crate" << icrate + 1 <<
" = " << numGoodCrystalCalibPerCrate_bhabha[icrate]);
337 for (
int ic_base1 = 1; ic_base1 <=
m_numCrystals; ic_base1++) {
338 int crate_id_from_crystal = crystalMapper->getCrateID(ic_base1);
339 cosmicCalibShifted[ic_base1 - 1] += meanGoodCrystalCalibPerCrate_bhabha[crate_id_from_crystal - 1] -
340 meanGoodCrystalCalibPerCrate_cosmic[crate_id_from_crystal - 1] ;
357 vector<float> newBhabhaMinusCustomPrevCalib__cid(
m_numCrystals);
358 vector<float> newBhabhaMinusCustomPrevBhabhaCalib__cid(
m_numCrystals);
359 vector<float> newBhabhaMinusCustomPrevCalib;
360 vector<float> newBhabhaMinusCustomPrevBhabhaCalib;
365 B2DEBUG(29,
"Merging crystal " << ic + 1);
366 int crate_id_from_crystal = crystalMapper->getCrateID(ic + 1);
369 bool isRefCrys = false ;
370 for (
int icrate = 0; icrate <
m_numCrates; icrate++) {
371 if (ic + 1 == refCrystalPerCrate[icrate]) {
373 B2INFO(
"Crystal " << ic + 1 <<
" is a reference crystal");
376 B2DEBUG(29,
"Checked reference crystal for crate " << icrate + 1);
378 B2DEBUG(29,
"Checked ref crystal");
380 string whichCalibUsed =
"bhabha";
383 newCalib[ic] = bhabhaCalib[ic];
384 newCalibUnc[ic] = fabs(bhabhaCalibUnc[ic]);
386 double newMinusMerged ;
387 double newMinusBhabha ;
395 ((bhabhaCalib[ic] == 0) || (! bhabhaCalibGoodQuality[ic]))) {
399 if (bhabhaGammaCalib[ic] != 0 || bhabhaGammaCalibGoodQuality[ic]) {
400 newCalib[ic] = bhabhaGammaCalib[ic] ;
401 newCalibUnc[ic] = fabs(bhabhaGammaCalibUnc[ic]);
402 whichCalibUsed =
"bhabhaGamma";
404 newCalib[ic] = cosmicCalibShifted[ic] ;
405 newCalibUnc[ic] = cosmicCalibShiftedUnc[ic];
406 whichCalibUsed =
"cosmic";
409 newMinusMerged = (newCalib[ic] - prevValuesCrys[ic]) * TICKS_TO_NS;
410 newMinusBhabha = (newCalib[ic] - prevBhabhaValuesCrys[ic]) * TICKS_TO_NS;
412 newBhabhaMinusCustomPrevCalib__cid[ic] = newMinusMerged;
413 newBhabhaMinusCustomPrevBhabhaCalib__cid[ic] = newMinusBhabha;
414 newBhabhaMinusCustomPrevCalib.push_back(newMinusMerged);
415 newBhabhaMinusCustomPrevBhabhaCalib.push_back(newMinusBhabha);
418 newMinusMerged = (newCalib[ic] - prevValuesCrys[ic]) * TICKS_TO_NS;
419 newMinusBhabha = (newCalib[ic] - prevBhabhaValuesCrys[ic]) * TICKS_TO_NS;
422 B2INFO(
"Crystal " << ic + 1 <<
", crate " << crate_id_from_crystal <<
423 "::: bhabha = " << bhabhaCalib[ic] <<
"+-" << bhabhaCalibUnc[ic] <<
424 " ticks, rad bhabha = " << bhabhaGammaCalib[ic] <<
"+-" << bhabhaGammaCalibUnc[ic] <<
425 " ticks, cosmic UNshifted = " << cosmicCalib[ic] <<
"+-" << cosmicCalibUnc[ic] <<
426 " ticks, cosmic shifted = " << cosmicCalibShifted[ic] <<
"+-" << cosmicCalibShiftedUnc[ic] <<
427 " ticks. ||| New calib = " << newCalib[ic] <<
" ticks, selected " << whichCalibUsed);
428 B2INFO(
" Crystal " << ic + 1 <<
", pre calib ts = " << prevValuesCrys[ic] <<
429 ", pre calib bhabha ts = " << prevBhabhaValuesCrys[ic] <<
430 " ticks ||| new - pre calib ts = " << newMinusMerged <<
431 ", new - pre calib bhabha ts = " << newMinusBhabha <<
" ns");
440 TDirectory* executeDir = gDirectory;
442 TString fname = debugFilename;
443 TFile hfile(fname,
"recreate");
445 TString htitle =
"ECLCrystalTimeOffsetBhabha;cellID;Bhabha ts values [ticks]";
446 TH1F* bhabhaPayload =
new TH1F(
"bhabhaPayload", htitle,
m_numCrystals, 1, 8737);
448 htitle =
"ECLCrystalTimeOffsetBhabhaGamma;cellID;Radiative bhabha ts values [ticks]";
449 TH1F* bhabhaGammaPayload =
new TH1F(
"bhabhaGammaPayload", htitle,
m_numCrystals, 1, 8737);
451 htitle =
"ECLCrystalTimeOffsetCosmic : unshifted;cellID;Unshifted cosmic ts values [ticks]";
452 TH1F* cosmicPayload =
new TH1F(
"cosmicUnshiftedPayload", htitle,
m_numCrystals, 1, 8737);
454 htitle =
"ECLCrystalTimeOffsetCosmic : shifted;cellID;Shifted cosmic ts values [ticks]";
455 TH1F* cosmicShiftedPayload =
new TH1F(
"cosmicShiftedPayload", htitle,
m_numCrystals, 1, 8737);
457 htitle =
"New ECLCrystalTimeOffset;cellID;New (merged) ts values [ticks]";
458 TH1F* newPayload =
new TH1F(
"newPayload", htitle,
m_numCrystals, 1, 8737);
461 htitle =
"ECLCrystalTimeOffsetBhabha : 'pre-calib' values;cellID;Pre-calibration bhabha ts values [ticks]";
462 TH1F* tsCustomPrevBhabha_payload =
new TH1F(
"tsCustomPrevBhabha_payload", htitle,
m_numCrystals, 1, 8737);
464 htitle =
"ECLCrystalTimeOffset : 'pre-calib' values;cellID;Pre-calibration merged ts values [ticks]";
465 TH1F* tsCustomPrev_payload =
new TH1F(
"tsCustomPrev_payload", htitle,
m_numCrystals, 1, 8737);
469 htitle =
"-Only for crystals where the new bhabha ts value is used-;cellID;ts(new | bhabha) - ts(old = 'pre-calib' | merged) [ns]";
470 TH1F* newBhabhaMinusCustomPrev__cid =
new TH1F(
"newBhabhaMinusCustomPrev__cid", htitle,
m_numCrystals, 1, 8737);
472 htitle =
"-Only for crystals where the new bhabha ts value is used-;cellID;ts(new | bhabha) - ts(old = 'pre-calib' | bhabha) [ns]";
473 TH1F* newBhabhaMinusCustomPrevBhabha__cid =
new TH1F(
"newBhabhaMinusCustomPrevBhabha__cid", htitle,
m_numCrystals, 1, 8737);
475 htitle =
"-Only for crystals where the new bhabha ts value is used-;ts(new | bhabha) - ts(old = 'pre-calib' | merged) [ns];Number of crystals";
476 auto tsNewBhabha_MINUS_tsCustomPrev =
new TH1F(
"TsNewBhabha_MINUS_TsCustomPrev", htitle, 285, -69.5801, 69.5801);
478 htitle =
"-Only for crystals where the new bhabha ts value is used-;ts(new | bhabha) - ts(old = 'pre-calib' | bhabha) [ns];Number of crystals";
479 auto tsNewBhabha_MINUS_tsCustomPrevBhabha =
new TH1F(
"TsNewBhabha_MINUS_TsCustomPrevBhabha", htitle, 285, -69.5801, 69.5801);
483 bhabhaPayload->SetBinContent(cellID, bhabhaCalib[cellID - 1]);
484 bhabhaPayload->SetBinError(cellID, fabs(bhabhaCalibUnc[cellID - 1]));
486 bhabhaGammaPayload->SetBinContent(cellID, bhabhaGammaCalib[cellID - 1]);
487 bhabhaGammaPayload->SetBinError(cellID, fabs(bhabhaGammaCalibUnc[cellID - 1]));
489 cosmicPayload->SetBinContent(cellID, cosmicCalib[cellID - 1]);
490 cosmicPayload->SetBinError(cellID, cosmicCalibUnc[cellID - 1]);
492 cosmicShiftedPayload->SetBinContent(cellID, cosmicCalibShifted[cellID - 1]);
493 cosmicShiftedPayload->SetBinError(cellID, cosmicCalibShiftedUnc[cellID - 1]);
495 newPayload->SetBinContent(cellID, newCalib[cellID - 1]);
496 newPayload->SetBinError(cellID, newCalibUnc[cellID - 1]);
500 newBhabhaMinusCustomPrev__cid->SetBinContent(cellID, newBhabhaMinusCustomPrevCalib__cid[cellID - 1]);
501 newBhabhaMinusCustomPrev__cid->SetBinError(cellID, 0);
503 newBhabhaMinusCustomPrevBhabha__cid->SetBinContent(cellID, newBhabhaMinusCustomPrevBhabhaCalib__cid[cellID - 1]);
504 newBhabhaMinusCustomPrevBhabha__cid->SetBinError(cellID, 0);
507 tsCustomPrev_payload->SetBinContent(cellID, prevValuesCrys[cellID - 1]);
508 tsCustomPrev_payload->SetBinError(cellID, prevValuesCrysUnc[cellID - 1]);
510 tsCustomPrevBhabha_payload->SetBinContent(cellID, prevBhabhaValuesCrys[cellID - 1]);
511 tsCustomPrevBhabha_payload->SetBinError(cellID, fabs(prevBhabhaValuesCrysUnc[cellID - 1]));
516 for (
long unsigned int ib = 0; ib < newBhabhaMinusCustomPrevCalib.size(); ib++) {
517 tsNewBhabha_MINUS_tsCustomPrev->Fill(newBhabhaMinusCustomPrevCalib[ib]);
520 for (
long unsigned int ib = 0; ib < newBhabhaMinusCustomPrevBhabhaCalib.size(); ib++) {
521 tsNewBhabha_MINUS_tsCustomPrevBhabha->Fill(newBhabhaMinusCustomPrevBhabhaCalib[ib]);
529 B2INFO(
"Debugging histograms written to " << fname);
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.
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.
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.
DBObjPtr< ECLCrystalCalib > m_ECLCrystalTimeOffsetBhabha
ECLCrystalTimeOffsetBhabha payload that we want to read from the DB.
DBObjPtr< ECLReferenceCrystalPerCrateCalib > m_ECLReferenceCrystalPerCrateCalib
ECLReferenceCrystalPerCrateCalib payload that we want to read from the DB.
static constexpr int m_numCrates
Number of Crates expected.
eclMergingCrystalTimingAlgorithm()
..Constructor
DBObjPtr< ECLCrystalCalib > m_ECLCrateTimeOffset
ECLCrateTimeOffset payload that we want to read from the DB.
static constexpr int m_numCrystals
Number of Crystals expected.
bool readPrevCrysPayload
< Read the previous crystal payload values for comparison
DBObjPtr< ECLCrystalCalib > m_ECLCrystalTimeOffsetCosmic
ECLCrystalTimeOffsetCosmic payload that we want to read from the DB.
DBObjPtr< ECLCrystalCalib > m_ECLCrystalTimeOffsetBhabhaGamma
ECLCrystalTimeOffsetBhabhaGamma payload that we want to read from the DB.
EResult calibrate() override
..Run algorithm
const int c_NCrystals
Number of crystals.
Abstract base class for different kinds of events.
Struct containing exp number and run number.