File indexing completed on 2025-01-18 09:11:36
0001
0002
0003
0004
0005
0006
0007
0008
0009 #include "ActsExamples/Geant4/ParticleKillAction.hpp"
0010
0011 #include "Acts/Definitions/Algebra.hpp"
0012 #include "Acts/Definitions/Units.hpp"
0013 #include "ActsFatras/EventData/Barcode.hpp"
0014 #include "ActsFatras/EventData/ParticleOutcome.hpp"
0015
0016 #include <ostream>
0017 #include <utility>
0018
0019 #include <G4RunManager.hh>
0020 #include <G4Step.hh>
0021 #include <G4StepPoint.hh>
0022 #include <G4Track.hh>
0023 #include <G4UnitsTable.hh>
0024 #include <G4VPhysicalVolume.hh>
0025
0026 namespace ActsExamples::Geant4 {
0027
0028 ParticleKillAction::ParticleKillAction(
0029 const Config& cfg, std::unique_ptr<const Acts::Logger> logger)
0030 : G4UserSteppingAction(), m_cfg(cfg), m_logger(std::move(logger)) {}
0031
0032 void ParticleKillAction::UserSteppingAction(const G4Step* step) {
0033 constexpr double convertLength = Acts::UnitConstants::mm / CLHEP::mm;
0034 constexpr double convertTime = Acts::UnitConstants::ns / CLHEP::ns;
0035
0036 G4Track* track = step->GetTrack();
0037
0038 const auto pos = convertLength * track->GetPosition();
0039 const auto time = convertTime * track->GetGlobalTime();
0040 const bool isSecondary =
0041 track->GetDynamicParticle()->GetPrimaryParticle() == nullptr;
0042
0043 const bool outOfVolume = m_cfg.volume && !m_cfg.volume->inside(Acts::Vector3{
0044 pos.x(), pos.y(), pos.z()});
0045 const bool outOfTime = time > m_cfg.maxTime;
0046 const bool invalidSecondary = m_cfg.secondaries && isSecondary;
0047
0048 if (outOfVolume || outOfTime || invalidSecondary) {
0049 ACTS_DEBUG("Kill track with internal track ID "
0050 << track->GetTrackID() << " at " << pos << " and global time "
0051 << time / Acts::UnitConstants::ns << "ns and isSecondary "
0052 << isSecondary);
0053 track->SetTrackStatus(G4TrackStatus::fStopAndKill);
0054 }
0055
0056
0057 auto trackIt = eventStore().trackIdMapping.find(track->GetTrackID());
0058
0059 if (trackIt != eventStore().trackIdMapping.end()) {
0060
0061 const ActsFatras::Barcode particleId = trackIt->second;
0062 if (outOfVolume) {
0063 eventStore().particleOutcome[particleId] =
0064 ActsFatras::ParticleOutcome::KilledVolumeExit;
0065 } else if (outOfTime) {
0066 eventStore().particleOutcome[particleId] =
0067 ActsFatras::ParticleOutcome::KilledTime;
0068 } else if (invalidSecondary) {
0069 eventStore().particleOutcome[particleId] =
0070 ActsFatras::ParticleOutcome::KilledSecondaryParticle;
0071 } else if (track->GetTrackStatus() == fStopAndKill) {
0072 eventStore().particleOutcome[particleId] =
0073 ActsFatras::ParticleOutcome::KilledInteraction;
0074 }
0075 }
0076 }
0077
0078 }