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 chain : mInputChains) {
47 delete chain;
48 }
49 mInputChains.clear();
50
51 if (mDigitizationContext && mOwningDigiContext) {
52 delete mDigitizationContext;
53 }
54}
55
56void MCKinematicsReader::initIndexedTrackRefs(std::vector<o2::TrackReference>& refs, o2::dataformats::MCTruthContainer<o2::TrackReference>& indexedrefs) const
57{
58 // sort trackrefs according to track index then according to track length
59 std::sort(refs.begin(), refs.end(), [](const o2::TrackReference& a, const o2::TrackReference& b) {
60 if (a.getTrackID() == b.getTrackID()) {
61 return a.getLength() < b.getLength();
62 }
63 return a.getTrackID() < b.getTrackID();
64 });
65
66 // make final indexed container for track references
67 indexedrefs.clear();
68 for (auto& ref : refs) {
69 if (ref.getTrackID() >= 0) {
70 indexedrefs.addElement(ref.getTrackID(), ref);
71 }
72 }
73}
74
75void MCKinematicsReader::initTracksForSource(int source) const
76{
77 auto chain = mInputChains[source];
78 if (chain) {
79 // todo: get name from NameConfig
80 auto br = chain->GetBranch("MCTrack");
81 mTracks[source].resize(br->GetEntries(), nullptr);
82 }
83}
84
85void MCKinematicsReader::loadTracksForSourceAndEvent(int source, int event) const
86{
87 auto chain = mInputChains[source];
88 if (chain) {
89 // todo: get name from NameConfig
90 auto br = chain->GetBranch("MCTrack");
91 if (br) {
92 std::vector<MCTrack>* loadtracks = nullptr;
93 br->SetAddress(&loadtracks);
94 br->GetEntry(event);
95 mTracks[source][event] = new std::vector<o2::MCTrack>;
96 *mTracks[source][event] = *loadtracks;
97 delete loadtracks;
98 }
99 }
100}
101
103{
104 if (mTracks.at(source).at(eventID) != nullptr) {
105 delete mTracks[source][eventID];
106 mTracks[source][eventID] = nullptr;
107 }
108}
109
110void MCKinematicsReader::loadHeadersForSource(int source) const
111{
112 auto chain = mInputChains[source];
113 if (chain) {
114 // todo: get name from NameConfig
115 auto br = chain->GetBranch("MCEventHeader.");
116 if (br) {
117 o2::dataformats::MCEventHeader* header = nullptr;
118 br->SetAddress(&header);
119 mHeaders[source].resize(br->GetEntries());
120 for (int event = 0; event < br->GetEntries(); ++event) {
121 br->GetEntry(event);
122 mHeaders[source][event] = *header;
123 }
124 delete header;
125 header = nullptr;
126 } else {
127 LOG(warn) << "MCHeader branch not found";
128 }
129 }
130}
131
132void MCKinematicsReader::loadTrackRefsForSource(int source) const
133{
134 auto chain = mInputChains[source];
135 if (chain) {
136 // todo: get name from NameConfig
137 auto br = chain->GetBranch("TrackRefs");
138 if (br) {
139 std::vector<o2::TrackReference>* refs = nullptr;
140 br->SetAddress(&refs);
141 mIndexedTrackRefs[source].resize(br->GetEntries());
142 for (int event = 0; event < br->GetEntries(); ++event) {
143 br->GetEntry(event);
144 if (refs) {
145 // we convert the original flat vector into an indexed structure
146 initIndexedTrackRefs(*refs, mIndexedTrackRefs[source][event]);
147 delete refs;
148 refs = nullptr;
149 }
150 }
151 } else {
152 LOG(warn) << "TrackRefs branch not found";
153 }
154 }
155}
156
158{
159 if (mInitialized) {
160 LOG(info) << "MCKinematicsReader already initialized; doing nothing";
161 return false;
162 }
163
164 mInitialized = true;
165 mDigitizationContext = context;
166
167 // get the chains to read
168 mDigitizationContext->initSimKinematicsChains(mInputChains);
169
170 // load the kinematics information
171 mTracks.resize(mInputChains.size());
172 mHeaders.resize(mInputChains.size());
173 mIndexedTrackRefs.resize(mInputChains.size());
174
175 // actual loading will be done only if someone asks
176 // the first time for a particular source ...
177
178 return true;
179}
180
182{
183 if (mInitialized) {
184 LOG(info) << "MCKinematicsReader already initialized; doing nothing";
185 return false;
186 }
187
189 if (!context) {
190 return false;
191 }
192 mOwningDigiContext = true;
193 return initFromDigitContext(context);
194}
195
197{
198 if (mInitialized) {
199 LOG(info) << "MCKinematicsReader already initialized; doing nothing";
200 return false;
201 }
202 mInputChains.emplace_back(new TChain("o2sim"));
203 mInputChains.back()->AddFile(o2::base::NameConf::getMCKinematicsFileName(name.data()).c_str());
204 mTracks.resize(1);
205 mHeaders.resize(1);
206 mIndexedTrackRefs.resize(1);
207 mInitialized = true;
208
209 return true;
210}
GPUChain * chain
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 (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
std::string to_string(gsl::span< T, Size > span)
Definition common.h:52
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"