Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-05-14 07:56:58

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/Podio/PodioReader.hpp"
0010 
0011 #include "Acts/Plugins/Podio/PodioUtil.hpp"
0012 #include "ActsExamples/Framework/DataHandle.hpp"
0013 
0014 #include <filesystem>
0015 
0016 #include <podio/Frame.h>
0017 #include <tbb/enumerable_thread_specific.h>
0018 
0019 namespace ActsExamples {
0020 
0021 namespace detail {
0022 
0023 class PodioReaderImpl {
0024  public:
0025   explicit PodioReaderImpl(PodioReader::Config cfg, PodioReader& parent)
0026       : m_frameWriteHandle(&parent, "EDM4hepFrameOutput"),
0027         m_cfg(std::move(cfg)) {
0028     m_eventsRange = std::make_pair(0, reader().getEntries("events"));
0029     if (!std::filesystem::exists(m_cfg.inputPath)) {
0030       throw std::invalid_argument("Input file does not exist");
0031     }
0032     if (m_cfg.outputFrame.empty()) {
0033       throw std::invalid_argument("Output frame name is not set");
0034     }
0035     if (m_cfg.category.empty()) {
0036       throw std::invalid_argument("Category name is not set");
0037     }
0038 
0039     m_frameWriteHandle.initialize(m_cfg.outputFrame);
0040   }
0041 
0042   Acts::PodioUtil::ROOTReader& reader() {
0043     bool exists = false;
0044     auto& reader = m_reader.local(exists);
0045     if (!exists) {
0046       reader.openFile(m_cfg.inputPath);
0047     }
0048 
0049     return reader;
0050   }
0051 
0052   WriteDataHandle<podio::Frame> m_frameWriteHandle;
0053   std::pair<std::size_t, std::size_t> m_eventsRange;
0054 
0055   tbb::enumerable_thread_specific<Acts::PodioUtil::ROOTReader> m_reader;
0056   PodioReader::Config m_cfg;
0057 };
0058 
0059 }  // namespace detail
0060 
0061 PodioReader::PodioReader(const Config& config, Acts::Logging::Level level)
0062     : m_impl(std::make_unique<detail::PodioReaderImpl>(config, *this)),
0063       m_logger(Acts::getDefaultLogger("PodioReader", level)) {}
0064 
0065 PodioReader::~PodioReader() = default;
0066 
0067 ProcessCode PodioReader::read(const AlgorithmContext& context) {
0068   ACTS_DEBUG("Reading EDM4hep inputs");
0069   podio::Frame frame = m_impl->reader().readEntry(
0070       m_impl->m_cfg.category, static_cast<unsigned int>(context.eventNumber));
0071   m_impl->m_frameWriteHandle(context, std::move(frame));
0072 
0073   return ProcessCode::SUCCESS;
0074 }
0075 
0076 std::pair<std::size_t, std::size_t> PodioReader::availableEvents() const {
0077   return m_impl->m_eventsRange;
0078 }
0079 
0080 std::string PodioReader::name() const {
0081   return "PodioReader";
0082 }
0083 
0084 const PodioReader::Config& PodioReader::config() const {
0085   return m_impl->m_cfg;
0086 }
0087 
0088 }  // namespace ActsExamples