Project
Loading...
Searching...
No Matches
DigitReaderSpec.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
16
18
19#include <memory>
20#include <stdexcept>
21#include <string>
22#include <vector>
23
24#include <fmt/format.h>
25
26#include <gsl/span>
27
28#include <TFile.h>
29#include <TTree.h>
30#include <TTreeReader.h>
31#include <TTreeReaderValue.h>
32
40#include "Framework/Logger.h"
42#include "Framework/Task.h"
47
48using namespace o2::framework;
49
50namespace o2
51{
52namespace mch
53{
54
56{
57 public:
58 DigitsReaderDeviceDPL(bool useMC) : mUseMC(useMC)
59 {
60 if (mUseMC) {
61 mLabels = std::make_unique<TTreeReaderValue<dataformats::MCTruthContainer<MCCompLabel>>>(mTreeReader, "MCHMCLabels");
62 }
63 }
64
65 void init(InitContext& ic)
66 {
67 auto fileName = utils::Str::concat_string(utils::Str::rectifyDirectory(ic.options().get<std::string>("input-dir")),
68 ic.options().get<std::string>("mch-digit-infile"));
69 connectTree(fileName);
70
71 if (ic.options().hasOption("ignore-irframes") && !ic.options().get<bool>("ignore-irframes")) {
72 mUseIRFrames = true;
73 }
74
75 mTimeOffset = ic.options().get<bool>("no-time-offset") ? 0 : DigitFilterParam::Instance().timeOffset;
76 }
77
79 {
80 if (mUseIRFrames) {
81 sendNextIRFrames(pc);
82 } else {
83 sendNextTF(pc);
84 }
85 }
86
87 private:
88 TTreeReader mTreeReader{};
89 TTreeReaderValue<std::vector<ROFRecord>> mRofs = {mTreeReader, "MCHROFRecords"};
90 TTreeReaderValue<std::vector<Digit>> mDigits = {mTreeReader, "MCHDigit"};
91 std::unique_ptr<TTreeReaderValue<dataformats::MCTruthContainer<MCCompLabel>>> mLabels{};
92 bool mUseMC = true;
93 bool mUseIRFrames = false;
94 int mTimeOffset = 0;
95
96 void connectTree(std::string fileName)
97 {
98 auto file = TFile::Open(fileName.c_str());
99 if (!file || file->IsZombie()) {
100 throw std::invalid_argument(fmt::format("Opening file {} failed", fileName));
101 }
102
103 auto tree = file->Get<TTree>("o2sim");
104 if (!tree) {
105 throw std::invalid_argument(fmt::format("Tree o2sim not found in {}", fileName));
106 }
107 mTreeReader.SetTree(tree);
108 mTreeReader.Restart();
109 }
110
111 void sendNextTF(ProcessingContext& pc)
112 {
113 // A timeframe holds no collision at all whenever the interaction rate is low enough, and the
114 // digit tree then has no entry. Send empty containers and finish, rather than throwing.
115 if (mTreeReader.GetEntries() == 0) {
116 LOG(info) << "digit tree has no entry, sending empty output";
117 pc.outputs().snapshot(OutputRef{"rofs"}, std::vector<ROFRecord>{});
118 pc.outputs().snapshot(OutputRef{"digits"}, std::vector<Digit>{});
119 if (mUseMC) {
121 }
122 pc.services().get<ControlService>().endOfStream();
123 pc.services().get<ControlService>().readyToQuit(QuitRequest::Me);
124 return;
125 }
126
127 // load the next TF and check its validity (missing branch, ...)
128 if (!mTreeReader.Next()) {
129 throw std::invalid_argument(mTreeReader.fgEntryStatusText[mTreeReader.GetEntryStatus()]);
130 }
131
132 // send the whole TF
133 pc.outputs().snapshot(OutputRef{"rofs"}, *mRofs);
134 pc.outputs().snapshot(OutputRef{"digits"}, *mDigits);
135 if (mUseMC) {
136 pc.outputs().snapshot(OutputRef{"labels"}, **mLabels);
137 }
138
139 // stop here if it was the last one
140 if (mTreeReader.GetCurrentEntry() + 1 >= mTreeReader.GetEntries()) {
141 pc.services().get<ControlService>().endOfStream();
142 pc.services().get<ControlService>().readyToQuit(QuitRequest::Me);
143 }
144 }
145
146 void sendNextIRFrames(ProcessingContext& pc)
147 {
148 std::vector<ROFRecord> rofs{};
149 std::vector<Digit> digits{};
151
152 // get the IR frames to select
153 auto irFrames = pc.inputs().get<gsl::span<dataformats::IRFrame>>("driverInfo");
154
155 if (mTreeReader.GetEntries() == 0) {
156 // A timeframe holds no collision at all whenever the interaction rate is low enough, and the
157 // digit tree then has no entry. Nothing to select. Send empty containers.
158 LOG(info) << "digit tree has no entry, sending empty output";
159 } else if (!irFrames.empty()) {
160 utils::IRFrameSelector irfSel{};
161 irfSel.setSelectedIRFrames(irFrames, 0, 0, -mTimeOffset, true);
162 const auto irMin = irfSel.getIRFrames().front().getMin();
163 const auto irMax = irfSel.getIRFrames().back().getMax();
164
165 // load the first TF if not already done
166 bool loadNextTF = mTreeReader.GetCurrentEntry() < 0;
167
168 while (true) {
169 // load the next TF if requested
170 if (loadNextTF && !mTreeReader.Next()) {
171 throw std::invalid_argument(mTreeReader.fgEntryStatusText[mTreeReader.GetEntryStatus()]);
172 }
173
174 // look for selected ROFs in this TF and copy them
175 if (!mRofs->empty() && mRofs->front().getBCData() <= irMax &&
176 mRofs->back().getBCData() + mRofs->back().getBCWidth() - 1 >= irMin) {
177 for (const auto& rof : *mRofs) {
178 if (irfSel.check({rof.getBCData(), rof.getBCData() + rof.getBCWidth() - 1}) != -1) {
179 rofs.emplace_back(rof);
180 rofs.back().setDataRef(digits.size(), rof.getNEntries());
181 digits.insert(digits.end(), mDigits->begin() + rof.getFirstIdx(), mDigits->begin() + rof.getFirstIdx() + rof.getNEntries());
182 if (mUseMC) {
183 for (auto i = 0; i < rof.getNEntries(); ++i) {
184 labels.addElements(labels.getIndexedSize(), (*mLabels)->getLabels(rof.getFirstIdx() + i));
185 }
186 }
187 }
188 }
189 }
190
191 // move to the next TF if needed and if any
192 if ((mRofs->empty() || mRofs->back().getBCData() + mRofs->back().getBCWidth() - 1 < irMax) &&
193 mTreeReader.GetCurrentEntry() + 1 < mTreeReader.GetEntries()) {
194 loadNextTF = true;
195 continue;
196 }
197
198 break;
199 }
200 }
201
202 // send the selected data
203 pc.outputs().snapshot(OutputRef{"rofs"}, rofs);
204 pc.outputs().snapshot(OutputRef{"digits"}, digits);
205 if (mUseMC) {
206 pc.outputs().snapshot(OutputRef{"labels"}, labels);
207 }
208
209 // stop here if they were the last IR frames to select
210 if (irFrames.empty() || irFrames.back().isLast()) {
211 pc.services().get<ControlService>().endOfStream();
212 pc.services().get<ControlService>().readyToQuit(QuitRequest::Me);
213 }
214 }
215};
216
217DataProcessorSpec getDigitReaderSpec(bool useMC, std::string_view specName,
218 std::string_view outputDigitDataDescription,
219 std::string_view outputDigitRofDataDescription,
220 std::string_view outputDigitLabelDataDescription)
221{
222 std::string output = fmt::format("digits:MCH/{}/0;rofs:MCH/{}/0", outputDigitDataDescription, outputDigitRofDataDescription);
223 if (useMC) {
224 output += fmt::format(";labels:MCH/{}/0", outputDigitLabelDataDescription);
225 }
226
227 std::vector<OutputSpec> outputs;
228 auto matchers = select(output.c_str());
229 for (auto& matcher : matchers) {
230 outputs.emplace_back(DataSpecUtils::asOutputSpec(matcher));
231 }
232
233 return DataProcessorSpec{
234 std::string(specName),
235 Inputs{},
236 outputs,
237 AlgorithmSpec{adaptFromTask<DigitsReaderDeviceDPL>(useMC)},
238 Options{{"mch-digit-infile", VariantType::String, "mchdigits.root", {"Name of the input file"}},
239 {"input-dir", VariantType::String, "none", {"Input directory"}},
240 {"no-time-offset", VariantType::Bool, false, {"no time offset between IRFrames and digits"}}}};
241}
242} // namespace mch
243} // namespace o2
std::vector< framework::ConcreteDataMatcher > matchers
int32_t i
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 increase X h configStandalone Camera window renderer OpenGL display display_camera Orient the camera such that the y axis is always upwards Full Screen MultiSample Anti Downsampling Anti Enable Vertical Sync Target frame or if the backend cannot limit to the refresh Use OpenGL indirect draws to reduce number of draw calls Display h configStandalone Vulkan display display_vulkan Have uniform buffers in host accessible device memory h configStandalone Internal display display_internal Number of updates the Qt GUI may send ahead of the last one the display has before it waits for the display Opacity of the console background Number of console lines the history is shortened to this when it reaches twice the size Time in seconds an info message fades out after setInfoTime h OpenGL display display Font size Do not use Freetype for font Renderer for event Frontend to use for event ROOT macros used as track filter Console script file
Class to check if give InteractionRecord or IRFrame is selected by the external IRFrame vector.
Class to delimit start and end IR of certain time period.
std::vector< o2::MCCompLabel > labels
void output(const std::map< std::string, ChannelStat > &channels)
Definition rawdump.cxx:197
Definition of a container to keep Monte Carlo truth external to simulation objects.
Definition of the MCH ROFrame record.
const char * specName
A container to hold and manage MC truth information/labels.
bool hasOption(const char *key) const
void snapshot(const Output &spec, T const &object)
ConfigParamRegistry const & options()
Definition InitContext.h:33
decltype(auto) get(R binding, int part=0) const
DataAllocator & outputs()
The data allocator is used to allocate memory for the output data.
InputRecord & inputs()
The inputs associated with this processing context.
ServiceRegistryRef services()
The services registry associated with this processing context.
void run(ProcessingContext &pc)
void setSelectedIRFrames(const SPAN &sp, size_t bwd=0, size_t fwd=0, long shift=0, bool removeOverlaps=true)
Defining ITS Vertex explicitly as messageable.
Definition Cartesian.h:288
std::vector< InputSpec > select(char const *matcher="")
std::vector< InputSpec > Inputs
framework::DataProcessorSpec getDigitReaderSpec(bool useMC, std::string_view specName="mch-digit-reader", std::string_view outputDigitDataDescription="DIGITS", std::string_view outputDigitRofDataDescription="DIGITROFS", std::string_view outputDigitLabelDataDescription="DIGITLABELS")
a couple of static helper functions to create timestamp values for CCDB queries or override obsolete ...
static OutputSpec asOutputSpec(InputSpec const &spec)
static std::string rectifyDirectory(const std::string_view p)
static std::string concat_string(Ts const &... ts)
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"
std::unique_ptr< TTree > tree((TTree *) flIn.Get(std::string(o2::base::NameConf::CTFTREENAME).c_str()))
std::vector< Digit > digits