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
51
52std::ostream& operator<<(std::ostream& os, const GTrueInfoData& data) {
53 auto idString = getIdentityString(data.gidentity);
54
55 os << "GTrueInfoData{identity=\"" << idString << "\"";
56
57 if (!data.doubleObservablesMap.empty()) {
58 os << ", doubleObservables={";
59 bool first = true;
60 for (const auto& [name, value] : data.doubleObservablesMap) {
61 if (!first) {
62 os << ", ";
63 }
64 os << name << ": " << value;
65 first = false;
66 }
67 os << "}";
68 }
69
70 if (!data.stringVariablesMap.empty()) {
71 os << ", stringObservables={";
72 bool first = true;
73 for (const auto& [name, value] : data.stringVariablesMap) {
74 if (!first) {
75 os << ", ";
76 }
77 os << name << ": \"" << value << "\"";
78 first = false;
79 }
80 os << "}";
81 }
82
83 os << "}";
84 return os;
85}
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::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.
std::string getIdentityString(std::vector< GIdentifier > gidentity)
#define FUNCTION_NAME