forked from AliceO2Group/AliceO2
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBadChannelCalibrator.cxx
More file actions
160 lines (140 loc) · 5.4 KB
/
BadChannelCalibrator.cxx
File metadata and controls
160 lines (140 loc) · 5.4 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
// 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.
#include "CommonUtils/MemFileHelper.h"
#include "DetectorsCalibration/Utils.h"
#include "Framework/Logger.h"
#include "MCHCalibration/BadChannelCalibrator.h"
#include "MCHCalibration/BadChannelCalibratorParam.h"
#include "MathUtils/fit.h"
#include <algorithm>
#include <cassert>
#include <iostream>
#include <iterator>
#include <numeric>
#include <limits>
#include <sstream>
namespace o2::mch::calibration
{
void BadChannelCalibrator::initOutput()
{
mPedestalsVector.clear();
mBadChannelsVector.clear();
}
bool BadChannelCalibrator::readyToSend(std::string& reason) const
{
reason = "";
// let's check our hypothesis about this object (nslots=1) is actually true
auto nslots = getNSlots();
if (nslots != 1) {
LOGP(error, "nslots={} while it is expected to be 1", nslots);
return false;
}
auto& slot = getFirstSlot();
bool statIsEnough = hasEnoughData(slot);
if (statIsEnough) {
reason = "enough statistics";
const o2::mch::calibration::PedestalData* pedData = slot.getContainer();
}
return statIsEnough;
}
void BadChannelCalibrator::finalize()
{
// let's check our hypothesis about this object (nslots=1) is actually true
auto nslots = getNSlots();
if (nslots != 1) {
LOGP(fatal, "nslots={} while it is expected to be 1", nslots);
}
auto& slot = getSlot(0);
finalizeSlot(slot);
}
bool BadChannelCalibrator::hasEnoughData(const Slot& slot) const
{
static auto loggerStart = std::chrono::high_resolution_clock::now();
static auto loggerEnd = loggerStart;
const int minNofEntries = BadChannelCalibratorParam::Instance().minRequiredNofEntriesPerChannel;
const o2::mch::calibration::PedestalData* pedData = slot.getContainer();
auto nofChannels = pedData->size();
const int requiredChannels = static_cast<int>(BadChannelCalibratorParam::Instance().minRequiredCalibratedFraction * nofChannels);
auto nofCalibrated = std::count_if(pedData->cbegin(), pedData->cend(),
[&](const PedestalChannel& c) { return c.mEntries > minNofEntries; });
bool hasEnough = nofCalibrated > requiredChannels;
// logging of calibration statistics
loggerEnd = std::chrono::high_resolution_clock::now();
std::chrono::duration<double, std::milli> loggerElapsed = loggerEnd - loggerStart;
if (mLoggingInterval > 0 && loggerElapsed.count() > mLoggingInterval) {
int minEntriesPerChannel{std::numeric_limits<int>::max()};
int maxEntriesPerChannel{0};
uint64_t averageEntriesPerChannel = 0;
std::for_each(pedData->cbegin(), pedData->cend(),
[&](const PedestalChannel& c) {
if (c.mEntries == 0) {
return;
}
if (c.mEntries > maxEntriesPerChannel) {
maxEntriesPerChannel = c.mEntries;
}
if (c.mEntries < minEntriesPerChannel) {
minEntriesPerChannel = c.mEntries;
}
averageEntriesPerChannel += c.mEntries;
});
if (nofChannels > 0) {
averageEntriesPerChannel /= nofChannels;
}
LOGP(warning, "channel stats: min={} max={} average={}", minEntriesPerChannel, maxEntriesPerChannel, averageEntriesPerChannel);
LOGP(warning,
"nofChannelWithEnoughStat(>{})={} nofChannels={} requiredChannels={} hasEnough={}",
minNofEntries, nofCalibrated, nofChannels, requiredChannels, hasEnough);
loggerStart = std::chrono::high_resolution_clock::now();
}
return hasEnough;
}
void BadChannelCalibrator::finalizeSlot(Slot& slot)
{
// Extract results for the single slot
auto pedestalThreshold = BadChannelCalibratorParam::Instance().maxPed;
auto noiseThreshold = BadChannelCalibratorParam::Instance().maxNoise;
mPedestalsVector.clear();
mBadChannelsVector.clear();
o2::mch::calibration::PedestalData* pedestalData = slot.getContainer();
LOG(warning) << "Finalize slot " << slot.getTFStart() << " <= TF <= " << slot.getTFEnd();
// keep track of first TimeFrame
if (slot.getTFStart() < mTFStart) {
mTFStart = slot.getTFStart();
}
for (const auto& ped : *pedestalData) {
if (ped.mEntries == 0) {
continue;
}
mPedestalsVector.emplace_back(ped);
bool bad = true;
if (ped.mPedestal < pedestalThreshold) {
if (ped.getRms() < noiseThreshold) {
bad = false;
}
}
if (bad) {
LOG(info) << ped;
mBadChannelsVector.emplace_back(ped.dsChannelId);
}
}
}
BadChannelCalibrator::Slot&
BadChannelCalibrator::emplaceNewSlot(bool front, TFType tstart, TFType tend)
{
const int nThreads = static_cast<int>(BadChannelCalibratorParam::Instance().nThreads);
auto& cont = getSlots();
auto& slot = front ? cont.emplace_front(tstart, tend) : cont.emplace_back(tstart, tend);
slot.setContainer(std::make_unique<PedestalData>());
slot.getContainer()->setNThreads(nThreads);
return slot;
}
} // namespace o2::mch::calibration