Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:01:46

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/PodioTrackOutputConverter.hpp"
0010 
0011 #include "Acts/EventData/TrackContainer.hpp"
0012 #include "Acts/Utilities/Holders.hpp"
0013 #include "ActsExamples/Io/EDM4hep/DD4hepPodioConversionHelper.hpp"
0014 #include "ActsPlugins/EDM4hep/PodioTrackContainer.hpp"
0015 #include "ActsPlugins/EDM4hep/PodioTrackStateContainer.hpp"
0016 #include "ActsPlugins/EDM4hep/PodioUtil.hpp"
0017 #include "ActsPodioEdm/BoundParametersCollection.h"
0018 #include "ActsPodioEdm/JacobianCollection.h"
0019 #include "ActsPodioEdm/TrackCollection.h"
0020 #include "ActsPodioEdm/TrackStateCollection.h"
0021 #include "ActsPodioEdm/TrackStateHitLinkCollection.h"
0022 
0023 #include <stdexcept>
0024 
0025 namespace ActsExamples {
0026 
0027 PodioTrackOutputConverter::PodioTrackOutputConverter(
0028     const Config& config, std::unique_ptr<const Acts::Logger> logger)
0029     : PodioOutputConverter("PodioTrackOutputConverter", std::move(logger)),
0030       m_cfg(config) {
0031   if (m_cfg.inputTracks.empty()) {
0032     throw std::invalid_argument("Missing input tracks collection");
0033   }
0034   if (m_cfg.outputTracks.empty()) {
0035     throw std::invalid_argument("Missing output tracks collection");
0036   }
0037   if (m_cfg.inputTrackerHitsLocal.empty()) {
0038     throw std::invalid_argument("Missing input tracker hits local collection");
0039   }
0040   if (m_cfg.detector == nullptr) {
0041     throw std::invalid_argument("Missing detector");
0042   }
0043   if (m_cfg.detector->trackingGeometry() == nullptr) {
0044     throw std::invalid_argument("Tracking geometry not found");
0045   }
0046 
0047   const auto prefix = m_cfg.outputTracks;
0048 
0049   m_inputTracks.initialize(m_cfg.inputTracks);
0050   m_inputTrackerHitsLocal.initialize(m_cfg.inputTrackerHitsLocal);
0051   m_outputTracks.initialize(prefix);
0052   m_outputTrackStates.initialize(
0053       ActsPlugins::PodioTrackStateContainerBase::trackStatesKey(prefix));
0054   m_outputTrackStateHitLinks.initialize(
0055       ActsPlugins::PodioTrackStateContainerBase::trackStateHitLinksKey(prefix));
0056 }
0057 
0058 ActsExamples::ProcessCode PodioTrackOutputConverter::execute(
0059     const AlgorithmContext& context) const {
0060   const auto& inputTracks = m_inputTracks(context);
0061 
0062   ACTS_VERBOSE("Converting " << inputTracks.size()
0063                              << " tracks to Podio format");
0064 
0065   const auto& trackerHitsLocal = m_inputTrackerHitsLocal(context);
0066 
0067   auto trackStateHitLinks =
0068       std::make_unique<ActsPodioEdm::TrackStateHitLinkCollection>();
0069 
0070   DD4hepPodioConversionHelper helper(*m_cfg.detector, trackerHitsLocal);
0071 
0072   auto trackCollection = std::make_unique<ActsPodioEdm::TrackCollection>();
0073   auto trackStateCollection =
0074       std::make_unique<ActsPodioEdm::TrackStateCollection>();
0075   auto paramsCollection =
0076       std::make_unique<ActsPodioEdm::BoundParametersCollection>();
0077   auto jacsCollection = std::make_unique<ActsPodioEdm::JacobianCollection>();
0078 
0079   ActsPlugins::MutablePodioTrackStateContainer trackStateContainer(
0080       helper, *trackStateCollection, *paramsCollection, *jacsCollection,
0081       trackStateHitLinks.get());
0082   ActsPlugins::MutablePodioTrackContainer trackContainer(helper,
0083                                                          *trackCollection);
0084 
0085   Acts::TrackContainer outputTracks{trackContainer, trackStateContainer};
0086 
0087   std::size_t nUncalibratedSourceLinks = 0;
0088 
0089   for (const auto& inputTrack : inputTracks) {
0090     for (const auto& ts : inputTrack.trackStatesReversed()) {
0091       if (ts.hasUncalibratedSourceLink()) {
0092         nUncalibratedSourceLinks++;
0093       }
0094     }
0095 
0096     auto outputTrack = outputTracks.makeTrack();
0097     outputTrack.copyFrom(inputTrack);
0098   }
0099 
0100   ACTS_VERBOSE("Copied " << outputTracks.size() << " tracks with "
0101                          << trackStateContainer.size() << " track states");
0102 
0103   std::size_t nUncalibratedSourceLinksInOutput = 0;
0104   for (const auto& outputTrack : outputTracks) {
0105     for (const auto& ts : outputTrack.trackStates()) {
0106       if (ts.hasUncalibratedSourceLink()) {
0107         nUncalibratedSourceLinksInOutput++;
0108       }
0109     }
0110   }
0111 
0112   if (nUncalibratedSourceLinks != nUncalibratedSourceLinksInOutput) {
0113     ACTS_ERROR(
0114         "Number of uncalibrated source links in input and output do not match: "
0115         << nUncalibratedSourceLinks
0116         << " != " << nUncalibratedSourceLinksInOutput);
0117     return ProcessCode::ABORT;
0118   }
0119 
0120   m_outputTracks(context, std::move(trackCollection));
0121   m_outputTrackStates(context, std::move(trackStateCollection));
0122   m_outputTrackStateHitLinks(context, std::move(trackStateHitLinks));
0123 
0124   return ProcessCode::SUCCESS;
0125 }
0126 
0127 std::vector<std::string> PodioTrackOutputConverter::collections() const {
0128   const auto prefix = m_cfg.outputTracks;
0129   return {
0130       prefix,
0131       ActsPlugins::PodioTrackStateContainerBase::trackStatesKey(prefix),
0132       ActsPlugins::PodioTrackStateContainerBase::trackStateHitLinksKey(prefix),
0133   };
0134 }
0135 
0136 }  // namespace ActsExamples