Project
Loading...
Searching...
No Matches
MCKinematicsReader.h
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#ifndef MC_KINEMATICS_READER_H
13#define MC_KINEMATICS_READER_H
14
21#include <vector>
22
23class TChain;
24
25namespace o2
26{
27
28namespace steer
29{
30
32{
33 public:
34 enum class Mode {
37 };
38
40 MCKinematicsReader() = default;
41
44
50 {
51 if (mode == Mode::kMCKine) {
53 } else if (mode == Mode::kDigiContext) {
55 }
56 }
57
63
66 bool initFromDigitContext(std::string_view filename);
67
69
71 bool initFromKinematics(std::string_view filename);
72
73 bool isInitialized() const { return mInitialized; }
74
77 MCTrack const* getTrack(o2::MCCompLabel const&) const;
78
81 MCTrack const* getTrack(int source, int event, int track) const;
82
85 MCTrack const* getTrack(int event, int track) const;
86
88 std::vector<MCTrack> const& getTracks(int source, int event) const;
89
92
94 std::vector<MCTrack> const& getTracks(int event) const;
95
97
99
101
103 gsl::span<o2::TrackReference> getTrackRefs(int source, int event, int track) const;
105 const std::vector<o2::TrackReference>& getTrackRefsByEvent(int source, int event) const;
107 gsl::span<o2::TrackReference> getTrackRefs(int event, int track) const;
108
111
113 size_t getNSources() const;
114
116 size_t getNEvents(int source) const;
117
119 {
120 return mDigitizationContext;
121 }
122
123 private:
125 void ensureTracksForSourceAndEvent(int source, int event) const;
126 [[noreturn]] static void reportMissingSource(int source, size_t available);
127 [[noreturn]] static void reportMissingEvent(const char* what, int source, int event, size_t available);
128
129 void initTracksForSource(int source) const;
130 void loadTracksForSourceAndEvent(int source, int eventID) const;
131 void loadHeadersForSource(int source) const;
132 void loadTrackRefsForSource(int source) const;
133 void initIndexedTrackRefs(std::vector<o2::TrackReference>& refs, o2::dataformats::MCTruthContainer<o2::TrackReference>& indexedrefs) const;
134
135 DigitizationContext const* mDigitizationContext = nullptr;
136 bool mOwningDigiContext = false;
137
138 // chains for each source
139 std::vector<TChain*> mInputChains;
140
141 // a vector of tracks foreach source and each collision
142 mutable std::vector<std::vector<std::vector<o2::MCTrack>*>> mTracks; // the in-memory track container
143 mutable std::vector<std::vector<o2::dataformats::MCEventHeader>> mHeaders; // the in-memory header container
144 mutable std::vector<std::vector<o2::dataformats::MCTruthContainer<o2::TrackReference>>> mIndexedTrackRefs; // the in-memory track ref container
145
146 bool mInitialized = false; // whether initialized
147};
148
150{
151 const auto source = label.getSourceID();
152 const auto event = label.getEventID();
153 const auto track = label.getTrackID();
154 return getTrack(source, event, track);
155}
156
157inline MCTrack const* MCKinematicsReader::getTrack(int source, int event, int track) const
158{
159 auto const& tracks = getTracks(source, event);
160 // one comparison, and it covers the negative track ID of a hit whose track was not kept
161 return static_cast<size_t>(track) < tracks.size() ? &tracks[track] : nullptr;
162}
163
164inline MCTrack const* MCKinematicsReader::getTrack(int event, int track) const
165{
166 return getTrack(0, event, track);
167}
168
169inline std::vector<MCTrack> const& MCKinematicsReader::getTracks(int source, int event) const
170{
171 if (static_cast<size_t>(source) >= mTracks.size()) {
172 reportMissingSource(source, mTracks.size());
173 }
174 auto& perEvent = mTracks[source];
175 if (perEvent.size() == 0) {
176 initTracksForSource(source);
177 }
178 // the event range shares the branch of the lazy load, so the fast path grows by one comparison
179 if (static_cast<size_t>(event) >= perEvent.size() || perEvent[event] == nullptr) {
180 ensureTracksForSourceAndEvent(source, event);
181 }
182 return *perEvent[event];
183}
184
185inline std::vector<MCTrack> const& MCKinematicsReader::getTracks(int event) const
186{
187 return getTracks(0, event);
188}
189
191{
192 auto const& headers = mHeaders.at(source);
193 if (headers.size() == 0) {
194 loadHeadersForSource(source);
195 }
196 if (static_cast<size_t>(event) >= headers.size()) {
197 reportMissingEvent("event headers", source, event, headers.size());
198 }
199 return headers[event];
200}
201
202inline gsl::span<o2::TrackReference> MCKinematicsReader::getTrackRefs(int source, int event, int track) const
203{
204 if (static_cast<size_t>(source) >= mIndexedTrackRefs.size()) {
205 return {};
206 }
207 auto& perEvent = mIndexedTrackRefs[source];
208 if (perEvent.size() == 0) {
209 loadTrackRefsForSource(source);
210 }
211 if (static_cast<size_t>(event) >= perEvent.size()) {
212 return {};
213 }
214 return perEvent[event].getLabels(track);
215}
216
217inline const std::vector<o2::TrackReference>& MCKinematicsReader::getTrackRefsByEvent(int source, int event) const
218{
219 auto const& perEvent = mIndexedTrackRefs.at(source);
220 if (perEvent.size() == 0) {
221 loadTrackRefsForSource(source);
222 }
223 if (static_cast<size_t>(event) >= perEvent.size()) {
224 reportMissingEvent("events of track references", source, event, perEvent.size());
225 }
226 return perEvent[event].getTruthArray();
227}
228
229inline gsl::span<o2::TrackReference> MCKinematicsReader::getTrackRefs(int event, int track) const
230{
231 return getTrackRefs(0, event, track);
232}
233
235{
236 return mTracks.size();
237}
238
240{
241 if (mTracks[source].size() == 0) {
242 initTracksForSource(source);
243 }
244 return mTracks[source].size();
245}
246
247} // namespace steer
248} // namespace o2
249
250#endif
Definition of the MCTrack class.
Definition of a container to keep Monte Carlo truth external to simulation objects.
A container to hold and manage MC truth information/labels.
bool initFromKinematics(std::string_view filename)
inits the reader from a simple kinematics file
bool initFromDigitContext(std::string_view filename)
DigitizationContext const * getDigitizationContext() const
MCKinematicsReader()=default
default constructor
MCTrack const * getTrack(o2::MCCompLabel const &) const
size_t getNEvents(int source) const
Get number of events.
MCKinematicsReader(std::string_view name, Mode mode=Mode::kDigiContext)
o2::dataformats::MCEventHeader const & getMCEventHeader(int source, int event) const
retrieves the MCEventHeader for a given eventID and sourceID
void releaseTracksForSourceAndEvent(int source, int event)
API to ask releasing tracks (freeing memory) for source + event.
size_t getNSources() const
Get number of sources.
const std::vector< o2::TrackReference > & getTrackRefsByEvent(int source, int event) const
return all track references associated to a source/event
MCKinematicsReader(o2::steer::DigitizationContext const *context)
constructing directly from a digitization context
gsl::span< o2::TrackReference > getTrackRefs(int source, int event, int track) const
get all primaries for a certain event
std::vector< MCTrack > const & getTracks(int source, int event) const
variant returning all tracks for source and event at once
struct _cl_event * event
Definition glcorearb.h:2982
GLenum mode
Definition glcorearb.h:266
GLsizeiptr size
Definition glcorearb.h:659
GLuint const GLchar * name
Definition glcorearb.h:781
GLsizei GLsizei GLchar * source
Definition glcorearb.h:798
GLuint GLsizei const GLchar * label
Definition glcorearb.h:2519
a couple of static helper functions to create timestamp values for CCDB queries or override obsolete ...
std::string filename()