Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-15 08:14:35

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