gdata
Loading...
Searching...
No Matches
gDigitizedData.cc
Go to the documentation of this file.
1
11
12#include "gDigitizedData.h"
13#include "gdataConventions.h"
14
15// c++
16#include <map>
17#include <string>
18#include <vector>
19
20#include <gemc/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 %1 from intObservablesMap.", which);
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 %1 from doubleObservablesMap.", which);
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 %1 with value %2", vname, 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 %1 with value %2", vname, value);
78 doubleObservablesMap[vname] = value;
79}
80
81void GDigitizedData::includeTransientVariable(const std::string& vname, double value) {
82 log->info(2, "Including transient variable %1 with value %2", vname, value);
83 transientVariablesMap[vname] = value;
84}
85
86bool GDigitizedData::hasTransientVariable(const std::string& vname) const {
87 return transientVariablesMap.contains(vname);
88}
89
90double GDigitizedData::getTransientVariable(const std::string& vname) const {
91 return transientVariablesMap.at(vname);
92}
93
94void GDigitizedData::accumulateVariable(const std::string& vname, int value) {
95 // Run/integrated accumulation by summation.
96 if (!intObservablesMap.contains(vname)) {
97 log->info(2, "Accumulating new int variable %1 with value %2", vname, value);
98 intObservablesMap[vname] = value;
99 }
100 else {
101 log->info(2, "Accumulating int variable %1 with value %2", vname, value);
102 intObservablesMap[vname] += value;
103 }
104}
105
106void GDigitizedData::accumulateVariable(const std::string& vname, double value) {
107 // Run/integrated accumulation by summation.
108 if (!doubleObservablesMap.contains(vname)) {
109 log->info(2, "Accumulating double variable %1 with value %2", vname, value);
110 doubleObservablesMap[vname] = value;
111 }
112 else {
113 log->info(2, "Accumulating double variable %1 with value %2", vname, value);
114 doubleObservablesMap[vname] += value;
115 }
116}
117
118std::optional<int> GDigitizedData::getTimeAtElectronics() const {
119 const auto time = intObservablesMap.find(TIMEATELECTRONICS);
120 if (time == intObservablesMap.end()) { return std::nullopt; }
121 log->info(2, "Getting TIMEATELECTRONICS from intObservablesMap.");
122 return time->second;
123}
124
125int GDigitizedData::getIntObservable(const std::string& varName) {
126 // Retrieve a single integer observable by key.
127 if (!intObservablesMap.contains(varName)) {
128 log->error(
129 ERR_VARIABLENOTFOUND, "variable name <%1> not found in GDigitizedData::intObservablesMap", varName);
130 }
131 return intObservablesMap[varName];
132}
133
134double GDigitizedData::getDblObservable(const std::string& varName) {
135 // Retrieve a single floating-point observable by key.
136 if (!doubleObservablesMap.contains(varName)) {
137 log->error(
138 ERR_VARIABLENOTFOUND, "variable name <%1> not found in GDigitizedData::doubleObservablesMap",
139 varName);
140 }
141 return doubleObservablesMap[varName];
142}
143
144
145std::ostream& operator<<(std::ostream& os, const GDigitizedData& data) {
146
147 auto idString = getIdentityString(data.gidentity);
148
149 os << "GDigitizedData{identity=\"" << idString << "\"";
150
151 if (!data.intObservablesMap.empty()) {
152 os << ", intObservables={";
153 bool first = true;
154 for (const auto& [name, value] : data.intObservablesMap) {
155 if (!first) {
156 os << ", ";
157 }
158 os << name << ": " << value;
159 first = false;
160 }
161 os << "}";
162 }
163
164 if (!data.doubleObservablesMap.empty()) {
165 os << ", doubleObservables={";
166 bool first = true;
167 for (const auto& [name, value] : data.doubleObservablesMap) {
168 if (!first) {
169 os << ", ";
170 }
171 os << name << ": " << value;
172 first = false;
173 }
174 os << "}";
175 }
176
177 if (!data.arrayIntObservablesMap.empty()) {
178 os << ", arrayIntObservables={";
179 bool firstMap = true;
180 for (const auto& [name, values] : data.arrayIntObservablesMap) {
181 if (!firstMap) {
182 os << ", ";
183 }
184 os << name << ": [";
185 bool firstVal = true;
186 for (const auto& value : values) {
187 if (!firstVal) {
188 os << ", ";
189 }
190 os << value;
191 firstVal = false;
192 }
193 os << "]";
194 firstMap = false;
195 }
196 os << "}";
197 }
198
199 if (!data.arrayDoubleObservablesMap.empty()) {
200 os << ", arrayDoubleObservables={";
201 bool firstMap = true;
202 for (const auto& [name, values] : data.arrayDoubleObservablesMap) {
203 if (!firstMap) {
204 os << ", ";
205 }
206 os << name << ": [";
207 bool firstVal = true;
208 for (const auto& value : values) {
209 if (!firstVal) {
210 os << ", ";
211 }
212 os << value;
213 firstVal = false;
214 }
215 os << "]";
216 firstMap = false;
217 }
218 os << "}";
219 }
220
221 os << "}";
222 return os;
223}
GBase(const std::shared_ptr< GOptions > &gopt, std::string logger_name="")
std::shared_ptr< GLogger > log
void accumulateVariable(const std::string &vname, int value)
Accumulates one integer observable into the current object.
bool hasTransientVariable(const std::string &vname) const
Returns whether a transient floating-point value is present.
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.
void includeVariable(const std::string &vname, int value)
Stores or overwrites one integer observable for this hit.
std::optional< int > getTimeAtElectronics() const
Returns the conventional timeAtElectronics integer observable when present.
std::map< std::string, double > getDblObservablesMap(int which) const
Returns a filtered copy of the floating-point observables map.
double getTransientVariable(const std::string &vname) const
Returns a transient floating-point value by key.
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.
void includeTransientVariable(const std::string &vname, double value)
Stores a non-published floating-point value for post-digitization decisions.
std::vector< GIdentifier > getGID() 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.
std::string getIdentityString(std::vector< GIdentifier > gidentity)