-
Notifications
You must be signed in to change notification settings - Fork 494
Expand file tree
/
Copy pathCDBInterface.cxx
More file actions
471 lines (394 loc) · 14.6 KB
/
CDBInterface.cxx
File metadata and controls
471 lines (394 loc) · 14.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
/// \file CDBInterface.h
/// \brief Simple interface to the CDB manager
/// \author Jens Wiechula, Jens.Wiechula@ikf.uni-frankfurt.de
// system includes
#include <cxxabi.h>
#include <ctime>
#include <memory>
#include <fmt/format.h>
#include <fmt/chrono.h>
// root includes
#include "TFile.h"
#include "TRandom.h"
// o2 includes
#include "DataFormatsTPC/CalibdEdxCorrection.h"
#include "TPCBase/CDBInterface.h"
#include "TPCBase/ParameterDetector.h"
#include "TPCBase/ParameterElectronics.h"
#include "TPCBase/ParameterGEM.h"
#include "TPCBase/ParameterGas.h"
#include "TPCBase/Utils.h"
using namespace o2::tpc;
// anonymous namespace to prevent usage outside of this file
namespace
{
/// utility function to demangle cxx type names
std::string demangle(std::string_view name)
{
int status = -4; // some arbitrary value to eliminate the compiler warning
std::unique_ptr<char, void (*)(void*)> res{abi::__cxa_demangle(name.data(), nullptr, nullptr, &status), std::free};
return (status == 0) ? res.get() : name.data();
}
} // end anonymous namespace
//______________________________________________________________________________
const CalPad& CDBInterface::getPedestals()
{
// ===| load noise and pedestals from file if requested |=====================
if (mPedestalNoiseFileName.size()) {
if (!mPedestals) {
loadNoiseAndPedestalFromFile();
}
} else if (mUseDefaults) {
if (!mPedestals) {
createDefaultPedestals();
}
} else {
// return from CDB, assume that check for object existence are done there
return getObjectFromCDB<CalPadMapType>(CDBTypeMap.at(CDBType::CalPedestalNoise)).at("Pedestals");
}
if (!mPedestals) {
LOG(fatal) << "No valid pedestal object was loaded";
}
return *mPedestals;
}
//______________________________________________________________________________
const CalPad& CDBInterface::getNoise()
{
// ===| load noise and pedestals from file if requested |=====================
if (mPedestalNoiseFileName.size()) {
if (!mNoise) {
loadNoiseAndPedestalFromFile();
}
} else if (mUseDefaults) {
if (!mNoise) {
createDefaultNoise();
}
} else {
// return from CDB, assume that check for object existence are done there
return getObjectFromCDB<CalPadMapType>(CDBTypeMap.at(CDBType::CalPedestalNoise)).at("Noise");
}
if (!mNoise) {
LOG(fatal) << "No valid noise object was loaded";
}
return *mNoise;
}
//______________________________________________________________________________
const CalPad& CDBInterface::getZeroSuppressionThreshold()
{
if (mUseDefaults) {
if (!mZeroSuppression) {
createDefaultZeroSuppression();
}
return *mZeroSuppression;
} else {
// return from CDB, assume that check for object existence are done there
return getObjectFromCDB<CalPadMapType>(CDBTypeMap.at(CDBType::ConfigFEEPad)).at("ThresholdMap");
;
}
}
//______________________________________________________________________________
const CalPad& CDBInterface::getGainMap()
{
// ===| load gain map from file if requested |=====================
if (mGainMapFileName.size()) {
if (!mGainMap) {
loadGainMapFromFile();
}
} else if (mUseDefaults) {
if (!mGainMap) {
createDefaultGainMap();
}
} else {
// return from CDB, assume that check for object existence are done there
return getObjectFromCDB<CalPad>(CDBTypeMap.at(CDBType::CalPadGainFull));
}
if (!mGainMap) {
LOG(fatal) << "No valid gain object was loaded";
}
return *mGainMap;
}
//______________________________________________________________________________
const ParameterDetector& CDBInterface::getParameterDetector()
{
if (mUseDefaults) {
return ParameterDetector::Instance();
}
// return from CDB, assume that check for object existence are done there
return getObjectFromCDB<ParameterDetector>(CDBTypeMap.at(CDBType::ParDetector));
}
//______________________________________________________________________________
const ParameterElectronics& CDBInterface::getParameterElectronics()
{
if (mUseDefaults) {
return ParameterElectronics::Instance();
}
// return from CDB, assume that check for object existence are done there
return getObjectFromCDB<ParameterElectronics>(CDBTypeMap.at(CDBType::ParElectronics));
}
//______________________________________________________________________________
const ParameterGas& CDBInterface::getParameterGas()
{
if (mUseDefaults) {
return ParameterGas::Instance();
}
// return from CDB, assume that check for object existence are done there
return getObjectFromCDB<ParameterGas>(CDBTypeMap.at(CDBType::ParGas));
}
//______________________________________________________________________________
const ParameterGEM& CDBInterface::getParameterGEM()
{
if (mUseDefaults) {
return ParameterGEM::Instance();
}
// return from CDB, assume that check for object existence are done there
return getObjectFromCDB<ParameterGEM>(CDBTypeMap.at(CDBType::ParGEM));
}
//______________________________________________________________________________
const CalPad& CDBInterface::getCalPad(const std::string_view path)
{
return getSpecificObjectFromCDB<CalPad>(path);
}
//______________________________________________________________________________
void CDBInterface::loadNoiseAndPedestalFromFile()
{
std::unique_ptr<TFile> file(TFile::Open(mPedestalNoiseFileName.data()));
CalPad* pedestals{nullptr};
CalPad* noise{nullptr};
file->GetObject("Pedestals", pedestals);
file->GetObject("Noise", noise);
if (!pedestals) {
LOG(fatal) << "No valid pedestal object was loaded";
}
if (!noise) {
LOG(fatal) << "No valid noise object was loaded";
}
mPedestals.reset(pedestals);
mNoise.reset(noise);
LOG(info) << "Loaded Noise and pedestal from file '" << mPedestalNoiseFileName << "'";
}
//______________________________________________________________________________
void CDBInterface::loadGainMapFromFile()
{
std::unique_ptr<TFile> file(TFile::Open(mGainMapFileName.data()));
CalPad* gain{nullptr};
file->GetObject("GainMap", gain);
if (!gain) {
LOG(fatal) << "No valid gain map object was loaded";
}
mGainMap.reset(gain);
LOG(info) << "Loaded gain map from file '" << mGainMapFileName << "'";
}
//______________________________________________________________________________
void CDBInterface::createDefaultPedestals()
{
// ===| create random pedestals |=============================================
mPedestals = std::make_unique<CalPad>("Pedestals");
// distribution based on test beam data
const float meanPedestal = 72.5;
const float sigmaPedestal = 9.0;
// set a minimum and maximum for the value
const float minPedestal = meanPedestal - 4 * sigmaPedestal;
const float maxPedestal = meanPedestal + 4 * sigmaPedestal;
for (auto& calArray : mPedestals->getData()) {
for (auto& val : calArray.getData()) {
float random = gRandom->Gaus(meanPedestal, sigmaPedestal);
if (random < minPedestal) {
random = minPedestal;
}
if (random > maxPedestal) {
random = maxPedestal;
}
val = random;
}
}
}
//______________________________________________________________________________
void CDBInterface::createDefaultNoise()
{
// ===| create random noise |=============================================
mNoise = std::make_unique<CalPad>("Noise");
// distribution based on test beam data
const float meanNoise = 1.0;
const float sigmaNoise = 0.05;
// set a minimum and maximum for the value
const float minNoise = meanNoise - 4 * sigmaNoise;
const float maxNoise = meanNoise + 8 * sigmaNoise;
for (auto& calArray : mNoise->getData()) {
for (auto& val : calArray.getData()) {
float random = gRandom->Gaus(meanNoise, sigmaNoise);
if (random < minNoise) {
random = minNoise;
}
if (random > maxNoise) {
random = maxNoise;
}
val = random;
}
}
}
//______________________________________________________________________________
void CDBInterface::createDefaultZeroSuppression()
{
// default map is 3*noise
mZeroSuppression = std::unique_ptr<CalPad>(new CalPad(getNoise()));
mZeroSuppression->setName("ThresholdMap");
for (auto& calArray : mZeroSuppression->getData()) {
auto& data = calArray.getData();
std::transform(data.begin(), data.end(), data.begin(), [](const auto value) { return 3.f * value; });
}
}
//______________________________________________________________________________
void CDBInterface::createDefaultGainMap()
{
// ===| create random gain map |=============================================
mGainMap = std::make_unique<CalPad>("Gain");
// distribution based on test beam data
const float meanGain = 1.0;
const float sigmaGain = 0.12;
// set a minimum and maximum for the value
const float minGain = meanGain - 4 * sigmaGain;
const float maxGain = meanGain + 8 * sigmaGain;
for (auto& calArray : mGainMap->getData()) {
for (auto& val : calArray.getData()) {
float random = gRandom->Gaus(meanGain, sigmaGain);
if (random < minGain) {
random = minGain;
}
if (random > maxGain) {
random = maxGain;
}
val = random;
}
}
}
//______________________________________________________________________________
bool CDBStorage::checkMetaData(MetaData_t metaData) const
{
if (!metaData.size()) {
LOGP(error, "no meta data set");
}
const std::array<std::string_view, 3> requirements{
"Required",
"Recommended",
"Optional"};
const std::array<std::vector<std::string_view>, 3> tests{{
{"Responsible", "Reason", "Intervention"}, // errors
{"JIRA"}, // warnings
{"Comment"} // infos
}};
std::array<int, 3> counts{};
for (size_t i = 0; i < requirements.size(); ++i) {
for (const auto& test : tests[i]) {
if (!metaData[test.data()].size()) {
const auto message = fmt::format("{} field {} not set in meta data", requirements[i], test);
if (i == 0) {
LOG(error) << message;
} else if (i == 1) {
LOG(warning) << message;
} else {
LOG(info) << message;
}
++counts[i];
} else {
LOGP(debug, "{} field '{}' set to '{}'", requirements[i], test, metaData[test.data()]);
}
}
}
return counts[0] == 0;
}
//______________________________________________________________________________
void CDBStorage::uploadNoiseAndPedestal(std::string_view fileName, long first, long last)
{
std::unique_ptr<TFile> file(TFile::Open(fileName.data()));
CalPad* pedestals{nullptr};
CalPad* noise{nullptr};
file->GetObject("Pedestals", pedestals);
file->GetObject("Noise", noise);
if (!pedestals) {
LOGP(fatal, "No valid pedestal object was loaded from file {}", fileName);
}
if (!noise) {
LOGP(fatal, "No valid noise object was loaded from file {}", fileName);
}
CDBInterface::CalPadMapType calib;
calib["Pedestals"] = *pedestals;
calib["Noise"] = *noise;
storeObject(&calib, CDBType::CalPedestalNoise, first, last);
}
//______________________________________________________________________________
void CDBStorage::uploadGainMap(std::string_view fileName, bool isFull, long first, long last)
{
std::unique_ptr<TFile> file(TFile::Open(fileName.data()));
CalPad* gain{nullptr};
file->GetObject("GainMap", gain);
if (!gain) {
LOG(fatal) << "No valid gain map object was loaded";
}
storeObject(gain, isFull ? CDBType::CalPadGainFull : CDBType::CalPadGainResidual, first, last);
}
//______________________________________________________________________________
void CDBStorage::uploadPulserOrCEData(CDBType type, std::string_view fileName, long first, long last)
{
auto calPads = o2::tpc::utils::readCalPads(fileName, "T0,Width,Qtot");
if (calPads.size() != 3) {
LOGP(fatal, "Missing pulser object in file {}", fileName);
}
auto t0 = calPads[0];
auto width = calPads[1];
auto qtot = calPads[2];
std::unordered_map<std::string, CalDet<float>> pulserCalib;
pulserCalib["T0"] = *t0;
pulserCalib["Width"] = *width;
pulserCalib["Qtot"] = *qtot;
storeObject(&pulserCalib, type, first, last);
}
//______________________________________________________________________________
void CDBStorage::uploadFEEConfigPad(std::string_view fileName, long first, long last)
{
auto calPads = o2::tpc::utils::readCalPads(fileName, "ThresholdMap");
if (calPads.size() != 1) {
LOGP(fatal, "Missing pulser object in file {}", fileName);
}
auto thresholdMap = calPads[0];
std::unordered_map<std::string, CalDet<float>> feeConfigPad;
feeConfigPad["ThresholdMap"] = *thresholdMap;
storeObject(&feeConfigPad, CDBType::ConfigFEEPad, first, last);
}
//______________________________________________________________________________
void CDBStorage::uploadTimeGain(std::string_view fileName, long first, long last)
{
std::unique_ptr<TFile> file(TFile::Open(fileName.data()));
auto timeGain = file->Get<o2::tpc::CalibdEdxCorrection>("CalibdEdxCorrection");
if (!timeGain) {
LOGP(fatal, "No valid timeGain object found in {}", fileName);
}
storeObject(timeGain, CDBType::CalTimeGain, first, last);
}
//______________________________________________________________________________
void CDBStorage::printObjectSummary(std::string_view name, CDBType const type, MetaData_t const& metadata, long start, long end) const
{
std::time_t tstart(start / 1000);
std::time_t tend(end / 1000);
auto tstartms = start % 1000;
auto tendms = end % 1000;
std::string message = fmt::format("Writing object of type '{}'\n", demangle(name)) +
fmt::format(" to storage '{}'\n", mCCDB.getURL()) +
fmt::format(" into path '{}'\n", CDBTypeMap.at(type)) +
fmt::format(" with validity [{}, {}] :", start, end) +
fmt::format(" [{:%d.%m.%Y %H:%M:%S}.{:03d}, {:%d.%m.%Y %H:%M:%S}.{:03d}]\n", fmt::localtime(tstart), tstartms, fmt::localtime(tend), tendms) +
std::string(" Meta data:\n");
for (const auto& [key, value] : metadata) {
message += fmt::format("{:>20} = {}\n", key, value);
}
LOGP(info, message);
}