gtouchable
Loading...
Searching...
No Matches
gtouchable.h
Go to the documentation of this file.
1#pragma once
2
3// gemc
4#include <gemc/gbase/gbase.h>
5
6// c++
7#include <vector>
8#include <string>
9#include <memory>
10#include <optional>
11
12// gtouchable
13#include <CLHEP/Units/SystemOfUnits.h>
14
15#include "gtouchable_options.h"
17
18
42
43// ------------------------------------------------------------------------
44// Convert enum to string for logging / debugging.
45// ------------------------------------------------------------------------
46namespace gtouchable {
58 inline const char* to_string(GTouchableType t) {
59 switch (t) {
60 case GTouchableType::readout: return "readout";
66 default: return "unknown-gtouchable";
67 }
68 }
69}
70
82{
83public:
90 GIdentifier(const std::string& n, int v) : idName{n}, idValue{v} {
91 }
92
101 bool operator==(const GIdentifier& gid) const { return this->idValue == gid.idValue; }
102
107 [[nodiscard]] inline std::string getName() const { return idName; }
108
113 [[nodiscard]] inline int getValue() const { return idValue; }
114
116 inline void setValue(int v) { idValue = v; }
117
118private:
119 std::string idName;
120 int idValue;
121
123 friend std::ostream& operator<<(std::ostream& stream, const GIdentifier& gidentifier);
124};
125
126
148class GTouchable : public GBase<GTouchable>
149{
150public:
151 GTouchable(const GTouchable&) = default;
152 GTouchable& operator=(const GTouchable&) = default;
153
169 GTouchable(const std::shared_ptr<GOptions>& gopt,
170 const std::string& digitization,
171 const std::string& gidentityString,
172 const std::vector<double>& dimensions,
173 const double& mass);
174
189 GTouchable(const std::shared_ptr<GLogger>& logger,
190 const std::string& digitization,
191 const std::string& gidentityString,
192 const std::vector<double>& dimensions,
193 const double& mass);
194
207 GTouchable(const std::shared_ptr<GTouchable>& base, int newTimeIndex)
208 : GBase<GTouchable>(*base),
209 gType(base->gType),
210 gidentity(base->gidentity),
211 trackId(base->trackId),
212 eMultiplier(base->eMultiplier),
213 stepTimeAtElectronicsIndex(newTimeIndex) {
214 log->debug(CONSTRUCTOR, "Copy-with-time-index", gtouchable::to_string(gType), " ", getIdentityString());
215 }
216
224
231 bool operator==(const GTouchable& gtouchable) const;
232
243 [[nodiscard]] std::string cellKey() const;
244
252 inline void assignTrackId(int tid) { trackId = tid; }
253
261 inline void assignPId(int p) { pid = p; }
262
271 [[nodiscard]] inline double getEnergyMultiplier() const { return eMultiplier; }
272
278 inline void assignStepTimeAtElectronicsIndex(int timeIndex) { stepTimeAtElectronicsIndex = timeIndex; }
279
285 [[nodiscard]] inline const std::optional<int>& getStepTimeAtElectronicsIndex() const {
286 return stepTimeAtElectronicsIndex;
287 }
288
294 [[nodiscard]] inline std::vector<GIdentifier> getIdentity() const { return gidentity; }
295
297 inline void setIdentityValue(size_t index, int value) { gidentity.at(index).setValue(value); }
298
307 [[nodiscard]] inline std::string getIdentityString() const {
308 std::string idString;
309 for (const auto& id : gidentity) { idString += id.getName() + ": " + std::to_string(id.getValue()) + " "; }
310 return idString;
311 }
312
320 [[nodiscard]] inline std::vector<double> getDetectorDimensions() const { return detectorDimensions; }
321
327 [[nodiscard]] inline double getMass() const { return mass; }
328
338 [[nodiscard]] bool exists_in_vector(const std::vector<GTouchable>& v) const {
339 for (const auto& gt : v) {
340 if (*this == gt) {
341 log->info(2, "GTouchable", this, " exists in vector.");
342 return true;
343 }
344 }
345 log->info(2, "GTouchable", this, " does not exist in vector.");
346
347 return false;
348 }
349
360 static std::shared_ptr<GTouchable> create(const std::shared_ptr<GOptions>& gopt) {
361 int touchableNumber = globalGTouchableCounter.fetch_add(1, std::memory_order_relaxed);
362 int sector = ( touchableNumber % 6 ) + 1;
363 int paddle = ( touchableNumber % 20 ) + 1;
364 std::string identity = "sector: " + std::to_string(sector) + ", paddle: " + std::to_string(paddle);
365 const auto& dimensions = {10.0, 20.0, 30.0};
366 const double& mass = 100 * CLHEP::g;
367
368 return std::make_shared<GTouchable>(gopt, "readout", identity, dimensions, mass);
369 }
370
381 static std::shared_ptr<GTouchable> create(const std::shared_ptr<GLogger>& logger) {
382 int touchableNumber = globalGTouchableCounter.fetch_add(1, std::memory_order_relaxed);
383 int sector = ( touchableNumber % 6 ) + 1;
384 int paddle = ( touchableNumber % 20 ) + 1;
385 std::string identity = "sector: " + std::to_string(sector) + ", paddle: " + std::to_string(paddle);
386 const auto& dimensions = {10.0, 20.0, 30.0};
387 const double& mass = 100 * CLHEP::g;
388
389 return std::make_shared<GTouchable>(logger, "readout", identity, dimensions, mass);
390 }
391
392private:
393 GTouchableType gType;
394 std::vector<GIdentifier> gidentity;
395 int trackId;
396 int pid;
397 double eMultiplier;
398 std::optional<int> stepTimeAtElectronicsIndex;
399 std::vector<double> detectorDimensions;
400 double mass;
401
403 friend std::ostream& operator<<(std::ostream& stream, const GTouchable& gtouchable);
404
406 inline static std::atomic<int> globalGTouchableCounter{0};
407};
GBase(const std::shared_ptr< GOptions > &gopt, std::string logger_name="")
std::shared_ptr< GLogger > log
Represents a touchable sensitive detector element used as a hit-collection discriminator.
Definition gtouchable.h:149
GTouchable(const GTouchable &)=default
static std::shared_ptr< GTouchable > create(const std::shared_ptr< GOptions > &gopt)
Creates a synthetic readout touchable for testing (options-based).
Definition gtouchable.h:360
std::vector< GIdentifier > getIdentity() const
Returns a copy of the identity vector.
Definition gtouchable.h:294
~GTouchable()
Destructor with debug trace.
Definition gtouchable.h:223
void assignPId(int p)
Assigns the particle id used by particle counter discrimination.
Definition gtouchable.h:261
bool operator==(const GTouchable &gtouchable) const
Compares two GTouchable instances using the module comparison semantics.
Definition gtouchable.cc:88
void assignTrackId(int tid)
Assigns the track id used by flux and dosimeter discrimination.
Definition gtouchable.h:252
void setIdentityValue(size_t index, int value)
Overwrites the value of the identity element at index. Throws std::out_of_range if out of bounds.
Definition gtouchable.h:297
void assignStepTimeAtElectronicsIndex(int timeIndex)
Assigns the electronics time-cell index used by readout discrimination.
Definition gtouchable.h:278
const std::optional< int > & getStepTimeAtElectronicsIndex() const
Returns the electronics time-cell index.
Definition gtouchable.h:285
friend std::ostream & operator<<(std::ostream &stream, const GTouchable &gtouchable)
Stream output helper used in logs and diagnostics.
double getEnergyMultiplier() const
Returns the energy multiplier used for energy sharing.
Definition gtouchable.h:271
static std::shared_ptr< GTouchable > create(const std::shared_ptr< GLogger > &logger)
Creates a synthetic readout touchable for testing (logger-based).
Definition gtouchable.h:381
GTouchable & operator=(const GTouchable &)=default
double getMass() const
Returns the mass of the sensitive g4volume.
Definition gtouchable.h:327
std::string cellKey() const
Builds a compact key identifying the hit cell of this touchable.
bool exists_in_vector(const std::vector< GTouchable > &v) const
Checks whether this touchable exists in a vector using operator== semantics.
Definition gtouchable.h:338
std::string getIdentityString() const
Builds a human-readable identity string from the stored identifiers.
Definition gtouchable.h:307
std::vector< double > getDetectorDimensions() const
Returns the detector dimensions stored at construction time.
Definition gtouchable.h:320
GTouchable(const std::shared_ptr< GTouchable > &base, int newTimeIndex)
Copy constructor that preserves identity but updates the electronics time-cell index.
Definition gtouchable.h:207
CONSTRUCTOR
DESTRUCTOR
Conventions and constants used by the gtouchable module.
GTouchableType
Enumeration representing the type of a touchable sensitive element.
Definition gtouchable.h:34
@ flux
Definition gtouchable.h:36
@ integral_counter
Definition gtouchable.h:40
@ readout
Definition gtouchable.h:35
@ dosimeter
Definition gtouchable.h:39
@ particle_counter
Definition gtouchable.h:38
@ gPhotonDetector
Definition gtouchable.h:37
Options definition entry point for the gtouchable module.
constexpr char INTEGRAL_COUNTERNAME[]
Digitization type name for integral_counter.
const char * to_string(GTouchableType t)
Converts a GTouchableType value to a stable string for logging.
Definition gtouchable.h:58
constexpr char FLUXNAME[]
Digitization type name for flux-like detectors.
constexpr char COUNTERNAME[]
Digitization type name for simple particle counters.
constexpr char GPHOTON_DETECTORNAME[]
Digitization type name for optical-photon flux detectors.
constexpr char DOSIMETERNAME[]
Digitization type name for dosimeters.
A single (name,value) identifier element used to build a touchable identity vector.
Definition gtouchable.h:82
GIdentifier(const std::string &n, int v)
Constructs a GIdentifier.
Definition gtouchable.h:90
friend std::ostream & operator<<(std::ostream &stream, const GIdentifier &gidentifier)
Stream output helper used in logs and diagnostics.
std::string getName() const
Returns the identifier name.
Definition gtouchable.h:107
bool operator==(const GIdentifier &gid) const
Compares identifiers by value only.
Definition gtouchable.h:101
int getValue() const
Returns the identifier value.
Definition gtouchable.h:113
void setValue(int v)
Sets the identifier value. Used by digitization plugins that recompute the wire/layer address.
Definition gtouchable.h:116