From 12806bd94f30695da351d90cc569f5b5ca3863a9 Mon Sep 17 00:00:00 2001 From: Paul Gessinger Date: Wed, 11 Dec 2024 16:23:42 +0100 Subject: [PATCH 1/2] fix: Optimize EDM4hep particle reading --- Examples/Io/EDM4hep/src/EDM4hepReader.cpp | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/Examples/Io/EDM4hep/src/EDM4hepReader.cpp b/Examples/Io/EDM4hep/src/EDM4hepReader.cpp index 0be54783e8e..957593ceb32 100644 --- a/Examples/Io/EDM4hep/src/EDM4hepReader.cpp +++ b/Examples/Io/EDM4hep/src/EDM4hepReader.cpp @@ -241,10 +241,10 @@ ProcessCode EDM4hepReader::read(const AlgorithmContext& ctx) { ACTS_DEBUG("Found " << unorderedParticlesInitial.size() << " particles"); - // @TODO: Order simhits by time - - SimParticleContainer particlesGenerator; - SimParticleContainer particlesSimulated; + std::vector particlesGeneratorUnordered; + particlesGeneratorUnordered.reserve(mcParticleCollection.size()); + std::vector particlesSimulatedUnordered; + particlesSimulatedUnordered.reserve(mcParticleCollection.size()); for (const auto& inParticle : mcParticleCollection) { auto particleIt = edm4hepParticleMap.find(inParticle.getObjectID().index); @@ -256,7 +256,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; @@ -286,9 +286,17 @@ 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{}); + + 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); From 2b9db42fb5ab98818ecd415bf2815ae13ff65512 Mon Sep 17 00:00:00 2001 From: Paul Gessinger Date: Wed, 11 Dec 2024 16:36:08 +0100 Subject: [PATCH 2/2] also insert simhits in one go --- Examples/Io/EDM4hep/src/EDM4hepReader.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/Examples/Io/EDM4hep/src/EDM4hepReader.cpp b/Examples/Io/EDM4hep/src/EDM4hepReader.cpp index 957593ceb32..1562105d338 100644 --- a/Examples/Io/EDM4hep/src/EDM4hepReader.cpp +++ b/Examples/Io/EDM4hep/src/EDM4hepReader.cpp @@ -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" @@ -304,13 +305,15 @@ ProcessCode EDM4hepReader::read(const AlgorithmContext& ctx) { graphviz(dot, unorderedParticlesInitial, parentRelationship); } - SimHitContainer simHits; + std::vector 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(name); + simHitsUnordered.reserve(simHitsUnordered.size() + inputHits.size()); + for (const auto& hit : inputHits) { auto simHit = EDM4hepUtil::readSimHit( hit, @@ -367,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{}); + + SimHitContainer simHits{simHitsUnordered.begin(), simHitsUnordered.end()}; + if (m_cfg.sortSimHitsInTime) { ACTS_DEBUG("Sorting sim hits in time"); std::multimap hitsByParticle;