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// constructor from gopt, digitization and gidentity strings
13// called in GDetectorConstruction::ConstructSDandField
14GTouchable::GTouchable(const std::shared_ptr<GOptions>& gopt,
15 const std::string& digitization,
16 const std::string& gidentityString,
17 const std::vector<double>& dimensions) : GBase(gopt, TOUCHABLE_LOGGER),
18 trackId(0),
19 eMultiplier(1),
20 stepTimeAtElectronicsIndex(GTOUCHABLEUNSETTIMEINDEX),
21 detectorDimensions(dimensions) {
22 // Determine the type based on the digitization string.
23 if (digitization == FLUXNAME) { gType = flux; }
24 else if (digitization == COUNTERNAME) { gType = particleCounter; }
25 else if (digitization == DOSIMETERNAME) { gType = dosimeter; }
26 else { gType = readout; }
27
28 // Parse the gidentity string.
29 // Expected format: "sector: 2, layer: 4, wire: 33"
30 std::vector<std::string> identity = gutilities::getStringVectorFromStringWithDelimiter(gidentityString, ",");
31 // Process each identifier (e.g., "sector: 2").
32 for (auto& gid : identity) {
33 std::vector<std::string> identifier = gutilities::getStringVectorFromStringWithDelimiter(gid, ":");
34
35 // Note: In production, consider adding try-catch here to handle conversion errors.
36 const std::string& idName = identifier[0];
37 int idValue = std::stoi(identifier[1]);
38
39 gidentity.emplace_back(idName, idValue);
40 }
41 log->debug(CONSTRUCTOR, "GTouchable", gtouchable::to_string(gType), " ", getIdentityString());
42}
43
44// constructor from logger, digitization and gidentity strings
45// called in GDetectorConstruction::ConstructSDandField
46GTouchable::GTouchable(const std::shared_ptr<GLogger>& logger,
47 const std::string& digitization,
48 const std::string& gidentityString,
49 const std::vector<double>& dimensions) : GBase(logger),
50 trackId(0),
51 eMultiplier(1),
52 stepTimeAtElectronicsIndex(GTOUCHABLEUNSETTIMEINDEX),
53 detectorDimensions(dimensions) {
54 // Determine the type based on the digitization string.
55 if (digitization == FLUXNAME) { gType = flux; }
56 else if (digitization == COUNTERNAME) { gType = particleCounter; }
57 else if (digitization == DOSIMETERNAME) { gType = dosimeter; }
58 else { gType = readout; }
59
60 // Parse the gidentity string.
61 // Expected format: "sector: 2, layer: 4, wire: 33"
62 std::vector<std::string> identity = gutilities::getStringVectorFromStringWithDelimiter(gidentityString, ",");
63 // Process each identifier (e.g., "sector: 2").
64 for (auto& gid : identity) {
65 std::vector<std::string> identifier = gutilities::getStringVectorFromStringWithDelimiter(gid, ":");
66
67 // Note: In production, consider adding try-catch here to handle conversion errors.
68 const std::string& idName = identifier[0];
69 int idValue = std::stoi(identifier[1]);
70
71 gidentity.emplace_back(idName, idValue);
72 }
73 log->debug(CONSTRUCTOR, "GTouchable", gtouchable::to_string(gType), " ", getIdentityString());
74}
75
76// Overloaded "==" operator for the class 'GTouchable'
77bool GTouchable::operator==(const GTouchable& that) const {
78 // First, check if both gidentity vectors are the same size.
79 // this should never happen because the same sensitivity should be assigned the same identifier structure
80 if (this->gidentity.size() != that.gidentity.size()) {
81 log->debug(NORMAL, "Touchable sizes are different");
82 return false;
83 }
84
85 log->debug(NORMAL, " + Touchable comparison: ");
86 for (size_t i = 0; i < this->gidentity.size(); ++i) {
87 bool equal = (this->gidentity[i].getValue() == that.gidentity[i].getValue());
88 std::string comparisonResult = equal ? " ✅" : " ❌";
89 log->debug(NORMAL, " ← ", this->gidentity[i], " → ", that.gidentity[i], comparisonResult);
90 if (!equal) { return false; }
91 }
92
93 bool typeComparison = false;
94 std::string result;
95
96 // all identities are the same
97 // now using gtouchable type
98 switch (this->gType) {
99 case readout:
100 typeComparison = this->stepTimeAtElectronicsIndex == that.stepTimeAtElectronicsIndex;
101 result = typeComparison ? " ✅" : " ❌";
102 log->debug(NORMAL, " Touchable type is readout. Time cell comparison: ", this->stepTimeAtElectronicsIndex, " ", that.stepTimeAtElectronicsIndex,
103 " result:", result);
104 break;
105 case flux:
106 typeComparison = this->trackId == that.trackId;
107 result = typeComparison ? " ✅" : " ❌";
108 log->debug(NORMAL, " Touchable type is flux. Track id comparison: ", this->trackId, " ", that.trackId,
109 " result:", result);
110 break;
111 case dosimeter:
112 typeComparison = this->trackId == that.trackId;
113 result = typeComparison ? " ✅" : " ❌";
114 log->debug(NORMAL, " Touchable type is dosimeter. Track id comparison: ", this->trackId, " ", that.trackId,
115 " result:", result);
116 break;
117 case particleCounter:
118 typeComparison = true;
119 log->debug(NORMAL, " Touchable type is particleCounter. No additional comparison needed, returning true ✅");
120 break;
121 }
122 return typeComparison;
123}
124
125// ostream GTouchable
126std::ostream& operator<<(std::ostream& stream, const GTouchable& gtouchable) {
127 stream << " GTouchable: ";
128 for (auto& gid : gtouchable.gidentity) {
129 stream << KRED << gid;
130 if (gid.getName() != gtouchable.gidentity.back().getName()) { stream << ", "; }
131 else { stream << RST; }
132 }
133 switch (gtouchable.gType) {
134 case readout:
135 // compare the time cell
136 stream << KGRN << " (readout), " << RST << " multiplier: " << gtouchable.eMultiplier << ", time cell index: " << gtouchable.stepTimeAtElectronicsIndex;
137 break;
138 case flux:
139 stream << KGRN << " (flux), " << RST << " g4 track id: " << gtouchable.trackId;
140 break;
141 case dosimeter:
142 stream << KGRN << " (dosimeter), " << RST << " g4 track id: " << gtouchable.trackId;
143 break;
144 case particleCounter:
145 stream << KGRN << " (particleCounter), " << RST << " g4 track id: " << gtouchable.trackId;
146 break;
147 }
148
149 return stream;
150}
151
153std::ostream& operator<<(std::ostream& stream, const GIdentifier& gidentifier) {
154 stream << gidentifier.idName << ": " << gidentifier.idValue;
155 return stream;
156}
Represents a touchable sensitive detector element.
Definition gtouchable.h:103
GTouchable(const GTouchable &)=default
bool operator==(const GTouchable &gtouchable) const
Equality operator comparing two GTouchable objects.
Definition gtouchable.cc:77
std::string getIdentityString() const
Returns a string formed by all identifiers.
Definition gtouchable.h:196
#define GTOUCHABLEUNSETTIMEINDEX
#define FLUXNAME
#define DOSIMETERNAME
#define COUNTERNAME
std::ostream & operator<<(std::ostream &stream, const GTouchable &gtouchable)
@ flux
Track-based discrimination.
Definition gtouchable.h:28
@ particleCounter
No additional discriminating factors.
Definition gtouchable.h:29
@ readout
Electronic readout with time window discrimination.
Definition gtouchable.h:27
@ dosimeter
Radiation digitization using track id.
Definition gtouchable.h:30
constexpr const char * TOUCHABLE_LOGGER
const char * to_string(GTouchableType t)
Definition gtouchable.h:37
Represents a unique identifier for a sensitive detector element.
Definition gtouchable.h:53