5#include "G4SDManager.hh"
10 const std::shared_ptr<GOptions>&
goptions) :
12 G4VSensitiveDetector(sdName) {
17 std::string hitCollectionName = sdName +
"__HitCollection";
18 collectionName.insert(hitCollectionName);
27 std::string sdName = GetName();
34 touchableVector.clear();
41 auto hcID = G4SDManager::GetSDMpointer()->GetCollectionID(collectionName[0]);
42 g4hc->AddHitsCollection(hcID, gHitsCollection);
44 log->
info(2,
"Added collection id ", hcID,
" to G4HCofThisEvent");
52 double depe = thisStep->GetTotalEnergyDeposit();
60 std::vector<std::shared_ptr<GTouchable>> thisStepProcessedTouchables =
63 auto hcsize = gHitsCollection->GetSize();
66 " with ", std::to_string(thisStepProcessedTouchables.size()),
" touchable(s), edep: ",
67 std::to_string(depe),
", Hit collection size: ", hcsize);
71 for (
auto thisGTouchable : thisStepProcessedTouchables) {
73 thisGTouchable->assignTrackId(thisStep->GetTrack()->GetTrackID());
75 if (isThisANewTouchable(thisGTouchable)) {
77 gHitsCollection->insert(
new GHit(thisGTouchable, gHitBitSet, thisStep));
81 GHit* existingHit = getHitInHitCollectionUsingTouchable(thisGTouchable);
82 if (existingHit !=
nullptr) {
94bool GSensitiveDetector::isThisANewTouchable(
const std::shared_ptr<GTouchable>& thisTouchable) {
95 log->
info(2,
"GSensitiveDetector::isThisANewTouchable for " + GetName(),
96 " with touchable: " + thisTouchable->getIdentityString());
98 if (thisTouchable->exists_in_vector(touchableVector)) {
99 log->
info(2,
" ❌ not a new GTouchable, it is found, retrieving hit...");
103 log->
info(2,
" ✅ yes, new GTouchable. Adding it to touchableVector.");
104 touchableVector.push_back(*thisTouchable);
112GHit* GSensitiveDetector::getHitInHitCollectionUsingTouchable(
const std::shared_ptr<GTouchable>&
gtouchable) {
113 for (
unsigned int i = 0; i < gHitsCollection->GetSize(); i++) {
114 GHit* thisHit = (*gHitsCollection)[i];
115 const std::shared_ptr<GTouchable>& thisHitGTouchable = thisHit->
getGTouchable();
118 log->
info(2,
"GSensitiveDetector::getHitInHitCollectionUsingTouchable for " + GetName() +
119 " found existing hit in collection for touchable: " +
gtouchable->getIdentityString() +
120 " at index: " + std::to_string(i));
126 "GHit not found in hit collection for touchable: " +
gtouchable->getIdentityString(),
134 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)
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.
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.