Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-29 08:22:33

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/EDM4hepMeasurementOutputConverter.hpp"
0010 
0011 #include "ActsExamples/EventData/Measurement.hpp"
0012 #include "ActsExamples/Io/EDM4hep/EDM4hepUtil.hpp"
0013 #include "ActsPlugins/EDM4hep/EDM4hepUtil.hpp"
0014 #include <ActsPodioEdm/TrackerHitLocalCollection.h>
0015 #include <ActsPodioEdm/TrackerHitLocalSimTrackerHitLinkCollection.h>
0016 
0017 #include <stdexcept>
0018 
0019 namespace ActsExamples {
0020 
0021 EDM4hepMeasurementOutputConverter::EDM4hepMeasurementOutputConverter(
0022     const EDM4hepMeasurementOutputConverter::Config& config,
0023     std::unique_ptr<const Acts::Logger> logger)
0024     : PodioOutputConverter("EDM4hepMeasurementOutputConverter",
0025                            std::move(logger)),
0026       m_cfg(config) {
0027   if (m_cfg.trackingGeometry == nullptr) {
0028     throw std::runtime_error(
0029         "EDM4hepMeasurementOutputConverter: trackingGeometry is null");
0030   }
0031 
0032   const bool hasAssoc = m_cfg.inputSimHitAssociation.has_value();
0033   const bool hasMap = m_cfg.inputMeasurementSimHitsMap.has_value();
0034   const bool hasLinks = m_cfg.outputSimHitLinks.has_value();
0035   if (hasAssoc != hasMap || hasMap != hasLinks) {
0036     throw std::invalid_argument(
0037         "EDM4hepMeasurementOutputConverter: inputSimHitAssociation, "
0038         "inputMeasurementSimHitsMap, and outputSimHitLinks must all be set or "
0039         "all be unset");
0040   }
0041 
0042   m_inputMeasurements.initialize(m_cfg.inputMeasurements);
0043   m_outputTrackerHitsLocal.initialize(m_cfg.outputTrackerHitsLocal);
0044   m_inputSimHitAssociation.maybeInitialize(m_cfg.inputSimHitAssociation);
0045   m_inputMeasurementSimHitsMap.maybeInitialize(
0046       m_cfg.inputMeasurementSimHitsMap);
0047   m_outputSimHitLinks.maybeInitialize(m_cfg.outputSimHitLinks);
0048 }
0049 
0050 ProcessCode EDM4hepMeasurementOutputConverter::execute(
0051     const AlgorithmContext& ctx) const {
0052   const auto& measurements = m_inputMeasurements(ctx);
0053 
0054   ACTS_VERBOSE("Writing " << measurements.size()
0055                           << " measurements in this event.");
0056 
0057   auto hits = std::make_unique<ActsPodioEdm::TrackerHitLocalCollection>();
0058 
0059   for (Index hitIdx = 0u; hitIdx < measurements.size(); ++hitIdx) {
0060     ConstVariableBoundMeasurementProxy from =
0061         measurements.getMeasurement(hitIdx);
0062 
0063     const Acts::Surface* surface =
0064         m_cfg.trackingGeometry->findSurface(from.geometryId());
0065     if (surface == nullptr) {
0066       throw std::runtime_error(
0067           "EDM4hepMeasurementOutputConverter: surface not found for geometry "
0068           "id " +
0069           std::to_string(from.geometryId().value()));
0070     }
0071 
0072     auto to = hits->create();
0073     EDM4hepUtil::writeMeasurement(ctx.recoGeoContext, from, to, *surface);
0074   }
0075 
0076   if (m_outputSimHitLinks.isInitialized()) {
0077     const auto& simHitAssociation = m_inputSimHitAssociation(ctx);
0078     const auto& measToSimHits = m_inputMeasurementSimHitsMap(ctx);
0079 
0080     ActsPlugins::EDM4hepUtil::SimHitForHitIndex lookup =
0081         [&measToSimHits, &simHitAssociation](
0082             std::size_t i) -> std::optional<edm4hep::SimTrackerHit> {
0083       auto it = measToSimHits.find(static_cast<Index>(i));
0084       if (it == measToSimHits.end()) {
0085         return std::nullopt;
0086       }
0087       return simHitAssociation.lookup(it->second);
0088     };
0089 
0090     auto links = std::make_unique<
0091         ActsPodioEdm::TrackerHitLocalSimTrackerHitLinkCollection>();
0092     ActsPlugins::EDM4hepUtil::writeTrackerHitSimHitLinks(*hits, *links, lookup);
0093     m_outputSimHitLinks(ctx, std::move(links));
0094   }
0095 
0096   m_outputTrackerHitsLocal(ctx, std::move(hits));
0097 
0098   return ProcessCode::SUCCESS;
0099 }
0100 
0101 std::vector<std::string> EDM4hepMeasurementOutputConverter::collections()
0102     const {
0103   std::vector<std::string> result = {m_cfg.outputTrackerHitsLocal};
0104   if (m_cfg.outputSimHitLinks.has_value()) {
0105     result.push_back(m_cfg.outputSimHitLinks.value());
0106   }
0107   return result;
0108 }
0109 
0110 }  // namespace ActsExamples