-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: particle killing 4 Geant4 and refactor user actions (#2057)
Out Geant4 algorithm allows to pass `std::vector`s of user actions. However, multiple actions per type seem not to be supported by Geant4. This changes this to allow only one user action per type (run, event, particle, step). I'm not sure if this was possible in former Geant4 versions, there is also a comment in the Geant4 source code that indicates it is possible to add multiple actions per type. However, this is not the case.
- Loading branch information
1 parent
2a90742
commit 9cff63a
Showing
11 changed files
with
219 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
Examples/Algorithms/Geant4/include/ActsExamples/Geant4/ActsSteppingActionList.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// This file is part of the Acts project. | ||
// | ||
// Copyright (C) 2023 CERN for the benefit of the Acts project | ||
// | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
#pragma once | ||
|
||
#include "Acts/Utilities/Logger.hpp" | ||
#include "ActsExamples/EventData/SimHit.hpp" | ||
#include "ActsExamples/EventData/SimParticle.hpp" | ||
|
||
#include <memory> | ||
#include <optional> | ||
#include <string> | ||
|
||
#include <G4UserSteppingAction.hh> | ||
|
||
namespace ActsExamples { | ||
|
||
/// Geant4 only allows one user action of each type. This simple wrapper | ||
/// dispatches multiple actions to Geant4. | ||
class ActsSteppingActionList : public G4UserSteppingAction { | ||
public: | ||
struct Config { | ||
std::vector<G4UserSteppingAction *> actions; | ||
}; | ||
|
||
ActsSteppingActionList(const Config &cfg) : m_cfg(cfg) {} | ||
|
||
void UserSteppingAction(const G4Step *step) override { | ||
for (auto action : m_cfg.actions) { | ||
if (action) { | ||
action->UserSteppingAction(step); | ||
} | ||
} | ||
} | ||
|
||
private: | ||
Config m_cfg; | ||
}; | ||
|
||
} // namespace ActsExamples |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
Examples/Algorithms/Geant4/include/ActsExamples/Geant4/ParticleKillAction.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// This file is part of the Acts project. | ||
// | ||
// Copyright (C) 2023 CERN for the benefit of the Acts project | ||
// | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
#pragma once | ||
|
||
#include "Acts/Geometry/Volume.hpp" | ||
#include "Acts/Utilities/Logger.hpp" | ||
|
||
#include <memory> | ||
#include <string> | ||
|
||
#include <G4UserSteppingAction.hh> | ||
|
||
namespace ActsExamples { | ||
|
||
/// A G4SteppingAction that is called for every step in | ||
/// the simulation process. | ||
/// | ||
/// It checks whether the particle can be killed according when its position | ||
/// exceeds the configured values for |z| or r. | ||
class ParticleKillAction : public G4UserSteppingAction { | ||
public: | ||
/// Configuration of the Stepping action | ||
struct Config { | ||
std::shared_ptr<const Acts::Volume> volume; | ||
}; | ||
|
||
/// Construct the stepping action | ||
/// | ||
/// @param cfg the configuration struct | ||
/// @param logger the ACTS logging instance | ||
ParticleKillAction(const Config& cfg, | ||
std::unique_ptr<const Acts::Logger> logger = | ||
Acts::getDefaultLogger("ParticleKillAction", | ||
Acts::Logging::INFO)); | ||
~ParticleKillAction() override = default; | ||
|
||
/// @brief Called every step, conditionally sets the tracking state to `fStopAndKill` | ||
/// @param step is the Geant4 step of the particle | ||
void UserSteppingAction(const G4Step* step) override; | ||
|
||
private: | ||
const Acts::Logger& logger() const { return *m_logger; } | ||
|
||
Config m_cfg; | ||
std::unique_ptr<const Acts::Logger> m_logger; | ||
}; | ||
|
||
} // namespace ActsExamples |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// This file is part of the Acts project. | ||
// | ||
// Copyright (C) 2021 CERN for the benefit of the Acts project | ||
// | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
#include "ActsExamples/Geant4/ParticleKillAction.hpp" | ||
|
||
#include "Acts/Definitions/Units.hpp" | ||
#include "ActsExamples/Geant4/EventStoreRegistry.hpp" | ||
#include "ActsExamples/Geant4/SensitiveSurfaceMapper.hpp" | ||
|
||
#include <G4RunManager.hh> | ||
#include <G4Step.hh> | ||
#include <G4StepPoint.hh> | ||
#include <G4Track.hh> | ||
#include <G4UnitsTable.hh> | ||
#include <G4VPhysicalVolume.hh> | ||
|
||
ActsExamples::ParticleKillAction::ParticleKillAction( | ||
const Config& cfg, std::unique_ptr<const Acts::Logger> logger) | ||
: G4UserSteppingAction(), m_cfg(cfg), m_logger(std::move(logger)) {} | ||
|
||
void ActsExamples::ParticleKillAction::UserSteppingAction(const G4Step* step) { | ||
constexpr double convertLength = Acts::UnitConstants::mm / CLHEP::mm; | ||
G4Track* track = step->GetTrack(); | ||
|
||
const auto pos = convertLength * track->GetPosition(); | ||
|
||
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); | ||
track->SetTrackStatus(G4TrackStatus::fStopAndKill); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.