gdata
Loading...
Searching...
No Matches
gDigitizedData.cc
Go to the documentation of this file.
1
12#include "gDigitizedData.h"
13#include "gdataConventions.h"
14
15// c++
16#include <map>
17#include <string>
18#include <vector>
19
20#include "gdynamicDigitization/gdynamicdigitization_options.h"
21
22// Thread-safe counter used only by the example/test factory create().
23std::atomic<int> GDigitizedData::globalDigitizedDataCounter{0};
24
25GDigitizedData::GDigitizedData(const std::shared_ptr<GOptions>& gopts, const GHit* ghit)
27 // Copy the hit identity so this object remains self-contained after the source hit expires.
28 gidentity = ghit->getGID();
29}
30
31std::map<std::string, int> GDigitizedData::getIntObservablesMap(int which) const {
32 // Return a filtered snapshot of the stored integer observables.
33 std::map<std::string, int> filteredIntObservablesMap;
34 for (const auto& [varName, value] : intObservablesMap) {
35 if (validVarName(varName, which)) { filteredIntObservablesMap[varName] = value; }
36 }
37 log->info(2, " getting ", which, " from intObservablesMap.");
38 return filteredIntObservablesMap;
39}
40
41std::map<std::string, double> GDigitizedData::getDblObservablesMap(int which) const {
42 // Return a filtered snapshot of the stored floating-point observables.
43 std::map<std::string, double> filteredDblObservablesMap;
44 for (const auto& [varName, value] : doubleObservablesMap) {
45 if (validVarName(varName, which)) { filteredDblObservablesMap[varName] = value; }
46 }
47 log->info(2, " getting ", which, " from doubleObservablesMap.");
48 return filteredDblObservablesMap;
49}
50
51bool GDigitizedData::validVarName(const std::string& varName, int which) {
52 // Classify variables as SRO or non-SRO using the conventional names from gdataConventions.h.
53 bool isSROVar = (varName == CRATESTRINGID || varName == SLOTSTRINGID || varName == CHANNELSTRINGID ||
54 varName == CHARGEATELECTRONICS || varName == TIMEATELECTRONICS);
55
56 // which == 0 means the caller wants non-SRO variables only.
57 if (which == 0) {
58 if (isSROVar) { return false; }
59 }
60 // which == 1 means the caller wants SRO variables only.
61 else if (which == 1) {
62 if (!isSROVar) { return false; }
63 }
64
65 // Any other selector currently behaves like "no additional filtering".
66 return true;
67}
68
69void GDigitizedData::includeVariable(const std::string& vname, int value) {
70 // Event-level insertion with overwrite semantics.
71 log->info(2, "Including int variable ", vname, " with value ", value);
72 intObservablesMap[vname] = value;
73}
74
75void GDigitizedData::includeVariable(const std::string& vname, double value) {
76 // Event-level insertion with overwrite semantics.
77 log->info(2, "Including double variable ", vname, " with value ", value);
78 doubleObservablesMap[vname] = value;
79}
80
81void GDigitizedData::accumulateVariable(const std::string& vname, int value) {
82 // Run/integrated accumulation by summation.
83 if (intObservablesMap.find(vname) == intObservablesMap.end()) {
84 log->info(2, "Accumulating new int variable ", vname, " with value ", value);
85 intObservablesMap[vname] = value;
86 }
87 else {
88 log->info(2, "Accumulating int variable ", vname, " with value ", value);
89 intObservablesMap[vname] += value;
90 }
91}
92
93void GDigitizedData::accumulateVariable(const std::string& vname, double value) {
94 // Run/integrated accumulation by summation.
95 if (doubleObservablesMap.find(vname) == doubleObservablesMap.end()) {
96 log->info(2, "Accumulating double variable ", vname, " with value ", value);
97 doubleObservablesMap[vname] = value;
98 }
99 else {
100 log->info(2, "Accumulating double variable ", vname, " with value ", value);
101 doubleObservablesMap[vname] += value;
102 }
103}
104
106 // Return the conventional electronics-time value or the sentinel if absent.
107 if (intObservablesMap.find(TIMEATELECTRONICS) == intObservablesMap.end()) { return TIMEATELECTRONICSNOTDEFINED; }
108 log->info(2, "Getting TIMEATELECTRONICS from intObservablesMap.");
109 return intObservablesMap[TIMEATELECTRONICS];
110}
111
112int GDigitizedData::getIntObservable(const std::string& varName) {
113 // Retrieve a single integer observable by key.
114 if (intObservablesMap.find(varName) == intObservablesMap.end()) {
116 "variable name <" + varName + "> not found in GDigitizedData::intObservablesMap");
117 }
118 return intObservablesMap[varName];
119}
120
121double GDigitizedData::getDblObservable(const std::string& varName) {
122 // Retrieve a single floating-point observable by key.
123 if (doubleObservablesMap.find(varName) == doubleObservablesMap.end()) {
125 "variable name <" + varName + "> not found in GDigitizedData::doubleObservablesMap");
126 }
127 return doubleObservablesMap[varName];
128}
129
131 // Build a compact label from the stored identifier vector.
132 std::string identifierString;
133 for (size_t i = 0; i < gidentity.size() - 1; i++) {
134 identifierString += gidentity[i].getName() + "->" + std::to_string(gidentity[i].getValue()) + ", ";
135 }
136 identifierString += gidentity.back().getName() + "->" + std::to_string(gidentity.back().getValue());
137 return identifierString;
138}
139
140std::ostream& operator<<(std::ostream& os, const GDigitizedData& data) {
141 os << "GDigitizedData{identity=\"" << data.getIdentityString() << "\"";
142
143 if (!data.intObservablesMap.empty()) {
144 os << ", intObservables={";
145 bool first = true;
146 for (const auto& [name, value] : data.intObservablesMap) {
147 if (!first) {
148 os << ", ";
149 }
150 os << name << ": " << value;
151 first = false;
152 }
153 os << "}";
154 }
155
156 if (!data.doubleObservablesMap.empty()) {
157 os << ", doubleObservables={";
158 bool first = true;
159 for (const auto& [name, value] : data.doubleObservablesMap) {
160 if (!first) {
161 os << ", ";
162 }
163 os << name << ": " << value;
164 first = false;
165 }
166 os << "}";
167 }
168
169 if (!data.arrayIntObservablesMap.empty()) {
170 os << ", arrayIntObservables={";
171 bool firstMap = true;
172 for (const auto& [name, values] : data.arrayIntObservablesMap) {
173 if (!firstMap) {
174 os << ", ";
175 }
176 os << name << ": [";
177 bool firstVal = true;
178 for (const auto& value : values) {
179 if (!firstVal) {
180 os << ", ";
181 }
182 os << value;
183 firstVal = false;
184 }
185 os << "]";
186 firstMap = false;
187 }
188 os << "}";
189 }
190
191 if (!data.arrayDoubleObservablesMap.empty()) {
192 os << ", arrayDoubleObservables={";
193 bool firstMap = true;
194 for (const auto& [name, values] : data.arrayDoubleObservablesMap) {
195 if (!firstMap) {
196 os << ", ";
197 }
198 os << name << ": [";
199 bool firstVal = true;
200 for (const auto& value : values) {
201 if (!firstVal) {
202 os << ", ";
203 }
204 os << value;
205 firstVal = false;
206 }
207 os << "]";
208 firstMap = false;
209 }
210 os << "}";
211 }
212
213 os << "}";
214 return os;
215}
std::shared_ptr< GLogger > log
Stores digitized, electronics-level observables for one hit.
void accumulateVariable(const std::string &vname, int value)
Accumulates one integer observable into the current object.
int getIntObservable(const std::string &varName)
Returns one integer observable by key.
std::map< std::string, int > getIntObservablesMap(int which) const
Returns a filtered copy of the integer observables map.
int getTimeAtElectronics()
Returns the conventional timeAtElectronics integer observable when present.
void includeVariable(const std::string &vname, int value)
Stores or overwrites one integer observable for this hit.
std::map< std::string, double > getDblObservablesMap(int which) const
Returns a filtered copy of the floating-point observables map.
GDigitizedData(const std::shared_ptr< GOptions > &gopts, const GHit *ghit)
Constructs the object and copies the hit identity from the source hit.
double getDblObservable(const std::string &varName)
Returns one floating-point observable by key.
std::string getIdentityString() const
Builds a readable identity string from the stored hit identifiers.
std::vector< GIdentifier > getGID() const
void info(int level, Args &&... args) const
void error(int exit_code, Args &&... args) const
std::ostream & operator<<(std::ostream &os, const GDigitizedData &data)
Container for digitized observables associated with one simulated hit.
constexpr const char * GDIGITIZED_DATA_LOGGER
Logger domain name used by GDigitizedData.
Shared constants, schema keys, and error codes for the GData module.
constexpr const char * TIMEATELECTRONICS
Electronics-stage time or TDC proxy.
constexpr const char * CRATESTRINGID
Electronics crate identifier.
constexpr const char * CHANNELSTRINGID
Channel index within the slot or module.
constexpr int ERR_VARIABLENOTFOUND
Requested observable key is missing.
constexpr const char * SLOTSTRINGID
Slot index within the crate.
constexpr const char * CHARGEATELECTRONICS
Electronics-stage charge or ADC proxy.
constexpr int TIMEATELECTRONICSNOTDEFINED
Sentinel value returned when timeAtElectronics is requested but not present.