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

fix: Optimize EDM4hep particle reading #3978

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all 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
31 changes: 23 additions & 8 deletions Examples/Io/EDM4hep/src/EDM4hepReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "Acts/Plugins/DD4hep/DD4hepDetectorElement.hpp"
#include "Acts/Plugins/EDM4hep/EDM4hepUtil.hpp"
#include "ActsExamples/DD4hepDetector/DD4hepDetector.hpp"
#include "ActsExamples/EventData/GeometryContainers.hpp"
#include "ActsExamples/EventData/SimHit.hpp"
#include "ActsExamples/EventData/SimParticle.hpp"
#include "ActsExamples/Framework/WhiteBoard.hpp"
Expand Down Expand Up @@ -241,10 +242,10 @@ ProcessCode EDM4hepReader::read(const AlgorithmContext& ctx) {

ACTS_DEBUG("Found " << unorderedParticlesInitial.size() << " particles");

// @TODO: Order simhits by time

SimParticleContainer particlesGenerator;
SimParticleContainer particlesSimulated;
std::vector<SimParticle> particlesGeneratorUnordered;
particlesGeneratorUnordered.reserve(mcParticleCollection.size());
std::vector<SimParticle> particlesSimulatedUnordered;
particlesSimulatedUnordered.reserve(mcParticleCollection.size());

for (const auto& inParticle : mcParticleCollection) {
auto particleIt = edm4hepParticleMap.find(inParticle.getObjectID().index);
Expand All @@ -256,7 +257,7 @@ ProcessCode EDM4hepReader::read(const AlgorithmContext& ctx) {
const std::size_t index = particleIt->second;
const auto& particleInitial = unorderedParticlesInitial.at(index);
if (!inParticle.isCreatedInSimulation()) {
particlesGenerator.insert(particleInitial);
particlesGeneratorUnordered.push_back(particleInitial);
}
SimParticle particleSimulated = particleInitial;

Expand Down Expand Up @@ -286,23 +287,33 @@ ProcessCode EDM4hepReader::read(const AlgorithmContext& ctx) {
<< particleInitial.fourMomentum().transpose() << " -> "
<< particleSimulated.final().fourMomentum().transpose());

particlesSimulated.insert(particleSimulated);
particlesSimulatedUnordered.push_back(particleSimulated);
}

std::ranges::sort(particlesGeneratorUnordered, detail ::CompareParticleId{});
std::ranges::sort(particlesSimulatedUnordered, detail ::CompareParticleId{});
Comment on lines +293 to +294
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
std::ranges::sort(particlesGeneratorUnordered, detail ::CompareParticleId{});
std::ranges::sort(particlesSimulatedUnordered, detail ::CompareParticleId{});
std::ranges::sort(particlesGeneratorUnordered, detail::CompareParticleId{});
std::ranges::sort(particlesSimulatedUnordered, detail::CompareParticleId{});


SimParticleContainer particlesGenerator{particlesGeneratorUnordered.begin(),
particlesGeneratorUnordered.end()};
SimParticleContainer particlesSimulated{particlesSimulatedUnordered.begin(),
particlesSimulatedUnordered.end()};

if (!m_cfg.graphvizOutput.empty()) {
std::string path = perEventFilepath(m_cfg.graphvizOutput, "particles.dot",
ctx.eventNumber);
std::ofstream dot(path);
graphviz(dot, unorderedParticlesInitial, parentRelationship);
}

SimHitContainer simHits;
std::vector<SimHit> simHitsUnordered;

ACTS_DEBUG("Reading sim hits from " << m_cfg.inputSimHits.size()
<< " sim hit collections");
for (const auto& name : m_cfg.inputSimHits) {
const auto& inputHits = frame.get<edm4hep::SimTrackerHitCollection>(name);

simHitsUnordered.reserve(simHitsUnordered.size() + inputHits.size());

for (const auto& hit : inputHits) {
auto simHit = EDM4hepUtil::readSimHit(
hit,
Expand Down Expand Up @@ -359,10 +370,14 @@ ProcessCode EDM4hepReader::read(const AlgorithmContext& ctx) {
return surface->geometryId();
});

simHits.insert(std::move(simHit));
simHitsUnordered.push_back(std::move(simHit));
}
}

std::ranges::sort(simHitsUnordered, detail ::CompareGeometryId{});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
std::ranges::sort(simHitsUnordered, detail ::CompareGeometryId{});
std::ranges::sort(simHitsUnordered, detail::CompareGeometryId{});


SimHitContainer simHits{simHitsUnordered.begin(), simHitsUnordered.end()};

if (m_cfg.sortSimHitsInTime) {
ACTS_DEBUG("Sorting sim hits in time");
std::multimap<ActsFatras::Barcode, std::size_t> hitsByParticle;
Expand Down
Loading