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, "GRootTree", "ROOT tree header");
13
14 root_tree = std::make_unique<TTree>(EVENTHEADERTREENAME, EVENTHEADERTREENAMEDESC);
15
16 // AutoFlush controls when buffered basket data are pushed to disk.
17 root_tree->SetAutoFlush(20 * 1024 * 1024);
18
19 // AutoSave periodically writes tree metadata snapshots for recoverability.
20 root_tree->SetAutoSave(50 * 1024 * 1024);
21
22 registerVariable("g4localEventNumber", gevent_header->getG4LocalEvn());
23 registerVariable("threadID", gevent_header->getThreadID());
24 registerVariable("timeStamp", gevent_header->getTimeStamp());
25}
26
27// fill the GEventHeader tree
28bool GRootTree::fillTree(const std::unique_ptr<GEventHeader>& gevent_header) {
29 log->info(2, "Filling header tree for local event n. ", gevent_header->getG4LocalEvn(), " threadID ",
30 gevent_header->getThreadID());
31
32 // Clear the vectors backing the branches before writing the next entry.
33 intVarsMap["g4localEventNumber"].clear();
34 intVarsMap["threadID"].clear();
35 stringVarsMap["timeStamp"].clear();
36
37 intVarsMap["g4localEventNumber"].emplace_back(gevent_header->getG4LocalEvn());
38 intVarsMap["threadID"].emplace_back(gevent_header->getThreadID());
39 stringVarsMap["timeStamp"].emplace_back(gevent_header->getTimeStamp());
40
41 root_tree->Fill();
42
43 return true;
44}
45
46// fill the GRunHeader tree
47bool GRootTree::fillTree(const std::unique_ptr<GRunHeader>& grun_header) {
48 log->info(2, "Filling header tree for run n. ", grun_header->getRunID());
49
50 // Clear and refill the vectors backing the run-header branches.
51 intVarsMap["runID"].clear();
52 intVarsMap["runID"].emplace_back(grun_header->getRunID());
53
54 root_tree->Fill();
55
56 return true;
57}
58
59GRootTree::GRootTree([[maybe_unused]] const std::unique_ptr<GRunHeader>& grun_header,
60 std::shared_ptr<GLogger>& logger) : log(logger) {
61 log->debug(CONSTRUCTOR, "GRootTree", "ROOT tree header");
62
63 root_tree = std::make_unique<TTree>(RUNHEADERTREENAME, RUNHEADERTREENAMEDESC);
64 root_tree->SetAutoFlush(20 * 1024 * 1024);
65 root_tree->SetAutoSave(50 * 1024 * 1024);
66
67 registerVariable("runID", grun_header->getRunID());
68}
69
70
71// Implementation summary:
72// Create a true-information detector tree whose branch schema is inferred
73// from the first observed hit.
74GRootTree::GRootTree(const std::string& detectorName,
75 const GTrueInfoData* gdata,
76 std::shared_ptr<GLogger>& logger) : log(logger) {
77 log->debug(CONSTRUCTOR, "GRootTree", "ROOT tree True Info");
78
79 root_tree = std::make_unique<TTree>(detectorName.c_str(), TRUEINFOTREENAMEDESC);
80 root_tree->SetAutoFlush(20 * 1024 * 1024);
81 root_tree->SetAutoSave(50 * 1024 * 1024);
82
83 // add identity vars
84 auto identityMap = getIdentityMap(gdata->getIdentity());
85 for (auto& [varname, value] : identityMap) { registerVariable(varname, value); }
86
87 for (auto& [varname, value] : gdata->getDoubleVariablesMap()) { registerVariable(varname, value); }
88 for (auto& [varname, value] : gdata->getStringVariablesMap()) { registerVariable(varname, value); }
89}
90
91GRootTree::GRootTree(const std::string& treeName,
92 [[maybe_unused]] const GGeneratedParticleBank& particles,
93 std::shared_ptr<GLogger>& logger) : log(logger) {
94 log->debug(CONSTRUCTOR, "GRootTree", "ROOT tree Generated Particles");
95
96 root_tree = std::make_unique<TTree>(treeName.c_str(), GENERATEDTREENAMEDESC);
97 root_tree->SetAutoFlush(20 * 1024 * 1024);
98 root_tree->SetAutoSave(50 * 1024 * 1024);
99
100 registerVariable("pid", 0);
101 registerVariable("type", 0);
102 registerVariable("multiplicity", 0);
103 registerVariable("p", 0.0);
104 registerVariable("theta", 0.0);
105 registerVariable("phi", 0.0);
106 registerVariable("vx", 0.0);
107 registerVariable("vy", 0.0);
108 registerVariable("vz", 0.0);
109 registerVariable("name", std::string());
110}
111
112
113// Implementation summary:
114// Create a digitized detector tree whose branch schema is inferred
115// from the first observed hit.
116GRootTree::GRootTree(const std::string& detectorName,
117 const GDigitizedData* gdata,
118 std::shared_ptr<GLogger>& logger) : log(logger) {
119 log->debug(CONSTRUCTOR, "GRootTree", "ROOT tree Digitized Data");
120
121 root_tree = std::make_unique<TTree>(detectorName.c_str(), DIGITIZEDTREENAMEDESC);
122 root_tree->SetAutoFlush(20 * 1024 * 1024);
123 root_tree->SetAutoSave(50 * 1024 * 1024);
124
125 // add identity vars
126 auto identityMap = getIdentityMap(gdata->getIdentity());
127 for (auto& [varname, value] : identityMap) { registerVariable(varname, value, true); }
128
129 for (auto& [varname, value] : gdata->getIntObservablesMap(0)) {
130 registerVariable(varname, value);
131 }
132 for (auto& [varname, value] : gdata->getDblObservablesMap(0)) { registerVariable(varname, value); }
133}
134
135
136// fill the True Info Tree
137bool GRootTree::fillTree(const vector<const GTrueInfoData*>& trueInfoData) {
138 // Reset all branch vectors before repopulating them for this detector collection.
139 for (auto& [varname, values] : intVarsMap) { values.clear(); }
140 for (auto& [varname, values] : doubleVarsMap) { values.clear(); }
141 for (auto& [varname, values] : stringVarsMap) { values.clear(); }
142
143
144 for (auto& dataHits : trueInfoData) {
145 // fill identity vars
146 auto identityMap = getIdentityMap(dataHits->getIdentity());
147 for (auto& [varname, value] : identityMap) {
148 intVarsMap[varname].push_back(value);
149 }
150
151 for (auto& [varname, value] : dataHits->getDoubleVariablesMap()) { doubleVarsMap[varname].push_back(value); }
152 for (auto& [varname, value] : dataHits->getStringVariablesMap()) { stringVarsMap[varname].push_back(value); }
153 }
154
155 root_tree->Fill();
156
157 return true;
158}
159
160// fill the Digitized Data Tree
161bool GRootTree::fillTree(const vector<const GDigitizedData*>& digitizedData) {
162 // Reset all branch vectors before repopulating them for this detector collection.
163 for (auto& [varname, values] : intVarsMap) { values.clear(); }
164 for (auto& [varname, values] : doubleVarsMap) { values.clear(); }
165 for (auto& [varname, values] : stringVarsMap) { values.clear(); }
166
167
168 for (auto& dataHits : digitizedData) {
169 // fill identity vars
170 auto identityMap = getIdentityMap(dataHits->getIdentity());
171 for (auto& [varname, value] : identityMap) {
172 intVarsMap[varname].push_back(value);
173 }
174
175
176 for (auto& [varname, value] : dataHits->getIntObservablesMap(0)) { intVarsMap[varname].push_back(value); }
177 for (auto& [varname, value] : dataHits->getDblObservablesMap(0)) { doubleVarsMap[varname].push_back(value); }
178 }
179 root_tree->Fill();
180
181 return true;
182}
183
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 for (const auto& particle : particles) {
190 intVarsMap["pid"].push_back(particle.pid);
191 intVarsMap["type"].push_back(particle.type);
192 intVarsMap["multiplicity"].push_back(particle.multiplicity);
193 doubleVarsMap["p"].push_back(particle.p);
194 doubleVarsMap["theta"].push_back(particle.theta);
195 doubleVarsMap["phi"].push_back(particle.phi);
196 doubleVarsMap["vx"].push_back(particle.vx);
197 doubleVarsMap["vy"].push_back(particle.vy);
198 doubleVarsMap["vz"].push_back(particle.vz);
199 stringVarsMap["name"].push_back(particle.name);
200 }
201
202 root_tree->Fill();
203 return true;
204}
205
206
207// Implementation summary:
208// Register one branch and bind it to the appropriate backing vector map.
209// The second parameter is used only to select the correct overload.
210
211void GRootTree::registerVariable(const std::string& varname, [[maybe_unused]] int value, bool can_ignore_duplicates) {
212 if (can_ignore_duplicates) {
213 root_tree->Branch(varname.c_str(), &intVarsMap[varname]);
214 }
215 else {
216 if (intVarsMap.find(varname) == intVarsMap.end()) { root_tree->Branch(varname.c_str(), &intVarsMap[varname]); }
217 else {
218 log->error(ERR_GSTREAMERVARIABLEEXISTS, "variable <", varname,
219 "> already registered in the int variable map of tree ", root_tree->GetName());
220 }
221 }
222}
223
224void GRootTree::registerVariable(const std::string& varname, [[maybe_unused]] double value) {
225 if (doubleVarsMap.find(varname) == doubleVarsMap.end()) {
226 root_tree->Branch(varname.c_str(), &doubleVarsMap[varname]);
227 }
228 else {
229 log->error(ERR_GSTREAMERVARIABLEEXISTS, "variable <", varname,
230 "> already registered in the double variable map of tree ", root_tree->GetName());
231 }
232}
233
234void GRootTree::registerVariable(const std::string& varname, [[maybe_unused]] const std::string& value) {
235 if (stringVarsMap.find(varname) == stringVarsMap.end()) {
236 root_tree->Branch(varname.c_str(), &stringVarsMap[varname]);
237 }
238 else {
239 log->error(ERR_GSTREAMERVARIABLEEXISTS, "variable <", varname,
240 "> already registered in the string variable map of tree ", root_tree->GetName());
241 }
242}
const std::vector< GIdentifier > & getIdentity() const
std::map< std::string, int > getIntObservablesMap(int which) const
std::map< std::string, double > getDblObservablesMap(int which) const
void debug(debug_type type, Args &&... args) const
void info(int level, Args &&... args) const
void error(int exit_code, Args &&... args) 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:28
std::map< std::string, double > getDoubleVariablesMap() const
const std::vector< GIdentifier > & getIdentity() const
std::map< std::string, std::string > getStringVariablesMap() const
std::vector< GGeneratedParticleData > GGeneratedParticleBank
ROOT tree adapter used internally by the ROOT gstreamer plugin.
#define DIGITIZEDTREENAMEDESC
Definition gRootTree.h:25
#define TRUEINFOTREENAMEDESC
Definition gRootTree.h:24
#define RUNHEADERTREENAMEDESC
Definition gRootTree.h:23
#define EVENTHEADERTREENAMEDESC
Definition gRootTree.h:22
#define EVENTHEADERTREENAME
Definition gRootTree.h:14
#define RUNHEADERTREENAME
Definition gRootTree.h:15
#define GENERATEDTREENAMEDESC
Definition gRootTree.h:18
std::map< std::string, int > getIdentityMap(std::vector< GIdentifier > gidentity)
CONSTRUCTOR
Shared constants and error codes for the gstreamer module.
#define ERR_GSTREAMERVARIABLEEXISTS
Duplicate variable registration was attempted in a streamer-specific schema.