gstreamer
Loading...
Searching...
No Matches
gRootTree.cc
Go to the documentation of this file.
1#include "gRootTree.h"
3
4using std::vector;
5
6// Implementation summary:
7// Build ROOT TTrees lazily from sample headers or hits, then clear and refill
8// vector branches on each fill call.
9
10GRootTree::GRootTree([[maybe_unused]] const std::unique_ptr<GEventHeader>& gevent_header,
11 std::shared_ptr<GLogger>& logger) : log(logger) {
12 log->debug(CONSTRUCTOR, "GRootTreeROOT tree header");
13
14 root_tree =
16
17 // AutoFlush controls when buffered basket data are pushed to disk.
18 root_tree->SetAutoFlush(20 * 1024 * 1024);
19
20 // AutoSave periodically writes tree metadata snapshots for recoverability.
21 root_tree->SetAutoSave(50 * 1024 * 1024);
22
23 registerVariable("g4localEventNumber", gevent_header->getG4LocalEvn());
24 registerVariable("threadID", gevent_header->getThreadID());
25 registerVariable("timeStamp", gevent_header->getTimeStamp());
26}
27
28// fill the GEventHeader tree
29bool GRootTree::fillTree(const std::unique_ptr<GEventHeader>& gevent_header) {
30 log->info(
31 2, "Filling header tree for local event n. %1 threadID %2", gevent_header->getG4LocalEvn(),
32 gevent_header->getThreadID());
33
34 // Clear the vectors backing the branches before writing the next entry.
35 intVarsMap["g4localEventNumber"].clear();
36 intVarsMap["threadID"].clear();
37 stringVarsMap["timeStamp"].clear();
38
39 intVarsMap["g4localEventNumber"].emplace_back(gevent_header->getG4LocalEvn());
40 intVarsMap["threadID"].emplace_back(gevent_header->getThreadID());
41 stringVarsMap["timeStamp"].emplace_back(gevent_header->getTimeStamp());
42
43 root_tree->Fill();
44
45 return true;
46}
47
48// fill the GRunHeader tree
49bool GRootTree::fillTree(const std::unique_ptr<GRunHeader>& grun_header) {
50 log->info(2, "Filling header tree for run n. %1", grun_header->getRunID());
51
52 // Clear and refill the vectors backing the run-header branches.
53 intVarsMap["runID"].clear();
54 intVarsMap["runID"].emplace_back(grun_header->getRunID());
55
56 root_tree->Fill();
57
58 return true;
59}
60
61GRootTree::GRootTree([[maybe_unused]] const std::unique_ptr<GRunHeader>& grun_header,
62 std::shared_ptr<GLogger>& logger) : log(logger) {
63 log->debug(CONSTRUCTOR, "GRootTreeROOT tree header");
64
65 root_tree =
67 root_tree->SetAutoFlush(20 * 1024 * 1024);
68 root_tree->SetAutoSave(50 * 1024 * 1024);
69
70 registerVariable("runID", grun_header->getRunID());
71}
72
73
74// Implementation summary:
75// Create a true-information detector tree whose branch schema is inferred
76// from the first observed hit.
77GRootTree::GRootTree(const std::string& detectorName,
78 const GTrueInfoData* gdata,
79 std::shared_ptr<GLogger>& logger) : log(logger) {
80 log->debug(CONSTRUCTOR, "GRootTreeROOT tree True Info");
81
82 root_tree = std::make_unique<TTree>(detectorName.c_str(), gstreamer::root::TRUEINFOTREENAMEDESC);
83 root_tree->SetAutoFlush(20 * 1024 * 1024);
84 root_tree->SetAutoSave(50 * 1024 * 1024);
85
86 // add identity vars
87 auto identityMap = getIdentityMap(gdata->getIdentity());
88 for (auto& [varname, value] : identityMap) { registerVariable(varname, value); }
89
90 for (auto& [varname, value] : gdata->getDoubleVariablesMap()) { registerVariable(varname, value); }
91 for (auto& [varname, value] : gdata->getStringVariablesMap()) { registerVariable(varname, value); }
92}
93
94GRootTree::GRootTree(const std::string& treeName,
95 [[maybe_unused]] const GGeneratedParticleBank& particles,
96 std::shared_ptr<GLogger>& logger) : log(logger) {
97 log->debug(CONSTRUCTOR, "GRootTreeROOT tree Generated Particles");
98
99 root_tree = std::make_unique<TTree>(treeName.c_str(), gstreamer::root::GENERATEDTREENAMEDESC);
100 root_tree->SetAutoFlush(20 * 1024 * 1024);
101 root_tree->SetAutoSave(50 * 1024 * 1024);
102
103 registerVariable("pid", 0);
104 registerVariable("type", 0);
105 registerVariable("multiplicity", 0);
106 registerVariable("p", 0.0);
107 registerVariable("theta", 0.0);
108 registerVariable("phi", 0.0);
109 registerVariable("vx", 0.0);
110 registerVariable("vy", 0.0);
111 registerVariable("vz", 0.0);
112 registerVariable("name", std::string());
113}
114
115GRootTree::GRootTree([[maybe_unused]] const GAncestorBank& ancestors,
116 std::shared_ptr<GLogger>& logger) : log(logger) {
117 log->debug(CONSTRUCTOR, "GRootTreeROOT tree Ancestors");
119 root_tree->SetAutoFlush(20 * 1024 * 1024);
120 root_tree->SetAutoSave(50 * 1024 * 1024);
121
122 registerVariable("pid", 0);
123 registerVariable("tid", 0);
124 registerVariable("mtid", 0);
125 registerVariable("trackE", 0.0);
126 registerVariable("px", 0.0);
127 registerVariable("py", 0.0);
128 registerVariable("pz", 0.0);
129 registerVariable("vx", 0.0);
130 registerVariable("vy", 0.0);
131 registerVariable("vz", 0.0);
132}
133
134
135// Implementation summary:
136// Create a digitized detector tree whose branch schema is inferred
137// from the first observed hit.
138GRootTree::GRootTree(const std::string& detectorName,
139 const GDigitizedData* gdata,
140 std::shared_ptr<GLogger>& logger) : log(logger) {
141 log->debug(CONSTRUCTOR, "GRootTreeROOT tree Digitized Data");
142
143 root_tree = std::make_unique<TTree>(detectorName.c_str(), gstreamer::root::DIGITIZEDTREENAMEDESC);
144 root_tree->SetAutoFlush(20 * 1024 * 1024);
145 root_tree->SetAutoSave(50 * 1024 * 1024);
146
147 // add identity vars
148 auto identityMap = getIdentityMap(gdata->getIdentity());
149 for (auto& [varname, value] : identityMap) { registerVariable(varname, value, true); }
150
151 for (auto& [varname, value] : gdata->getIntObservablesMap(0)) {
152 registerVariable(varname, value);
153 }
154 for (auto& [varname, value] : gdata->getDblObservablesMap(0)) { registerVariable(varname, value); }
155}
156
157
158// fill the True Info Tree
159bool GRootTree::fillTree(const std::vector<const GTrueInfoData*>& trueInfoData) {
160 // Reset all branch vectors before repopulating them for this detector collection.
161 for (auto& [varname, values] : intVarsMap) { values.clear(); }
162 for (auto& [varname, values] : doubleVarsMap) { values.clear(); }
163 for (auto& [varname, values] : stringVarsMap) { values.clear(); }
164
165
166 for (auto& dataHits : trueInfoData) {
167 // fill identity vars
168 auto identityMap = getIdentityMap(dataHits->getIdentity());
169 for (auto& [varname, value] : identityMap) {
170 intVarsMap[varname].push_back(value);
171 }
172
173 for (auto& [varname, value] : dataHits->getDoubleVariablesMap()) { doubleVarsMap[varname].push_back(value); }
174 for (auto& [varname, value] : dataHits->getStringVariablesMap()) { stringVarsMap[varname].push_back(value); }
175 }
176
177 root_tree->Fill();
178
179 return true;
180}
181
182// fill the Digitized Data Tree
183bool GRootTree::fillTree(const std::vector<const GDigitizedData*>& digitizedData) {
184 // Reset all branch vectors before repopulating them for this detector collection.
185 for (auto& [varname, values] : intVarsMap) { values.clear(); }
186 for (auto& [varname, values] : doubleVarsMap) { values.clear(); }
187 for (auto& [varname, values] : stringVarsMap) { values.clear(); }
188
189
190 for (auto& dataHits : digitizedData) {
191 // fill identity vars
192 auto identityMap = getIdentityMap(dataHits->getIdentity());
193 for (auto& [varname, value] : identityMap) {
194 intVarsMap[varname].push_back(value);
195 }
196
197
198 for (auto& [varname, value] : dataHits->getIntObservablesMap(0)) { intVarsMap[varname].push_back(value); }
199 for (auto& [varname, value] : dataHits->getDblObservablesMap(0)) { doubleVarsMap[varname].push_back(value); }
200 }
201 root_tree->Fill();
202
203 return true;
204}
205
207 for (auto& [varname, values] : intVarsMap) { values.clear(); }
208 for (auto& [varname, values] : doubleVarsMap) { values.clear(); }
209 for (auto& [varname, values] : stringVarsMap) { values.clear(); }
210
211 for (const auto& particle : particles) {
212 intVarsMap["pid"].push_back(particle.pid);
213 intVarsMap["type"].push_back(particle.type);
214 intVarsMap["multiplicity"].push_back(particle.multiplicity);
215 doubleVarsMap["p"].push_back(particle.p);
216 doubleVarsMap["theta"].push_back(particle.theta);
217 doubleVarsMap["phi"].push_back(particle.phi);
218 doubleVarsMap["vx"].push_back(particle.vx);
219 doubleVarsMap["vy"].push_back(particle.vy);
220 doubleVarsMap["vz"].push_back(particle.vz);
221 stringVarsMap["name"].push_back(particle.name);
222 }
223
224 root_tree->Fill();
225 return true;
226}
227
228bool GRootTree::fillTree(const GAncestorBank& ancestors) {
229 for (auto& [varname, values] : intVarsMap) { values.clear(); }
230 for (auto& [varname, values] : doubleVarsMap) { values.clear(); }
231
232 for (const auto& ancestor : ancestors) {
233 intVarsMap["pid"].push_back(ancestor.pid);
234 intVarsMap["tid"].push_back(ancestor.tid);
235 intVarsMap["mtid"].push_back(ancestor.mtid);
236 doubleVarsMap["trackE"].push_back(ancestor.trackE);
237 doubleVarsMap["px"].push_back(ancestor.px);
238 doubleVarsMap["py"].push_back(ancestor.py);
239 doubleVarsMap["pz"].push_back(ancestor.pz);
240 doubleVarsMap["vx"].push_back(ancestor.vx);
241 doubleVarsMap["vy"].push_back(ancestor.vy);
242 doubleVarsMap["vz"].push_back(ancestor.vz);
243 }
244
245 root_tree->Fill();
246 return true;
247}
248
249
250// Implementation summary:
251// Register one branch and bind it to the appropriate backing vector map.
252// The second parameter is used only to select the correct overload.
253
254void GRootTree::registerVariable(const std::string& varname, [[maybe_unused]] int value,
255 bool can_ignore_duplicates) {
256 if (can_ignore_duplicates) {
257 root_tree->Branch(varname.c_str(), &intVarsMap[varname]);
258 }
259 else {
260 if (!intVarsMap.contains(varname)) {
261 root_tree->Branch(varname.c_str(), &intVarsMap[varname]);
262 }
263 else {
264 log->error(
266 "variable <%1> already registered in the int variable map of tree %2", varname,
267 root_tree->GetName());
268 }
269 }
270}
271
272void GRootTree::registerVariable(const std::string& varname, [[maybe_unused]] double value) {
273 if (!doubleVarsMap.contains(varname)) {
274 root_tree->Branch(varname.c_str(), &doubleVarsMap[varname]);
275 }
276 else {
277 log->error(
279 "variable <%1> already registered in the double variable map of tree %2", varname,
280 root_tree->GetName());
281 }
282}
283
284void GRootTree::registerVariable(const std::string& varname, [[maybe_unused]] const std::string& value) {
285 if (!stringVarsMap.contains(varname)) {
286 root_tree->Branch(varname.c_str(), &stringVarsMap[varname]);
287 }
288 else {
289 log->error(
291 "variable <%1> already registered in the string variable map of tree %2", varname,
292 root_tree->GetName());
293 }
294}
const std::vector< GIdentifier > & getIdentity() const
std::map< std::string, int > getIntObservablesMap(int which) const
std::map< std::string, double > getDblObservablesMap(int which) const
GRootTree(const std::unique_ptr< GEventHeader > &gevent_header, std::shared_ptr< GLogger > &log)
Construct an event-header tree and register its branches.
Definition gRootTree.cc:10
bool fillTree(const std::unique_ptr< GEventHeader > &gevent_header)
Fill the event-header tree with one event header entry.
Definition gRootTree.cc:29
std::map< std::string, double > getDoubleVariablesMap() const
const std::vector< GIdentifier > & getIdentity() const
std::map< std::string, std::string > getStringVariablesMap() const
std::vector< GAncestorData > GAncestorBank
std::vector< GGeneratedParticleData > GGeneratedParticleBank
ROOT tree adapter used internally by the ROOT gstreamer plugin.
std::map< std::string, int > getIdentityMap(std::vector< GIdentifier > gidentity)
CONSTRUCTOR
Shared constants and error codes for the gstreamer module.
constexpr char RUNHEADERTREENAMEDESC[]
Definition gRootTree.h:24
constexpr char GENERATEDTREENAMEDESC[]
Definition gRootTree.h:19
constexpr char RUNHEADERTREENAME[]
Definition gRootTree.h:16
constexpr char EVENTHEADERTREENAME[]
Definition gRootTree.h:15
constexpr char ANCESTORTREENAMEDESC[]
Definition gRootTree.h:21
constexpr char EVENTHEADERTREENAMEDESC[]
Definition gRootTree.h:23
constexpr char TRUEINFOTREENAMEDESC[]
Definition gRootTree.h:25
constexpr char ANCESTORTREENAME[]
Definition gRootTree.h:20
constexpr char DIGITIZEDTREENAMEDESC[]
Definition gRootTree.h:26
constexpr int ERR_GSTREAMERVARIABLEEXISTS
Duplicate variable registration was attempted in a streamer-specific schema.