gdata
Loading...
Searching...
No Matches
gTrueInfoData.cc
Go to the documentation of this file.
1
12#include "gTrueInfoData.h"
13#include <string>
14#include <utility>
15
16// Thread-safe counter used only by the example/test factory create().
17std::atomic<int> GTrueInfoData::globalTrueInfoDataCounter{0};
18
19GTrueInfoData::GTrueInfoData(const std::shared_ptr<GOptions>& gopts, const GHit* ghit)
20 : GBase(gopts, GTRUEDATA_LOGGER) {
21 // Copy the hit identity so this object remains self-contained after the source hit expires.
22 gidentity = ghit->getGID();
23}
24
25void GTrueInfoData::includeVariable(const std::string& varName, double value) {
26 // Event-level insertion with overwrite semantics.
27 doubleObservablesMap[varName] = value;
28 log->info(2, FUNCTION_NAME, " including ", varName, " in trueInfoDoublesVariablesMap with value: ", value);
29}
30
31void GTrueInfoData::includeVariable(const std::string& varName, std::string value) {
32 // Event-level insertion with overwrite semantics.
33 log->info(2, FUNCTION_NAME, " including ", varName, " in trueInfoStringVariablesMap with value:", value);
34 stringVariablesMap[varName] = std::move(value);
35}
36
37void GTrueInfoData::accumulateVariable(const std::string& vname, double value) {
38 // Run/integrated accumulation by summation.
39 if (doubleObservablesMap.find(vname) == doubleObservablesMap.end()) {
40 doubleObservablesMap[vname] = value;
41 log->info(2, FUNCTION_NAME, "Creating double variable ", vname, " with value ", value, ", sum is now:",
42 doubleObservablesMap[vname]);
43 }
44 else {
45 doubleObservablesMap[vname] += value;
46 log->info(2, FUNCTION_NAME, "Accumulating double variable ", vname, " with value ", value, ", sum is now:",
47 doubleObservablesMap[vname]);
48 }
49}
50
52 // Build a compact label from the stored identifier vector.
53 std::string identifierString;
54 for (size_t i = 0; i < gidentity.size() - 1; i++) {
55 identifierString += gidentity[i].getName() + "->" + std::to_string(gidentity[i].getValue()) + ", ";
56 }
57 identifierString += gidentity.back().getName() + "->" + std::to_string(gidentity.back().getValue());
58 return identifierString;
59}
60
61std::ostream& operator<<(std::ostream& os, const GTrueInfoData& data) {
62 os << "GTrueInfoData{identity=\"" << data.getIdentityString() << "\"";
63
64 if (!data.doubleObservablesMap.empty()) {
65 os << ", doubleObservables={";
66 bool first = true;
67 for (const auto& [name, value] : data.doubleObservablesMap) {
68 if (!first) {
69 os << ", ";
70 }
71 os << name << ": " << value;
72 first = false;
73 }
74 os << "}";
75 }
76
77 if (!data.stringVariablesMap.empty()) {
78 os << ", stringObservables={";
79 bool first = true;
80 for (const auto& [name, value] : data.stringVariablesMap) {
81 if (!first) {
82 os << ", ";
83 }
84 os << name << ": \"" << value << "\"";
85 first = false;
86 }
87 os << "}";
88 }
89
90 os << "}";
91 return os;
92}
std::shared_ptr< GLogger > log
std::vector< GIdentifier > getGID() const
void info(int level, Args &&... args) const
Stores simulation-level observables for one hit.
void includeVariable(const std::string &varName, double var)
Stores or overwrites one numeric truth observable.
void accumulateVariable(const std::string &vname, double value)
Accumulates a numeric observable into the current object.
GTrueInfoData(const std::shared_ptr< GOptions > &gopts, const GHit *ghit)
Constructs the object and copies the hit identity from the source hit.
std::string getIdentityString() const
Builds a readable identity string from the stored hit identifiers.
std::ostream & operator<<(std::ostream &os, const GTrueInfoData &data)
Container for simulation-level observables associated with a single hit.
constexpr const char * GTRUEDATA_LOGGER
Logger domain name used by GTrueInfoData.
#define FUNCTION_NAME