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