gdynamicDigitization
Loading...
Searching...
No Matches
gdynamicdigitization.cc
Go to the documentation of this file.
1
14
15// gemc
17#include "gdataConventions.h"
19
20// c++
21#include <iostream>
22
23// See header for API docs.
24std::unique_ptr<GTrueInfoData> GDynamicDigitization::collectTrueInformationImpl(GHit* ghit, size_t hitn) {
25 auto trueInfoData = std::make_unique<GTrueInfoData>(gopts, ghit);
26
27 std::vector<GIdentifier> identities = ghit->getGID();
28
29 // Include all identities first so they are always present in the record.
30 for (auto& identity : identities) {
31 trueInfoData->includeVariable(identity.getName(), identity.getValue());
32 }
33
34 // Bit 0 is expected to contain the always-present true-hit quantities.
35 ghit->calculateInfosForBit(0);
36
37 // Average positions are computed at the hit level by GHit and returned here.
38 G4ThreeVector avgGlobalPos = ghit->getAvgGlobaPosition();
39 G4ThreeVector avgLocalPos = ghit->getAvgLocalPosition();
40
41 trueInfoData->includeVariable("totalEDeposited", ghit->getTotalEnergyDeposited());
42 trueInfoData->includeVariable("avgTime", ghit->getAverageTime());
43 trueInfoData->includeVariable("avgx", avgGlobalPos.getX());
44 trueInfoData->includeVariable("avgy", avgGlobalPos.getY());
45 trueInfoData->includeVariable("avgz", avgGlobalPos.getZ());
46 trueInfoData->includeVariable("avglx", avgLocalPos.getX());
47 trueInfoData->includeVariable("avgly", avgLocalPos.getY());
48 trueInfoData->includeVariable("avglz", avgLocalPos.getZ());
49 trueInfoData->includeVariable("hitn", static_cast<int>(hitn)); // assume hitn < INT_MAX
50
51 // Bit 1 typically includes metadata like the process name.
52 trueInfoData->includeVariable("processName", ghit->getProcessName());
53
54 return trueInfoData;
55}
56
57// See header for API docs.
58void GDynamicDigitization::chargeAndTimeAtHardware(int time, int q, const GHit* ghit, GDigitizedData& gdata) {
60
61 if (translationTable == nullptr) {
62 log->error(EC__TTNOTFOUNDINTT, "Translation Table not found");
63 return;
64 }
65
66 // Translate a TT id into a crate/slot/channel triple.
67 std::vector<int> haddress = translationTable->getElectronics(ghit->getTTID()).getHAddress();
68
69 // The translation table uses a sentinel to indicate an uninitialized hardware address.
70 if (haddress.front() == UNINITIALIZEDNUMBERQUANTITY) {
71 log->error(EC__GIDENTITYNOTFOUNDINTT, "Translation Table found, but haddress was not initialized");
72 return;
73 }
74
75 gdata.includeVariable(CRATESTRINGID, haddress[0]);
76 gdata.includeVariable(SLOTSTRINGID, haddress[1]);
77 gdata.includeVariable(CHANNELSTRINGID, haddress[2]);
80}
81
82// See header for API docs.
83GTouchableModifiers::GTouchableModifiers(const std::vector<std::string>& touchableNames) {
84 // Pre-declare keys so later access is well-defined for known touchables.
85 for (const auto& tname : touchableNames) {
86 modifierWeightsMap[tname] = {};
87 }
88}
89
90// See header for API docs.
91void GTouchableModifiers::insertIdAndWeight(const std::string& touchableName, int idValue, double weight) {
92 // Stored as a flat vector: (id, weight, id, weight, ...)
93 modifierWeightsMap[touchableName].push_back(idValue);
94 modifierWeightsMap[touchableName].push_back(weight);
95}
96
97// See header for API docs.
98void GTouchableModifiers::insertIdWeightAndTime(const std::string& touchableName, int idValue, double weight,
99 double time) {
100 // Stored as a flat vector: (id, weight, time, id, weight, time, ...)
101 modifierWeightsAndTimesMap[touchableName].push_back(idValue);
102 modifierWeightsAndTimesMap[touchableName].push_back(weight);
103 modifierWeightsAndTimesMap[touchableName].push_back(time);
104}
105
106// See header for API docs.
107void GTouchableModifiers::assignOverallWeight(const std::string& tname, double totalWeight) {
108 // Normalize (id, weight) pairs.
109 size_t countWeights = modifierWeightsMap[tname].size() / 2;
110 for (size_t h = 0; h < countWeights; h++) {
111 modifierWeightsMap[tname][h * 2 + 1] = modifierWeightsMap[tname][h * 2 + 1] / totalWeight;
112 }
113
114 // Normalize (id, weight, time) triplets.
115 size_t countWeightsAndTimes = modifierWeightsAndTimesMap[tname].size() / 3;
116 for (size_t h = 0; h < countWeightsAndTimes; h++) {
117 modifierWeightsAndTimesMap[tname][h * 3 + 1] = modifierWeightsAndTimesMap[tname][h * 3 + 1] / totalWeight;
118 }
119}
120
121// See header for API docs.
122double GDynamicDigitization::processStepTimeImpl([[maybe_unused]] const std::shared_ptr<GTouchable>& gTouchID,
123 [[maybe_unused]] G4Step* thisStep) {
124 // Default time definition: global time of the post-step point.
125 return thisStep->GetPostStepPoint()->GetGlobalTime();
126}
127
128// See header for API docs.
129std::vector<std::shared_ptr<GTouchable>> GDynamicDigitization::processTouchableImpl(
130 std::shared_ptr<GTouchable> gtouchable, G4Step* thisStep) {
131 double stepTimeAtElectronics = processStepTime(gtouchable, thisStep);
132 int stepTimeAtElectronicsIndex = readoutSpecs->timeCellIndex(stepTimeAtElectronics);
133
134 std::vector<std::shared_ptr<GTouchable>> result;
135
136 // If the touchable does not yet have a time index, or it matches the current step's
137 // index, we can reuse it.
138 if (stepTimeAtElectronicsIndex == gtouchable->getStepTimeAtElectronicsIndex() ||
139 gtouchable->getStepTimeAtElectronicsIndex() == GTOUCHABLEUNSETTIMEINDEX) {
140 gtouchable->assignStepTimeAtElectronicsIndex(stepTimeAtElectronicsIndex);
141 result.emplace_back(gtouchable);
142 }
143 else {
144 // Otherwise, create a cloned touchable with the new time index and return both.
145 auto cloned = std::make_shared<GTouchable>(gtouchable, stepTimeAtElectronicsIndex);
146
147 // std::initializer_list requires copyable elements, so build the vector explicitly.
148 result.emplace_back(gtouchable);
149 result.emplace_back(cloned);
150 }
151
152 return result;
153}
154
155// See header for API docs.
156std::vector<std::shared_ptr<GTouchable>> GDynamicDigitization::processGTouchableModifiersImpl(
157 [[maybe_unused]] const std::shared_ptr<GTouchable>& gTouchID,
158 [[maybe_unused]] const GTouchableModifiers& gmods) {
159 // Default behavior: no modifier processing.
160 std::vector<std::shared_ptr<GTouchable>> touchables;
161 return touchables;
162}
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.
std::vector< GIdentifier > getGID() const
std::vector< int > getTTID() const
void calculateInfosForBit(int bit)
G4ThreeVector getAvgGlobaPosition()
double getTotalEnergyDeposited()
double getAverageTime()
G4ThreeVector getAvgLocalPosition()
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()