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
13
14#include <vector>
15
18#include "Framework/Logger.h"
24#include <cassert>
25#include <stdexcept>
26
27using namespace o2::framework;
28using namespace o2::itsmft;
29
30namespace o2::its3
31{
32
33ITS3DigitReader::ITS3DigitReader(bool useMC, bool doStag, bool useCalib) : mUseMC(useMC), mDoStaggering(doStag), mUseCalib(useCalib), mDetNameLC(mDetName = "IT3"), mDigTreeName("o2sim")
34{
38
39 std::transform(mDetNameLC.begin(), mDetNameLC.end(), mDetNameLC.begin(), ::tolower);
40}
41
43{
44 mFileName = ic.options().get<std::string>((mDetNameLC + "-digit-infile").c_str());
46}
47
49{
50 auto ent = mTree->GetReadEntry() + 1;
51 // A timeframe holds no collision at all whenever the interaction rate is low enough, and
52 // the tree then has no entry to read. Publish empty containers instead of reading past the
53 // end and pushing branch addresses that GetEntry has not filled, so that the consumers
54 // downstream still see the timeframe. (This used to be an assert, which is compiled out of
55 // every production build since ENABLE_CASSERT defaults to OFF.)
56 const bool noEntry = ent >= mTree->GetEntries();
57 if (noEntry) {
58 LOG(info) << "no entry to read, publishing empty output";
59 } else {
60 mTree->GetEntry(ent);
61 }
62 static const std::vector<o2::itsmft::ROFRecord> noDigROFRec;
63 static const std::vector<o2::itsmft::Digit> noDigits;
64 for (uint32_t iLayer = 0; iLayer < (mDoStaggering ? NLayers : 1); ++iLayer) {
65 if (!noEntry && (!mDigROFRec[iLayer] || !mDigits[iLayer])) {
66 throw std::runtime_error("ITS3 digit reader requires all 7 layer branches to be present and populated in every entry");
67 }
68 const auto& digROFRec = noEntry ? noDigROFRec : *mDigROFRec[iLayer];
69 const auto& digits = noEntry ? noDigits : *mDigits[iLayer];
70 LOG(info) << mDetName << "DigitReader pushes " << digROFRec.size() << " ROFRecords, " << digits.size() << " digits at entry " << ent << " on layer " << iLayer;
71 pc.outputs().snapshot(Output{mOrigin, "DIGITSROF", iLayer}, digROFRec);
72 pc.outputs().snapshot(Output{mOrigin, "DIGITS", iLayer}, digits);
73 if (mUseMC) {
74 if (!noEntry && !mPLabels[iLayer]) {
75 throw std::runtime_error("ITS3 digit reader requires MC truth branches for all 7 layers to be present and populated in every entry");
76 }
77 auto& sharedlabels = pc.outputs().make<o2::dataformats::ConstMCTruthContainer<o2::MCCompLabel>>(Output{mOrigin, "DIGITSMCTR", iLayer});
78 if (noEntry) {
80 noLabels.flatten_to(sharedlabels);
81 } else {
82 mPLabels[iLayer]->copyandflatten(sharedlabels);
83 delete mPLabels[iLayer];
84 mPLabels[iLayer] = nullptr;
85 }
86 }
87 }
88
89 if (noEntry || mTree->GetReadEntry() + 1 >= mTree->GetEntries()) {
91 pc.services().get<ControlService>().readyToQuit(QuitRequest::Me);
92 }
93}
94
95template <typename Ptr>
96void ITS3DigitReader::setBranchAddress(const std::string& base, Ptr& addr, int layer)
97{
98 const auto name = getBranchName(base, layer);
99 if (Int_t ret = mTree->SetBranchAddress(name.c_str(), &addr); ret != 0) {
100 LOGP(fatal, "failed to set branch address for {} ret={}", name, ret);
101 }
102}
103
105{
106 mTree.reset(nullptr); // in case it was already loaded
107 mFile.reset(TFile::Open(filename.c_str()));
108 assert(mFile && !mFile->IsZombie());
109 mTree.reset((TTree*)mFile->Get(mDigTreeName.c_str()));
110 assert(mTree);
111 for (int iLayer = 0; iLayer < (mDoStaggering ? NLayers : 1); ++iLayer) {
112 const auto rofBranchName = getBranchName(mDigROFBranchName, iLayer);
113 const auto digBranchName = getBranchName(mDigBranchName, iLayer);
114 if (!mTree->GetBranch(rofBranchName.c_str()) || !mTree->GetBranch(digBranchName.c_str())) {
115 throw std::runtime_error("ITS3 digit reader requires all branches in the input file");
116 }
118 setBranchAddress(mDigBranchName, mDigits[iLayer], iLayer);
119 if (mUseMC) {
120 if (!mTree->GetBranch(getBranchName(mDigMCTruthBranchName, iLayer).c_str())) {
121 throw std::runtime_error("ITS3 digit reader requires MC truth branches for all 7 layers in the input file");
122 }
123 if (!mPLabels[iLayer]) {
125 }
126 }
127 }
128 LOG(info) << "Loaded tree from " << filename << " with " << mTree->GetEntries() << " entries";
129}
130
131DataProcessorSpec getITS3DigitReaderSpec(bool useMC, bool doStag, bool useCalib, std::string defname)
132{
133 std::vector<OutputSpec> outputSpec;
134 for (uint32_t iLayer = 0; iLayer < (doStag ? ITS3DigitReader::NLayers : 1); ++iLayer) {
135 outputSpec.emplace_back("IT3", "DIGITS", iLayer, Lifetime::Timeframe);
136 outputSpec.emplace_back("IT3", "DIGITSROF", iLayer, Lifetime::Timeframe);
137 if (useMC) {
138 outputSpec.emplace_back("IT3", "DIGITSMCTR", iLayer, Lifetime::Timeframe);
139 }
140 }
141
142 return DataProcessorSpec{
143 .name = "its3-digit-reader",
144 .inputs = Inputs{},
145 .outputs = outputSpec,
146 .algorithm = AlgorithmSpec{adaptFromTask<ITS3DigitReader>(useMC, doStag, useCalib)},
147 .options = Options{
148 {"it3-digit-infile", VariantType::String, defname, {"Name of the input digit file"}}}};
149}
150
151} // namespace o2::its3
A const (ready only) version of MCTruthContainer.
A special IO container - splitting a given vector to enable ROOT IO.
Definition of a container to keep Monte Carlo truth external to simulation objects.
A read-only version of MCTruthContainer allowing for storage optimisation.
A container to hold and manage MC truth information/labels.
size_t flatten_to(ContainerType &container) const
void snapshot(const Output &spec, T const &object)
decltype(auto) make(const Output &spec, Args... args)
ConfigParamRegistry const & options()
Definition InitContext.h:33
DataAllocator & outputs()
The data allocator is used to allocate memory for the output data.
ServiceRegistryRef services()
The services registry associated with this processing context.
virtual void endOfStream(EndOfStreamContext &context)
This is invoked whenever we have an EndOfStream event.
Definition Task.h:43
std::unique_ptr< TFile > mFile
std::array< std::vector< o2::itsmft::ROFRecord > *, NLayers > mDigROFRec
std::unique_ptr< TTree > mTree
void run(ProcessingContext &pc) final
static constexpr int NLayers
void setBranchAddress(const std::string &base, Ptr &addr, int layer)
std::array< o2::dataformats::IOMCTruthContainerView *, NLayers > mPLabels
void init(InitContext &ic) final
std::string getBranchName(const std::string &base, int index) const
std::array< std::vector< o2::itsmft::Digit > *, NLayers > mDigits
ITS3DigitReader(bool useMC, bool doStag, bool useCalib)
void connectTree(const std::string &filename)
const o2::header::DataOrigin mOrigin
GLuint const GLchar * name
Definition glcorearb.h:781
GLenum GLuint GLint GLint layer
Definition glcorearb.h:1310
Defining ITS Vertex explicitly as messageable.
Definition Cartesian.h:288
std::vector< ConfigParamSpec > Options
std::vector< InputSpec > Inputs
framework::DataProcessorSpec getITS3DigitReaderSpec(bool useMC=true, bool doStag=false, bool useCalib=false, std::string defname="it3digits.root")
std::string filename()
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"
std::vector< Digit > digits