diff --git a/core/opengate_core/opengate_lib/GatePhaseSpaceActor.cpp b/core/opengate_core/opengate_lib/GatePhaseSpaceActor.cpp index 6a42507598..b38f7caafd 100644 --- a/core/opengate_core/opengate_lib/GatePhaseSpaceActor.cpp +++ b/core/opengate_core/opengate_lib/GatePhaseSpaceActor.cpp @@ -132,6 +132,14 @@ void GatePhaseSpaceActor::SteppingAction(G4Step *step) { auto &l = fThreadLocalData.Get(); + // Check if this SteppingAction was triggered via the sensitive detector + // attached to the top volume, i.e. the "attached_to" volume + // if it was triggered by a daughter volume, this is not truely a step + // at the outer surface, but rather an irrelevant (for the purpose of this + // actor) internal surface crossing + if (!IsStepInTopAttachedVolume(step)) + return; + // Particle enters the volume if the pre step is at the volume boundary const bool entering = step->GetPreStepPoint()->GetStepStatus() == fGeomBoundary; diff --git a/core/opengate_core/opengate_lib/GateVActor.cpp b/core/opengate_core/opengate_lib/GateVActor.cpp index be90078b20..8a2514e0b7 100644 --- a/core/opengate_core/opengate_lib/GateVActor.cpp +++ b/core/opengate_core/opengate_lib/GateVActor.cpp @@ -175,6 +175,28 @@ bool GateVActor::IsStepEnteringVolume( return false; } +bool GateVActor::IsStepInTopAttachedVolume(const G4Step *step) const { + const auto *touchable = step->GetPreStepPoint()->GetTouchable(); + if (touchable == nullptr) + return false; + + const auto history_depth = touchable->GetHistoryDepth(); + int attached_volume_depth = -1; + for (int i = 0; i <= history_depth; i++) { + const auto *volume = touchable->GetVolume(i); + if (volume == nullptr) + continue; + const auto *logical_volume = volume->GetLogicalVolume(); + if (logical_volume != nullptr && + logical_volume->GetName() == fAttachedToVolumeName) { + attached_volume_depth = i; + break; + } + } + + return attached_volume_depth == 0; +} + bool GateVActor::IsStepExitingAttachedVolume(const G4Step *step) const { if (fAttachedToVolumeMotherName == "None") { Fatal("Cannot use IsStepExitingAttachedVolume when " diff --git a/core/opengate_core/opengate_lib/GateVActor.h b/core/opengate_core/opengate_lib/GateVActor.h index 4ae316bccc..c46cba2797 100644 --- a/core/opengate_core/opengate_lib/GateVActor.h +++ b/core/opengate_core/opengate_lib/GateVActor.h @@ -122,6 +122,8 @@ class GateVActor : public G4VPrimitiveScorer { IsStepEnteringVolume(const G4Step *step, const std::vector &volumes); + bool IsStepInTopAttachedVolume(const G4Step *step) const; + bool IsStepExitingAttachedVolume(const G4Step *step) const; inline static std::string fOutputNameRoot = "root_output";