Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:11:52

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/EDM4hepSimHitWriter.hpp"
0010 
0011 #include "Acts/Definitions/Units.hpp"
0012 #include "ActsExamples/EventData/SimHit.hpp"
0013 #include "ActsExamples/EventData/SimParticle.hpp"
0014 #include "ActsExamples/Framework/WhiteBoard.hpp"
0015 #include "ActsExamples/Io/EDM4hep/EDM4hepUtil.hpp"
0016 
0017 #include <stdexcept>
0018 
0019 #include <edm4hep/MCParticle.h>
0020 #include <edm4hep/SimTrackerHit.h>
0021 #include <podio/Frame.h>
0022 
0023 namespace ActsExamples {
0024 
0025 EDM4hepSimHitWriter::EDM4hepSimHitWriter(
0026     const EDM4hepSimHitWriter::Config& config, Acts::Logging::Level level)
0027     : WriterT(config.inputSimHits, "EDM4hepSimHitWriter", level),
0028       m_cfg(config),
0029       m_writer(config.outputPath) {
0030   ACTS_VERBOSE("Created output file " << config.outputPath);
0031 
0032   if (m_cfg.inputSimHits.empty()) {
0033     throw std::invalid_argument("Missing simulated hits input collection");
0034   }
0035 
0036   if (m_cfg.outputParticles.empty()) {
0037     throw std::invalid_argument("Missing output particle name");
0038   }
0039 
0040   if (m_cfg.outputSimTrackerHits.empty()) {
0041     throw std::invalid_argument("Missing output sim tracker hit name");
0042   }
0043 
0044   m_inputParticles.maybeInitialize(m_cfg.inputParticles);
0045 }
0046 
0047 ActsExamples::ProcessCode EDM4hepSimHitWriter::finalize() {
0048   m_writer.finish();
0049 
0050   return ProcessCode::SUCCESS;
0051 }
0052 
0053 ProcessCode EDM4hepSimHitWriter::writeT(const AlgorithmContext& ctx,
0054                                         const SimHitContainer& simHits) {
0055   podio::Frame frame;
0056 
0057   EDM4hepUtil::MapParticleIdTo particleMapper;
0058   std::unordered_map<ActsFatras::Barcode, edm4hep::MutableMCParticle>
0059       particleMap;
0060 
0061   edm4hep::MCParticleCollection mcParticles;
0062   edm4hep::SimTrackerHitCollection simTrackerHitCollection;
0063 
0064   if (!m_cfg.inputParticles.empty()) {
0065     auto particles = m_inputParticles(ctx);
0066 
0067     for (const auto& particle : particles) {
0068       auto p = mcParticles->create();
0069       particleMap[particle.particleId()] = p;
0070       EDM4hepUtil::writeParticle(particle, p);
0071     }
0072 
0073     particleMapper = [&](ActsFatras::Barcode particleId) {
0074       auto it = particleMap.find(particleId);
0075       if (it == particleMap.end()) {
0076         throw std::runtime_error("Particle not found in map");
0077       }
0078       return it->second;
0079     };
0080   }
0081 
0082   for (const auto& simHit : simHits) {
0083     auto simTrackerHit = simTrackerHitCollection->create();
0084     EDM4hepUtil::writeSimHit(
0085         simHit, simTrackerHit, particleMapper,
0086         [](Acts::GeometryIdentifier id) { return id.value(); });
0087   }
0088 
0089   frame.put(std::move(mcParticles), m_cfg.outputParticles);
0090   frame.put(std::move(simTrackerHitCollection), m_cfg.outputSimTrackerHits);
0091 
0092   std::lock_guard lock{m_writeMutex};
0093   m_writer.writeFrame(frame, "events");
0094 
0095   return ProcessCode::SUCCESS;
0096 }
0097 
0098 }  // namespace ActsExamples