forked from AliceO2Group/AliceO2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathITSMFTDigitizerSpec.cxx
More file actions
412 lines (372 loc) · 19.2 KB
/
ITSMFTDigitizerSpec.cxx
File metadata and controls
412 lines (372 loc) · 19.2 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
// 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 "ITSMFTDigitizerSpec.h"
#include "Framework/ControlService.h"
#include "Framework/ConfigParamRegistry.h"
#include "Framework/DataProcessorSpec.h"
#include "Framework/DataRefUtils.h"
#include "Framework/Lifetime.h"
#include "Framework/Task.h"
#include "Framework/CCDBParamSpec.h"
#include "Steer/HitProcessingManager.h" // for DigitizationContext
#include "DataFormatsITSMFT/Digit.h"
#include "DataFormatsITSMFT/NoiseMap.h"
#include "DataFormatsITSMFT/TimeDeadMap.h"
#include "SimulationDataFormat/ConstMCTruthContainer.h"
#include "DetectorsBase/BaseDPLDigitizer.h"
#include "DetectorsCommonDataFormats/DetID.h"
#include "DetectorsCommonDataFormats/SimTraits.h"
#include "DetectorsCommonDataFormats/DetectorNameConf.h"
#include "DataFormatsParameters/GRPObject.h"
#include "DataFormatsITSMFT/ROFRecord.h"
#include "ITSMFTSimulation/Digitizer.h"
#include "ITSMFTSimulation/DPLDigitizerParam.h"
#include "ITSMFTBase/DPLAlpideParam.h"
#include "ITSBase/GeometryTGeo.h"
#include "MFTBase/GeometryTGeo.h"
#include <TChain.h>
#include <TStopwatch.h>
#include <string>
using namespace o2::framework;
using SubSpecificationType = o2::framework::DataAllocator::SubSpecificationType;
namespace o2
{
namespace itsmft
{
using namespace o2::base;
class ITSMFTDPLDigitizerTask : BaseDPLDigitizer
{
public:
using BaseDPLDigitizer::init;
void initDigitizerTask(framework::InitContext& ic) override
{
mDisableQED = ic.options().get<bool>("disable-qed");
}
void run(framework::ProcessingContext& pc)
{
if (mFinished) {
return;
}
mFirstOrbitTF = pc.services().get<o2::framework::TimingInfo>().firstTForbit;
mID == o2::detectors::DetID::ITS ? updateTimeDependentParams<o2::detectors::DetID::ITS>(pc) : updateTimeDependentParams<o2::detectors::DetID::MFT>(pc);
std::string detStr = mID.getName();
// read collision context from input
auto context = pc.inputs().get<o2::steer::DigitizationContext*>("collisioncontext");
context->initSimChains(mID, mSimChains);
const bool withQED = context->isQEDProvided() && !mDisableQED;
auto& timesview = context->getEventRecords(withQED);
LOG(info) << "GOT " << timesview.size() << " COLLISSION TIMES";
LOG(info) << "SIMCHAINS " << mSimChains.size();
// if there is nothing to do ... return
if (timesview.size() == 0) {
return;
}
TStopwatch timer;
timer.Start();
LOG(info) << " CALLING ITS DIGITIZATION ";
mDigitizer.setDigits(&mDigits);
mDigitizer.setROFRecords(&mROFRecords);
mDigitizer.setMCLabels(&mLabels);
// digits are directly put into DPL owned resource
auto& digitsAccum = pc.outputs().make<std::vector<itsmft::Digit>>(Output{mOrigin, "DIGITS", 0});
auto accumulate = [this, &digitsAccum]() {
// accumulate result of single event processing, called after processing every event supplied
// AND after the final flushing via digitizer::fillOutputContainer
if (!mDigits.size()) {
return; // no digits were flushed, nothing to accumulate
}
auto ndigAcc = digitsAccum.size();
std::copy(mDigits.begin(), mDigits.end(), std::back_inserter(digitsAccum));
// fix ROFrecords references on ROF entries
auto nROFRecsOld = mROFRecordsAccum.size();
for (int i = 0; i < mROFRecords.size(); i++) {
auto& rof = mROFRecords[i];
rof.setFirstEntry(ndigAcc + rof.getFirstEntry());
rof.print();
if (mFixMC2ROF < mMC2ROFRecordsAccum.size()) { // fix ROFRecord entry in MC2ROF records
for (int m2rid = mFixMC2ROF; m2rid < mMC2ROFRecordsAccum.size(); m2rid++) {
// need to register the ROFRecors entry for MC event starting from this entry
auto& mc2rof = mMC2ROFRecordsAccum[m2rid];
if (rof.getROFrame() == mc2rof.minROF) {
mFixMC2ROF++;
mc2rof.rofRecordID = nROFRecsOld + i;
mc2rof.print();
}
}
}
}
std::copy(mROFRecords.begin(), mROFRecords.end(), std::back_inserter(mROFRecordsAccum));
if (mWithMCTruth) {
mLabelsAccum.mergeAtBack(mLabels);
}
LOG(info) << "Added " << mDigits.size() << " digits ";
// clean containers from already accumulated stuff
mLabels.clear();
mDigits.clear();
mROFRecords.clear();
}; // and accumulate lambda
auto& eventParts = context->getEventParts(withQED);
int bcShift = mDigitizer.getParams().getROFrameBiasInBC();
// loop over all composite collisions given from context (aka loop over all the interaction records)
for (int collID = 0; collID < timesview.size(); ++collID) {
auto irt = timesview[collID];
if (irt.toLong() < bcShift) { // due to the ROF misalignment the collision would go to negative ROF ID, discard
continue;
}
irt -= bcShift; // account for the ROF start shift
mDigitizer.setEventTime(irt);
mDigitizer.resetEventROFrames(); // to estimate min/max ROF for this collID
// for each collision, loop over the constituents event and source IDs
// (background signal merging is basically taking place here)
for (auto& part : eventParts[collID]) {
// get the hits for this event and this source
mHits.clear();
context->retrieveHits(mSimChains, o2::detectors::SimTraits::DETECTORBRANCHNAMES[mID][0].c_str(), part.sourceID, part.entryID, &mHits);
if (mHits.size() > 0) {
LOG(debug) << "For collision " << collID << " eventID " << part.entryID
<< " found " << mHits.size() << " hits ";
mDigitizer.process(&mHits, part.entryID, part.sourceID); // call actual digitization procedure
}
}
mMC2ROFRecordsAccum.emplace_back(collID, -1, mDigitizer.getEventROFrameMin(), mDigitizer.getEventROFrameMax());
accumulate();
}
mDigitizer.fillOutputContainer();
accumulate();
// here we have all digits and labels and we can send them to consumer (aka snapshot it onto output)
pc.outputs().snapshot(Output{mOrigin, "DIGITSROF", 0}, mROFRecordsAccum);
if (mWithMCTruth) {
pc.outputs().snapshot(Output{mOrigin, "DIGITSMC2ROF", 0}, mMC2ROFRecordsAccum);
auto& sharedlabels = pc.outputs().make<o2::dataformats::ConstMCTruthContainer<o2::MCCompLabel>>(Output{mOrigin, "DIGITSMCTR", 0});
mLabelsAccum.flatten_to(sharedlabels);
// free space of existing label containers
mLabels.clear_andfreememory();
mLabelsAccum.clear_andfreememory();
}
LOG(info) << mID.getName() << ": Sending ROMode= " << mROMode << " to GRPUpdater";
pc.outputs().snapshot(Output{mOrigin, "ROMode", 0}, mROMode);
timer.Stop();
LOG(info) << "Digitization took " << timer.CpuTime() << "s";
// we should be only called once; tell DPL that this process is ready to exit
pc.services().get<ControlService>().readyToQuit(QuitRequest::Me);
mFinished = true;
}
void finaliseCCDB(ConcreteDataMatcher& matcher, void* obj)
{
if (matcher == ConcreteDataMatcher(mOrigin, "NOISEMAP", 0)) {
LOG(info) << mID.getName() << " noise map updated";
mDigitizer.setNoiseMap((const o2::itsmft::NoiseMap*)obj);
return;
}
if (matcher == ConcreteDataMatcher(mOrigin, "DEADMAP", 0)) {
LOG(info) << mID.getName() << " static dead map updated";
mDeadMap = (o2::itsmft::NoiseMap*)obj;
mDigitizer.setDeadChannelsMap(mDeadMap);
return;
}
if (matcher == ConcreteDataMatcher(mOrigin, "TimeDeadMap", 0)) {
o2::itsmft::TimeDeadMap* timedeadmap = (o2::itsmft::TimeDeadMap*)obj;
if (!timedeadmap->isDefault()) {
timedeadmap->decodeMap(mFirstOrbitTF, *mDeadMap, true);
if (mTimeDeadMapUpdated) {
LOGP(fatal, "Attempt to add time-dependent map to already modified static map");
}
mTimeDeadMapUpdated = true;
mDigitizer.setDeadChannelsMap(mDeadMap);
LOG(info) << mID.getName() << " time-dependent dead map updated";
} else {
LOG(info) << mID.getName() << " time-dependent dead map is default/empty";
}
return;
}
if (matcher == ConcreteDataMatcher(mOrigin, "ALPIDEPARAM", 0)) {
LOG(info) << mID.getName() << " Alpide param updated";
if (mID == o2::detectors::DetID::ITS) {
const auto& par = o2::itsmft::DPLAlpideParam<o2::detectors::DetID::ITS>::Instance();
par.printKeyValues();
} else {
const auto& par = o2::itsmft::DPLAlpideParam<o2::detectors::DetID::MFT>::Instance();
par.printKeyValues();
}
return;
}
if (matcher == ConcreteDataMatcher(mOrigin, "ALPIDERESPVbb0", 0)) {
LOG(info) << mID.getName() << " loaded AlpideResponseData for Vbb=0V";
mDigitizer.setAlpideResponse((o2::itsmft::AlpideSimResponse*)obj, 0);
}
if (matcher == ConcreteDataMatcher(mOrigin, "ALPIDERESPVbbM3", 0)) {
LOG(info) << mID.getName() << " loaded AlpideResponseData for Vbb=-3V";
mDigitizer.setAlpideResponse((o2::itsmft::AlpideSimResponse*)obj, 1);
}
}
protected:
ITSMFTDPLDigitizerTask(bool mctruth = true) : BaseDPLDigitizer(InitServices::FIELD | InitServices::GEOM), mWithMCTruth(mctruth) {}
template <int DETID>
void updateTimeDependentParams(ProcessingContext& pc)
{
std::string detstr(o2::detectors::DetID::getName(DETID));
pc.inputs().get<o2::itsmft::NoiseMap*>(detstr + "_noise");
pc.inputs().get<o2::itsmft::NoiseMap*>(detstr + "_dead");
// TODO: the code should run even if this object does not exist. Or: create default object
pc.inputs().get<o2::itsmft::TimeDeadMap*>(detstr + "_time_dead");
pc.inputs().get<o2::itsmft::DPLAlpideParam<DETID>*>(detstr + "_alppar");
pc.inputs().get<o2::itsmft::AlpideSimResponse*>(detstr + "_alpiderespvbb0");
pc.inputs().get<o2::itsmft::AlpideSimResponse*>(detstr + "_alpiderespvbbm3");
auto& dopt = o2::itsmft::DPLDigitizerParam<DETID>::Instance();
auto& aopt = o2::itsmft::DPLAlpideParam<DETID>::Instance();
auto& digipar = mDigitizer.getParams();
digipar.setContinuous(dopt.continuous);
digipar.setROFrameBiasInBC(aopt.roFrameBiasInBC);
if (dopt.continuous) {
auto frameNS = aopt.roFrameLengthInBC * o2::constants::lhc::LHCBunchSpacingNS;
digipar.setROFrameLengthInBC(aopt.roFrameLengthInBC);
digipar.setROFrameLength(frameNS); // RO frame in ns
digipar.setStrobeDelay(aopt.strobeDelay); // Strobe delay wrt beginning of the RO frame, in ns
digipar.setStrobeLength(aopt.strobeLengthCont > 0 ? aopt.strobeLengthCont : frameNS - aopt.strobeDelay); // Strobe length in ns
} else {
digipar.setROFrameLength(aopt.roFrameLengthTrig); // RO frame in ns
digipar.setStrobeDelay(aopt.strobeDelay); // Strobe delay wrt beginning of the RO frame, in ns
digipar.setStrobeLength(aopt.strobeLengthTrig); // Strobe length in ns
}
// parameters of signal time response: flat-top duration, max rise time and q @ which rise time is 0
digipar.getSignalShape().setParameters(dopt.strobeFlatTop, dopt.strobeMaxRiseTime, dopt.strobeQRiseTime0);
digipar.setChargeThreshold(dopt.chargeThreshold); // charge threshold in electrons
digipar.setNoisePerPixel(dopt.noisePerPixel); // noise level
digipar.setTimeOffset(dopt.timeOffset);
digipar.setNSimSteps(dopt.nSimSteps);
digipar.setIBVbb(dopt.IBVbb);
digipar.setOBVbb(dopt.OBVbb);
digipar.setVbb(dopt.Vbb);
mROMode = digipar.isContinuous() ? o2::parameters::GRPObject::CONTINUOUS : o2::parameters::GRPObject::PRESENT;
LOG(info) << mID.getName() << " simulated in "
<< ((mROMode == o2::parameters::GRPObject::CONTINUOUS) ? "CONTINUOUS" : "TRIGGERED")
<< " RO mode";
// configure digitizer
o2::itsmft::GeometryTGeo* geom = nullptr;
if (mID == o2::detectors::DetID::ITS) {
geom = o2::its::GeometryTGeo::Instance();
} else {
geom = o2::mft::GeometryTGeo::Instance();
}
geom->fillMatrixCache(o2::math_utils::bit2Mask(o2::math_utils::TransformType::L2G)); // make sure L2G matrices are loaded
mDigitizer.setGeometry(geom);
mDigitizer.init();
}
bool mWithMCTruth = true;
bool mFinished = false;
bool mDisableQED = false;
unsigned long mFirstOrbitTF = 0x0;
o2::detectors::DetID mID;
o2::header::DataOrigin mOrigin = o2::header::gDataOriginInvalid;
o2::itsmft::Digitizer mDigitizer;
std::vector<o2::itsmft::Digit> mDigits;
std::vector<o2::itsmft::ROFRecord> mROFRecords;
std::vector<o2::itsmft::ROFRecord> mROFRecordsAccum;
std::vector<o2::itsmft::Hit> mHits;
std::vector<o2::itsmft::Hit>* mHitsP = &mHits;
o2::dataformats::MCTruthContainer<o2::MCCompLabel> mLabels;
o2::dataformats::MCTruthContainer<o2::MCCompLabel> mLabelsAccum;
std::vector<o2::itsmft::MC2ROFRecord> mMC2ROFRecordsAccum;
std::vector<TChain*> mSimChains;
o2::itsmft::NoiseMap* mDeadMap = nullptr;
int mFixMC2ROF = 0; // 1st entry in mc2rofRecordsAccum to be fixed for ROFRecordID
bool mTimeDeadMapUpdated = false;
o2::parameters::GRPObject::ROMode mROMode = o2::parameters::GRPObject::PRESENT; // readout mode
};
//_______________________________________________
class ITSDPLDigitizerTask : public ITSMFTDPLDigitizerTask
{
public:
// FIXME: origin should be extractable from the DetID, the problem is 3d party header dependencies
static constexpr o2::detectors::DetID::ID DETID = o2::detectors::DetID::ITS;
static constexpr o2::header::DataOrigin DETOR = o2::header::gDataOriginITS;
ITSDPLDigitizerTask(bool mctruth = true) : ITSMFTDPLDigitizerTask(mctruth)
{
mID = DETID;
mOrigin = DETOR;
}
};
constexpr o2::detectors::DetID::ID ITSDPLDigitizerTask::DETID;
constexpr o2::header::DataOrigin ITSDPLDigitizerTask::DETOR;
//_______________________________________________
class MFTDPLDigitizerTask : public ITSMFTDPLDigitizerTask
{
public:
// FIXME: origina should be extractable from the DetID, the problem is 3d party header dependencies
static constexpr o2::detectors::DetID::ID DETID = o2::detectors::DetID::MFT;
static constexpr o2::header::DataOrigin DETOR = o2::header::gDataOriginMFT;
MFTDPLDigitizerTask(bool mctruth) : ITSMFTDPLDigitizerTask(mctruth)
{
mID = DETID;
mOrigin = DETOR;
}
};
constexpr o2::detectors::DetID::ID MFTDPLDigitizerTask::DETID;
constexpr o2::header::DataOrigin MFTDPLDigitizerTask::DETOR;
std::vector<OutputSpec> makeOutChannels(o2::header::DataOrigin detOrig, bool mctruth)
{
std::vector<OutputSpec> outputs;
outputs.emplace_back(detOrig, "DIGITS", 0, Lifetime::Timeframe);
outputs.emplace_back(detOrig, "DIGITSROF", 0, Lifetime::Timeframe);
if (mctruth) {
outputs.emplace_back(detOrig, "DIGITSMC2ROF", 0, Lifetime::Timeframe);
outputs.emplace_back(detOrig, "DIGITSMCTR", 0, Lifetime::Timeframe);
}
outputs.emplace_back(detOrig, "ROMode", 0, Lifetime::Timeframe);
return outputs;
}
DataProcessorSpec getITSDigitizerSpec(int channel, bool mctruth)
{
std::string detStr = o2::detectors::DetID::getName(ITSDPLDigitizerTask::DETID);
auto detOrig = ITSDPLDigitizerTask::DETOR;
std::stringstream parHelper;
parHelper << "Params as " << o2::itsmft::DPLDigitizerParam<ITSDPLDigitizerTask::DETID>::getParamName().data() << ".<param>=value;... with"
<< o2::itsmft::DPLDigitizerParam<ITSDPLDigitizerTask::DETID>::Instance()
<< "\n or " << o2::itsmft::DPLAlpideParam<ITSDPLDigitizerTask::DETID>::getParamName().data() << ".<param>=value;... with"
<< o2::itsmft::DPLAlpideParam<ITSDPLDigitizerTask::DETID>::Instance();
std::vector<InputSpec> inputs;
inputs.emplace_back("collisioncontext", "SIM", "COLLISIONCONTEXT", static_cast<SubSpecificationType>(channel), Lifetime::Timeframe);
inputs.emplace_back("ITS_noise", "ITS", "NOISEMAP", 0, Lifetime::Condition, ccdbParamSpec("ITS/Calib/NoiseMap"));
inputs.emplace_back("ITS_dead", "ITS", "DEADMAP", 0, Lifetime::Condition, ccdbParamSpec("ITS/Calib/DeadMap"));
inputs.emplace_back("ITS_time_dead", "ITS", "TimeDeadMap", 0, Lifetime::Condition, ccdbParamSpec("ITS/Calib/TimeDeadMap"));
inputs.emplace_back("ITS_alppar", "ITS", "ALPIDEPARAM", 0, Lifetime::Condition, ccdbParamSpec("ITS/Config/AlpideParam"));
inputs.emplace_back("ITS_alpiderespvbb0", "ITS", "ALPIDERESPVbb0", 0, Lifetime::Condition, ccdbParamSpec("ITSMFT/Calib/ALPIDEResponseVbb0"));
inputs.emplace_back("ITS_alpiderespvbbm3", "ITS", "ALPIDERESPVbbM3", 0, Lifetime::Condition, ccdbParamSpec("ITSMFT/Calib/ALPIDEResponseVbbM3"));
return DataProcessorSpec{(detStr + "Digitizer").c_str(),
inputs, makeOutChannels(detOrig, mctruth),
AlgorithmSpec{adaptFromTask<ITSDPLDigitizerTask>(mctruth)},
Options{
{"disable-qed", o2::framework::VariantType::Bool, false, {"disable QED handling"}}}};
}
DataProcessorSpec getMFTDigitizerSpec(int channel, bool mctruth)
{
std::string detStr = o2::detectors::DetID::getName(MFTDPLDigitizerTask::DETID);
auto detOrig = MFTDPLDigitizerTask::DETOR;
std::stringstream parHelper;
std::vector<InputSpec> inputs;
inputs.emplace_back("collisioncontext", "SIM", "COLLISIONCONTEXT", static_cast<SubSpecificationType>(channel), Lifetime::Timeframe);
inputs.emplace_back("MFT_noise", "MFT", "NOISEMAP", 0, Lifetime::Condition, ccdbParamSpec("MFT/Calib/NoiseMap"));
inputs.emplace_back("MFT_dead", "MFT", "DEADMAP", 0, Lifetime::Condition, ccdbParamSpec("MFT/Calib/DeadMap"));
inputs.emplace_back("MFT_time_dead", "MFT", "TimeDeadMap", 0, Lifetime::Condition, ccdbParamSpec("MFT/Calib/TimeDeadMap"));
inputs.emplace_back("MFT_alppar", "MFT", "ALPIDEPARAM", 0, Lifetime::Condition, ccdbParamSpec("MFT/Config/AlpideParam"));
inputs.emplace_back("MFT_alpiderespvbb0", "MFT", "ALPIDERESPVbb0", 0, Lifetime::Condition, ccdbParamSpec("ITSMFT/Calib/ALPIDEResponseVbb0"));
inputs.emplace_back("MFT_alpiderespvbbm3", "MFT", "ALPIDERESPVbbM3", 0, Lifetime::Condition, ccdbParamSpec("ITSMFT/Calib/ALPIDEResponseVbbM3"));
parHelper << "Params as " << o2::itsmft::DPLDigitizerParam<ITSDPLDigitizerTask::DETID>::getParamName().data() << ".<param>=value;... with"
<< o2::itsmft::DPLDigitizerParam<ITSDPLDigitizerTask::DETID>::Instance()
<< " or " << o2::itsmft::DPLAlpideParam<ITSDPLDigitizerTask::DETID>::getParamName().data() << ".<param>=value;... with"
<< o2::itsmft::DPLAlpideParam<ITSDPLDigitizerTask::DETID>::Instance();
return DataProcessorSpec{(detStr + "Digitizer").c_str(),
inputs, makeOutChannels(detOrig, mctruth),
AlgorithmSpec{adaptFromTask<MFTDPLDigitizerTask>(mctruth)},
Options{{"disable-qed", o2::framework::VariantType::Bool, false, {"disable QED handling"}}}};
}
} // end namespace itsmft
} // end namespace o2