Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-17 08:02:36

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 CERN for the benefit of the ACTS project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
0008 
0009 #include "ActsExamples/Geant4/ParticleKillAction.hpp"
0010 
0011 #include "Acts/Definitions/Algebra.hpp"
0012 #include "Acts/Definitions/Units.hpp"
0013 #include "ActsExamples/Geant4/AlgebraConverters.hpp"
0014 #include "ActsFatras/EventData/Barcode.hpp"
0015 #include "ActsFatras/EventData/ParticleOutcome.hpp"
0016 
0017 #include <ostream>
0018 #include <utility>
0019 
0020 #include <G4RunManager.hh>
0021 #include <G4Step.hh>
0022 #include <G4StepPoint.hh>
0023 #include <G4Track.hh>
0024 #include <G4UnitsTable.hh>
0025 #include <G4VPhysicalVolume.hh>
0026 
0027 namespace ActsExamples::Geant4 {
0028 
0029 ParticleKillAction::ParticleKillAction(
0030     const Config& cfg, std::unique_ptr<const Acts::Logger> logger)
0031     : G4UserSteppingAction(), m_cfg(cfg), m_logger(std::move(logger)) {}
0032 
0033 void ParticleKillAction::UserSteppingAction(const G4Step* step) {
0034   constexpr double convertTime = Acts::UnitConstants::ns / CLHEP::ns;
0035 
0036   G4Track* track = step->GetTrack();
0037 
0038   const auto time = convertTime * track->GetGlobalTime();
0039   const bool isSecondary =
0040       track->GetDynamicParticle()->GetPrimaryParticle() == nullptr;
0041 
0042   const bool outOfVolume =
0043       m_cfg.volume &&
0044       !m_cfg.volume->inside(convertPosition(track->GetPosition()));
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 "
0051                << convertPosition(track->GetPosition()) << " and global time "
0052                << time / Acts::UnitConstants::ns << "ns and isSecondary "
0053                << isSecondary);
0054     track->SetTrackStatus(G4TrackStatus::fStopAndKill);
0055   }
0056 
0057   // store the outcome of the particle
0058   auto trackIt = eventStore().trackIdMapping.find(track->GetTrackID());
0059   // check if we have a particle assigned to track
0060   if (trackIt != eventStore().trackIdMapping.end()) {
0061     // set the outcome of the particle
0062     const ActsFatras::Barcode particleId = trackIt->second;
0063     if (outOfVolume) {
0064       eventStore().particleOutcome[particleId] =
0065           ActsFatras::ParticleOutcome::KilledVolumeExit;
0066     } else if (outOfTime) {
0067       eventStore().particleOutcome[particleId] =
0068           ActsFatras::ParticleOutcome::KilledTime;
0069     } else if (invalidSecondary) {
0070       eventStore().particleOutcome[particleId] =
0071           ActsFatras::ParticleOutcome::KilledSecondaryParticle;
0072     } else if (track->GetTrackStatus() == fStopAndKill) {
0073       eventStore().particleOutcome[particleId] =
0074           ActsFatras::ParticleOutcome::KilledInteraction;
0075     }
0076   }
0077 }
0078 
0079 }  // namespace ActsExamples::Geant4