Skip to content

Commit

Permalink
fix: Fix Geant4 initial particle translation (#2492)
Browse files Browse the repository at this point in the history
Observed problem: Particles put into G4 will not have the same properties after `BeanOn` is called. We relied on this in order to translate them back into Acts.

The following image shows how pt=1GeV looked like in `particles_initial` after a G4 forth and back.

![image](https://github.com/acts-project/acts/assets/487211/41c83669-0f3d-45ab-8b24-bcfe5d639680)

Solution: Put particles into the initial collection on translation and adopt generic translation code so it will accept already translated particles.
  • Loading branch information
andiwand authored Oct 3, 2023
1 parent ff17e5b commit 7719ac6
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 13 deletions.
23 changes: 19 additions & 4 deletions Examples/Algorithms/Geant4/src/Geant4Simulation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,6 @@ ActsExamples::Geant4Simulation::Geant4Simulation(const Config& cfg,
runManager().SetUserAction(primaryGeneratorAction);
}

// Please note:
// The following two blocks rely on the fact that the Acts
// detector constructions cache the world volume

// Particle action
{
// Clear tracking action if it exists
Expand Down Expand Up @@ -251,6 +247,10 @@ ActsExamples::Geant4Simulation::Geant4Simulation(const Config& cfg,
// Get the g4World cache
G4VPhysicalVolume* g4World = m_detectorConstruction->Construct();

// Please note:
// The following two blocks rely on the fact that the Acts
// detector constructions cache the world volume

// Set the magnetic field
if (cfg.magneticField) {
ACTS_INFO("Setting ACTS configured field to Geant4.");
Expand Down Expand Up @@ -360,6 +360,21 @@ ActsExamples::Geant4MaterialRecording::Geant4MaterialRecording(
runManager().SetUserAction(primaryGeneratorAction);
}

// Particle action
{
// Clear tracking action if it exists
if (runManager().GetUserTrackingAction() != nullptr) {
delete runManager().GetUserTrackingAction();
}
ParticleTrackingAction::Config trackingCfg;
trackingCfg.eventStore = m_eventStore;
trackingCfg.keepParticlesWithoutHits = true;
// G4RunManager will take care of deletion
auto trackingAction = new ParticleTrackingAction(
trackingCfg, m_logger->cloneWithSuffix("ParticleTracking"));
runManager().SetUserAction(trackingAction);
}

// Stepping action
{
// Clear stepping action if it exists
Expand Down
19 changes: 11 additions & 8 deletions Examples/Algorithms/Geant4/src/ParticleTrackingAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,25 @@ void ActsExamples::ParticleTrackingAction::PreUserTrackingAction(
ACTS_WARNING("Hit buffer not empty after track");
}

auto particleId = makeParticleId(aTrack->GetTrackID(), aTrack->GetParentID());
auto barcode = makeParticleId(aTrack->GetTrackID(), aTrack->GetParentID());

// There is already a warning printed in the makeParticleId function
if (not particleId) {
// There is already a warning printed in the makeParticleId function if this
// indicates a failure
if (not barcode) {
return;
}

auto [it, success] =
eventStore().particlesInitial.insert(convert(*aTrack, *particleId));
auto particle = convert(*aTrack, *barcode);
auto [it, success] = eventStore().particlesInitial.insert(particle);

// Only register particle at the initial state AND if there is no particle ID
// collision
if (success) {
eventStore().trackIdMapping[aTrack->GetTrackID()] = *particleId;
eventStore().trackIdMapping[aTrack->GetTrackID()] = particle.particleId();
} else {
eventStore().particleIdCollisionsInitial++;
ACTS_WARNING("Particle ID collision with "
<< *particleId
<< particle.particleId()
<< " detected for initial particles. Skip particle");
}
}
Expand All @@ -72,6 +73,8 @@ void ActsExamples::ParticleTrackingAction::PostUserTrackingAction(
// collision
if (eventStore().trackIdMapping.find(aTrack->GetTrackID()) ==
eventStore().trackIdMapping.end()) {
ACTS_WARNING("Particle ID for track ID " << aTrack->GetTrackID()
<< " not registered. Skip");
return;
}

Expand Down Expand Up @@ -132,7 +135,7 @@ ActsExamples::ParticleTrackingAction::makeParticleId(G4int trackId,
// or we are making a final particle state)
if (eventStore().trackIdMapping.find(trackId) !=
eventStore().trackIdMapping.end()) {
return eventStore().trackIdMapping.at(trackId);
return std::nullopt;
}

if (eventStore().trackIdMapping.find(parentId) ==
Expand Down
1 change: 1 addition & 0 deletions Examples/Algorithms/Geant4/src/SimParticleTranslation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ void ActsExamples::SimParticleTranslation::GeneratePrimaries(G4Event* anEvent) {
// Add the primary to the vertex
pVertex->SetPrimary(particle);

eventStore().particlesInitial.insert(part);
eventStore().trackIdMapping[particle->GetTrackID()] = part.particleId();

++pCounter;
Expand Down
2 changes: 1 addition & 1 deletion Examples/Python/tests/root_file_hashes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ test_fatras__particles_final.root: d3a900a7151623109f2e22e11beb477635eb549d5e611
test_fatras__particles_initial.root: 0e2d9974fdd4aa5549c5c05817ec54fe6c7d9cbe7969647f227956167e31110d
test_fatras__hits.root: d3ec06cd12e3737b5c783302ca29a532c885dc35930901d86b2f6d17041037c6
test_geant4__particles_final.root: abf91622e96c0723651e864e050c7fdeee84b230aacb1c261ad81ff4e5df5f2c
test_geant4__particles_initial.root: 5be86aa4a2fc369093746451f37085e2c93f80f079b66023f5fe4b67e20100f6
test_geant4__particles_initial.root: cfe3d50a9c38b45eefc2e0c0948d5b0899ced6332dd183e1d19cb0b766f76363
test_geant4__hits.root: 70b592a546fd362c9341d87c9068b1f8fed657e0036e62e7262c01fa5eb2d469
test_seeding__estimatedparams.root: f11b6bdc2b9b13e84f974f2dfbd9f409255f4f0eab212fe8581c352b3f2cfc27
test_seeding__performance_seeding.root: 992f9c611d30dde0d3f3ab676bab19ada61ab6a4442828e27b65ec5e5b7a2880
Expand Down

0 comments on commit 7719ac6

Please sign in to comment.