gtouchable
Loading...
Searching...
No Matches
gtouchable.cc
Go to the documentation of this file.
1// gtouchable
2#include "gtouchable.h"
4
5// gemc
6#include "gutilities.h"
7#include "gutsConventions.h"
8
9// Define the static counter here
10std::atomic<int> GTouchable::globalGTouchableCounter{0};
11
12// See header for API docs.
13
14// constructor from gopt, digitization and gidentity strings
15// called in GDetectorConstruction::ConstructSDandField
16GTouchable::GTouchable(const std::shared_ptr<GOptions>& gopt,
17 const std::string& digitization,
18 const std::string& gidentityString,
19 const std::vector<double>& dimensions,
20 const double& dm) :
22 trackId(0),
23 eMultiplier(1),
24 stepTimeAtElectronicsIndex(GTOUCHABLEUNSETTIMEINDEX),
25 detectorDimensions(dimensions),
26 mass(dm) {
27 // Determine the type based on the digitization string.
28 // The string constants are defined in gtouchableConventions.h : FLUXNAME, COUNTERNAME, DOSIMETERNAME.
29 if (digitization == FLUXNAME) { gType = flux; }
30 else if (digitization == COUNTERNAME) { gType = particleCounter; }
31 else if (digitization == DOSIMETERNAME) { gType = dosimeter; }
32 else { gType = readout; }
33
34 // Parse the gidentity string into (name,value) pairs.
35 // Expected format: "sector: 2, layer: 4, wire: 33"
36 // The identity vector order is preserved because comparisons assume the same schema/order.
37 std::vector<std::string> identity = gutilities::getStringVectorFromStringWithDelimiter(gidentityString, ",");
38 // Process each identifier token (e.g., "sector: 2").
39 for (auto& gid : identity) {
40 std::vector<std::string> identifier = gutilities::getStringVectorFromStringWithDelimiter(gid, ":");
41
42 // The parser assumes the token splits into [name, value].
43 // Note: In production, consider adding try-catch here to handle conversion errors.
44 const std::string& idName = identifier[0];
45 int idValue = std::stoi(identifier[1]);
46
47 gidentity.emplace_back(idName, idValue);
48 }
49 log->debug(CONSTRUCTOR, "GTouchable", gtouchable::to_string(gType), " ", getIdentityString());
50}
51
52// constructor from logger, digitization and gidentity strings
53// called in GDetectorConstruction::ConstructSDandField
54GTouchable::GTouchable(const std::shared_ptr<GLogger>& logger,
55 const std::string& digitization,
56 const std::string& gidentityString,
57 const std::vector<double>& dimensions,
58 const double& dm) :
59 GBase(logger),
60 trackId(0),
61 eMultiplier(1),
62 stepTimeAtElectronicsIndex(GTOUCHABLEUNSETTIMEINDEX),
63 detectorDimensions(dimensions),
64 mass(dm) {
65 // Determine the type based on the digitization string.
66 // The string constants are defined in gtouchableConventions.h : FLUXNAME, COUNTERNAME, DOSIMETERNAME.
67 if (digitization == FLUXNAME) { gType = flux; }
68 else if (digitization == COUNTERNAME) { gType = particleCounter; }
69 else if (digitization == DOSIMETERNAME) { gType = dosimeter; }
70 else { gType = readout; }
71
72 // Parse the gidentity string into (name,value) pairs.
73 // Expected format: "sector: 2, layer: 4, wire: 33"
74 // The identity vector order is preserved because comparisons assume the same schema/order.
75 std::vector<std::string> identity = gutilities::getStringVectorFromStringWithDelimiter(gidentityString, ",");
76 // Process each identifier token (e.g., "sector: 2").
77 for (auto& gid : identity) {
78 std::vector<std::string> identifier = gutilities::getStringVectorFromStringWithDelimiter(gid, ":");
79
80 // The parser assumes the token splits into [name, value].
81 // Note: In production, consider adding try-catch here to handle conversion errors.
82 const std::string& idName = identifier[0];
83 int idValue = std::stoi(identifier[1]);
84
85 gidentity.emplace_back(idName, idValue);
86 }
87 log->debug(CONSTRUCTOR, "GTouchable", gtouchable::to_string(gType), " ", getIdentityString());
88}
89
90// Overloaded "==" operator for the class 'GTouchable'
91bool GTouchable::operator==(const GTouchable& that) const {
92 // First, check if both gidentity vectors are the same size.
93 // this should never happen because the same sensitivity should be assigned the same identifier structure
94 if (this->gidentity.size() != that.gidentity.size()) {
95 log->debug(NORMAL, "Touchable sizes are different");
96 return false;
97 }
98
99 // Compare identifiers positionally.
100 // Only the identifier values are compared (schema/order is assumed identical for the same sensitivity).
101 log->debug(NORMAL, " + Touchable comparison: ");
102 for (size_t i = 0; i < this->gidentity.size(); ++i) {
103 bool equal = ( this->gidentity[i].getValue() == that.gidentity[i].getValue() );
104 std::string comparisonResult = equal ? " ✅" : " ❌";
105 log->debug(NORMAL, " ← ", this->gidentity[i], " → ", that.gidentity[i], comparisonResult);
106 if (!equal) { return false; }
107 }
108
109 bool typeComparison = false;
110 std::string result;
111
112 // All identity values matched; apply the type-specific discriminator.
113 switch (this->gType) {
114 case readout:
115 typeComparison = this->stepTimeAtElectronicsIndex == that.stepTimeAtElectronicsIndex;
116 result = typeComparison ? " ✅" : " ❌";
117 log->debug(NORMAL, " Touchable type is readout. Time cell comparison: ",
118 this->stepTimeAtElectronicsIndex,
119 " ", that.stepTimeAtElectronicsIndex,
120 " result:", result);
121 break;
122 case flux:
123 typeComparison = this->trackId == that.trackId;
124 result = typeComparison ? " ✅" : " ❌";
125 log->debug(NORMAL, " Touchable type is flux. Track id comparison: ", this->trackId, " ", that.trackId,
126 " result:", result);
127 break;
128 case dosimeter:
129 typeComparison = this->trackId == that.trackId;
130 result = typeComparison ? " ✅" : " ❌";
131 log->debug(NORMAL, " Touchable type is dosimeter. Track id comparison: ", this->trackId, " ",
132 that.trackId,
133 " result:", result);
134 break;
135 case particleCounter:
136 typeComparison = true;
138 " Touchable type is particleCounter. No additional comparison needed, returning true ✅");
139 break;
140 }
141 return typeComparison;
142}
143
144// ostream GTouchable
145std::ostream& operator<<(std::ostream& stream, const GTouchable& gtouchable) {
146 stream << " GTouchable: ";
147 for (auto& gid : gtouchable.gidentity) {
148 stream << KRED << gid;
149 if (gid.getName() != gtouchable.gidentity.back().getName()) { stream << ", "; }
150 else { stream << RST; }
151 }
152 switch (gtouchable.gType) {
153 case readout:
154 // compare the time cell
155 stream << KGRN << " (readout), " << RST << " multiplier: " << gtouchable.eMultiplier <<
156 ", time cell index: " <<
157 gtouchable.stepTimeAtElectronicsIndex;
158 break;
159 case flux:
160 stream << KGRN << " (flux), " << RST << " g4 track id: " << gtouchable.trackId;
161 break;
162 case dosimeter:
163 stream << KGRN << " (dosimeter), " << RST << " g4 track id: " << gtouchable.trackId;
164 break;
165 case particleCounter:
166 stream << KGRN << " (particleCounter), " << RST << " g4 track id: " << gtouchable.trackId;
167 break;
168 }
169
170 return stream;
171}
172
174std::ostream& operator<<(std::ostream& stream, const GIdentifier& gidentifier) {
175 stream << gidentifier.idName << ": " << gidentifier.idValue;
176 return stream;
177}
std::shared_ptr< GLogger > log
void debug(debug_type type, Args &&... args) const
Represents a touchable sensitive detector element used as a hit-collection discriminator.
Definition gtouchable.h:142
GTouchable(const GTouchable &)=default
bool operator==(const GTouchable &gtouchable) const
Compares two GTouchable instances using the module comparison semantics.
Definition gtouchable.cc:91
std::string getIdentityString() const
Builds a human-readable identity string from the stored identifiers.
Definition gtouchable.h:272
CONSTRUCTOR
NORMAL
Conventions and constants used by the gtouchable module.
#define GTOUCHABLEUNSETTIMEINDEX
Sentinel value for an unset electronics time-cell index.
#define FLUXNAME
Digitization type name for flux-like detectors.
#define DOSIMETERNAME
Digitization type name for dosimeters.
#define COUNTERNAME
Digitization type name for simple particle counters.
std::ostream & operator<<(std::ostream &stream, const GTouchable &gtouchable)
@ flux
Flux-like discrimination using track id.
Definition gtouchable.h:38
@ particleCounter
Identity vector only; no additional discriminating factor.
Definition gtouchable.h:39
@ readout
Electronic readout with time-window discrimination (time-cell index).
Definition gtouchable.h:37
@ dosimeter
Radiation digitization; discrimination using track id.
Definition gtouchable.h:40
constexpr const char * TOUCHABLE_LOGGER
Logger name used by the gtouchable module.
#define KRED
#define KGRN
#define RST
const char * to_string(GTouchableType t)
Converts a GTouchableType value to a stable string for logging.
Definition gtouchable.h:58
vector< string > getStringVectorFromStringWithDelimiter(const string &input, const string &x)
A single (name,value) identifier element used to build a touchable identity vector.
Definition gtouchable.h:80