Project
Loading...
Searching...
No Matches
MCKinematicsReader.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#include <TChain.h>
17#include <stdexcept>
18#include <string>
19#include <vector>
20#include <fairlogger/Logger.h>
21
22using namespace o2::steer;
23
24void MCKinematicsReader::reportMissingSource(int source, size_t available)
25{
26 throw std::out_of_range("MCKinematicsReader: there are " + std::to_string(available) + " sources; source " +
27 std::to_string(source) + " is not one of them");
28}
29
30void MCKinematicsReader::reportMissingEvent(const char* what, int source, int event, size_t available)
31{
32 throw std::out_of_range("MCKinematicsReader: source " + std::to_string(source) + " has " +
33 std::to_string(available) + " " + what + "; there is no event " + std::to_string(event));
34}
35
36void MCKinematicsReader::ensureTracksForSourceAndEvent(int source, int event) const
37{
38 if (static_cast<size_t>(event) >= mTracks[source].size()) {
39 reportMissingEvent("events", source, event, mTracks[source].size());
40 }
41 loadTracksForSourceAndEvent(source, event);
42}
43
45{
46 for (auto& perSource : mTracks) {
47 for (auto tracks : perSource) {
48 delete tracks;
49 }
50 }
51 mTracks.clear();
52
53 for (auto chain : mInputChains) {
54 delete chain;
55 }
56 mInputChains.clear();
57
58 if (mDigitizationContext && mOwningDigiContext) {
59 delete mDigitizationContext;
60 }
61}
62
63void MCKinematicsReader::initIndexedTrackRefs(std::vector<o2::TrackReference>& refs, o2::dataformats::MCTruthContainer<o2::TrackReference>& indexedrefs) const
64{
65 // sort trackrefs according to track index then according to track length
66 std::sort(refs.begin(), refs.end(), [](const o2::TrackReference& a, const o2::TrackReference& b) {
67 if (a.getTrackID() == b.getTrackID()) {
68 return a.getLength() < b.getLength();
69 }
70 return a.getTrackID() < b.getTrackID();
71 });
72
73 // make final indexed container for track references
74 indexedrefs.clear();
75 for (auto& ref : refs) {
76 if (ref.getTrackID() >= 0) {
77 indexedrefs.addElement(ref.getTrackID(), ref);
78 }
79 }
80}
81
82void MCKinematicsReader::initTracksForSource(int source) const
83{
84 auto chain = mInputChains[source];
85 if (chain) {
86 // todo: get name from NameConfig
87 auto br = chain->GetBranch("MCTrack");
88 mTracks[source].resize(br->GetEntries(), nullptr);
89 }
90}
91
92void MCKinematicsReader::loadTracksForSourceAndEvent(int source, int event) const
93{
94 auto chain = mInputChains[source];
95 if (chain) {
96 // todo: get name from NameConfig
97 auto br = chain->GetBranch("MCTrack");
98 if (br) {
99 std::vector<MCTrack>* loadtracks = nullptr;
100 br->SetAddress(&loadtracks);
101 br->GetEntry(event);
102 // ROOT allocated the vector for us and we own it (we passed a pointer to nullptr): keep it instead of copying it
103 mTracks[source][event] = loadtracks;
104 br->ResetAddress(); // the branch must not refer to the stored vector (nor to the local pointer) any more
105 // free the decompressed baskets (~ the size of the event) if no later entry reads them, i.e. at the end of its cluster
106 auto clusterIt = br->GetTree()->GetClusterIterator(event);
107 clusterIt.Next();
108 if (event + 1 >= clusterIt.GetNextEntry()) {
109 br->DropBaskets("all");
110 }
111 }
112 }
113}
114
116{
117 if (mTracks.at(source).at(eventID) != nullptr) {
118 delete mTracks[source][eventID];
119 mTracks[source][eventID] = nullptr;
120 }
121 // the track references of this event as well (reloaded on demand)
122 if (static_cast<size_t>(eventID) < mTrackRefsLoaded.at(source).size() && mTrackRefsLoaded[source][eventID]) {
124 mTrackRefsLoaded[source][eventID] = false;
125 }
126}
127
128void MCKinematicsReader::loadHeadersForSource(int source) const
129{
130 auto chain = mInputChains[source];
131 if (chain) {
132 // todo: get name from NameConfig
133 auto br = chain->GetBranch("MCEventHeader.");
134 if (br) {
136 br->SetAddress(&header);
137 mHeaders[source].resize(br->GetEntries());
138 for (int event = 0; event < br->GetEntries(); ++event) {
139 br->GetEntry(event);
140 mHeaders[source][event] = *header;
141 }
142 delete header;
143 header = nullptr;
144 } else {
145 LOG(warn) << "MCHeader branch not found";
146 }
147 }
148}
149
150void MCKinematicsReader::initTrackRefsForSource(int source) const
151{
152 auto chain = mInputChains[source];
153 if (chain) {
154 // todo: get name from NameConfig
155 auto br = chain->GetBranch("TrackRefs");
156 if (br) {
157 mIndexedTrackRefs[source].resize(br->GetEntries());
158 mTrackRefsLoaded[source].assign(br->GetEntries(), false);
159 } else {
160 LOG(warn) << "TrackRefs branch not found";
161 }
162 }
163}
164
165void MCKinematicsReader::loadTrackRefsForSourceAndEvent(int source, int event) const
166{
167 // todo: get name from NameConfig
168 auto br = mInputChains[source]->GetBranch("TrackRefs");
169 std::vector<o2::TrackReference>* refs = nullptr; // allocated by ROOT, owned by us
170 br->SetAddress(&refs);
171 br->GetEntry(event);
172 if (refs) {
173 // we convert the original flat vector into an indexed structure
174 initIndexedTrackRefs(*refs, mIndexedTrackRefs[source][event]);
175 delete refs;
176 }
177 br->ResetAddress();
178 // free the decompressed baskets if no later entry reads them, i.e. at the end of the cluster of this event
179 auto clusterIt = br->GetTree()->GetClusterIterator(event);
180 clusterIt.Next();
181 if (event + 1 >= clusterIt.GetNextEntry()) {
182 br->DropBaskets("all");
183 }
184 mTrackRefsLoaded[source][event] = true;
185}
186
188{
189 if (mInitialized) {
190 LOG(info) << "MCKinematicsReader already initialized; doing nothing";
191 return false;
192 }
193
194 mInitialized = true;
195 mDigitizationContext = context;
196
197 // get the chains to read
198 mDigitizationContext->initSimKinematicsChains(mInputChains);
199
200 // load the kinematics information
201 mTracks.resize(mInputChains.size());
202 mHeaders.resize(mInputChains.size());
203 mIndexedTrackRefs.resize(mInputChains.size());
204 mTrackRefsLoaded.resize(mInputChains.size());
205
206 // actual loading will be done only if someone asks
207 // the first time for a particular source ...
208
209 return true;
210}
211
213{
214 if (mInitialized) {
215 LOG(info) << "MCKinematicsReader already initialized; doing nothing";
216 return false;
217 }
218
220 if (!context) {
221 return false;
222 }
223 mOwningDigiContext = true;
224 return initFromDigitContext(context);
225}
226
228{
229 if (mInitialized) {
230 LOG(info) << "MCKinematicsReader already initialized; doing nothing";
231 return false;
232 }
233 mInputChains.emplace_back(new TChain("o2sim"));
234 mInputChains.back()->AddFile(o2::base::NameConf::getMCKinematicsFileName(name.data()).c_str());
235 mTracks.resize(1);
236 mHeaders.resize(1);
237 mIndexedTrackRefs.resize(1);
238 mTrackRefsLoaded.resize(1);
239 mInitialized = true;
240
241 return true;
242}
std::vector< std::string > header
GPUChain * chain
std::vector< o2::its::TrackITS > tracks
Definition of the Names Generator class.
static std::string getMCKinematicsFileName(const std::string_view prefix=STANDARDSIMPREFIX)
Definition NameConf.h:46
A container to hold and manage MC truth information/labels.
bool initSimKinematicsChains(std::vector< TChain * > &simkinematicschains) const
static DigitizationContext * loadFromFile(std::string_view filename="")
bool initFromKinematics(std::string_view filename)
inits the reader from a simple kinematics file
bool initFromDigitContext(std::string_view filename)
void releaseTracksForSourceAndEvent(int source, int event)
API to ask releasing tracks and track references (freeing memory) for source + event.
struct _cl_event * event
Definition glcorearb.h:2982
GLsizeiptr size
Definition glcorearb.h:659
GLuint const GLchar * name
Definition glcorearb.h:781
GLboolean GLboolean GLboolean b
Definition glcorearb.h:1233
GLsizei GLsizei GLchar * source
Definition glcorearb.h:798
GLboolean GLboolean GLboolean GLboolean a
Definition glcorearb.h:1233
GLint ref
Definition glcorearb.h:291
std::string to_string(gsl::span< T, Size > span)
Definition common.h:52
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"