Skip to content

Commit b1827f0

Browse files
authored
[EMCAL-566] Added function for fast projection of boost histograms
1 parent f3bedb4 commit b1827f0

File tree

3 files changed

+68
-8
lines changed

3 files changed

+68
-8
lines changed

Common/Utils/include/CommonUtils/BoostHistogramUtils.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,31 @@ auto ProjectBoostHistoX(boost::histogram::histogram<axes...>& hist2d, const int
427427
return histoProj;
428428
}
429429

430+
/// \brief Function to project 2d boost histogram onto x-axis
431+
/// \param hist2d 2d boost histogram
432+
/// \param binLow lower bin in y for projection
433+
/// \param binHigh lower bin in y for projection
434+
/// \return result
435+
/// 1d boost histogram from projection of the input 2d boost histogram
436+
template <typename... axes>
437+
auto ProjectBoostHistoXFast(boost::histogram::histogram<axes...>& hist2d, const int binLow, const int binHigh)
438+
{
439+
unsigned int nbins = hist2d.axis(0).size();
440+
double binStartX = hist2d.axis(0).bin(0).lower();
441+
double binEndX = hist2d.axis(0).bin(nbins - 1).upper();
442+
auto histoProj = boost::histogram::make_histogram(boost::histogram::axis::regular<>(nbins, binStartX, binEndX));
443+
444+
// Now rewrite the bin content of the 1d histogram to get the summed bin content in the specified range
445+
for (int x = 0; x < nbins; ++x) {
446+
histoProj.at(x) = 0;
447+
for (int y = binLow; y < binHigh; ++y) {
448+
histoProj.at(x) = histoProj.at(x) + hist2d.at(x, y);
449+
}
450+
}
451+
452+
return histoProj;
453+
}
454+
430455
} // end namespace utils
431456
} // end namespace o2
432457

Detectors/EMCAL/calibration/include/EMCALCalibration/EMCALCalibExtractor.h

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,10 @@ class EMCALCalibExtractor
9393
return mOutputBCM;
9494
}
9595

96-
/// \brief For now a dummy function to calibrate the time.
96+
/// \brief Calibrate time for all cells
97+
/// \param hist -- 2d boost histogram: cell-time vs. cell-ID
98+
/// \param minTime -- min. time considered for fit
99+
/// \param maxTime -- max. time considered for fit
97100
template <typename... axes>
98101
o2::emcal::TimeCalibrationParams calibrateTime(boost::histogram::histogram<axes...>& hist, double minTime = 0, double maxTime = 1000)
99102
{
@@ -102,6 +105,8 @@ class EMCALCalibExtractor
102105

103106
o2::emcal::TimeCalibrationParams TCP;
104107

108+
double mean = 0;
109+
105110
#if (defined(WITH_OPENMP) && !defined(__CLING__))
106111
if (mNThreads < 1) {
107112
mNThreads = std::min(omp_get_max_threads(), NCELLS);
@@ -114,18 +119,18 @@ class EMCALCalibExtractor
114119
#endif
115120

116121
for (unsigned int i = 0; i < NCELLS; ++i) {
122+
117123
// project boost histogram to 1d just for 1 cell
118-
auto boostHist1d = o2::utils::ProjectBoostHistoX(histReduced, i, i + 1);
119-
// maybe cut histo to max +- 25 ns
124+
auto boostHist1d = o2::utils::ProjectBoostHistoXFast(histReduced, i, i + 1);
125+
// ToDo: maybe cut histo to max +- 25 ns
120126

121-
// fit with gaussian to extract mean
122127
try {
123128
auto fitValues = o2::utils::fitBoostHistoWithGaus<double>(boostHist1d);
124-
double mean = fitValues.at(1);
129+
mean = fitValues.at(1);
125130
// add mean to time calib params
126131
TCP.addTimeCalibParam(i, mean, 0);
127132
} catch (o2::utils::FitGausError_t) {
128-
TCP.addTimeCalibParam(i, 400, 0); // 400 ns shift default value
133+
TCP.addTimeCalibParam(i, mean, 0); // take calib value of last cell; or 400 ns shift default value
129134
}
130135
}
131136
return TCP;

Detectors/EMCAL/calibration/run/runCalibOffline.cxx

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,19 @@ int main(int argc, char** argv)
4444
std::string ccdbServerPath;
4545
bool doBadChannelCalib;
4646
std::string nameCalibInputHist; // hCellIdVsTimeAbove300 for time, hCellIdVsEnergy for bad channel
47+
std::string namePathStoreLocal; // name for path + histogram to store the calibration locally in root TH1 format
4748

4849
unsigned int nthreads; // number of threads used by openMP
4950

5051
unsigned long rangestart; //30/10/2021, 01:02:32 for run 505566 -> 1635548552000
5152
unsigned long rangeend; // 30/10/2021, 02:31:10 for run 505566 -> 1635553870000
5253

54+
double timeRangeLow;
55+
double timeRangeHigh;
56+
5357
try {
5458
bpo::options_description desc("Allowed options");
55-
desc.add_options()("help", "Print this help message")("CalibInputPath", bpo::value<std::string>()->required(), "Set root input histogram")("ccdbServerPath", bpo::value<std::string>()->default_value(o2::base::NameConf::getCCDBServer()), "Set path to ccdb server")("mode", bpo::value<std::string>()->required(), "Set if time or bad channel calib")("nameInputHisto", bpo::value<std::string>()->default_value("hCellIdVsTimeAbove300"), "Set name of input histogram")("nthreads", bpo::value<unsigned int>()->default_value(1), "Set number of threads for OpenMP")("timestampStart", bpo::value<unsigned long>()->default_value(1635548552000), "Set timestamp from start of run")("timestampEnd", bpo::value<unsigned long>()->default_value(1635553870000), "Set timestamp from end of run");
59+
desc.add_options()("help", "Print this help message")("CalibInputPath", bpo::value<std::string>()->required(), "Set root input histogram")("ccdbServerPath", bpo::value<std::string>()->default_value(o2::base::NameConf::getCCDBServer()), "Set path to ccdb server")("mode", bpo::value<std::string>()->required(), "Set if time or bad channel calib")("nameInputHisto", bpo::value<std::string>()->default_value("hCellIdVsTimeAbove300"), "Set name of input histogram")("nthreads", bpo::value<unsigned int>()->default_value(1), "Set number of threads for OpenMP")("timestampStart", bpo::value<unsigned long>()->default_value(1635548552000), "Set timestamp from start of run")("timestampEnd", bpo::value<unsigned long>()->default_value(1635553870000), "Set timestamp from end of run")("namePathStoreLocal", bpo::value<std::string>()->default_value(""), "Set path to store histo of time calib locally")("timeRangeLow", bpo::value<double>()->default_value(1), "Set lower boundary of fit interval for time calibration (in ns)")("timeRangeHigh", bpo::value<double>()->default_value(1000), "Set upper boundary of fit interval for time calibration (in ns)");
5660

5761
bpo::store(bpo::parse_command_line(argc, argv, desc), vm);
5862

@@ -116,6 +120,23 @@ int main(int argc, char** argv)
116120
rangeend = vm["timestampEnd"].as<unsigned long>();
117121
}
118122

123+
if (vm.count("namePathStoreLocal")) {
124+
std::cout << "namePathStoreLocal was set to "
125+
<< vm["namePathStoreLocal"].as<std::string>() << ".\n";
126+
namePathStoreLocal = vm["namePathStoreLocal"].as<std::string>();
127+
}
128+
129+
if (vm.count("timeRangeLow")) {
130+
std::cout << "timeRangeLow was set to "
131+
<< vm["timeRangeLow"].as<double>() << ".\n";
132+
timeRangeLow = vm["timeRangeLow"].as<double>();
133+
}
134+
if (vm.count("timeRangeHigh")) {
135+
std::cout << "timeRangeHigh was set to "
136+
<< vm["timeRangeHigh"].as<double>() << ".\n";
137+
timeRangeHigh = vm["timeRangeHigh"].as<double>();
138+
}
139+
119140
} catch (bpo::error& e) {
120141
std::cerr << "ERROR: " << e.what() << std::endl
121142
<< std::endl;
@@ -159,10 +180,19 @@ int main(int argc, char** argv)
159180

160181
// calibrate the time
161182
o2::emcal::TimeCalibrationParams TCparams;
162-
TCparams = CalibExtractor.calibrateTime(hCalibInputHist);
183+
TCparams = CalibExtractor.calibrateTime(hCalibInputHist, timeRangeLow, timeRangeHigh);
163184

164185
// store parameters in ccdb via emcal calibdb
165186
std::map<std::string, std::string> metadata;
166187
calibdb.storeTimeCalibParam(&TCparams, metadata, rangestart, rangeend);
188+
189+
if (namePathStoreLocal.find(".root") != std::string::npos) {
190+
TFile fLocalStorage(namePathStoreLocal.c_str(), "update");
191+
fLocalStorage.cd();
192+
TH1F* histTCparams = (TH1F*)TCparams.getHistogramRepresentation(false);
193+
std::string nameTCHist = "TCParams_" + std::to_string(rangestart) + "_" + std::to_string(rangeend);
194+
histTCparams->Write(nameTCHist.c_str(), TObject::kOverwrite);
195+
fLocalStorage.Close();
196+
}
167197
}
168198
}

0 commit comments

Comments
 (0)