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
91
92// Implementation summary:
93// Create a digitized detector tree whose branch schema is inferred
94// from the first observed hit.
95GRootTree::GRootTree(const std::string& detectorName,
96 const GDigitizedData* gdata,
97 std::shared_ptr<GLogger>& logger) : log(logger) {
98 log->debug(CONSTRUCTOR, "GRootTree", "ROOT tree Digitized Data");
99
100 root_tree = std::make_unique<TTree>(detectorName.c_str(), DIGITIZEDTREENAMEDESC);
101 root_tree->SetAutoFlush(20 * 1024 * 1024);
102 root_tree->SetAutoSave(50 * 1024 * 1024);
103
104 // add identity vars
105 auto identityMap = getIdentityMap(gdata->getIdentity());
106 for (auto& [varname, value] : identityMap) { registerVariable(varname, value, true); }
107
108 for (auto& [varname, value] : gdata->getIntObservablesMap(0)) {
109 registerVariable(varname, value);
110 }
111 for (auto& [varname, value] : gdata->getDblObservablesMap(0)) { registerVariable(varname, value); }
112}
113
114
115// fill the True Info Tree
116bool GRootTree::fillTree(const vector<const GTrueInfoData*>& trueInfoData) {
117 // Reset all branch vectors before repopulating them for this detector collection.
118 for (auto& [varname, values] : intVarsMap) { values.clear(); }
119 for (auto& [varname, values] : doubleVarsMap) { values.clear(); }
120 for (auto& [varname, values] : stringVarsMap) { values.clear(); }
121
122
123 for (auto& dataHits : trueInfoData) {
124 // fill identity vars
125 auto identityMap = getIdentityMap(dataHits->getIdentity());
126 for (auto& [varname, value] : identityMap) {
127 intVarsMap[varname].push_back(value);
128 }
129
130 for (auto& [varname, value] : dataHits->getDoubleVariablesMap()) { doubleVarsMap[varname].push_back(value); }
131 for (auto& [varname, value] : dataHits->getStringVariablesMap()) { stringVarsMap[varname].push_back(value); }
132 }
133
134 root_tree->Fill();
135
136 return true;
137}
138
139// fill the Digitized Data Tree
140bool GRootTree::fillTree(const vector<const GDigitizedData*>& digitizedData) {
141 // Reset all branch vectors before repopulating them for this detector collection.
142 for (auto& [varname, values] : intVarsMap) { values.clear(); }
143 for (auto& [varname, values] : doubleVarsMap) { values.clear(); }
144 for (auto& [varname, values] : stringVarsMap) { values.clear(); }
145
146
147 for (auto& dataHits : digitizedData) {
148 // fill identity vars
149 auto identityMap = getIdentityMap(dataHits->getIdentity());
150 for (auto& [varname, value] : identityMap) {
151 intVarsMap[varname].push_back(value);
152 }
153
154
155 for (auto& [varname, value] : dataHits->getIntObservablesMap(0)) { intVarsMap[varname].push_back(value); }
156 for (auto& [varname, value] : dataHits->getDblObservablesMap(0)) { doubleVarsMap[varname].push_back(value); }
157 }
158 root_tree->Fill();
159
160 return true;
161}
162
163
164// Implementation summary:
165// Register one branch and bind it to the appropriate backing vector map.
166// The second parameter is used only to select the correct overload.
167
168void GRootTree::registerVariable(const std::string& varname, [[maybe_unused]] int value, bool can_ignore_duplicates) {
169 if (can_ignore_duplicates) {
170 root_tree->Branch(varname.c_str(), &intVarsMap[varname]);
171 }
172 else {
173 if (intVarsMap.find(varname) == intVarsMap.end()) { root_tree->Branch(varname.c_str(), &intVarsMap[varname]); }
174 else {
175 log->error(ERR_GSTREAMERVARIABLEEXISTS, "variable <", varname,
176 "> already registered in the int variable map of tree ", root_tree->GetName());
177 }
178 }
179}
180
181void GRootTree::registerVariable(const std::string& varname, [[maybe_unused]] double value) {
182 if (doubleVarsMap.find(varname) == doubleVarsMap.end()) {
183 root_tree->Branch(varname.c_str(), &doubleVarsMap[varname]);
184 }
185 else {
186 log->error(ERR_GSTREAMERVARIABLEEXISTS, "variable <", varname,
187 "> already registered in the double variable map of tree ", root_tree->GetName());
188 }
189}
190
191void GRootTree::registerVariable(const std::string& varname, [[maybe_unused]] const std::string& value) {
192 if (stringVarsMap.find(varname) == stringVarsMap.end()) {
193 root_tree->Branch(varname.c_str(), &stringVarsMap[varname]);
194 }
195 else {
196 log->error(ERR_GSTREAMERVARIABLEEXISTS, "variable <", varname,
197 "> already registered in the string variable map of tree ", root_tree->GetName());
198 }
199}
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
ROOT tree adapter used internally by the ROOT gstreamer plugin.
#define DIGITIZEDTREENAMEDESC
Definition gRootTree.h:24
#define TRUEINFOTREENAMEDESC
Definition gRootTree.h:23
#define RUNHEADERTREENAMEDESC
Definition gRootTree.h:22
#define EVENTHEADERTREENAMEDESC
Definition gRootTree.h:21
#define EVENTHEADERTREENAME
Definition gRootTree.h:14
#define RUNHEADERTREENAME
Definition gRootTree.h:15
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.