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
12#include <vector>
13
14#include "TTree.h"
15
18#include "Framework/Logger.h"
24#include <cassert>
25
26using namespace o2::framework;
27using namespace o2::itsmft;
28
29namespace o2
30{
31namespace trk
32{
33
34DigitReader::DigitReader(o2::detectors::DetID id, bool useMC, bool useCalib)
35{
36 assert(id == o2::detectors::DetID::TRK);
37 mDetNameLC = mDetName = id.getName();
38 mDigTreeName = "o2sim";
39
40 mDigits.resize(mLayers, nullptr);
41 mDigROFRec.resize(mLayers, nullptr);
42 mPLabels.resize(mLayers, nullptr);
43
47
49
50 mUseMC = useMC;
51 mUseCalib = useCalib;
52 std::transform(mDetNameLC.begin(), mDetNameLC.end(), mDetNameLC.begin(), ::tolower);
53}
54
56{
57 mFileName = ic.options().get<std::string>((mDetNameLC + "-digit-infile").c_str());
59}
60
62{
63 auto ent = mTree->GetReadEntry() + 1;
64 // A timeframe holds no collision at all whenever the interaction rate is low enough, and
65 // the tree then has no entry to read. Publish empty containers instead of reading past the
66 // end and pushing branch addresses that GetEntry has not filled, so that the consumers
67 // downstream still see the timeframe. (This used to be an assert, which is compiled out of
68 // every production build since ENABLE_CASSERT defaults to OFF.)
69 const bool noEntry = ent >= mTree->GetEntries();
70 if (noEntry) {
71 LOG(info) << "no entry to read, publishing empty output";
72 } else {
73 mTree->GetEntry(ent);
74 }
75
76 static const std::vector<o2::trkft3::ROFRecord> noDigROFRec;
77 static const std::vector<o2::trkft3::Digit> noDigits;
78 for (int iLayer = 0; iLayer < mLayers; ++iLayer) {
79 const auto& digROFRec = noEntry ? noDigROFRec : *mDigROFRec[iLayer];
80 const auto& digits = noEntry ? noDigits : *mDigits[iLayer];
81 LOG(info) << mDetName << "DigitReader on layer " << iLayer << " pushes " << digROFRec.size() << " ROFRecords, "
82 << digits.size() << " digits at entry " << ent;
83
84 pc.outputs().snapshot(Output{mOrigin, "DIGITSROF", static_cast<o2::framework::DataAllocator::SubSpecificationType>(iLayer)}, digROFRec);
86
87 if (mUseMC) {
89 if (noEntry) {
91 noLabels.flatten_to(sharedlabels);
92 } else {
93 mPLabels[iLayer]->copyandflatten(sharedlabels);
94 delete mPLabels[iLayer];
95 mPLabels[iLayer] = nullptr;
96 }
97 }
98 }
99
100 if (mUseCalib) {
101 pc.outputs().snapshot(Output{mOrigin, "GBTCALIB", 0}, mCalib);
102 }
103
104 if (noEntry || mTree->GetReadEntry() + 1 >= mTree->GetEntries()) {
106 pc.services().get<ControlService>().readyToQuit(QuitRequest::Me);
107 }
108}
109
110void DigitReader::connectTree(const std::string& filename)
111{
112 mTree.reset(nullptr); // in case it was already loaded
113 mFile.reset(TFile::Open(filename.c_str()));
114 assert(mFile && !mFile->IsZombie());
115 mTree.reset((TTree*)mFile->Get(mDigTreeName.c_str()));
116 assert(mTree);
117
118 for (int iLayer = 0; iLayer < mLayers; ++iLayer) {
120 setBranchAddress(mDigitBranchName, mDigits[iLayer], iLayer);
121 if (mUseMC) {
122 const auto mctruthBranch = getBranchName(mDigtMCTruthBranchName, iLayer);
123 if (!mTree->GetBranch(mctruthBranch.c_str())) {
124 throw std::runtime_error("MC data requested but missing branch(es) at layer " + std::to_string(iLayer) +
125 ": " + mctruthBranch);
126 }
128 }
129 }
130
131 if (mUseCalib) {
132 if (!mTree->GetBranch(mCalibBranchName.c_str())) {
133 throw std::runtime_error("GBT calibration data requested but not found in the tree");
134 }
136 }
137 LOG(info) << "Loaded tree from " << filename << " with " << mTree->GetEntries() << " entries";
138}
139
140std::string DigitReader::getBranchName(const std::string& base, int index) const
141{
142 if (index >= 0) {
143 return base + "_" + std::to_string(index);
144 }
145 return base;
146}
147
148template <typename Ptr>
149void DigitReader::setBranchAddress(const std::string& base, Ptr& addr, int layer)
150{
151 const auto name = getBranchName(base, layer);
152 if (Int_t ret = mTree->SetBranchAddress(name.c_str(), &addr); ret != 0) {
153 LOGP(fatal, "failed to set branch address for {} ret={}", name, ret);
154 }
155}
156
157DataProcessorSpec getTRKDigitReaderSpec(bool useMC, bool useCalib, std::string defname)
158{
159 static constexpr int nLayers = o2::trk::AlmiraParam::kNLayers;
160 std::vector<OutputSpec> outputSpec;
161 for (int iLayer = 0; iLayer < nLayers; ++iLayer) {
162 outputSpec.emplace_back("TRK", "DIGITS", iLayer, Lifetime::Timeframe);
163 outputSpec.emplace_back("TRK", "DIGITSROF", iLayer, Lifetime::Timeframe);
164 if (useMC) {
165 outputSpec.emplace_back("TRK", "DIGITSMCTR", iLayer, Lifetime::Timeframe);
166 }
167 }
168 if (useCalib) {
169 outputSpec.emplace_back("TRK", "GBTCALIB", 0, Lifetime::Timeframe);
170 }
171
172 return DataProcessorSpec{
173 "trk-digit-reader",
174 Inputs{},
175 outputSpec,
176 AlgorithmSpec{adaptFromTask<TRKDigitReader>(useMC, useCalib)},
177 Options{
178 {"trk-digit-infile", VariantType::String, defname, {"Name of the input digit file"}}}};
179}
180
181} // namespace trk
182} // namespace o2
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
Static class with identifiers, bitmasks and names for ALICE detectors.
Definition DetID.h:58
void snapshot(const Output &spec, T const &object)
o2::header::DataHeader::SubSpecificationType SubSpecificationType
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
static constexpr int mLayers
std::vector< std::vector< o2::trkft3::ROFRecord > * > mDigROFRec
void run(ProcessingContext &pc) final
std::unique_ptr< TTree > mTree
std::vector< o2::dataformats::IOMCTruthContainerView * > mPLabels
std::string mDigtMCTruthBranchName
std::vector< std::vector< o2::trkft3::Digit > * > mDigits
o2::header::DataOrigin mOrigin
void setBranchAddress(const std::string &base, Ptr &addr, int layer=-1)
std::string mDigitBranchName
std::vector< o2::itsmft::GBTCalibData > * mCalibPtr
std::string mCalibBranchName
std::unique_ptr< TFile > mFile
std::string getBranchName(const std::string &base, int index) const
void init(InitContext &ic) final
std::string mDigROFBranchName
std::vector< o2::itsmft::GBTCalibData > mCalib
void connectTree(const std::string &filename)
GLuint index
Definition glcorearb.h:781
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 getTRKDigitReaderSpec(bool useMC=true, bool useCalib=false, std::string defname="trkdigits.root")
a couple of static helper functions to create timestamp values for CCDB queries or override obsolete ...
std::string to_string(gsl::span< T, Size > span)
Definition common.h:52
std::string filename()
static constexpr size_t kNLayers
Definition AlmiraParam.h:29
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"
std::vector< Digit > digits