Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-12 08:21:19

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 CERN for the benefit of the ACTS project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
0008 
0009 #include "ActsExamples/Io/EDM4hep/EDM4hepSimHitOutputConverter.hpp"
0010 
0011 #include "ActsExamples/EventData/SimParticle.hpp"
0012 #include "ActsExamples/Io/EDM4hep/EDM4hepUtil.hpp"
0013 
0014 #include <stdexcept>
0015 
0016 #include <edm4hep/MCParticle.h>
0017 #include <edm4hep/MCParticleCollection.h>
0018 #include <edm4hep/SimTrackerHit.h>
0019 #include <edm4hep/SimTrackerHitCollection.h>
0020 #include <podio/Frame.h>
0021 
0022 namespace ActsExamples {
0023 
0024 EDM4hepSimHitOutputConverter::EDM4hepSimHitOutputConverter(
0025     const EDM4hepSimHitOutputConverter::Config& config,
0026     std::unique_ptr<const Acts::Logger> logger)
0027     : PodioOutputConverter("EDM4hepSimHitOutputConverter", std::move(logger)),
0028       m_cfg(config) {
0029   if (m_cfg.inputSimHits.empty()) {
0030     throw std::invalid_argument("Missing simulated hits input collection");
0031   }
0032 
0033   if (m_cfg.outputSimTrackerHits.empty()) {
0034     throw std::invalid_argument("Missing output sim tracker hit name");
0035   }
0036 
0037   if (m_cfg.outputParticles.empty() != m_cfg.inputParticles.empty()) {
0038     throw std::invalid_argument(
0039         "Output particles and input particles must both be set or not set");
0040   }
0041 
0042   m_inputParticles.maybeInitialize(m_cfg.inputParticles);
0043   m_outputParticles.maybeInitialize(m_cfg.outputParticles);
0044   m_inputSimHits.initialize(m_cfg.inputSimHits);
0045   m_outputSimTrackerHits.initialize(m_cfg.outputSimTrackerHits);
0046 }
0047 
0048 ProcessCode EDM4hepSimHitOutputConverter::execute(
0049     const AlgorithmContext& ctx) const {
0050   EDM4hepUtil::MapParticleIdTo particleMapper;
0051   std::unordered_map<SimBarcode, edm4hep::MutableMCParticle> particleMap;
0052 
0053   edm4hep::SimTrackerHitCollection simTrackerHitCollection;
0054 
0055   if (!m_cfg.inputParticles.empty()) {
0056     edm4hep::MCParticleCollection mcParticles;
0057     auto particles = m_inputParticles(ctx);
0058 
0059     for (const auto& particle : particles) {
0060       auto p = mcParticles.create();
0061       particleMap[particle.particleId()] = p;
0062       EDM4hepUtil::writeParticle(particle, p);
0063     }
0064 
0065     particleMapper = [&](SimBarcode particleId) {
0066       auto it = particleMap.find(particleId);
0067       if (it == particleMap.end()) {
0068         throw std::runtime_error("Particle not found in map");
0069       }
0070       return it->second;
0071     };
0072     m_outputParticles(ctx, std::move(mcParticles));
0073   }
0074 
0075   const auto& simHits = m_inputSimHits(ctx);
0076 
0077   for (const auto& simHit : simHits) {
0078     auto simTrackerHit = simTrackerHitCollection.create();
0079     EDM4hepUtil::writeSimHit(
0080         simHit, simTrackerHit, particleMapper,
0081         [](Acts::GeometryIdentifier id) { return id.value(); });
0082   }
0083 
0084   m_outputSimTrackerHits(ctx, std::move(simTrackerHitCollection));
0085 
0086   return ProcessCode::SUCCESS;
0087 }
0088 
0089 std::vector<std::string> EDM4hepSimHitOutputConverter::collections() const {
0090   std::vector<std::string> result{m_cfg.outputSimTrackerHits};
0091   if (!m_cfg.outputParticles.empty()) {
0092     result.push_back(m_cfg.outputParticles);
0093   }
0094   return result;
0095 }
0096 
0097 }  // namespace ActsExamples