Project
Loading...
Searching...
No Matches
runCalibOffline.cxx
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
14
15#include <iostream>
16#include <boost/program_options.hpp>
17
18#include <fairlogger/Logger.h>
19
22#include "EMCALCalib/CalibDB.h"
24
25#include "CCDB/CcdbApi.h"
26
27#include "TFile.h"
28#include "TH1.h"
29#include "TH2.h"
30
32namespace bpo = boost::program_options;
33
34int main(int argc, char** argv)
35{
36
37 bpo::variables_map vm;
38 bpo::options_description opt_general("");
39 bpo::options_description opt_hidden("");
40 bpo::options_description opt_all;
41 bpo::positional_options_description opt_pos;
42
43 std::string CalibInputPath;
44 std::string ccdbServerPath;
45 bool doBadChannelCalib;
46 bool debugMode = false;
47 bool doLocal = false;
48 bool doScale = false;
49 bool doBCCalibWithTime = false;
50 std::string nameCalibInputHist; // hCellIdVsTimeAbove300 for time, hCellIdVsEnergy for bad channel
51 std::string nameCalibInputHistAdd; // additional input histogram for bad channel calibration if time should be considered
52 std::string namePathStoreLocal; // name for path + histogram to store the calibration locally in root TH1 format
53 unsigned int nthreads; // number of threads used by openMP
54 unsigned long rangestart; // 30/10/2021, 01:02:32 for run 505566 -> 1635548552000
55 unsigned long rangeend; // 30/10/2021, 02:31:10 for run 505566 -> 1635553870000
56
57 double timeRangeLow;
58 double timeRangeHigh;
59
60 try {
61 bpo::options_description desc("Allowed options");
62 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")("debug", bpo::value<bool>()->default_value(false), "Enable debug statements")("storeCalibLocally", bpo::value<bool>()->default_value(false), "Enable local storage of calib")("scaleBadChannelMap", bpo::value<bool>()->default_value(false), "Enable the application of scale factors")("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")("nameInputHistoAdditional", bpo::value<std::string>()->default_value(""), "Set name of additional 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)");
63
64 bpo::store(bpo::parse_command_line(argc, argv, desc), vm);
65
66 if (vm.count("help")) {
67 std::cout << "Please specify: CalibInputPath :Path to TFile with input histograms: \n mode: time or badchannel \n nameInputHisto: name of input calibration histogram\n";
68 }
69
70 if (vm.count("CalibInputPath")) {
71 std::cout << "CalibInputPath was set to "
72 << vm["CalibInputPath"].as<std::string>() << ".\n";
73 CalibInputPath = vm["CalibInputPath"].as<std::string>();
74 } else {
75 std::cout << "CalibInputPath was not set...\n";
76 }
77
78 if (vm.count("ccdbServerPath")) {
79 std::cout << "ccdbServerPath was set to "
80 << vm["ccdbServerPath"].as<std::string>() << ".\n";
81 ccdbServerPath = vm["ccdbServerPath"].as<std::string>();
82 } else {
83 printf("ccdbServerPath was not set.\nWill use standard path %s", ccdbServerPath.c_str());
84 }
85
86 if (vm.count("debug")) {
87 std::cout << "Enable debug mode" << std::endl;
88 debugMode = vm["debug"].as<bool>();
89 }
90
91 if (vm.count("storeCalibLocally")) {
92 std::cout << "Enable local storage of calib" << std::endl;
93 doLocal = vm["storeCalibLocally"].as<bool>();
94 }
95
96 if (vm.count("scaleBadChannelMap")) {
97 doScale = vm["scaleBadChannelMap"].as<bool>();
98 if (doScale) {
99 std::cout << "Enable scaling of the bad channel map" << std::endl;
100 }
101 }
102
103 if (vm.count("mode")) {
104 std::cout << "mode was set to "
105 << vm["mode"].as<std::string>() << ".\n";
106 std::string smode = vm["mode"].as<std::string>();
107 if (smode.find("time") != std::string::npos) {
108 std::cout << "performing time calibration" << std::endl;
109 doBadChannelCalib = false;
110 } else if (smode.find("badchannel") != std::string::npos) {
111 std::cout << "performing bad channel calibration" << std::endl;
112 doBadChannelCalib = true;
113 } else {
114 std::cout << "mode not set... returning\n";
115 return 0;
116 }
117 }
118
119 if (vm.count("nameInputHisto")) {
120 std::cout << "nameInputHisto was set to "
121 << vm["nameInputHisto"].as<std::string>() << ".\n";
122 nameCalibInputHist = vm["nameInputHisto"].as<std::string>();
123 }
124
125 if (vm.count("nameInputHistoAdditional")) {
126 std::cout << "nameInputHistoAdditional was set to "
127 << vm["nameInputHistoAdditional"].as<std::string>() << ".\n";
128 nameCalibInputHistAdd = vm["nameInputHistoAdditional"].as<std::string>();
129 }
130
131 if (vm.count("nthreads")) {
132 std::cout << "number of threads was set to "
133 << vm["nthreads"].as<unsigned int>() << ".\n";
134 nthreads = vm["nthreads"].as<unsigned int>();
135 }
136
137 if (vm.count("timestampStart")) {
138 std::cout << "timestampStart was set to "
139 << vm["timestampStart"].as<unsigned long>() << ".\n";
140 rangestart = vm["timestampStart"].as<unsigned long>();
141 }
142
143 if (vm.count("timestampEnd")) {
144 std::cout << "timestampEnd was set to "
145 << vm["timestampEnd"].as<unsigned long>() << ".\n";
146 rangeend = vm["timestampEnd"].as<unsigned long>();
147 }
148
149 if (vm.count("namePathStoreLocal")) {
150 std::cout << "namePathStoreLocal was set to "
151 << vm["namePathStoreLocal"].as<std::string>() << ".\n";
152 namePathStoreLocal = vm["namePathStoreLocal"].as<std::string>();
153 }
154
155 if (vm.count("timeRangeLow")) {
156 std::cout << "timeRangeLow was set to "
157 << vm["timeRangeLow"].as<double>() << ".\n";
158 timeRangeLow = vm["timeRangeLow"].as<double>();
159 }
160 if (vm.count("timeRangeHigh")) {
161 std::cout << "timeRangeHigh was set to "
162 << vm["timeRangeHigh"].as<double>() << ".\n";
163 timeRangeHigh = vm["timeRangeHigh"].as<double>();
164 }
165
166 } catch (bpo::error& e) {
167 std::cerr << "ERROR: " << e.what() << std::endl
168 << std::endl;
169 std::cerr << opt_general << std::endl;
170 exit(1);
171 } catch (std::exception& e) {
172 std::cerr << e.what() << ", application will now exit" << std::endl;
173 exit(2);
174 }
175
176 if (debugMode) {
177 fair::Logger::SetConsoleSeverity("debug");
178 } else {
179 fair::Logger::SetConsoleSeverity("info");
180 }
181
182 // Set input file and get histogram
183 TFile* fTimeCalibInput = TFile::Open(CalibInputPath.c_str());
184 if (!fTimeCalibInput) {
185 printf("%s not there... returning\n", CalibInputPath.c_str());
186 return 0;
187 }
188
189 // load calibration histogram (cellID vs energy for BC calibration, cellID vs time for time calibration)
190 TH2D* hCalibInputHist_ROOT = (TH2D*)fTimeCalibInput->Get(nameCalibInputHist.c_str());
191 if (!hCalibInputHist_ROOT) {
192 printf("%s not there... returning\n", nameCalibInputHist.c_str());
193 return 0;
194 }
195
196 // load time vs cellID histogram for the bad channel calibration if specified
197 TH2D* hCalibInputHistAdd_ROOT = nullptr;
198 if (!nameCalibInputHistAdd.empty()) {
199 doBCCalibWithTime = true;
200 hCalibInputHistAdd_ROOT = (TH2D*)fTimeCalibInput->Get(nameCalibInputHistAdd.c_str());
201 if (!hCalibInputHistAdd_ROOT) {
202 printf("%s not there... returning\n", nameCalibInputHist.c_str());
203 return 0;
204 }
205 }
206
207 // instance of the calib extractor
208 o2::emcal::EMCALCalibExtractor CalibExtractor;
209 CalibExtractor.setNThreads(nthreads);
210
211 // convert the test root histogram to boost
212 boostHisto2d_VarAxis hCalibInputHist = o2::utils::boostHistoFromRoot_2D<boostHisto2d_VarAxis>(hCalibInputHist_ROOT);
213
214 // instance of CalibDB
215 o2::emcal::CalibDB calibdb(ccdbServerPath);
216
217 if (doBadChannelCalib) {
218 std::map<std::string, std::string> dummymeta;
219 if (doScale) {
220 CalibExtractor.setBCMScaleFactors(calibdb.readChannelScaleFactors(1546300800001, dummymeta));
221 }
222 printf("perform bad channel analysis\n");
224
225 if (doBCCalibWithTime) {
226 boostHisto2d_VarAxis hCalibInputHistAdd = o2::utils::boostHistoFromRoot_2D<boostHisto2d_VarAxis>(hCalibInputHistAdd_ROOT);
227 BCMap = CalibExtractor.calibrateBadChannels(hCalibInputHist, hCalibInputHistAdd);
228 } else {
229 BCMap = CalibExtractor.calibrateBadChannels(hCalibInputHist);
230 }
231 // store bad channel map in ccdb via emcal calibdb
232 if (doLocal) {
233 std::unique_ptr<TFile> writer(TFile::Open(Form("bcm_%lu.root", rangestart), "RECREATE"));
234 writer->WriteObjectAny(&BCMap, "o2::emcal::BadChannelMap", "ccdb_object");
235 } else {
236 std::map<std::string, std::string> metadata;
237 calibdb.storeBadChannelMap(&BCMap, metadata, rangestart, rangeend);
238 }
239
240 } else {
241 printf("perform time calibration analysis\n");
242
243 // calibrate the time
245 TCparams = CalibExtractor.calibrateTime(hCalibInputHist, timeRangeLow, timeRangeHigh);
246
247 if (doLocal) {
248 std::unique_ptr<TFile> writer(TFile::Open(Form("timecalib_%lu.root", rangestart), "RECREATE"));
249 writer->WriteObjectAny(&TCparams, "o2::emcal::TimeCalibrationParams", "ccdb_object");
250 } else {
251 // store parameters in ccdb via emcal calibdb
252 std::map<std::string, std::string> metadata;
253 calibdb.storeTimeCalibParam(&TCparams, metadata, rangestart, rangeend);
254 }
255
256 if (namePathStoreLocal.find(".root") != std::string::npos) {
257 TFile fLocalStorage(namePathStoreLocal.c_str(), "update");
258 fLocalStorage.cd();
259 TH1F* histTCparams = (TH1F*)TCparams.getHistogramRepresentation(false);
260 std::string nameTCHist = "TCParams_" + std::to_string(rangestart) + "_" + std::to_string(rangeend);
261 histTCparams->Write(nameTCHist.c_str(), TObject::kOverwrite);
262 fLocalStorage.Close();
263 }
264 }
265}
boost::histogram::histogram< std::tuple< boost::histogram::axis::variable< double, boost::use_default, boost::use_default, std::allocator< double > >, boost::histogram::axis::variable< double, boost::use_default, boost::use_default, std::allocator< double > > > > boostHisto2d_VarAxis
Timeframe settings Average distance in cm of events merged into Randomize distance around average distance of merged events Simulate correct bunch interactions instead of placing only the average number of events A value[n] sets TFlen for[n] collisions in Number of bunches per trainsort Number of bunch trains Instantaneous interaction rate Do not simulate border Total number of collisions to be placed in the interior of all time Do not select random but walk over array of events in stride steps h Event generator settings h configStandalone Use CPU for GPU Force usage of the specified GPU device no CPU fallback Continue processing after an error First event to Number of events to Number of iterations to Number of iterations to Number of initial iterations excluded from const char e
static std::string getCCDBServer()
Definition NameConf.cxx:123
CCDB container for masked cells in EMCAL.
Interface to calibration data from CCDB for EMCAL.
Definition CalibDB.h:68
EMCALChannelScaleFactors * readChannelScaleFactors(ULong_t timestamp, const std::map< std::string, std::string > &metadata)
Find scale factors used for bad channel calibration in the CCDB for given timestamp.
Definition CalibDB.cxx:189
void storeBadChannelMap(BadChannelMap *bcm, const std::map< std::string, std::string > &metadata, ULong_t timestart, ULong_t timeend)
Store bad channel map in the CCDB.
Definition CalibDB.cxx:39
void storeTimeCalibParam(TimeCalibrationParams *tcp, const std::map< std::string, std::string > &metadata, ULong_t timestart, ULong_t timeend)
Store time calibration coefficiencts in the CCDB.
Definition CalibDB.cxx:47
o2::emcal::BadChannelMap calibrateBadChannels(const boost::histogram::histogram< axes... > &hist, const boost::histogram::histogram< axes... > &histTime=boost::histogram::make_histogram(boost::histogram::axis::variable<>{0., 1.}, boost::histogram::axis::variable<>{0., 1.}))
Function to perform the calibration of bad channels.
void setBCMScaleFactors(EMCALChannelScaleFactors *scalefactors)
o2::emcal::TimeCalibrationParams calibrateTime(const boost::histogram::histogram< axes... > &hist, double minTime=0, double maxTime=1000, double restrictFitRangeToMax=25)
Calibrate time for all cells.
TH1 * getHistogramRepresentation(bool isLowGain) const
Convert the time calibration coefficient array to a histogram.
std::string to_string(gsl::span< T, Size > span)
Definition common.h:52
#define main