gdata
Loading...
Searching...
No Matches
gTrueInfoData.cc
Go to the documentation of this file.
1
11
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(
29 2, "%1 including %2 in trueInfoDoublesVariablesMap with value: %3", FUNCTION_NAME, varName, value);
30}
31
32void GTrueInfoData::includeVariable(const std::string& varName, std::string value) {
33 // Event-level insertion with overwrite semantics.
34 log->info(2, "%1 including %2 in trueInfoStringVariablesMap with value:%3", FUNCTION_NAME, varName, value);
35 stringVariablesMap[varName] = std::move(value);
36}
37
38void GTrueInfoData::accumulateVariable(const std::string& vname, double value) {
39 // Run/integrated accumulation by summation.
40 if (!doubleObservablesMap.contains(vname)) {
41 doubleObservablesMap[vname] = value;
42 log->info(
43 2, "%1Creating double variable %2 with value %3, sum is now:%4", FUNCTION_NAME, vname, value,
44 doubleObservablesMap[vname]);
45 }
46 else {
47 doubleObservablesMap[vname] += value;
48 log->info(
49 2, "%1Accumulating double variable %2 with value %3, sum is now:%4", FUNCTION_NAME, vname, value,
50 doubleObservablesMap[vname]);
51 }
52}
53
54
55std::ostream& operator<<(std::ostream& os, const GTrueInfoData& data) {
56 auto idString = getIdentityString(data.gidentity);
57
58 os << "GTrueInfoData{identity=\"" << idString << "\"";
59
60 if (!data.doubleObservablesMap.empty()) {
61 os << ", doubleObservables={";
62 bool first = true;
63 for (const auto& [name, value] : data.doubleObservablesMap) {
64 if (!first) {
65 os << ", ";
66 }
67 os << name << ": " << value;
68 first = false;
69 }
70 os << "}";
71 }
72
73 if (!data.stringVariablesMap.empty()) {
74 os << ", stringObservables={";
75 bool first = true;
76 for (const auto& [name, value] : data.stringVariablesMap) {
77 if (!first) {
78 os << ", ";
79 }
80 os << name << ": \"" << value << "\"";
81 first = false;
82 }
83 os << "}";
84 }
85
86 os << "}";
87 return os;
88}
GBase(const std::shared_ptr< GOptions > &gopt, std::string logger_name="")
std::shared_ptr< GLogger > log
std::vector< GIdentifier > getGID() const
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