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