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 <vector>
18#include <fairlogger/Logger.h>
19
20using namespace o2::steer;
21
23{
24 for (auto chain : mInputChains) {
25 delete chain;
26 }
27 mInputChains.clear();
28
29 if (mDigitizationContext && mOwningDigiContext) {
30 delete mDigitizationContext;
31 }
32}
33
34void MCKinematicsReader::initIndexedTrackRefs(std::vector<o2::TrackReference>& refs, o2::dataformats::MCTruthContainer<o2::TrackReference>& indexedrefs) const
35{
36 // sort trackrefs according to track index then according to track length
37 std::sort(refs.begin(), refs.end(), [](const o2::TrackReference& a, const o2::TrackReference& b) {
38 if (a.getTrackID() == b.getTrackID()) {
39 return a.getLength() < b.getLength();
40 }
41 return a.getTrackID() < b.getTrackID();
42 });
43
44 // make final indexed container for track references
45 indexedrefs.clear();
46 for (auto& ref : refs) {
47 if (ref.getTrackID() >= 0) {
48 indexedrefs.addElement(ref.getTrackID(), ref);
49 }
50 }
51}
52
53void MCKinematicsReader::initTracksForSource(int source) const
54{
55 auto chain = mInputChains[source];
56 if (chain) {
57 // todo: get name from NameConfig
58 auto br = chain->GetBranch("MCTrack");
59 mTracks[source].resize(br->GetEntries(), nullptr);
60 }
61}
62
63void MCKinematicsReader::loadTracksForSourceAndEvent(int source, int event) const
64{
65 auto chain = mInputChains[source];
66 if (chain) {
67 // todo: get name from NameConfig
68 auto br = chain->GetBranch("MCTrack");
69 if (br) {
70 std::vector<MCTrack>* loadtracks = nullptr;
71 br->SetAddress(&loadtracks);
72 br->GetEntry(event);
73 mTracks[source][event] = new std::vector<o2::MCTrack>;
74 *mTracks[source][event] = *loadtracks;
75 delete loadtracks;
76 }
77 }
78}
79
81{
82 if (mTracks.at(source).at(eventID) != nullptr) {
83 delete mTracks[source][eventID];
84 mTracks[source][eventID] = nullptr;
85 }
86}
87
88void MCKinematicsReader::loadHeadersForSource(int source) const
89{
90 auto chain = mInputChains[source];
91 if (chain) {
92 // todo: get name from NameConfig
93 auto br = chain->GetBranch("MCEventHeader.");
94 if (br) {
95 o2::dataformats::MCEventHeader* header = nullptr;
96 br->SetAddress(&header);
97 mHeaders[source].resize(br->GetEntries());
98 for (int event = 0; event < br->GetEntries(); ++event) {
99 br->GetEntry(event);
100 mHeaders[source][event] = *header;
101 }
102 delete header;
103 header = nullptr;
104 } else {
105 LOG(warn) << "MCHeader branch not found";
106 }
107 }
108}
109
110void MCKinematicsReader::loadTrackRefsForSource(int source) const
111{
112 auto chain = mInputChains[source];
113 if (chain) {
114 // todo: get name from NameConfig
115 auto br = chain->GetBranch("TrackRefs");
116 if (br) {
117 std::vector<o2::TrackReference>* refs = nullptr;
118 br->SetAddress(&refs);
119 mIndexedTrackRefs[source].resize(br->GetEntries());
120 for (int event = 0; event < br->GetEntries(); ++event) {
121 br->GetEntry(event);
122 if (refs) {
123 // we convert the original flat vector into an indexed structure
124 initIndexedTrackRefs(*refs, mIndexedTrackRefs[source][event]);
125 delete refs;
126 refs = nullptr;
127 }
128 }
129 } else {
130 LOG(warn) << "TrackRefs branch not found";
131 }
132 }
133}
134
136{
137 if (mInitialized) {
138 LOG(info) << "MCKinematicsReader already initialized; doing nothing";
139 return false;
140 }
141
142 mInitialized = true;
143 mDigitizationContext = context;
144
145 // get the chains to read
146 mDigitizationContext->initSimKinematicsChains(mInputChains);
147
148 // load the kinematics information
149 mTracks.resize(mInputChains.size());
150 mHeaders.resize(mInputChains.size());
151 mIndexedTrackRefs.resize(mInputChains.size());
152
153 // actual loading will be done only if someone asks
154 // the first time for a particular source ...
155
156 return true;
157}
158
160{
161 if (mInitialized) {
162 LOG(info) << "MCKinematicsReader already initialized; doing nothing";
163 return false;
164 }
165
167 if (!context) {
168 return false;
169 }
170 mOwningDigiContext = true;
171 return initFromDigitContext(context);
172}
173
175{
176 if (mInitialized) {
177 LOG(info) << "MCKinematicsReader already initialized; doing nothing";
178 return false;
179 }
180 mInputChains.emplace_back(new TChain("o2sim"));
181 mInputChains.back()->AddFile(o2::base::NameConf::getMCKinematicsFileName(name.data()).c_str());
182 mTracks.resize(1);
183 mHeaders.resize(1);
184 mIndexedTrackRefs.resize(1);
185 mInitialized = true;
186
187 return true;
188}
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
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
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"