Skip to content

Commit

Permalink
feat: Geant4 kill particle after configurable time (#2097)
Browse files Browse the repository at this point in the history
This adds an option to the particle killer to kill a particle after a certain time. Also applies this feature to the full-chain-example of the ODD with a (hopefully) reasonable value of 25ns.

Co-authored-by: Andreas Stefl <487211+andiwand@users.noreply.github.com>
  • Loading branch information
benjaminhuth and andiwand committed May 26, 2023
1 parent 2c02d29 commit 672b144
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class ParticleKillAction : public G4UserSteppingAction {
/// Configuration of the Stepping action
struct Config {
std::shared_ptr<const Acts::Volume> volume;
double maxTime = std::numeric_limits<double>::infinity();
};

/// Construct the stepping action
Expand Down
16 changes: 12 additions & 4 deletions Examples/Algorithms/Geant4/src/ParticleKillAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,22 @@ ActsExamples::ParticleKillAction::ParticleKillAction(

void ActsExamples::ParticleKillAction::UserSteppingAction(const G4Step* step) {
constexpr double convertLength = Acts::UnitConstants::mm / CLHEP::mm;
constexpr double convertTime = Acts::UnitConstants::ns / CLHEP::ns;

G4Track* track = step->GetTrack();

const auto pos = convertLength * track->GetPosition();
const auto time = convertTime * track->GetGlobalTime();

const bool outOfVolume =
m_cfg.volume and
not m_cfg.volume->inside(Acts::Vector3{pos.x(), pos.y(), pos.z()});
const bool outOfTime = time > m_cfg.maxTime;

if (m_cfg.volume and
not m_cfg.volume->inside(Acts::Vector3{pos.x(), pos.y(), pos.z()})) {
ACTS_DEBUG("Kill track with internal track ID " << track->GetTrackID()
<< " at " << pos);
if (outOfVolume or outOfTime) {
ACTS_DEBUG("Kill track with internal track ID "
<< track->GetTrackID() << " at " << pos << " and global time "
<< time / Acts::UnitConstants::ns << "ns");
track->SetTrackStatus(G4TrackStatus::fStopAndKill);
}
}
5 changes: 4 additions & 1 deletion Examples/Python/python/acts/examples/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,6 @@ def addSimWriters(
particlesInitial="particles_initial",
particlesFinal="particles_final",
) -> None:

customLogLevel = acts.examples.defaultLogging(s, logLevel)

if outputDirCsv is not None:
Expand Down Expand Up @@ -577,6 +576,7 @@ def addGeant4(
outputDirRoot: Optional[Union[Path, str]] = None,
logLevel: Optional[acts.logging.Level] = None,
killVolume: Optional[acts.Volume] = None,
killAfterTime: float = float("inf"),
) -> None:
"""This function steers the detector simulation using Geant4
Expand All @@ -600,6 +600,8 @@ def addGeant4(
the output folder for the Root output, None triggers no output
killVolume: acts.Volume, None
if given, particles are killed when going outside of this volume.
killAfterTime: float, None
if given, particle are killed after the global time since event creation exceeds the given value
"""

from acts.examples.geant4 import Geant4Simulation, makeGeant4SimulationConfig
Expand Down Expand Up @@ -633,6 +635,7 @@ def addGeant4(
volumeMappings=volumeMappings,
materialMappings=materialMappings,
killVolume=killVolume,
killAfterTime=killAfterTime,
recordHitsOfSecondaries=recordHitsOfSecondaries,
keepParticlesWithoutHits=keepParticlesWithoutHits,
)
Expand Down
3 changes: 1 addition & 2 deletions Examples/Python/src/Base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ namespace Acts::Python {
void addUnits(Context& ctx) {
auto& m = ctx.get("main");
auto u = m.def_submodule("UnitConstants");
using namespace Acts::UnitConstants;

#define UNIT(x) u.attr(#x) = x;
#define UNIT(x) u.attr(#x) = Acts::UnitConstants::x;

UNIT(fm)
UNIT(pm)
Expand Down
9 changes: 5 additions & 4 deletions Examples/Python/src/Geant4Component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ PYBIND11_MODULE(ActsPythonBindingsGeant4, mod) {
magneticField,
const std::vector<std::string>& volumeMappings,
const std::vector<std::string>& materialMappings,
std::shared_ptr<const Acts::Volume> killVolume,
std::shared_ptr<const Acts::Volume> killVolume, double killAfterTime,
bool recordHitsOfSecondaries, bool keepParticlesWithoutHits) {
auto logger = Acts::getDefaultLogger("Geant4", level);

Expand All @@ -180,9 +180,9 @@ PYBIND11_MODULE(ActsPythonBindingsGeant4, mod) {
steppingCfg.actions.push_back(new SensitiveSteppingAction(
g4StepCfg, logger->cloneWithSuffix("SensitiveStepping")));

steppingCfg.actions.push_back(
new ParticleKillAction(ParticleKillAction::Config{killVolume},
logger->cloneWithSuffix("Killer")));
steppingCfg.actions.push_back(new ParticleKillAction(
ParticleKillAction::Config{killVolume, killAfterTime},
logger->cloneWithSuffix("Killer")));

g4Cfg.steppingAction = new ActsSteppingActionList(steppingCfg);

Expand Down Expand Up @@ -219,6 +219,7 @@ PYBIND11_MODULE(ActsPythonBindingsGeant4, mod) {
py::arg("volumeMappings") = std::vector<std::string>{},
py::arg("materialMappings") = std::vector<std::string>{},
py::arg("killVolume") = nullptr,
py::arg("killAfterTime") = std::numeric_limits<double>::infinity(),
py::arg("recordHitsOfSecondaries") = true,
py::arg("keepParticlesWithoutHits") = true);

Expand Down
3 changes: 2 additions & 1 deletion Examples/Scripts/Python/full_chain_odd.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@
outputDirRoot=outputDir,
# outputDirCsv=outputDir,
rnd=rnd,
killVolume=acts.Volume.makeCylinderVolume(r=1100, halfZ=3000),
killVolume=acts.Volume.makeCylinderVolume(r=1.1 * u.m, halfZ=3.0 * u.m),
killAfterTime=25 * u.ns,
)
else:
addFatras(
Expand Down

0 comments on commit 672b144

Please sign in to comment.