5#include "G4SDManager.hh"
10 const std::shared_ptr<GOptions>&
goptions) :
12 G4VSensitiveDetector(sdName),
13 gHitsCollection(nullptr) {
18 std::string hitCollectionName = sdName +
"__HitCollection";
19 collectionName.insert(hitCollectionName);
28 std::string sdName = GetName();
32 touchableVector.clear();
40 auto hcID = G4SDManager::GetSDMpointer()->GetCollectionID(collectionName[0]);
41 g4hc->AddHitsCollection(hcID, gHitsCollection);
43 log->
info(2,
"Added collection id ", hcID,
" to G4HCofThisEvent");
52 double depe = thisStep->GetTotalEnergyDeposit();
61 std::vector<std::shared_ptr<GTouchable>> thisStepProcessedTouchables =
64 auto hcsize = gHitsCollection->GetSize();
67 " with ", std::to_string(thisStepProcessedTouchables.size()),
" touchable(s), edep: ",
68 std::to_string(depe),
", Hit collection size: ", hcsize);
70 for (
auto thisGTouchable : thisStepProcessedTouchables) {
72 thisGTouchable->assignTrackId(thisStep->GetTrack()->GetTrackID());
73 thisGTouchable->assignPId(thisStep->GetTrack()->GetDefinition()->GetPDGEncoding());
75 if (isThisANewTouchable(thisGTouchable)) {
76 gHitsCollection->insert(
new GHit(thisGTouchable, thisStep));
79 GHit* existingHit = getHitInHitCollectionUsingTouchable(thisGTouchable);
80 if (existingHit !=
nullptr) {
92bool GSensitiveDetector::isThisANewTouchable(
const std::shared_ptr<GTouchable>& thisTouchable) {
93 log->
info(2,
"GSensitiveDetector::isThisANewTouchable for " + GetName(),
94 " with touchable: " + thisTouchable->getIdentityString());
96 if (thisTouchable->exists_in_vector(touchableVector)) {
97 log->
info(2,
" ❌ not a new GTouchable, it is found, retrieving hit...");
101 log->
info(2,
" ✅ yes, new GTouchable. Adding it to touchableVector.");
102 touchableVector.push_back(*thisTouchable);
110GHit* GSensitiveDetector::getHitInHitCollectionUsingTouchable(
const std::shared_ptr<GTouchable>&
gtouchable) {
111 for (
unsigned int i = 0; i < gHitsCollection->GetSize(); i++) {
112 GHit* thisHit = (*gHitsCollection)[i];
113 const std::shared_ptr<GTouchable>& thisHitGTouchable = thisHit->
getGTouchable();
116 log->
info(2,
"GSensitiveDetector::getHitInHitCollectionUsingTouchable for " + GetName() +
117 " found existing hit in collection for touchable: " +
gtouchable->getIdentityString() +
118 " at index: " + std::to_string(i));
124 "GHit not found in hit collection for touchable: " +
gtouchable->getIdentityString(),
132 log->
info(1,
"GSensitiveDetector::EndOfEvent for " + GetName());
std::shared_ptr< GLogger > log
std::vector< std::shared_ptr< GTouchable > > processTouchable(std::shared_ptr< GTouchable > gtouchable, G4Step *thisStep)
virtual bool decisionToSkipHit(double energy)
static void clearTrackVertexCache()
std::shared_ptr< GTouchable > getGTouchable() const
void addHitInfos(const G4Step *thisStep)
void debug(debug_type type, Args &&... args) const
void info(int level, Args &&... args) const
void error(int exit_code, Args &&... args) const
GSensitiveDetector(const std::string &sdName, const std::shared_ptr< GOptions > &goptions)
Constructs a sensitive detector instance for a given detector name.
void EndOfEvent(G4HCofThisEvent *g4HitCollection) override
End-of-event hook called by Geant4.
void Initialize(G4HCofThisEvent *g4hc) override
Per-event initialization hook called by Geant4.
G4bool ProcessHits(G4Step *thisStep, G4TouchableHistory *g4th) override
Processes a Geant4 step and creates or updates hits in the current hits collection.
G4THitsCollection< GHit > GHitsCollection
Convenience alias for the Geant4 hits collection used by this module.
Declares the GSensitiveDetector class and module-level constants.
constexpr int ERR_NOCOLLECTION
Error code used when the Geant4 hits collection is unexpectedly missing.
constexpr int ERR_HITNOTFOUNDINCOLLECTION
Error code used when a hit is expected to exist but cannot be found in the current hit collection.
constexpr const char * GSENSITIVE_LOGGER
Logger name used by this module.