gdynamicDigitization
Loading...
Searching...
No Matches
gdynamicdigitization.cc
Go to the documentation of this file.
1
14
15// gemc
17#include "gdataConventions.h"
19
20
21// See header for API docs.
22std::unique_ptr<GTrueInfoData> GDynamicDigitization::collectTrueInformationImpl(GHit* ghit, size_t hitn) {
23 auto trueInfoData = std::make_unique<GTrueInfoData>(gopts, ghit);
24
25 std::vector<GIdentifier> identities = ghit->getGID();
26
27
28
29 // Bit 0 is expected to contain the always-present true-hit quantities.
30 ghit->calculateInfosForBit(0);
31
32 // Average positions are computed at the hit level by GHit and returned here.
33 G4ThreeVector avgGlobalPos = ghit->getAvgGlobaPosition();
34 G4ThreeVector avgLocalPos = ghit->getAvgLocalPosition();
35
36 trueInfoData->includeVariable("pid", ghit->getPid());
37 trueInfoData->includeVariable("tid", ghit->getTid());
38 trueInfoData->includeVariable("totalEDeposited", ghit->getTotalEnergyDeposited());
39 trueInfoData->includeVariable("avgTime", ghit->getAverageTime());
40 trueInfoData->includeVariable("avgx", avgGlobalPos.getX());
41 trueInfoData->includeVariable("avgy", avgGlobalPos.getY());
42 trueInfoData->includeVariable("avgz", avgGlobalPos.getZ());
43 trueInfoData->includeVariable("avglx", avgLocalPos.getX());
44 trueInfoData->includeVariable("avgly", avgLocalPos.getY());
45 trueInfoData->includeVariable("avglz", avgLocalPos.getZ());
46 trueInfoData->includeVariable("hitn", static_cast<int>(hitn)); // assume hitn < INT_MAX
47
48 // Bit 1 typically includes metadata like the process name.
49 trueInfoData->includeVariable("processName", ghit->getProcessName());
50
51 return trueInfoData;
52}
53
54// See header for API docs.
55void GDynamicDigitization::chargeAndTimeAtHardware(int time, int q, const GHit* ghit, GDigitizedData& gdata) {
57
58 if (translationTable == nullptr) {
59 log->error(EC__TTNOTFOUNDINTT, "Translation Table not found");
60 }
61
62 // Translate a TT id into a crate/slot/channel triple.
63 std::vector<int> haddress = translationTable->getElectronics(ghit->getTTID()).getHAddress();
64
65 // The translation table uses a sentinel to indicate an uninitialized hardware address.
66 if (haddress.front() == UNINITIALIZEDNUMBERQUANTITY) {
67 log->error(EC__GIDENTITYNOTFOUNDINTT, "Translation Table found, but haddress was not initialized");
68 }
69
70 gdata.includeVariable(CRATESTRINGID, haddress[0]);
71 gdata.includeVariable(SLOTSTRINGID, haddress[1]);
72 gdata.includeVariable(CHANNELSTRINGID, haddress[2]);
75}
76
77// See header for API docs.
78GTouchableModifiers::GTouchableModifiers(const std::vector<std::string>& touchableNames) {
79 // Pre-declare keys so later access is well-defined for known touchables.
80 for (const auto& tname : touchableNames) {
81 modifierWeightsMap[tname] = {};
82 }
83}
84
85// See header for API docs.
86void GTouchableModifiers::insertIdAndWeight(const std::string& touchableName, int idValue, double weight) {
87 // Stored as a flat vector: (id, weight, id, weight, ...)
88 modifierWeightsMap[touchableName].push_back(idValue);
89 modifierWeightsMap[touchableName].push_back(weight);
90}
91
92// See header for API docs.
93void GTouchableModifiers::insertIdWeightAndTime(const std::string& touchableName, int idValue, double weight,
94 double time) {
95 // Stored as a flat vector: (id, weight, time, id, weight, time, ...)
96 modifierWeightsAndTimesMap[touchableName].push_back(idValue);
97 modifierWeightsAndTimesMap[touchableName].push_back(weight);
98 modifierWeightsAndTimesMap[touchableName].push_back(time);
99}
100
101// See header for API docs.
102void GTouchableModifiers::assignOverallWeight(const std::string& tname, double totalWeight) {
103 // Normalize (id, weight) pairs.
104 size_t countWeights = modifierWeightsMap[tname].size() / 2;
105 for (size_t h = 0; h < countWeights; h++) {
106 modifierWeightsMap[tname][h * 2 + 1] = modifierWeightsMap[tname][h * 2 + 1] / totalWeight;
107 }
108
109 // Normalize (id, weight, time) triplets.
110 size_t countWeightsAndTimes = modifierWeightsAndTimesMap[tname].size() / 3;
111 for (size_t h = 0; h < countWeightsAndTimes; h++) {
112 modifierWeightsAndTimesMap[tname][h * 3 + 1] = modifierWeightsAndTimesMap[tname][h * 3 + 1] / totalWeight;
113 }
114}
115
116// See header for API docs.
117double GDynamicDigitization::processStepTimeImpl([[maybe_unused]] const std::shared_ptr<GTouchable>& gTouchID,
118 [[maybe_unused]] G4Step* thisStep) {
119 // Default time definition: global time of the post-step point.
120 return thisStep->GetPostStepPoint()->GetGlobalTime();
121}
122
123// See header for API docs.
124std::vector<std::shared_ptr<GTouchable>> GDynamicDigitization::processTouchableImpl(
125 std::shared_ptr<GTouchable> gtouchable, G4Step* thisStep) {
126 double stepTimeAtElectronics = processStepTime(gtouchable, thisStep);
127 int stepTimeAtElectronicsIndex = readoutSpecs->timeCellIndex(stepTimeAtElectronics);
128
129 std::vector<std::shared_ptr<GTouchable>> result;
130
131 // If the touchable does not yet have a time index, or it matches the current step's
132 // index, we can reuse it.
133 if (stepTimeAtElectronicsIndex == gtouchable->getStepTimeAtElectronicsIndex() ||
134 gtouchable->getStepTimeAtElectronicsIndex() == GTOUCHABLEUNSETTIMEINDEX) {
135 gtouchable->assignStepTimeAtElectronicsIndex(stepTimeAtElectronicsIndex);
136 result.emplace_back(gtouchable);
137 }
138 else {
139 // Otherwise, create a cloned touchable with the new time index and return both.
140 auto cloned = std::make_shared<GTouchable>(gtouchable, stepTimeAtElectronicsIndex);
141
142 // std::initializer_list requires copyable elements, so build the vector explicitly.
143 result.emplace_back(gtouchable);
144 result.emplace_back(cloned);
145 }
146
147 return result;
148}
149
150// See header for API docs.
151std::vector<std::shared_ptr<GTouchable>> GDynamicDigitization::processGTouchableModifiersImpl(
152 [[maybe_unused]] const std::shared_ptr<GTouchable>& gTouchID,
153 [[maybe_unused]] const GTouchableModifiers& gmods) {
154 // Default behavior: no modifier processing.
155 std::vector<std::shared_ptr<GTouchable>> touchables;
156 return touchables;
157}
std::shared_ptr< GLogger > log
void includeVariable(const std::string &vname, int value)
std::shared_ptr< GOptions > gopts
Options used by the digitization plugin instance.
virtual std::unique_ptr< GTrueInfoData > collectTrueInformationImpl(GHit *ghit, size_t hitn)
Implementation hook for true-information collection.
double processStepTime(const std::shared_ptr< GTouchable > &gTouchID, G4Step *thisStep)
Computes the time associated with a simulation step for electronics binning.
virtual std::vector< std::shared_ptr< GTouchable > > processGTouchableModifiersImpl(const std::shared_ptr< GTouchable > &gTouchID, const GTouchableModifiers &gmods)
Implementation hook for touchable modifier application.
void check_if_log_defined() const
Ensures options/logging are configured before plugin methods run.
virtual double processStepTimeImpl(const std::shared_ptr< GTouchable > &gTouchID, G4Step *thisStep)
Implementation hook for step time computation.
std::shared_ptr< const GTranslationTable > translationTable
Translation table is typically loaded during initialization and treated as immutable.
virtual std::vector< std::shared_ptr< GTouchable > > processTouchableImpl(std::shared_ptr< GTouchable > gtouchable, G4Step *thisStep)
Implementation hook for touchable processing.
std::shared_ptr< const GReadoutSpecs > readoutSpecs
Readout specs are created during initialization and treated as immutable.
void chargeAndTimeAtHardware(int time, int q, const GHit *ghit, GDigitizedData &gdata)
Adds hardware-level time/charge and address fields to a digitized record.
int getPid() const
std::vector< GIdentifier > getGID() const
std::vector< int > getTTID() const
void calculateInfosForBit(int bit)
G4ThreeVector getAvgGlobaPosition()
double getTotalEnergyDeposited()
double getAverageTime()
G4ThreeVector getAvgLocalPosition()
int getTid() const
std::string getProcessName() const
void error(int exit_code, Args &&... args) const
int timeCellIndex(double time) const
Computes the 1-based electronics time-cell index for a given time.
Helper container for representing touchable “modifier” information.
void insertIdAndWeight(const std::string &touchableName, int idValue, double weight)
Inserts a new (id, weight) pair for the specified touchable.
void insertIdWeightAndTime(const std::string &touchableName, int idValue, double weight, double time)
Inserts a new (id, weight, time) triplet for the specified touchable.
void assignOverallWeight(const std::string &touchableName, double totalWeight)
Normalizes modifier weights by dividing by a provided total.
GTouchableModifiers(const std::vector< std::string > &touchableNames)
Constructs the container and declares supported touchable names.
GElectronic getElectronics(const std::vector< int > &identity) const
constexpr const char * TIMEATELECTRONICS
constexpr const char * CRATESTRINGID
constexpr const char * CHANNELSTRINGID
constexpr const char * SLOTSTRINGID
constexpr const char * CHARGEATELECTRONICS
#define GTOUCHABLEUNSETTIMEINDEX
#define EC__TTNOTFOUNDINTT
#define EC__GIDENTITYNOTFOUNDINTT
#define UNINITIALIZEDNUMBERQUANTITY
std::vector< int > getHAddress()