Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-05 08:09:46

0001 // This file is part of the Acts project.
0002 //
0003 // Copyright (C) 2021-2024 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 http://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 "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 ActsExamples::ParticleKillAction::ParticleKillAction(
0027     const Config& cfg, std::unique_ptr<const Acts::Logger> logger)
0028     : G4UserSteppingAction(), m_cfg(cfg), m_logger(std::move(logger)) {}
0029 
0030 void ActsExamples::ParticleKillAction::UserSteppingAction(const G4Step* step) {
0031   constexpr double convertLength = Acts::UnitConstants::mm / CLHEP::mm;
0032   constexpr double convertTime = Acts::UnitConstants::ns / CLHEP::ns;
0033 
0034   G4Track* track = step->GetTrack();
0035 
0036   const auto pos = convertLength * track->GetPosition();
0037   const auto time = convertTime * track->GetGlobalTime();
0038   const bool isSecondary =
0039       track->GetDynamicParticle()->GetPrimaryParticle() == nullptr;
0040 
0041   const bool outOfVolume = m_cfg.volume && !m_cfg.volume->inside(Acts::Vector3{
0042                                                pos.x(), pos.y(), pos.z()});
0043   const bool outOfTime = time > m_cfg.maxTime;
0044   const bool invalidSecondary = m_cfg.secondaries && isSecondary;
0045 
0046   if (outOfVolume || outOfTime || invalidSecondary) {
0047     ACTS_DEBUG("Kill track with internal track ID "
0048                << track->GetTrackID() << " at " << pos << " and global time "
0049                << time / Acts::UnitConstants::ns << "ns and isSecondary "
0050                << isSecondary);
0051     track->SetTrackStatus(G4TrackStatus::fStopAndKill);
0052   }
0053 
0054   // store the outcome of the particle
0055   auto trackIt = eventStore().trackIdMapping.find(track->GetTrackID());
0056   // check if we have a particle assigned to track
0057   if (trackIt != eventStore().trackIdMapping.end()) {
0058     // set the outcome of the particle
0059     const ActsFatras::Barcode particleId = trackIt->second;
0060     if (outOfVolume) {
0061       eventStore().particleOutcome[particleId] =
0062           ActsFatras::ParticleOutcome::KilledVolumeExit;
0063     } else if (outOfTime) {
0064       eventStore().particleOutcome[particleId] =
0065           ActsFatras::ParticleOutcome::KilledTime;
0066     } else if (invalidSecondary) {
0067       eventStore().particleOutcome[particleId] =
0068           ActsFatras::ParticleOutcome::KilledSecondaryParticle;
0069     } else if (track->GetTrackStatus() == fStopAndKill) {
0070       eventStore().particleOutcome[particleId] =
0071           ActsFatras::ParticleOutcome::KilledInteraction;
0072     }
0073   }
0074 }