Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Fuse initial and final sim particles in Examples #3804

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 19 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CI/physmon/config/pythia8_ttbar.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,8 @@ exclude:
- particle
- generation
- sub_particle
- e_loss
- total_x0
- total_l0
- number_of_hits
- outcome
Binary file modified CI/physmon/reference/simulation/particles_fatras_hist.root
Binary file not shown.
Binary file modified CI/physmon/reference/simulation/particles_geant4_hist.root
Binary file not shown.
6 changes: 2 additions & 4 deletions CI/physmon/workflows/physmon_simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@
preSelectParticles=None,
postSelectParticles=ParticleSelectorConfig(removeSecondaries=True),
inputParticles="particles_input",
outputParticlesInitial="particles_initial_fatras",
outputParticlesFinal="particles_final_fatras",
outputParticles="particles_fatras",
outputSimHits="simhits_fatras",
outputDirRoot=tp / "fatras",
)
Expand All @@ -99,8 +98,7 @@
killAfterTime=25 * u.ns,
killSecondaries=True,
inputParticles="particles_input",
outputParticlesInitial="particles_initial_geant4",
outputParticlesFinal="particles_final_geant4",
outputParticles="particles_geant4",
outputSimHits="simhits_geant4",
outputDirRoot=tp / "geant4",
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,8 @@ class FatrasSimulation final : public IAlgorithm {
struct Config {
/// The particles input collection.
std::string inputParticles;
/// The simulated particles initial state collection.
std::string outputParticlesInitial;
/// The simulated particles final state collection.
std::string outputParticlesFinal;
/// The simulated particles collection.
std::string outputParticles;
andiwand marked this conversation as resolved.
Show resolved Hide resolved
/// The simulated hits output collection.
std::string outputSimHits;
/// Parametrisation of nuclear interaction
Expand Down Expand Up @@ -110,10 +108,8 @@ class FatrasSimulation final : public IAlgorithm {

WriteDataHandle<SimHitContainer> m_outputSimHits{this, "OutputSimHits"};

WriteDataHandle<SimParticleContainer> m_outputParticlesInitial{
this, "OutputParticlesInitial"};
WriteDataHandle<SimParticleContainer> m_outputParticlesFinal{
this, "OutputParticlesFinal"};
WriteDataHandle<SimParticleContainer> m_outputParticles{this,
"OutputParticles"};

private:
Config m_cfg;
Expand Down
77 changes: 47 additions & 30 deletions Examples/Algorithms/Fatras/src/FatrasSimulation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

#include "ActsExamples/Fatras/FatrasSimulation.hpp"

#include "Acts/Definitions/Direction.hpp"
#include "Acts/EventData/TrackParameters.hpp"
#include "Acts/Geometry/GeometryContext.hpp"
#include "Acts/MagneticField/MagneticFieldContext.hpp"
#include "Acts/Propagator/Navigator.hpp"
Expand All @@ -24,20 +22,17 @@
#include "ActsExamples/Framework/AlgorithmContext.hpp"
#include "ActsExamples/Framework/IAlgorithm.hpp"
#include "ActsExamples/Framework/RandomNumbers.hpp"
#include "ActsFatras/EventData/Barcode.hpp"
#include "ActsFatras/EventData/Hit.hpp"
#include "ActsFatras/EventData/Particle.hpp"
#include "ActsFatras/Kernel/InteractionList.hpp"
#include "ActsFatras/Kernel/Simulation.hpp"
#include "ActsFatras/Physics/Decay/NoDecay.hpp"
#include "ActsFatras/Physics/ElectroMagnetic/PhotonConversion.hpp"
#include "ActsFatras/Physics/NuclearInteraction/NuclearInteractionParameters.hpp"
#include "ActsFatras/Physics/StandardInteractions.hpp"
#include "ActsFatras/Selectors/SelectorHelpers.hpp"
#include "ActsFatras/Selectors/SurfaceSelectors.hpp"

#include <algorithm>
#include <array>
#include <map>
#include <ostream>
#include <stdexcept>
#include <system_error>
Expand Down Expand Up @@ -73,10 +68,9 @@ struct ActsExamples::detail::FatrasSimulation {
virtual ~FatrasSimulation() = default;
virtual Acts::Result<std::vector<ActsFatras::FailedParticle>> simulate(
const Acts::GeometryContext &, const Acts::MagneticFieldContext &,
ActsExamples::RandomEngine &, const ActsExamples::SimParticleContainer &,
ActsExamples::SimParticleContainer::sequence_type &,
ActsExamples::SimParticleContainer::sequence_type &,
ActsExamples::SimHitContainer::sequence_type &) const = 0;
ActsExamples::RandomEngine &, const std::vector<ActsFatras::Particle> &,
std::vector<ActsFatras::Particle> &, std::vector<ActsFatras::Particle> &,
std::vector<ActsFatras::Hit> &) const = 0;
};

namespace {
Expand Down Expand Up @@ -172,12 +166,10 @@ struct FatrasSimulationT final : ActsExamples::detail::FatrasSimulation {
Acts::Result<std::vector<ActsFatras::FailedParticle>> simulate(
const Acts::GeometryContext &geoCtx,
const Acts::MagneticFieldContext &magCtx, ActsExamples::RandomEngine &rng,
const ActsExamples::SimParticleContainer &inputParticles,
ActsExamples::SimParticleContainer::sequence_type
&simulatedParticlesInitial,
ActsExamples::SimParticleContainer::sequence_type
&simulatedParticlesFinal,
ActsExamples::SimHitContainer::sequence_type &simHits) const final {
const std::vector<ActsFatras::Particle> &inputParticles,
std::vector<ActsFatras::Particle> &simulatedParticlesInitial,
std::vector<ActsFatras::Particle> &simulatedParticlesFinal,
std::vector<ActsFatras::Hit> &simHits) const final {
return simulation.simulate(geoCtx, magCtx, rng, inputParticles,
simulatedParticlesInitial,
simulatedParticlesFinal, simHits);
Expand Down Expand Up @@ -212,8 +204,7 @@ ActsExamples::FatrasSimulation::FatrasSimulation(Config cfg,
m_sim = std::make_unique<FatrasSimulationT>(m_cfg, lvl);

m_inputParticles.initialize(m_cfg.inputParticles);
m_outputParticlesInitial.initialize(m_cfg.outputParticlesInitial);
m_outputParticlesFinal.initialize(m_cfg.outputParticlesFinal);
m_outputParticles.initialize(m_cfg.outputParticles);
m_outputSimHits.initialize(m_cfg.outputSimHits);
}

Expand All @@ -227,10 +218,17 @@ ActsExamples::ProcessCode ActsExamples::FatrasSimulation::execute(

ACTS_DEBUG(inputParticles.size() << " input particles");

// prepare input container
std::vector<ActsFatras::Particle> particlesInput;
particlesInput.reserve(inputParticles.size());
for (const auto &p : inputParticles) {
particlesInput.push_back(p.initial());
}

// prepare output containers
SimParticleContainer::sequence_type particlesInitialUnordered;
SimParticleContainer::sequence_type particlesFinalUnordered;
SimHitContainer::sequence_type simHitsUnordered;
std::vector<ActsFatras::Particle> particlesInitialUnordered;
std::vector<ActsFatras::Particle> particlesFinalUnordered;
std::vector<ActsFatras::Hit> simHitsUnordered;
// reserve appropriate resources
particlesInitialUnordered.reserve(inputParticles.size());
particlesFinalUnordered.reserve(inputParticles.size());
Expand All @@ -240,7 +238,7 @@ ActsExamples::ProcessCode ActsExamples::FatrasSimulation::execute(
// run the simulation w/ a local random generator
auto rng = m_cfg.randomNumbers->spawnGenerator(ctx);
auto ret = m_sim->simulate(ctx.geoContext, ctx.magFieldContext, rng,
inputParticles, particlesInitialUnordered,
particlesInput, particlesInitialUnordered,
particlesFinalUnordered, simHitsUnordered);
// fatal error leads to panic
if (!ret.ok()) {
Expand All @@ -263,19 +261,23 @@ ActsExamples::ProcessCode ActsExamples::FatrasSimulation::execute(
<< " simulated particles (final state)");
ACTS_DEBUG(simHitsUnordered.size() << " simulated hits");

if (particlesInitialUnordered.size() != particlesFinalUnordered.size()) {
ACTS_ERROR("number of initial and final state particles differ");
}
andiwand marked this conversation as resolved.
Show resolved Hide resolved

// order output containers
#if BOOST_VERSION >= 107800
SimParticleContainer particlesInitial(particlesInitialUnordered.begin(),
particlesInitialUnordered.end());
SimParticleContainer particlesFinal(particlesFinalUnordered.begin(),
particlesFinalUnordered.end());
SimParticleStateContainer particlesInitial(particlesInitialUnordered.begin(),
particlesInitialUnordered.end());
SimParticleStateContainer particlesFinal(particlesFinalUnordered.begin(),
particlesFinalUnordered.end());
SimHitContainer simHits(simHitsUnordered.begin(), simHitsUnordered.end());
#else
// working around a nasty boost bug
// https://github.com/boostorg/container/issues/244

SimParticleContainer particlesInitial;
SimParticleContainer particlesFinal;
SimParticleStateContainer particlesInitial;
SimParticleStateContainer particlesFinal;
andiwand marked this conversation as resolved.
Show resolved Hide resolved
SimHitContainer simHits;

particlesInitial.reserve(particlesInitialUnordered.size());
Expand All @@ -293,9 +295,24 @@ ActsExamples::ProcessCode ActsExamples::FatrasSimulation::execute(
}
#endif

SimParticleContainer particlesSimulated;
particlesSimulated.reserve(particlesInitial.size());
for (const auto &particleInitial : particlesInitial) {
SimParticle particleSimulated(particleInitial, particleInitial);

if (auto it = particlesFinal.find(particleInitial.particleId());
it != particlesFinal.end()) {
particleSimulated.final() = *it;
} else {
ACTS_ERROR("particle " << particleInitial.particleId()
<< " has no final state");
}

particlesSimulated.insert(particleSimulated);
}
andiwand marked this conversation as resolved.
Show resolved Hide resolved

// store ordered output containers
m_outputParticlesInitial(ctx, std::move(particlesInitial));
m_outputParticlesFinal(ctx, std::move(particlesFinal));
m_outputParticles(ctx, std::move(particlesSimulated));
m_outputSimHits(ctx, std::move(simHits));

return ActsExamples::ProcessCode::SUCCESS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ struct EventStore {
/// Use a std::set here because it allows for fast insertion and ensures
/// uniqueness. Thus particle collisions are detected early.
using ParticleContainer =
std::set<ActsFatras::Particle, ActsExamples::detail::CompareParticleId>;
std::set<SimParticle, ActsExamples::detail::CompareParticleId>;

/// Initial and final particle collections
/// Initial particle collection
ParticleContainer particlesInitial;
ParticleContainer particlesFinal;

/// Simulated particle collection
ParticleContainer particlesSimulated;
andiwand marked this conversation as resolved.
Show resolved Hide resolved

/// The hits in sensitive detectors
SimHitContainer::sequence_type hits;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,8 @@ class Geant4Simulation final : public Geant4SimulationBase {
/// Name of the output collection : hits
std::string outputSimHits = "simhits";

/// Name of the output collection : initial particles
std::string outputParticlesInitial = "particles_initial";

/// Name of the output collection : final particles
std::string outputParticlesFinal = "particles_final";
/// Name of the output collection : simulated particles
std::string outputParticles = "particles_simulated";
andiwand marked this conversation as resolved.
Show resolved Hide resolved

/// The ACTS sensitive surfaces in a mapper, used for hit creation
std::shared_ptr<const SensitiveSurfaceMapper> sensitiveSurfaceMapper =
Expand Down Expand Up @@ -170,10 +167,8 @@ class Geant4Simulation final : public Geant4SimulationBase {
std::unique_ptr<G4MagneticField> m_magneticField;
std::unique_ptr<G4FieldManager> m_fieldManager;

WriteDataHandle<SimParticleContainer> m_outputParticlesInitial{
this, "OutputParticlesInitial"};
WriteDataHandle<SimParticleContainer> m_outputParticlesFinal{
this, "OutputParticlesFinal"};
WriteDataHandle<SimParticleContainer> m_outputParticles{this,
"OutputParticles"};
WriteDataHandle<SimHitContainer> m_outputSimHits{this, "OutputSimHIts"};
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,12 @@ class ParticleTrackingAction : public G4UserTrackingAction {
Config m_cfg;

private:
/// Convert a G4Track to a SimParticle
/// Convert a G4Track to a SimParticleState
///
/// @param aTrack the current Geant4 track
/// @param particleId the particle ID the particle will have
SimParticle convert(const G4Track& aTrack, SimBarcode particleId) const;
/// @return SimParticleState the converted particle state
SimParticleState convert(const G4Track& aTrack, SimBarcode particleId) const;
andiwand marked this conversation as resolved.
Show resolved Hide resolved
andiwand marked this conversation as resolved.
Show resolved Hide resolved

/// Make the particle id
std::optional<SimBarcode> makeParticleId(G4int trackId, G4int parentId) const;
Expand Down
25 changes: 4 additions & 21 deletions Examples/Algorithms/Geant4/src/Geant4Simulation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include "Acts/Definitions/Algebra.hpp"
#include "Acts/Plugins/FpeMonitoring/FpeMonitor.hpp"
#include "Acts/Utilities/Logger.hpp"
#include "Acts/Utilities/MultiIndex.hpp"
#include "ActsExamples/Framework/AlgorithmContext.hpp"
#include "ActsExamples/Framework/IAlgorithm.hpp"
#include "ActsExamples/Framework/RandomNumbers.hpp"
Expand All @@ -28,12 +27,8 @@
#include "ActsExamples/Geant4/SensitiveSurfaceMapper.hpp"
#include "ActsExamples/Geant4/SimParticleTranslation.hpp"
#include "ActsExamples/Geant4/SteppingActionList.hpp"
#include "ActsFatras/EventData/Barcode.hpp"

#include <cassert>
#include <cstddef>
#include <iostream>
#include <map>
#include <stdexcept>
#include <utility>

Expand Down Expand Up @@ -135,14 +130,6 @@ ActsExamples::ProcessCode ActsExamples::Geant4SimulationBase::execute(
runManager().BeamOn(1);
}

// Since these are std::set, this ensures that each particle is in both sets
throw_assert(
eventStore().particlesInitial.size() ==
eventStore().particlesFinal.size(),
"initial and final particle collections does not have the same size: "
<< eventStore().particlesInitial.size() << " vs "
<< eventStore().particlesFinal.size());

// Print out warnings about possible particle collision if happened
if (eventStore().particleIdCollisionsInitial > 0 ||
eventStore().particleIdCollisionsFinal > 0 ||
Expand Down Expand Up @@ -298,8 +285,7 @@ ActsExamples::Geant4Simulation::Geant4Simulation(const Config& cfg,

m_inputParticles.initialize(cfg.inputParticles);
m_outputSimHits.initialize(cfg.outputSimHits);
m_outputParticlesInitial.initialize(cfg.outputParticlesInitial);
m_outputParticlesFinal.initialize(cfg.outputParticlesFinal);
m_outputParticles.initialize(cfg.outputParticles);
}

ActsExamples::Geant4Simulation::~Geant4Simulation() = default;
Expand All @@ -312,12 +298,9 @@ ActsExamples::ProcessCode ActsExamples::Geant4Simulation::execute(
}

// Output handling: Simulation
m_outputParticlesInitial(
ctx, SimParticleContainer(eventStore().particlesInitial.begin(),
eventStore().particlesInitial.end()));
m_outputParticlesFinal(
ctx, SimParticleContainer(eventStore().particlesFinal.begin(),
eventStore().particlesFinal.end()));
m_outputParticles(
ctx, SimParticleContainer(eventStore().particlesSimulated.begin(),
eventStore().particlesSimulated.end()));
andiwand marked this conversation as resolved.
Show resolved Hide resolved

#if BOOST_VERSION < 107800
SimHitContainer container;
Expand Down
Loading
Loading