forked from AliceO2Group/AliceO2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClustererSpec.cxx
More file actions
333 lines (309 loc) · 16.1 KB
/
ClustererSpec.cxx
File metadata and controls
333 lines (309 loc) · 16.1 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
// 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 ClustererSpec.cxx
#include <vector>
#include "ITSMFTWorkflow/ClustererSpec.h"
#include "Framework/ControlService.h"
#include "Framework/ConfigParamRegistry.h"
#include "Framework/CCDBParamSpec.h"
#include "DataFormatsITSMFT/Digit.h"
#include "Framework/InputRecordWalker.h"
#include "ITSMFTReconstruction/ChipMappingMFT.h"
#include "ITSMFTReconstruction/ChipMappingITS.h"
#include "DataFormatsITSMFT/CompCluster.h"
#include "DataFormatsITSMFT/TopologyDictionary.h"
#include "SimulationDataFormat/MCCompLabel.h"
#include "SimulationDataFormat/ConstMCTruthContainer.h"
#include "DataFormatsITSMFT/ROFRecord.h"
#include "DataFormatsParameters/GRPObject.h"
#include "ITSMFTReconstruction/DigitPixelReader.h"
#include "DetectorsBase/GeometryManager.h"
#include "ITSMFTBase/DPLAlpideParam.h"
#include "CommonConstants/LHCConstants.h"
#include "DetectorsCommonDataFormats/DetectorNameConf.h"
#include "ITSMFTReconstruction/ClustererParam.h"
namespace o2::itsmft
{
template <int N>
void ClustererDPL<N>::init(InitContext& ic)
{
mClusterer = std::make_unique<o2::itsmft::Clusterer>();
mClusterer->setNChips((N == o2::detectors::DetID::ITS) ? o2::itsmft::ChipMappingITS::getNChips() : o2::itsmft::ChipMappingMFT::getNChips());
mUseClusterDictionary = !ic.options().get<bool>("ignore-cluster-dictionary");
o2::base::GRPGeomHelper::instance().setRequest(mGGCCDBRequest);
mNThreads = std::max(1, ic.options().get<int>("nthreads"));
mDetName = Origin.as<std::string>();
// prepare data filter
for (int iLayer = 0; iLayer < NLayers; ++iLayer) {
mFilter.emplace_back("digits", Origin, "DIGITS", iLayer, Lifetime::Timeframe);
mFilter.emplace_back("ROframe", Origin, "DIGITSROF", iLayer, Lifetime::Timeframe);
if (mUseMC) {
mFilter.emplace_back("labels", Origin, "DIGITSMCTR", iLayer, Lifetime::Timeframe);
mFilter.emplace_back("MC2ROframes", Origin, "DIGITSMC2ROF", iLayer, Lifetime::Timeframe);
}
}
}
template <int N>
void ClustererDPL<N>::run(ProcessingContext& pc)
{
updateTimeDependentParams(pc);
// filter input and compose
std::array<gsl::span<const o2::itsmft::Digit>, NLayers> digits;
std::array<gsl::span<const o2::itsmft::ROFRecord>, NLayers> rofs;
std::array<gsl::span<const char>, NLayers> labelsbuffer;
std::array<gsl::span<const o2::itsmft::MC2ROFRecord>, NLayers> mc2rofs;
for (const DataRef& ref : InputRecordWalker{pc.inputs(), mFilter}) {
auto const* dh = DataRefUtils::getHeader<o2::header::DataHeader*>(ref);
if (DataRefUtils::match(ref, {"digits", ConcreteDataTypeMatcher{Origin, "DIGITS"}})) {
digits[dh->subSpecification] = pc.inputs().get<gsl::span<o2::itsmft::Digit>>(ref);
}
if (DataRefUtils::match(ref, {"ROframe", ConcreteDataTypeMatcher{Origin, "DIGITSROF"}})) {
rofs[dh->subSpecification] = pc.inputs().get<gsl::span<o2::itsmft::ROFRecord>>(ref);
}
if (DataRefUtils::match(ref, {"labels", ConcreteDataTypeMatcher{Origin, "DIGITSMCTR"}})) {
labelsbuffer[dh->subSpecification] = pc.inputs().get<gsl::span<char>>(ref);
}
if (DataRefUtils::match(ref, {"MC2ROframes", ConcreteDataTypeMatcher{Origin, "DIGITSMC2ROF"}})) {
mc2rofs[dh->subSpecification] = pc.inputs().get<gsl::span<o2::itsmft::MC2ROFRecord>>(ref);
}
}
// query the first orbit in this TF
const auto firstTForbit = pc.services().get<o2::framework::TimingInfo>().firstTForbit;
const o2::InteractionRecord firstIR(0, firstTForbit);
const auto& par = DPLAlpideParam<N>::Instance();
// process received inputs
uint64_t nClusters{0};
TStopwatch sw;
o2::itsmft::DigitPixelReader reader;
for (uint32_t iLayer{0}; iLayer < NLayers; ++iLayer) {
int layer = (DPLAlpideParam<N>::supportsStaggering()) ? iLayer : -1;
sw.Start();
LOG(info) << mDetName << "Clusterer:" << layer << " pulled " << digits[iLayer].size() << " digits, in " << rofs[iLayer].size() << " RO frames";
mClusterer->setMaxROFDepthToSquash(mClusterer->getMaxROFDepthToSquash(layer));
o2::dataformats::ConstMCTruthContainerView<o2::MCCompLabel> labels(labelsbuffer[iLayer]);
reader.setSquashingDepth(mClusterer->getMaxROFDepthToSquash(layer));
reader.setSquashingDist(mClusterer->getMaxRowColDiffToMask()); // Sharing same parameter/logic with masking
reader.setMaxBCSeparationToSquash(mClusterer->getMaxBCSeparationToSquash(layer));
reader.setDigits(digits[iLayer]);
reader.setROFRecords(rofs[iLayer]);
if (mUseMC) {
reader.setMC2ROFRecords(mc2rofs[iLayer]);
LOG(info) << mDetName << "Clusterer:" << layer << " pulled " << labels.getNElements() << " labels ";
reader.setDigitsMCTruth(labels.getIndexedSize() > 0 ? &labels : nullptr);
}
reader.init();
std::vector<o2::itsmft::CompClusterExt> clusCompVec;
std::vector<o2::itsmft::ROFRecord> clusROFVec;
std::vector<unsigned char> clusPattVec;
std::unique_ptr<o2::dataformats::MCTruthContainer<o2::MCCompLabel>> clusterLabels;
if (mUseMC) {
clusterLabels = std::make_unique<o2::dataformats::MCTruthContainer<o2::MCCompLabel>>();
}
mClusterer->process(mNThreads, reader, &clusCompVec, &clusPattVec, &clusROFVec, clusterLabels.get());
// ensure that the rof output is continuous
size_t nROFs = clusROFVec.size();
const int nROFsPerOrbit = o2::constants::lhc::LHCMaxBunches / par.getROFLengthInBC(iLayer);
const int nROFsTF = nROFsPerOrbit * o2::base::GRPGeomHelper::getNHBFPerTF();
// It can happen that in the digitization rofs without contributing hits are skipped or there are stray ROFs
// We will preserve the clusters as they are but the stray ROFs will be removed (leaving their clusters unaddressed).
std::vector<o2::itsmft::ROFRecord> expClusRofVec(nROFsTF);
for (int iROF{0}; iROF < nROFsTF; ++iROF) {
auto& rof = expClusRofVec[iROF];
int orb = iROF * par.getROFLengthInBC(iLayer) / o2::constants::lhc::LHCMaxBunches + firstTForbit;
int bc = iROF * par.getROFLengthInBC(iLayer) % o2::constants::lhc::LHCMaxBunches;
o2::InteractionRecord ir(bc, orb);
rof.setBCData(ir);
rof.setROFrame(iROF);
rof.setNEntries(0);
rof.setFirstEntry(-1);
}
uint32_t prevEntry{0};
for (const auto& rof : clusROFVec) {
const auto& ir = rof.getBCData();
if (ir < firstIR) {
LOGP(warn, "Discard ROF {} preceding TF 1st orbit {}, layer:{}", ir.asString(), firstTForbit, iLayer);
continue;
}
const auto irToFirst = ir - firstIR;
const long irROF = irToFirst.toLong() / par.getROFLengthInBC(iLayer);
if (irROF >= nROFsTF) {
LOGP(warn, "Discard ROF {} exceding TF orbit range, layer:{}", ir.asString(), iLayer);
continue;
}
auto& expROF = expClusRofVec[irROF];
if (expROF.getNEntries() == 0) {
expROF.setFirstEntry(rof.getFirstEntry());
expROF.setNEntries(rof.getNEntries());
} else {
if (expROF.getNEntries() < rof.getNEntries()) {
LOGP(warn, "Repeating ROF {} with {} clusters, prefer to already processed instance with {} clusters", rof.asString(), rof.getNEntries(), expROF.getNEntries());
expROF.setFirstEntry(rof.getFirstEntry());
expROF.setNEntries(rof.getNEntries());
} else {
LOGP(warn, "Repeating ROF {} with {} clusters, discard preferring already processed instance with {} clusters", rof.asString(), rof.getNEntries(), expROF.getNEntries());
}
}
}
int prevLast{0};
for (auto& rof : expClusRofVec) {
if (rof.getFirstEntry() < 0) {
rof.setFirstEntry(prevLast);
}
prevLast = rof.getFirstEntry() + rof.getNEntries();
}
nROFs = expClusRofVec.size();
pc.outputs().snapshot(Output{Origin, "CLUSTERSROF", iLayer}, expClusRofVec);
pc.outputs().snapshot(Output{Origin, "COMPCLUSTERS", iLayer}, clusCompVec);
pc.outputs().snapshot(Output{Origin, "PATTERNS", iLayer}, clusPattVec);
nClusters += clusCompVec.size();
if (mUseMC) {
pc.outputs().snapshot(Output{Origin, "CLUSTERSMCTR", iLayer}, *clusterLabels); // at the moment requires snapshot
std::vector<o2::itsmft::MC2ROFRecord> clusterMC2ROframes(mc2rofs[iLayer].size());
for (int i = mc2rofs[iLayer].size(); i--;) {
clusterMC2ROframes[i] = mc2rofs[iLayer][i]; // Simply, replicate it from digits ?
}
pc.outputs().snapshot(Output{Origin, "CLUSTERSMC2ROF", iLayer}, clusterMC2ROframes);
}
reader.reset();
// TODO: in principle, after masking "overflow" pixels the MC2ROFRecord maxROF supposed to change, nominally to minROF
// -> consider recalculationg maxROF
sw.Stop();
LOG(info) << mDetName << "Clusterer:" << layer << " pushed " << clusCompVec.size() << " clusters, in " << nROFs << " RO frames in " << sw.RealTime() << " s";
}
LOG(info) << mDetName << "Clusterer produced " << nClusters << " clusters";
}
///_______________________________________
template <int N>
void ClustererDPL<N>::updateTimeDependentParams(ProcessingContext& pc)
{
o2::base::GRPGeomHelper::instance().checkUpdates(pc);
static bool initOnceDone = false;
if (!initOnceDone) { // this params need to be queried only once
initOnceDone = true;
pc.inputs().get<TopologyDictionary*>("cldict"); // just to trigger the finaliseCCDB
pc.inputs().get<o2::itsmft::DPLAlpideParam<N>*>("alppar");
pc.inputs().get<o2::itsmft::ClustererParam<N>*>("cluspar");
mClusterer->setContinuousReadOut(true);
// settings for the fired pixel overflow masking
const auto& alpParams = o2::itsmft::DPLAlpideParam<N>::Instance();
const auto& clParams = o2::itsmft::ClustererParam<N>::Instance();
if (clParams.maxBCDiffToMaskBias > 0 && clParams.maxBCDiffToSquashBias > 0) {
LOGP(fatal, "maxBCDiffToMaskBias = {} and maxBCDiffToSquashBias = {} cannot be set at the same time. Either set masking or squashing with a BCDiff > 0", clParams.maxBCDiffToMaskBias, clParams.maxBCDiffToSquashBias);
}
mClusterer->setDropHugeClusters(clParams.dropHugeClusters);
auto nbc = clParams.maxBCDiffToMaskBias;
nbc += mClusterer->isContinuousReadOut() ? alpParams.roFrameLengthInBC : (alpParams.roFrameLengthTrig / o2::constants::lhc::LHCBunchSpacingNS);
mClusterer->setMaxBCSeparationToMask(nbc);
mClusterer->setMaxRowColDiffToMask(clParams.maxRowColDiffToMask);
// Squasher
int rofBC = mClusterer->isContinuousReadOut() ? alpParams.roFrameLengthInBC : (alpParams.roFrameLengthTrig / o2::constants::lhc::LHCBunchSpacingNS); // ROF length in BC
mClusterer->setMaxBCSeparationToSquash(rofBC + clParams.maxBCDiffToSquashBias);
int nROFsToSquash = 0; // squashing disabled if no reset due to maxSOTMUS>0.
if (clParams.maxSOTMUS > 0 && rofBC > 0) {
nROFsToSquash = 2 + int(clParams.maxSOTMUS / (rofBC * o2::constants::lhc::LHCBunchSpacingMUS)); // use squashing
}
mClusterer->setMaxROFDepthToSquash(nROFsToSquash);
if constexpr (DPLAlpideParam<N>::supportsStaggering()) {
if (mClusterer->isContinuousReadOut()) {
for (int iLayer{0}; iLayer < NLayers; ++iLayer) {
mClusterer->addMaxBCSeparationToSquash(alpParams.getROFLengthInBC(iLayer) + clParams.getMaxBCDiffToSquashBias(iLayer));
mClusterer->addMaxROFDepthToSquash((clParams.getMaxBCDiffToSquashBias(iLayer) > 0) ? 2 + int(clParams.maxSOTMUS / (alpParams.getROFLengthInBC(iLayer) * o2::constants::lhc::LHCBunchSpacingMUS)) : 0);
}
}
}
mClusterer->print(false);
}
// we may have other params which need to be queried regularly
}
///_______________________________________
template <int N>
void ClustererDPL<N>::finaliseCCDB(ConcreteDataMatcher& matcher, void* obj)
{
if (o2::base::GRPGeomHelper::instance().finaliseCCDB(matcher, obj)) {
return;
}
if (matcher == ConcreteDataMatcher(Origin, "CLUSDICT", 0)) {
LOG(info) << "cluster dictionary updated" << (!mUseClusterDictionary ? " but its using is disabled" : "");
if (mUseClusterDictionary) {
mClusterer->setDictionary((const TopologyDictionary*)obj);
}
return;
}
// Note: strictly speaking, for Configurable params we don't need finaliseCCDB check, the singletons are updated at the CCDB fetcher level
if (matcher == ConcreteDataMatcher(Origin, "ALPIDEPARAM", 0)) {
LOG(info) << "Alpide param updated";
const auto& par = o2::itsmft::DPLAlpideParam<N>::Instance();
par.printKeyValues();
return;
}
if (matcher == ConcreteDataMatcher(Origin, "CLUSPARAM", 0)) {
LOG(info) << "Cluster param updated";
const auto& par = o2::itsmft::ClustererParam<N>::Instance();
par.printKeyValues();
return;
}
}
namespace
{
template <int N>
DataProcessorSpec getClustererSpec(bool useMC)
{
constexpr o2::header::DataOrigin Origin{N == o2::detectors::DetID::ITS ? o2::header::gDataOriginITS : o2::header::gDataOriginMFT};
std::vector<InputSpec> inputs;
constexpr uint32_t nLayers = (DPLAlpideParam<N>::supportsStaggering()) ? DPLAlpideParam<N>::getNLayers() : 1;
for (uint32_t iLayer = 0; iLayer < nLayers; ++iLayer) {
inputs.emplace_back("digits", Origin, "DIGITS", iLayer, Lifetime::Timeframe);
inputs.emplace_back("ROframes", Origin, "DIGITSROF", iLayer, Lifetime::Timeframe);
if (useMC) {
inputs.emplace_back("labels", Origin, "DIGITSMCTR", iLayer, Lifetime::Timeframe);
inputs.emplace_back("MC2ROframes", Origin, "DIGITSMC2ROF", iLayer, Lifetime::Timeframe);
}
}
inputs.emplace_back("cldict", Origin, "CLUSDICT", 0, Lifetime::Condition, ccdbParamSpec(Origin.as<std::string>() + "/Calib/ClusterDictionary"));
inputs.emplace_back("cluspar", Origin, "CLUSPARAM", 0, Lifetime::Condition, ccdbParamSpec(Origin.as<std::string>() + "/Config/ClustererParam"));
inputs.emplace_back("alppar", Origin, "ALPIDEPARAM", 0, Lifetime::Condition, ccdbParamSpec(Origin.as<std::string>() + "/Config/AlpideParam"));
auto ggRequest = std::make_shared<o2::base::GRPGeomRequest>(false, // orbitResetTime
false, // GRPECS=false
false, // GRPLHCIF
false, // GRPMagField
false, // askMatLUT
o2::base::GRPGeomRequest::None, // geometry
inputs,
true);
std::vector<OutputSpec> outputs;
for (uint32_t iLayer = 0; iLayer < nLayers; ++iLayer) {
outputs.emplace_back(Origin, "COMPCLUSTERS", iLayer, Lifetime::Timeframe);
outputs.emplace_back(Origin, "PATTERNS", iLayer, Lifetime::Timeframe);
outputs.emplace_back(Origin, "CLUSTERSROF", iLayer, Lifetime::Timeframe);
if (useMC) {
outputs.emplace_back(Origin, "CLUSTERSMCTR", iLayer, Lifetime::Timeframe);
outputs.emplace_back(Origin, "CLUSTERSMC2ROF", iLayer, Lifetime::Timeframe);
}
}
return DataProcessorSpec{
.name = (N == o2::detectors::DetID::ITS) ? "its-clusterer" : "mft-clusterer",
.inputs = inputs,
.outputs = outputs,
.algorithm = AlgorithmSpec{adaptFromTask<ClustererDPL<N>>(ggRequest, useMC)},
.options = Options{
{"ignore-cluster-dictionary", VariantType::Bool, false, {"do not use cluster dictionary, always store explicit patterns"}},
{"nthreads", VariantType::Int, 1, {"Number of clustering threads"}}}};
}
} // namespace
framework::DataProcessorSpec getITSClustererSpec(bool useMC)
{
return getClustererSpec<o2::detectors::DetID::ITS>(useMC);
}
framework::DataProcessorSpec getMFTClustererSpec(bool useMC)
{
return getClustererSpec<o2::detectors::DetID::MFT>(useMC);
}
} // namespace o2::itsmft