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
130
131std::ostream& operator<<(std::ostream& os, const GDigitizedData& data) {
132
133 auto idString = getIdentityString(data.gidentity);
134
135 os << "GDigitizedData{identity=\"" << idString << "\"";
136
137 if (!data.intObservablesMap.empty()) {
138 os << ", intObservables={";
139 bool first = true;
140 for (const auto& [name, value] : data.intObservablesMap) {
141 if (!first) {
142 os << ", ";
143 }
144 os << name << ": " << value;
145 first = false;
146 }
147 os << "}";
148 }
149
150 if (!data.doubleObservablesMap.empty()) {
151 os << ", doubleObservables={";
152 bool first = true;
153 for (const auto& [name, value] : data.doubleObservablesMap) {
154 if (!first) {
155 os << ", ";
156 }
157 os << name << ": " << value;
158 first = false;
159 }
160 os << "}";
161 }
162
163 if (!data.arrayIntObservablesMap.empty()) {
164 os << ", arrayIntObservables={";
165 bool firstMap = true;
166 for (const auto& [name, values] : data.arrayIntObservablesMap) {
167 if (!firstMap) {
168 os << ", ";
169 }
170 os << name << ": [";
171 bool firstVal = true;
172 for (const auto& value : values) {
173 if (!firstVal) {
174 os << ", ";
175 }
176 os << value;
177 firstVal = false;
178 }
179 os << "]";
180 firstMap = false;
181 }
182 os << "}";
183 }
184
185 if (!data.arrayDoubleObservablesMap.empty()) {
186 os << ", arrayDoubleObservables={";
187 bool firstMap = true;
188 for (const auto& [name, values] : data.arrayDoubleObservablesMap) {
189 if (!firstMap) {
190 os << ", ";
191 }
192 os << name << ": [";
193 bool firstVal = true;
194 for (const auto& value : values) {
195 if (!firstVal) {
196 os << ", ";
197 }
198 os << value;
199 firstVal = false;
200 }
201 os << "]";
202 firstMap = false;
203 }
204 os << "}";
205 }
206
207 os << "}";
208 return os;
209}
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::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.
std::string getIdentityString(std::vector< GIdentifier > gidentity)
constexpr int TIMEATELECTRONICSNOTDEFINED
Sentinel value returned when timeAtElectronics is requested but not present.