Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-06 08:10:48

0001 // This file is part of the Acts project.
0002 //
0003 // Copyright (C) 2018-2020 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 http://mozilla.org/MPL/2.0/.
0008 
0009 #include "ActsExamples/Io/HepMC3/HepMC3Reader.hpp"
0010 
0011 #include "ActsExamples/Framework/WhiteBoard.hpp"
0012 #include "ActsExamples/Utilities/Paths.hpp"
0013 
0014 #include <HepMC3/Units.h>
0015 
0016 namespace ActsExamples {
0017 
0018 bool HepMC3AsciiReader::readEvent(HepMC3::ReaderAscii& reader,
0019                                   HepMC3::GenEvent& event) {
0020   // Read event and store it
0021   return reader.read_event(event);
0022 }
0023 
0024 bool HepMC3AsciiReader::status(HepMC3::ReaderAscii& reader) {
0025   return !reader.failed();
0026 }
0027 
0028 HepMC3AsciiReader::HepMC3AsciiReader(const HepMC3AsciiReader::Config& cfg,
0029                                      Acts::Logging::Level lvl)
0030     : m_cfg(cfg),
0031       m_eventsRange(
0032           determineEventFilesRange(cfg.inputDir, cfg.inputStem + ".hepmc3")),
0033       m_logger(Acts::getDefaultLogger("HepMC3AsciiReader", lvl)) {
0034   if (m_cfg.inputStem.empty()) {
0035     throw std::invalid_argument("Missing input filename stem");
0036   }
0037   if (m_cfg.outputEvents.empty()) {
0038     throw std::invalid_argument("Missing output collection");
0039   }
0040 
0041   m_outputEvents.initialize(m_cfg.outputEvents);
0042 }
0043 
0044 std::string HepMC3AsciiReader::HepMC3AsciiReader::name() const {
0045   return "HepMC3AsciiReader";
0046 }
0047 
0048 std::pair<std::size_t, std::size_t> HepMC3AsciiReader::availableEvents() const {
0049   return m_eventsRange;
0050 }
0051 
0052 ProcessCode HepMC3AsciiReader::read(const AlgorithmContext& ctx) {
0053   std::vector<HepMC3::GenEvent> events;
0054   HepMC3::GenEvent event(HepMC3::Units::GEV, HepMC3::Units::MM);
0055 
0056   auto path = perEventFilepath(m_cfg.inputDir, m_cfg.inputStem + ".hepmc3",
0057                                ctx.eventNumber);
0058 
0059   ACTS_DEBUG("Attempting to read event from " << path);
0060   HepMC3::ReaderAscii reader(path);
0061 
0062   reader.read_event(event);
0063   while (!reader.failed()) {
0064     events.push_back(std::move(event));
0065     event.clear();
0066     reader.read_event(event);
0067   }
0068 
0069   if (events.empty()) {
0070     return ProcessCode::ABORT;
0071   }
0072 
0073   ACTS_VERBOSE(events.size()
0074                << " events read, writing to " << m_cfg.outputEvents);
0075   m_outputEvents(ctx, std::move(events));
0076 
0077   reader.close();
0078   return ProcessCode::SUCCESS;
0079 }
0080 
0081 }  // namespace ActsExamples