gsd
Loading...
Searching...
No Matches
gsd.cc
Go to the documentation of this file.
1// gemc
2#include "gsd.h"
3
4// geant4
5#include "G4SDManager.hh"
6
7// Thread-local sensitive detector instance.
8// Constructor: initializes base logging, Geant4 SD name, and the hits collection name used for this detector.
9GSensitiveDetector::GSensitiveDetector(const std::string& sdName,
10 const std::shared_ptr<GOptions>& goptions) :
12 G4VSensitiveDetector(sdName) {
13 log->info(2, FUNCTION_NAME, " for " + sdName);
14
15 // collectionName is a G4VSensitiveDetector G4CollectionNameVector.
16 // GEMC uses its own handling of hits; this is set primarily for compatibility with Geant4 conventions/examples.
17 std::string hitCollectionName = sdName + "__HitCollection";
18 collectionName.insert(hitCollectionName);
19
20 log->debug(CONSTRUCTOR, FUNCTION_NAME, " for " + sdName);
21}
22
23
24// Thread-local; called at the beginning of each event.
25// Allocates and registers the per-event hits collection and resets per-event caches.
26void GSensitiveDetector::Initialize(G4HCofThisEvent* g4hc) {
27 std::string sdName = GetName();
28 log->info(1, FUNCTION_NAME, sdName);
29
30 // Hit content definition is read once per event from the digitization routine.
31 gHitBitSet = digitization_routine->readoutSpecs->getHitBitSet();
32
33 // Clearing touchableVector at the start of the event (per-event hit identity cache).
34 touchableVector.clear();
35
36 // Initializing gHitsCollection using the Geant4 G4THitsCollection constructor (expects detector and collection names).
37 gHitsCollection = new GHitsCollection(sdName, collectionName[0]);
38
39 // Add hits collection to the Geant4 event container.
40 // hcID is assigned by Geant4 and can be retrieved later by collection name.
41 auto hcID = G4SDManager::GetSDMpointer()->GetCollectionID(collectionName[0]);
42 g4hc->AddHitsCollection(hcID, gHitsCollection);
43
44 log->info(2, "Added collection id ", hcID, " to G4HCofThisEvent");
45}
46
47
48// Thread-local; called for each step in sensitive volumes.
49// Applies plugin filtering and touchable transformation, then creates new hits or updates existing hits.
50G4bool GSensitiveDetector::ProcessHits(G4Step* thisStep, [[maybe_unused]] G4TouchableHistory* g4th) {
51 // If there is a decision to skip this hit based on deposited energy, return immediately.
52
53 double depe = thisStep->GetTotalEnergyDeposit();
54
55 if (digitization_routine->decisionToSkipHit(depe)) { return true; }
56
57 // The hits collection should have been created in Initialize().
58 if (gHitsCollection == nullptr) { log->error(ERR_NOCOLLECTION, "No hit collection found"); }
59
60 // Get the vector of processed touchables returned by the digitization routine.
61 // If not overridden by the plugin, the base implementation typically returns a vector with the input touchable only.
62 std::vector<std::shared_ptr<GTouchable>> thisStepProcessedTouchables =
63 digitization_routine->processTouchable(getGTouchable(thisStep), thisStep);
64
65 auto hcsize = gHitsCollection->GetSize();
66
67 log->info(2, FUNCTION_NAME, " for ", GetName(),
68 " with ", std::to_string(thisStepProcessedTouchables.size()), " touchable(s), edep: ",
69 std::to_string(depe), ", Hit collection size: ", hcsize);
70
71 // If a new touchable is created, create a new GHit.
72 // Otherwise, retrieve the existing hit and append step information according to gHitBitSet.
73 for (auto thisGTouchable : thisStepProcessedTouchables) {
74 // Track id is attached to the touchable to keep hit identity consistent across updates.
75 thisGTouchable->assignTrackId(thisStep->GetTrack()->GetTrackID());
76 thisGTouchable->assignPId(thisStep->GetTrack()->GetDefinition()->GetPDGEncoding());
77
78 if (isThisANewTouchable(thisGTouchable)) {
79 // New hit: constructor fills initial step information based on gHitBitSet.
80 gHitsCollection->insert(new GHit(thisGTouchable, gHitBitSet, thisStep));
81 }
82 else {
83 // Existing hit: locate it and add additional information from this step according to gHitBitSet.
84 GHit* existingHit = getHitInHitCollectionUsingTouchable(thisGTouchable);
85 if (existingHit != nullptr) {
86 existingHit->addHitInfosForBitset(gHitBitSet, thisStep);
87 }
88 }
89 }
90
91 return true;
92}
93
94
95// Checks whether a touchable has already been seen in this event.
96// If not present, it is appended to the per-event cache.
97bool GSensitiveDetector::isThisANewTouchable(const std::shared_ptr<GTouchable>& thisTouchable) {
98 log->info(2, "GSensitiveDetector::isThisANewTouchable for " + GetName(),
99 " with touchable: " + thisTouchable->getIdentityString());
100
101 if (thisTouchable->exists_in_vector(touchableVector)) {
102 log->info(2, " ❌ not a new GTouchable, it is found, retrieving hit...");
103 return false; // Not a new touchable.
104 }
105 else {
106 log->info(2, " ✅ yes, new GTouchable. Adding it to touchableVector.");
107 touchableVector.push_back(*thisTouchable);
108 return true; // It's a new touchable.
109 }
110}
111
112
113// Linear search for the hit matching the provided touchable.
114// The collection is expected to contain a matching entry when called from ProcessHits().
115GHit* GSensitiveDetector::getHitInHitCollectionUsingTouchable(const std::shared_ptr<GTouchable>& gtouchable) {
116 for (unsigned int i = 0; i < gHitsCollection->GetSize(); i++) {
117 GHit* thisHit = (*gHitsCollection)[i];
118 const std::shared_ptr<GTouchable>& thisHitGTouchable = thisHit->getGTouchable();
119
120 if (*gtouchable == *thisHitGTouchable) {
121 log->info(2, "GSensitiveDetector::getHitInHitCollectionUsingTouchable for " + GetName() +
122 " found existing hit in collection for touchable: " + gtouchable->getIdentityString() +
123 " at index: " + std::to_string(i));
124 return thisHit;
125 }
126 }
127
129 "GHit not found in hit collection for touchable: " + gtouchable->getIdentityString(),
130 "in ", GetName());
131}
132
133
134// Thread-local end-of-event hook.
135// At this stage, Geant4 owns the event hit container that references the hits collection.
136void GSensitiveDetector::EndOfEvent([[maybe_unused]] G4HCofThisEvent* g4hc) {
137 log->info(1, "GSensitiveDetector::EndOfEvent for " + GetName());
138}
std::shared_ptr< GLogger > log
std::vector< std::shared_ptr< GTouchable > > processTouchable(std::shared_ptr< GTouchable > gtouchable, G4Step *thisStep)
std::shared_ptr< const GReadoutSpecs > readoutSpecs
virtual bool decisionToSkipHit(double energy)
void addHitInfosForBitset(HitBitSet hbs, const G4Step *thisStep)
std::shared_ptr< GTouchable > getGTouchable() const
void debug(debug_type type, Args &&... args) const
void info(int level, Args &&... args) const
void error(int exit_code, Args &&... args) const
HitBitSet getHitBitSet() const
GSensitiveDetector(const std::string &sdName, const std::shared_ptr< GOptions > &goptions)
Constructs a sensitive detector instance for a given detector name.
Definition gsd.cc:9
void EndOfEvent(G4HCofThisEvent *g4HitCollection) override
End-of-event hook called by Geant4.
Definition gsd.cc:136
void Initialize(G4HCofThisEvent *g4hc) override
Per-event initialization hook called by Geant4.
Definition gsd.cc:26
G4bool ProcessHits(G4Step *thisStep, G4TouchableHistory *g4th) override
Processes a Geant4 step and creates or updates hits in the current hits collection.
Definition gsd.cc:50
#define FUNCTION_NAME
CONSTRUCTOR
G4THitsCollection< GHit > GHitsCollection
Convenience alias for the Geant4 hits collection used by this module.
Definition gsd.h:73
Declares the GSensitiveDetector class and module-level constants.
constexpr int ERR_NOCOLLECTION
Error code used when the Geant4 hits collection is unexpectedly missing.
Definition gsd.h:40
constexpr int ERR_HITNOTFOUNDINCOLLECTION
Error code used when a hit is expected to exist but cannot be found in the current hit collection.
Definition gsd.h:35
constexpr const char * GSENSITIVE_LOGGER
Logger name used by this module.
Definition gsd.h:47