Project
Loading...
Searching...
No Matches
TPCDigitizerSpec.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
12#ifdef NDEBUG
13#undef NDEBUG
14#endif
15#include <cassert>
17#include "TPCDigitizerSpec.h"
24#include "Framework/Lifetime.h"
26#include "Headers/DataHeader.h"
27#include "TStopwatch.h"
28#include "Steer/HitProcessingManager.h" // for DigitizationContext
29#include "TChain.h"
33#include "Framework/Task.h"
47#include <TRandom.h>
48#include <filesystem>
50
51using namespace o2::framework;
55
56namespace o2
57{
58namespace tpc
59{
60
61template <typename T>
62void copyHelper(T const& origin, T& target)
63{
64 std::copy(origin.begin(), origin.end(), std::back_inserter(target));
65}
66template <>
67void copyHelper<o2::dataformats::MCTruthContainer<o2::MCCompLabel>>(o2::dataformats::MCTruthContainer<o2::MCCompLabel> const& origin, o2::dataformats::MCTruthContainer<o2::MCCompLabel>& target)
68{
69 target.mergeAtBack(origin);
70}
71
72template <typename T>
73void writeToBranchHelper(TTree& tree, const char* name, T* accum)
74{
75 auto targetbr = o2::base::getOrMakeBranch(tree, name, accum);
76 targetbr->Fill();
77 targetbr->ResetAddress();
78 targetbr->DropBaskets("all");
79}
80template <>
81void writeToBranchHelper<o2::dataformats::MCTruthContainer<o2::MCCompLabel>>(TTree& tree,
83{
84 // we convert first of all to IOMCTruthContainer
85 std::vector<char> buffer;
86 accum->flatten_to(buffer);
87 accum->clear_andfreememory();
89 auto targetbr = o2::base::getOrMakeBranch(tree, name, &view);
90 targetbr->Fill();
91 targetbr->ResetAddress();
92 targetbr->DropBaskets("all");
93}
94
95using namespace o2::base;
97{
98 public:
99 TPCDPLDigitizerTask(bool internalwriter, int distortionType) : mInternalWriter(internalwriter), BaseDPLDigitizer(InitServices::FIELD | InitServices::GEOM), mDistortionType(distortionType)
100 {
101 }
102
104 {
105 LOG(info) << "Initializing TPC digitization";
106
107 mLaneId = ic.services().get<const o2::framework::DeviceSpec>().rank;
108
110 auto triggeredMode = ic.options().get<bool>("TPCtriggered");
111 mConditions.init(ic, mDistortionType);
112
113 mDigitizer.setContinuousReadout(!triggeredMode);
114 mDigitizer.setDistortionScaleType(mDistortionType);
115
116 if (ic.options().get<bool>("tpc-sector-seeding")) {
117 const auto seed = o2::conf::DigiParams::Instance().seed;
118 if (seed == 0) {
119 LOG(warning) << "TPC sector seeding with DigiParams.seed=0 is not reproducible";
120 }
121 // with seed 0, gRandom was seeded randomly in the base class
122 mDigitizer.setUseSectorSeeding(seed != 0 ? seed : gRandom->Integer(kMaxUInt));
123 LOG(info) << "TPC: using sector seeding";
124 }
125
126 // we send the GRP data once if the corresponding output channel is available
127 // and set the flag to false after
128 mWriteGRP = true;
129
130 // clean up (possibly) existing digit files
131 if (mInternalWriter) {
133 }
134 }
135
137 {
138 // since we update digit files during ordinary processing
139 // it is better to remove possibly existing files in the same dir
140 std::stringstream tmp;
141 tmp << "tpc_driftime_digits_lane" << mLaneId << ".root";
142 if (std::filesystem::exists(tmp.str())) {
143 std::filesystem::remove(tmp.str());
144 }
145 }
146
148 {
149 if (!mInternalROOTFlushFile) {
150 std::stringstream tmp;
151 tmp << "tpc_driftime_digits_lane" << mLaneId << ".root";
152 mInternalROOTFlushFile = new TFile(tmp.str().c_str(), "UPDATE");
153 std::stringstream trname;
154 trname << mSector;
155 mInternalROOTFlushTTree = new TTree(trname.str().c_str(), "o2sim");
156 }
157 {
158 std::stringstream brname;
159 brname << "TPCDigit_" << mSector;
160 auto br = o2::base::getOrMakeBranch(*mInternalROOTFlushTTree, brname.str().c_str(), &mFlushBuffers.digits);
161 br->Fill();
162 br->ResetAddress();
163 }
164 if (mWithMCTruth) {
165 // labels
166 std::stringstream brname;
167 brname << "TPCDigitMCTruth_" << mSector;
168 auto br = o2::base::getOrMakeBranch(*mInternalROOTFlushTTree, brname.str().c_str(), &mFlushBuffers.labels);
169 br->Fill();
170 br->ResetAddress();
171 }
172 {
173 // common
174 std::stringstream brname;
175 brname << "TPCCommonMode_" << mSector;
176 auto br = o2::base::getOrMakeBranch(*mInternalROOTFlushTTree, brname.str().c_str(), &mFlushBuffers.commonMode);
177 br->Fill();
178 br->ResetAddress();
179 }
180 }
181
183 {
184 mConditions.finaliseCCDB(matcher, obj, mDigitizer);
185 }
186
188 {
189 LOG(info) << "Processing TPC digitization";
190
191 mConditions.update(pc, mDigitizer, [this](float vdrift, float tdriftOffset) {
192 mDigitizer.setVDrift(vdrift);
193 mDigitizer.setTDriftOffset(tdriftOffset);
194 });
195
196 for (auto it = pc.inputs().begin(), end = pc.inputs().end(); it != end; ++it) {
197 for (auto const& inputref : it.parts()) {
198 if (inputref.spec->lifetime == o2::framework::Lifetime::Condition) { // process does not need conditions
199 continue;
200 }
201 process(pc, inputref);
202 if (mInternalWriter) {
203 mInternalROOTFlushTTree->SetEntries(mFlushCounter);
204 mInternalROOTFlushFile->Write("", TObject::kOverwrite);
205 mInternalROOTFlushFile->Close();
206 // delete mInternalROOTFlushTTree; --> automatically done by ->Close()
207 delete mInternalROOTFlushFile;
208 mInternalROOTFlushFile = nullptr;
209 }
210 // TODO: make generic reset method?
211 mFlushCounter = 0;
212 }
213 }
214 }
215
216 // process one sector
218 {
219 // read collision context from input
220 auto context = pc.inputs().get<o2::steer::DigitizationContext*>(inputref);
221 context->initSimChains(o2::detectors::DetID::TPC, mSimChains);
222 auto& irecords = context->getEventRecords();
223 // A timeframe holds no collision at all whenever the interaction rate is low enough. Do not
224 // return here: the sector still has to produce its usual output, an empty one, so that the
225 // digit file has the same shape as an ordinary timeframe that happens to contain nothing.
226 // Returning also left the chunked writer without a file and a null tree to flush.
227 LOG(info) << "TPC: Processing " << irecords.size() << " collisions";
228 auto const* dh = DataRefUtils::getHeader<o2::header::DataHeader*>(inputref);
229
230 bool isContinuous = mDigitizer.isContinuousReadout();
231 // we publish the GRP data once if the output channel is there
232 if (mWriteGRP && pc.outputs().isAllowed({"TPC", "ROMode", 0})) {
234 LOG(info) << "TPC: Sending ROMode= " << (mDigitizer.isContinuousReadout() ? "Continuous" : "Triggered")
235 << " to GRPUpdater from channel " << dh->subSpecification;
236 pc.outputs().snapshot(Output{"TPC", "ROMode", 0}, roMode);
237 }
238 mWriteGRP = false;
239
240 // extract which sector to treat
241 auto const* sectorHeader = DataRefUtils::getHeader<TPCSectorHeader*>(inputref);
242 if (sectorHeader == nullptr) {
243 LOG(error) << "TPC: Sector header missing, skipping processing";
244 return;
245 }
246 auto sector = sectorHeader->sector();
247 mSector = sector;
248 mListOfSectors.push_back(sector);
249 LOG(info) << "TPC: Processing sector " << sector;
250 // the active sectors need to be propagated
251 uint64_t activeSectors = 0;
252 activeSectors = sectorHeader->activeSectors;
253
254 // lambda that creates a DPL owned buffer to accumulate the digits (in shared memory)
255 auto makeDigitBuffer = [this, sector, &pc, activeSectors, &dh]() {
257 header.activeSectors = activeSectors;
258 if (mInternalWriter) {
259 using ContainerType = std::decay_t<decltype(pc.outputs().make<std::vector<o2::tpc::Digit>>(Output{"", "", 0}))>*;
260 return ContainerType(nullptr);
261 } else {
262 // default case
263 return &pc.outputs().make<std::vector<o2::tpc::Digit>>(Output{"TPC", "DIGITS", static_cast<SubSpecificationType>(dh->subSpecification), header});
264 }
265 };
266 // lambda that snapshots the common mode vector to be sent out; prepares and attaches header with sector information
267 auto snapshotCommonMode = [this, sector, &pc, activeSectors, &dh](std::vector<o2::tpc::CommonMode> const& commonMode) {
269 header.activeSectors = activeSectors;
270 if (!mInternalWriter) {
271 // note that snapshoting only works with non-const references (to be fixed?)
272 pc.outputs().snapshot(Output{"TPC", "COMMONMODE", static_cast<SubSpecificationType>(dh->subSpecification), header},
273 const_cast<std::vector<o2::tpc::CommonMode>&>(commonMode));
274 }
275 };
276 // lambda that snapshots labels to be sent out; prepares and attaches header with sector information
277 auto snapshotLabels = [this, &sector, &pc, activeSectors, &dh](o2::dataformats::MCTruthContainer<o2::MCCompLabel> const& labels) {
279 header.activeSectors = activeSectors;
280 if (mWithMCTruth) {
281 if (!mInternalWriter) {
282 auto& sharedlabels = pc.outputs().make<o2::dataformats::ConstMCTruthContainer<o2::MCCompLabel>>(Output{"TPC", "DIGITSMCTR", static_cast<SubSpecificationType>(dh->subSpecification), header});
283 labels.flatten_to(sharedlabels);
284 }
285 }
286 };
287 // lambda that snapshots digits grouping (triggers) to be sent out; prepares and attaches header with sector information
288 auto snapshotEvents = [this, sector, &pc, activeSectors, &dh](const std::vector<DigiGroupRef>& events) {
290 header.activeSectors = activeSectors;
291 if (!mInternalWriter) {
292 LOG(info) << "TPC: Send TRIGGERS for sector " << sector << " channel " << dh->subSpecification << " | size " << events.size();
293 pc.outputs().snapshot(Output{"TPC", "DIGTRIGGERS", static_cast<SubSpecificationType>(dh->subSpecification), header},
294 const_cast<std::vector<DigiGroupRef>&>(events));
295 }
296 };
297
298 auto digitsAccum = makeDigitBuffer(); // accumulator for digits
299 o2::dataformats::MCTruthContainer<o2::MCCompLabel> labelAccum; // timeframe accumulator for labels
300 std::vector<CommonMode> commonModeAccum;
301 std::vector<DigiGroupRef> eventAccum;
302
303 // this should not happen any more, legacy condition when the sector variable was used
304 // to transport control information
305 if (sector < 0) {
306 throw std::runtime_error("Legacy control information is not expected any more");
307 }
308
309 // the TPCSectorHeader now allows to transport information for more than one sector,
310 // e.g. for transporting clusters in one single data block. The digitization is however
311 // only on sector level
313 throw std::runtime_error("Digitizer can only work on single sectors");
314 }
315
316 TStopwatch timer;
317 timer.Start();
318
319 auto onFlush = [this, digitsAccum, &labelAccum, &commonModeAccum]() {
320 mFlushCounter++;
321 if (mInternalWriter) {
322 // the natural place to write out this independent datachunk immediately ...
324 } else {
325 // ... or to accumulate and later forward to next DPL proc
326 std::copy(mFlushBuffers.digits.begin(), mFlushBuffers.digits.end(), std::back_inserter(*digitsAccum));
327 if (mWithMCTruth) {
328 labelAccum.mergeAtBack(mFlushBuffers.labels);
329 }
330 std::copy(mFlushBuffers.commonMode.begin(), mFlushBuffers.commonMode.end(), std::back_inserter(commonModeAccum));
331 }
332 };
333 eventAccum = digitizeSector(mDigitizer, *context, mSimChains, sector, dh->tfCounter, mFlushBuffers, onFlush);
334
335 if (!mInternalWriter) {
336 // send out to next stage
337 snapshotEvents(eventAccum);
338 // snapshotDigits(digitsAccum); --> done automatically
339 snapshotCommonMode(commonModeAccum);
340 snapshotLabels(labelAccum);
341 }
342
343 timer.Stop();
344 LOG(info) << "TPC: Digitization took " << timer.CpuTime() << "s";
345 }
346
347 private:
348 o2::tpc::Digitizer mDigitizer;
349 TPCDigitizerConditions mConditions;
350 std::vector<TChain*> mSimChains;
351 SectorFlushBuffers mFlushBuffers; // output of the latest flush
352 std::vector<int> mListOfSectors; // a list of sectors treated by this task
353 TFile* mInternalROOTFlushFile = nullptr;
354 TTree* mInternalROOTFlushTTree = nullptr;
355 size_t mFlushCounter = 0;
356 int mLaneId = 0; // the id of the current process within the parallel pipeline
357 int mSector = 0;
358 bool mWriteGRP = false;
359 bool mWithMCTruth = true;
360 bool mInternalWriter = false;
361 int mDistortionType = 0;
362};
363
364o2::framework::DataProcessorSpec getTPCDigitizerSpec(int channel, bool writeGRP, bool mctruth, bool internalwriter, int distortionType)
365{
366 // create the full data processor spec using
367 // a name identifier
368 // input description
369 // algorithmic description (here a lambda getting called once to setup the actual processing function)
370 // options that can be used for this processor (here: input file names where to take the hits)
371 std::stringstream id;
372 id << "TPCDigitizer" << channel;
373
374 std::vector<OutputSpec> outputs; // define channel by triple of (origin, type id of data to be sent on this channel, subspecification)
375
376 if (!internalwriter) {
377 outputs.emplace_back("TPC", "DIGITS", static_cast<SubSpecificationType>(channel), Lifetime::Timeframe);
378 outputs.emplace_back("TPC", "DIGTRIGGERS", static_cast<SubSpecificationType>(channel), Lifetime::Timeframe);
379 if (mctruth) {
380 outputs.emplace_back("TPC", "DIGITSMCTR", static_cast<SubSpecificationType>(channel), Lifetime::Timeframe);
381 }
382 outputs.emplace_back("TPC", "COMMONMODE", static_cast<SubSpecificationType>(channel), Lifetime::Timeframe);
383 }
384 if (writeGRP) {
385 outputs.emplace_back("TPC", "ROMode", 0, Lifetime::Timeframe);
386 LOG(debug) << "TPC: Channel " << channel << " will supply ROMode";
387 }
388
389 std::vector<InputSpec> inputs{InputSpec{"collisioncontext", "SIM", "COLLISIONCONTEXT", static_cast<SubSpecificationType>(channel), Lifetime::Timeframe}};
390 auto options = TPCDigitizerConditions::options();
391 options.push_back({"tpc-sector-seeding", VariantType::Bool, false, {"Seed the random numbers of each sector from DigiParams.seed and the sector, so that the digits do not depend on the lane assignment"}});
392 return DataProcessorSpec{
393 id.str().c_str(),
394 inputs,
395 outputs,
396 AlgorithmSpec{adaptFromTask<TPCDPLDigitizerTask>(internalwriter, distortionType)},
397 options};
398}
399
400o2::framework::WorkflowSpec getTPCDigitizerSpec(int nLanes, std::vector<int> const& sectors, bool mctruth, bool internalwriter, int distortionType)
401{
402 // channel parameter is deprecated in the TPCDigitizer processor, all descendants
403 // are initialized not to publish GRP mode, but the channel will be added to the first
404 // processor after the pipelines have been created. The processor will decide upon
405 // the index in the ParallelContext whether to publish
406 WorkflowSpec pipelineTemplate{getTPCDigitizerSpec(0, false, mctruth, internalwriter, distortionType)};
407 // override the predefined name, index will be added by parallelPipeline method
408 pipelineTemplate[0].name = "TPCDigitizer";
410 pipelineTemplate, nLanes, [size = sectors.size()]() { return size; }, [&sectors](size_t index) { return sectors[index]; });
411 // add the channel for the GRP information to the first processor
412 for (auto& spec : pipelines) {
413 TPCDigitizerConditions::addInputs(spec.inputs, distortionType); // the same CCDB request for each pipeline
414 }
415 pipelines[0].outputs.emplace_back("TPC", "ROMode", 0, Lifetime::Timeframe);
416 return pipelines;
417}
418
419} // end namespace tpc
420} // end namespace o2
header::DataOrigin origin
Definition of the base digitizer task class.
Definition of the Detector class.
Simple interface to the CDB manager.
A const (ready only) version of MCTruthContainer.
Definition of the TPC Digit.
std::ostringstream debug
std::vector< std::string > header
configStandalone Light OpenGL display display_light Apply smoothing to points Enable Z buffer Highlight links Highlight seeds Highlight sector tracks Highlight final tracks Show individual Show related Highlight grid Show only individual collision Color clusters belonging to track objects Set line Enable drawing TPC data Enable drawing TOF data Enable drawing magnetic field Set field line step count Invert colors configStandalone Heavy OpenGL display display_heavy Show tracks with ITS contribution Show tracks with TOF contribution Enable propagation of loopers Show clusters at nominal x of pad row for early transformed data Split CE tracks when they cross the central Mark clusters Mark adjacent clusters Hide rejected tracks Apply filter on tracks to be displayed Separate sectors
configStandalone Light OpenGL display display_light Apply smoothing to points Enable Z buffer Highlight links Highlight seeds Highlight sector tracks Highlight final tracks Show individual sector
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 events
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 process
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 Directory with events to Events directory is and not inside events Show standalone event Run event generator maximum time bin of continuous for triggered for automatic continuous for automatic continuous triggered uint64_t
Header of the General Run Parameters object.
char error[CURL_ERROR_SIZE]
A special IO container - splitting a given vector to enable ROOT IO.
std::vector< o2::MCCompLabel > labels
Definition of the parameter class for the GEM stack.
Class to refer to the 1st entry and N elements of some group in the continuous container.
This class contains the algorithms for calculation the distortions and corrections.
Definition of the ALICE TPC digitizer.
Helper class to extract VDrift from different sources.
A read-only version of MCTruthContainer allowing for storage optimisation.
A container to hold and manage MC truth information/labels.
void mergeAtBack(MCTruthContainer< TruthElement > const &other)
void snapshot(const Output &spec, T const &object)
o2::header::DataHeader::SubSpecificationType SubSpecificationType
decltype(auto) make(const Output &spec, Args... args)
bool isAllowed(Output const &query)
check if a certain output is allowed
ServiceRegistryRef services()
Definition InitContext.h:34
ConfigParamRegistry const & options()
Definition InitContext.h:33
const_iterator begin() const
decltype(auto) get(R binding, int part=0) const
const_iterator end() const
DataAllocator & outputs()
The data allocator is used to allocate memory for the output data.
InputRecord & inputs()
The inputs associated with this processing context.
bool initSimChains(o2::detectors::DetID detid, std::vector< TChain * > &simchains) const
void setDistortionScaleType(int distortionScaleType)
Definition Digitizer.h:137
void setUseSectorSeeding(ULong64_t seed)
Definition Digitizer.cxx:85
void setVDrift(float v)
Definition Digitizer.h:134
void setContinuousReadout(bool isContinuous)
Definition Digitizer.h:109
void setTDriftOffset(float t)
Definition Digitizer.h:135
bool isContinuousReadout()
Definition Digitizer.h:113
void initDigitizerTask(framework::InitContext &ic) override
void run(framework::ProcessingContext &pc)
void process(framework::ProcessingContext &pc, framework::DataRef const &inputref)
TPCDPLDigitizerTask(bool internalwriter, int distortionType)
void finaliseCCDB(framework::ConcreteDataMatcher &matcher, void *obj)
CCDB conditions and calibration files of the TPC digitizer devices.
static void addInputs(std::vector< framework::InputSpec > &inputs, int distortionType)
CCDB inputs common to the TPC digitizer devices.
bool finaliseCCDB(framework::ConcreteDataMatcher &matcher, void *obj, Digitizer &digitizer)
pass a CCDB object to the digitizer; true if it was a TPC condition
static std::vector< framework::ConfigParamSpec > options()
options common to the TPC digitizer devices
void init(framework::InitContext &ic, int distortionType)
void update(framework::ProcessingContext &pc, Digitizer &digitizer, const std::function< void(float, float)> &setVDrift)
const GLuint * pipelines
Definition glcorearb.h:1909
GLuint buffer
Definition glcorearb.h:655
GLsizeiptr size
Definition glcorearb.h:659
GLuint GLuint end
Definition glcorearb.h:469
GLuint const GLchar * name
Definition glcorearb.h:781
GLenum target
Definition glcorearb.h:1641
GLuint id
Definition glcorearb.h:650
TBranch * getOrMakeBranch(TTree &tree, const char *brname, T *ptr)
Definition Detector.h:313
Defining ITS Vertex explicitly as messageable.
Definition Cartesian.h:288
WorkflowSpec parallelPipeline(const WorkflowSpec &specs, size_t nPipelines, std::function< size_t()> getNumberOfSubspecs, std::function< size_t(size_t)> getSubSpec)
std::vector< DataProcessorSpec > WorkflowSpec
std::vector< dataformats::RangeReference< int, int > > digitizeSector(Digitizer &digitizer, const steer::DigitizationContext &context, const std::vector< TChain * > &simChains, int sector, ULong64_t timeframe, SectorFlushBuffers &buffers, const std::function< void()> &onFlush)
void writeToBranchHelper(TTree &tree, const char *name, T *accum)
o2::framework::DataProcessorSpec getTPCDigitizerSpec(int channel, bool writeGRP, bool mctruth, bool internalwriter, int distortionType)
void copyHelper(T const &origin, R &target)
a couple of static helper functions to create timestamp values for CCDB queries or override obsolete ...
class listing possible services
Output buffers of one flush of a sector.
std::vector< CommonMode > commonMode
dataformats::MCTruthContainer< MCCompLabel > labels
static constexpr int NSectors
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"
std::unique_ptr< TTree > tree((TTree *) flIn.Get(std::string(o2::base::NameConf::CTFTREENAME).c_str()))