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
28GRootTree::GRootTree([[maybe_unused]] const std::unique_ptr<GRunHeader>& grun_header,
29 std::shared_ptr<GLogger>& logger) : log(logger) {
30
31 log->debug(CONSTRUCTOR, "GRootTree", "ROOT tree header");
32
33 root_tree = std::make_unique<TTree>(RUNHEADERTREENAME, RUNHEADERTREENAMEDESC);
34 root_tree->SetAutoFlush(20 * 1024 * 1024);
35 root_tree->SetAutoSave(50 * 1024 * 1024);
36
37 registerVariable("runID", grun_header->getRunID());
38}
39
40
41// Implementation summary:
42// Create a true-information detector tree whose branch schema is inferred
43// from the first observed hit.
44GRootTree::GRootTree(const std::string& detectorName,
45 const GTrueInfoData* gdata,
46 std::shared_ptr<GLogger>& logger) : log(logger) {
47 log->debug(CONSTRUCTOR, "GRootTree", "ROOT tree True Info");
48
49 root_tree = std::make_unique<TTree>(detectorName.c_str(), TRUEINFOTREENAMEDESC);
50 root_tree->SetAutoFlush(20 * 1024 * 1024);
51 root_tree->SetAutoSave(50 * 1024 * 1024);
52
53 for (auto& [varname, value] : gdata->getDoubleVariablesMap()) { registerVariable(varname, value); }
54 for (auto& [varname, value] : gdata->getStringVariablesMap()) { registerVariable(varname, value); }
55}
56
57
58// Implementation summary:
59// Create a digitized detector tree whose branch schema is inferred
60// from the first observed hit.
61GRootTree::GRootTree(const std::string& detectorName,
62 const GDigitizedData* gdata,
63 std::shared_ptr<GLogger>& logger) : log(logger) {
64 log->debug(CONSTRUCTOR, "GRootTree", "ROOT tree Digitized Data");
65
66 root_tree = std::make_unique<TTree>(detectorName.c_str(), DIGITIZEDTREENAMEDESC);
67 root_tree->SetAutoFlush(20 * 1024 * 1024);
68 root_tree->SetAutoSave(50 * 1024 * 1024);
69
70 for (auto& [varname, value] : gdata->getIntObservablesMap(0)) { registerVariable(varname, value); }
71 for (auto& [varname, value] : gdata->getDblObservablesMap(0)) { registerVariable(varname, value); }
72}
73
74// fill the header tree
75bool GRootTree::fillTree(const std::unique_ptr<GEventHeader>& gevent_header) {
76 log->info(2, "Filling header tree for local event n. ", gevent_header->getG4LocalEvn(), " threadID ",
77 gevent_header->getThreadID());
78
79 // Clear the vectors backing the branches before writing the next entry.
80 intVarsMap["g4localEventNumber"].clear();
81 intVarsMap["threadID"].clear();
82 stringVarsMap["timeStamp"].clear();
83
84 intVarsMap["g4localEventNumber"].emplace_back(gevent_header->getG4LocalEvn());
85 intVarsMap["threadID"].emplace_back(gevent_header->getThreadID());
86 stringVarsMap["timeStamp"].emplace_back(gevent_header->getTimeStamp());
87
88 root_tree->Fill();
89
90 return true;
91}
92
93
94bool GRootTree::fillTree(const std::unique_ptr<GRunHeader>& grun_header) {
95 log->info(2, "Filling header tree for run n. ", grun_header->getRunID());
96
97 // Clear and refill the vectors backing the run-header branches.
98 intVarsMap["runID"].clear();
99 intVarsMap["runID"].emplace_back(grun_header->getRunID());
100
101 root_tree->Fill();
102
103 return true;
104}
105
106// fill the True Info Tree
107bool GRootTree::fillTree(const vector<const GTrueInfoData*>& trueInfoData) {
108 // Reset all branch vectors before repopulating them for this detector collection.
109 for (auto& [varname, values] : doubleVarsMap) { values.clear(); }
110 for (auto& [varname, values] : stringVarsMap) { values.clear(); }
111
112 for (auto& dataHits : trueInfoData) {
113 for (auto& [varname, value] : dataHits->getDoubleVariablesMap()) { doubleVarsMap[varname].push_back(value); }
114 for (auto& [varname, value] : dataHits->getStringVariablesMap()) { stringVarsMap[varname].push_back(value); }
115 }
116
117 root_tree->Fill();
118
119 return true;
120}
121
122// fill the Digitized Data Tree
123bool GRootTree::fillTree(const vector<const GDigitizedData*>& digitizedData) {
124 // Reset all branch vectors before repopulating them for this detector collection.
125 for (auto& [varname, values] : intVarsMap) { values.clear(); }
126 for (auto& [varname, values] : doubleVarsMap) { values.clear(); }
127
128 for (auto& dataHits : digitizedData) {
129 for (auto& [varname, value] : dataHits->getIntObservablesMap(0)) { intVarsMap[varname].push_back(value); }
130 for (auto& [varname, value] : dataHits->getDblObservablesMap(0)) { doubleVarsMap[varname].push_back(value); }
131 }
132 root_tree->Fill();
133
134 return true;
135}
136
137
138// Implementation summary:
139// Register one branch and bind it to the appropriate backing vector map.
140// The second parameter is used only to select the correct overload.
141
142void GRootTree::registerVariable(const std::string& varname, [[maybe_unused]] int value) {
143 if (intVarsMap.find(varname) == intVarsMap.end()) { root_tree->Branch(varname.c_str(), &intVarsMap[varname]); }
144 else {
145 log->error(ERR_GSTREAMERVARIABLEEXISTS, "variable < ", varname,
146 "< already exist in the int variable map of tree ", root_tree->GetName());
147 }
148}
149
150void GRootTree::registerVariable(const std::string& varname, [[maybe_unused]] double value) {
151 if (doubleVarsMap.find(varname) == doubleVarsMap.end()) {
152 root_tree->Branch(varname.c_str(), &doubleVarsMap[varname]);
153 }
154 else {
155 log->error(ERR_GSTREAMERVARIABLEEXISTS, "variable < ", varname,
156 "< already exist in the double variable map of tree ", root_tree->GetName());
157 }
158}
159
160void GRootTree::registerVariable(const std::string& varname, [[maybe_unused]] const std::string& value) {
161 if (stringVarsMap.find(varname) == stringVarsMap.end()) {
162 root_tree->Branch(varname.c_str(), &stringVarsMap[varname]);
163 }
164 else {
165 log->error(ERR_GSTREAMERVARIABLEEXISTS, "variable < ", varname,
166 "< already exist in the string variable map of tree ", root_tree->GetName());
167 }
168}
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:75
std::map< std::string, double > getDoubleVariablesMap() 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
CONSTRUCTOR
Shared constants and error codes for the gstreamer module.
#define ERR_GSTREAMERVARIABLEEXISTS
Duplicate variable registration was attempted in a streamer-specific schema.