18#ifndef EMCAL_CHANNEL_CALIBRATOR_H_
19#define EMCAL_CHANNEL_CALIBRATOR_H_
38#include <boost/histogram.hpp>
39#include <boost/histogram/ostream.hpp>
40#include <boost/format.hpp>
43#include <boost/histogram.hpp>
52template <
typename DataInput,
typename DataOutput>
59 using CcdbObjectInfoVector = std::vector<CcdbObjectInfo>;
74 o2::calibration::TimeSlot<DataInput>&
emplaceNewSlot(
bool front, TFType tstart, TFType tend) final;
78 bool adoptSavedData(const
o2::calibration::TimeSlotMetaData& metadata, TFile& fl) final;
82 bool isTest()
const {
return mTest; }
84 const CcdbObjectInfoVector&
getInfoVector()
const {
return mInfoVector; }
85 const std::vector<DataOutput>&
getOutputVector()
const {
return mCalibObjectVector; }
120 bool mSaveAtEOR =
false;
121 bool mLoadAtSOR =
false;
124 int mStartTSCalib = -1;
125 std::shared_ptr<EMCALCalibExtractor> mCalibrator;
128 CcdbObjectInfoVector mInfoVector;
129 std::vector<DataOutput> mCalibObjectVector;
135template <
typename DataInput,
typename DataOutput>
139 mCalibObjectVector.clear();
140 std::string nameFile =
"tcp.root";
141 if constexpr (std::is_same<DataInput, o2::emcal::EMCALChannelData>::value) {
142 nameFile =
"bcm.root";
151template <
typename DataInput,
typename DataOutput>
159 return (mTest ?
true :
c->hasEnoughData());
163template <
typename DataInput,
typename DataOutput>
176 if constexpr (std::is_same<DataInput, o2::emcal::EMCALChannelData>::value) {
179 this->saveLastSlot();
182 }
else if constexpr (std::is_same<DataInput, o2::emcal::EMCALTimeCalibData>::value) {
185 this->saveLastSlot();
197 std::map<std::string, std::string> md;
198 if constexpr (std::is_same<DataInput, o2::emcal::EMCALChannelData>::value) {
199 LOG(
debug) <<
"Launching the calibration.";
201 LOG(
debug) <<
"Done with the calibraiton";
206 mCalibObjectVector.push_back(bcm);
215 std::string nameBCHist =
"BadChannels_" +
std::to_string(tsStart);
216 histBCMap->Write(nameBCHist.c_str(), TObject::kOverwrite);
219 std::string nameBCInputHist =
"EnergyVsCellID_" +
std::to_string(tsStart);
220 hCalibHist.Write(nameBCInputHist.c_str(), TObject::kOverwrite);
223 std::string nameBCInputHistTime =
"TimeVsCellID_" +
std::to_string(tsStart);
224 hCalibHistTime.Write(nameBCInputHistTime.c_str(), TObject::kOverwrite);
226 fLocalStorage.Close();
228 }
else if constexpr (std::is_same<DataInput, o2::emcal::EMCALTimeCalibData>::value) {
237 mCalibObjectVector.push_back(tcd);
245 std::string nameTCHist =
"TCParams_HG_" +
std::to_string(tsStart);
246 histTCparams->Write(nameTCHist.c_str(), TObject::kOverwrite);
249 std::string nameTCHist_LG =
"TCParams_LG_" +
std::to_string(tsStart);
250 histTCparams_LG->Write(nameTCHist_LG.c_str(), TObject::kOverwrite);
253 std::string nameTCInputHist =
"TimeVsCellID_" +
std::to_string(tsStart);
254 hCalibHist.Write(nameTCInputHist.c_str(), TObject::kOverwrite);
255 fLocalStorage.Close();
260template <
typename DataInput,
typename DataOutput>
264 auto& slot = front ? cont.emplace_front(tstart, tend) : cont.emplace_back(tstart, tend);
265 slot.setContainer(std::make_unique<DataInput>());
271template <
typename DataInput,
typename DataOutput>
274 LOG(info) <<
"EMC calib histos are saved in " << fl.GetName();
277 auto& slot = cont.at(0);
278 DataInput*
c = slot.getContainer();
281 long tsStart = slot.getStartTimeMS();
283 tsStart = slot.getStaticStartTimeMS();
290 TH1I hGlobalProperties(
"hGlobalProperties",
"hGlobalProperties", 4, -0.5, 3.5);
291 hGlobalProperties.GetXaxis()->SetBinLabel(1,
"Fill nr.");
292 hGlobalProperties.GetXaxis()->SetBinLabel(2,
"run type");
293 hGlobalProperties.GetXaxis()->SetBinLabel(3,
"ts in hours");
294 hGlobalProperties.GetXaxis()->SetBinLabel(4,
"ts in seconds");
295 hGlobalProperties.SetBinContent(1, mFillNr);
296 hGlobalProperties.SetBinContent(2, mRunType);
297 hGlobalProperties.SetBinContent(3, timeNowHour);
298 hGlobalProperties.SetBinContent(4, timeNowSec);
300 if constexpr (std::is_same<DataInput, o2::emcal::EMCALChannelData>::value) {
301 auto hist =
c->getHisto();
302 auto histTime =
c->getHistoTime();
306 TH1D hNEvents(
"hNEvents",
"hNEvents", 1, 0, 1);
307 hNEvents.SetBinContent(1,
c->getNEvents());
310 hEnergy.Write(
"EnergyVsCellID");
311 hTime.Write(
"TimeVsCellID");
312 hNEvents.Write(
"NEvents");
313 hGlobalProperties.Write(
"GlobalProperties");
315 }
else if constexpr (std::is_same<DataInput, o2::emcal::EMCALTimeCalibData>::value) {
316 auto histTime =
c->getHisto();
318 TH1D hNEvents(
"hNEvents",
"hNEvents", 1, 0, 1);
319 hNEvents.SetBinContent(1,
c->getNEvents());
322 hTime.Write(
"TimeVsCellID");
323 hNEvents.Write(
"NEvents");
324 hGlobalProperties.Write(
"GlobalProperties");
333template <
typename DataInput,
typename DataOutput>
336 LOG(info) <<
"Loading data from previous run";
338 if (!this->getSavedSlotAllowed() || !this->getLoadAtSOR())
343 if (cont.size() == 0) {
344 LOG(warning) <<
"cont.size() is 0, calibration objects from previous run cannot be loaded...";
347 auto& slot = cont.at(0);
348 DataInput*
c = slot.getContainer();
351 TH1I* hGlobalProperties = (TH1I*)fl.Get(
"GlobalProperties");
352 if (!hGlobalProperties) {
353 LOG(error) <<
"GlobalProperties histogram not found. Will not load previous calibration histograms";
355 int fillNr = hGlobalProperties->GetBinContent(1);
356 int runType = hGlobalProperties->GetBinContent(2);
357 int tsOld = hGlobalProperties->GetBinContent(3);
358 int tsOldSec = (hGlobalProperties->GetNbinsX() > 3) ? hGlobalProperties->GetBinContent(4) : 0;
363 LOG(info) <<
"adoptSavedData: Same run type required but run types differ: " << runType <<
" != " <<
static_cast<int>(mRunType);
367 LOG(info) <<
"adoptSavedData: Same fill nr. required but fills differ: " << fillNr <<
" != " << mFillNr;
380 if constexpr (std::is_same<DataInput, o2::emcal::EMCALChannelData>::value) {
381 TH2D* hEnergy = (TH2D*)fl.Get(
"EnergyVsCellID");
382 TH2D* hTime = (TH2D*)fl.Get(
"TimeVsCellID");
383 if (!hEnergy || !hTime) {
386 auto hEnergyBoost = o2::utils::boostHistoFromRoot_2D<boostHisto2d>(hEnergy);
387 auto hTimeBoost = o2::utils::boostHistoFromRoot_2D<boostHisto2d>(hTime);
389 c->setHisto(hEnergyBoost);
390 c->setHistoTime(hTimeBoost);
392 }
else if constexpr (std::is_same<DataInput, o2::emcal::EMCALTimeCalibData>::value) {
393 TH2D* hTime = (TH2D*)fl.Get(
"TimeVsCellID");
397 auto hTimeBoost = o2::utils::boostHistoFromRoot_2D<boostHisto2d>(hTime);
399 c->setHisto(hTimeBoost);
401 TH1D* hEvents = (TH1D*)fl.Get(
"NEvents");
405 c->setNEvents(hEvents->GetBinContent(1));
406 LOG(info) <<
"successfully loaded histograms with " << hEvents->GetBinContent(1) <<
" events";
411template <
typename DataInput,
typename DataOutput>
416 if (cont.size() == 0) {
420 auto& slot = cont.at(0);
421 DataInput*
c = slot.getContainer();
422 c->setGainCalibFactors(gainCalibFactors);
Utils and constants for calibration and related workflows.
Header of the AggregatedRunInfo struct.
void setSaveDirectory(const std::string &n)
long getStaticStartTimeMS() const
long getStartTimeMS() const
long getEndTimeMS() const
const Container * getContainer() const
TFType getTFStart() const
static std::string generateFileName(const std::string &inp)
static constexpr long HOUR
static constexpr long SECOND
static const EMCALCalibParams & Instance()
CCDB container for masked cells in EMCAL.
TH2 * getHistogramRepresentation() const
Convert map into 2D histogram representation.
static const char * getCDBPathTimeCalibrationParams()
Get CDB path for the time calibration.
static const char * getCDBPathBadChannelMap()
Get CDB path for the bad channel map.
EMCAL compressed cell information.
class used for managment of bad channel and time calibration template DataInput can be ChannelData or...
const std::vector< DataOutput > & getOutputVector() const
void initOutput() final
Initialize the vector of our output objects.
void setIsTest(bool isTest)
Set the testing status.
bool getSaveAtEOR() const
get if has enough data should be circumvented at EOR
bool adoptSavedData(const o2::calibration::TimeSlotMetaData &metadata, TFile &fl) final
Read histograms for energy and time vs cell ID to file.
bool getLoadAtSOR() const
get if has enough data should be circumvented at EOR
void setRunType(o2::parameters::GRPECSObject::RunType rt)
Set current run type.
o2::calibration::TimeSlot< DataInput > & emplaceNewSlot(bool front, TFType tstart, TFType tend) final
void setCurrTSInHours(int ts)
Set current timestamp obtained from data.
void SetCalibExtractor(std::shared_ptr< EMCALCalibExtractor > extr)
setter for mCalibrator
std::shared_ptr< EMCALCalibExtractor > getCalibExtractor()
Configure the calibrator.
const CcdbObjectInfoVector & getInfoVector() const
~EMCALChannelCalibrator() final=default
void setSaveAtEOR(bool tmp)
set if has enough data should be circumvented at EOR
bool hasEnoughData(const Slot &slot) const final
Checking if all channels have enough data to do calibration.
EMCALChannelCalibrator(int nb=1000, float r=0.35)
void setFillNr(int fn)
Set current fill number.
void setLoadAtSOR(bool tmp)
set if has enough data should be circumvented at EOR
void finalizeSlot(Slot &slot) final
bool saveLastSlotData(TFile &fl) final
Write histograms for energy and time vs cell ID to file.
bool setGainCalibrationFactors(o2::emcal::GainCalibrationFactors *gainCalibFactors)
CCDB container for the gain calibration factors.
TH1 * getHistogramRepresentation(bool isLowGain) const
Convert the time calibration coefficient array to a histogram.
long getCurrentTimestamp()
returns the timestamp in long corresponding to "now"
TH2F TH2FFromBoost(BoostHist hist, const char *name="hist")
Convert a 2D boost histogram to a root histogram.
a couple of static helper functions to create timestamp values for CCDB queries or override obsolete ...
std::string to_string(gsl::span< T, Size > span)
unsigned int minNEventsSaveSlot
minimum amount a slot has to have in order to be taken into accoutn in finalize slot....
int tsDiffMax
if loading calib objects from previous run, limit time between the object being stored and loaded aga...
int minTimeForFit_tc
minimum cell time considered for the time calibration in ns
int restrictFitRangeToMax_tc
window around the largest entry within the minTimeForFit in which the fit is performed in ns
unsigned int minNEvents_tc
minimum number of events to trigger the calibration
long endTimeMargin
set end TS to 30 days after slot ends (1000 * 60 * 60 * 24 * 30)
unsigned int minNEvents_bc
minimum number of events to trigger the calibration
bool requireSameFill
if loading calib objects from previous run, require it to be in the same fill as the current one
bool requireSameRunType
if loading calib objects from previous run, require it to be the same run type
int maxTimeForFit_tc
maximum cell time considered for the time calibration in ns
static std::string getClassName(const T &obj)
get the class name of the object
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"