Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-05-15 07:57:13

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     Acts::Logging::Level level)
0027     : EDM4hepOutputConverter("EDM4hepSimHitOutputConverter", level),
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<ActsFatras::Barcode, edm4hep::MutableMCParticle>
0052       particleMap;
0053 
0054   edm4hep::SimTrackerHitCollection simTrackerHitCollection;
0055 
0056   if (!m_cfg.inputParticles.empty()) {
0057     edm4hep::MCParticleCollection mcParticles;
0058     auto particles = m_inputParticles(ctx);
0059 
0060     for (const auto& particle : particles) {
0061       auto p = mcParticles->create();
0062       particleMap[particle.particleId()] = p;
0063       EDM4hepUtil::writeParticle(particle, p);
0064     }
0065 
0066     particleMapper = [&](ActsFatras::Barcode particleId) {
0067       auto it = particleMap.find(particleId);
0068       if (it == particleMap.end()) {
0069         throw std::runtime_error("Particle not found in map");
0070       }
0071       return it->second;
0072     };
0073     m_outputParticles(ctx, std::move(mcParticles));
0074   }
0075 
0076   const auto& simHits = m_inputSimHits(ctx);
0077 
0078   for (const auto& simHit : simHits) {
0079     auto simTrackerHit = simTrackerHitCollection->create();
0080     EDM4hepUtil::writeSimHit(
0081         simHit, simTrackerHit, particleMapper,
0082         [](Acts::GeometryIdentifier id) { return id.value(); });
0083   }
0084 
0085   m_outputSimTrackerHits(ctx, std::move(simTrackerHitCollection));
0086 
0087   return ProcessCode::SUCCESS;
0088 }
0089 
0090 std::vector<std::string> EDM4hepSimHitOutputConverter::collections() const {
0091   std::vector<std::string> result{m_cfg.outputSimTrackerHits};
0092   if (!m_cfg.outputParticles.empty()) {
0093     result.push_back(m_cfg.outputParticles);
0094   }
0095   return result;
0096 }
0097 
0098 }  // namespace ActsExamples