Back to home page

sPhenix code displayed by LXR

 
 

    


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

0001 // This file is part of the Acts project.
0002 //
0003 // Copyright (C) 2017 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/Csv/CsvParticleReader.hpp"
0010 
0011 #include "Acts/Definitions/PdgParticle.hpp"
0012 #include "Acts/Definitions/Units.hpp"
0013 #include "Acts/Utilities/Logger.hpp"
0014 #include "ActsExamples/EventData/SimParticle.hpp"
0015 #include "ActsExamples/Framework/AlgorithmContext.hpp"
0016 #include "ActsExamples/Utilities/Paths.hpp"
0017 #include "ActsFatras/EventData/Barcode.hpp"
0018 #include "ActsFatras/EventData/Particle.hpp"
0019 #include "ActsFatras/EventData/ProcessType.hpp"
0020 
0021 #include <array>
0022 #include <cmath>
0023 #include <stdexcept>
0024 #include <string>
0025 
0026 #include <dfe/dfe_io_dsv.hpp>
0027 
0028 #include "CsvOutputData.hpp"
0029 
0030 ActsExamples::CsvParticleReader::CsvParticleReader(
0031     const ActsExamples::CsvParticleReader::Config& config,
0032     Acts::Logging::Level level)
0033     : m_cfg(config),
0034       m_eventsRange(
0035           determineEventFilesRange(m_cfg.inputDir, m_cfg.inputStem + ".csv")),
0036       m_logger(Acts::getDefaultLogger("CsvParticleReader", level)) {
0037   if (m_cfg.inputStem.empty()) {
0038     throw std::invalid_argument("Missing input filename stem");
0039   }
0040   if (m_cfg.outputParticles.empty()) {
0041     throw std::invalid_argument("Missing output collection");
0042   }
0043 
0044   m_outputParticles.initialize(m_cfg.outputParticles);
0045 }
0046 
0047 std::string ActsExamples::CsvParticleReader::CsvParticleReader::name() const {
0048   return "CsvParticleReader";
0049 }
0050 
0051 std::pair<std::size_t, std::size_t>
0052 ActsExamples::CsvParticleReader::availableEvents() const {
0053   return m_eventsRange;
0054 }
0055 
0056 ActsExamples::ProcessCode ActsExamples::CsvParticleReader::read(
0057     const ActsExamples::AlgorithmContext& ctx) {
0058   SimParticleContainer::sequence_type unordered;
0059 
0060   auto path = perEventFilepath(m_cfg.inputDir, m_cfg.inputStem + ".csv",
0061                                ctx.eventNumber);
0062   // vt and m are an optional columns
0063   dfe::NamedTupleCsvReader<ParticleData> reader(path, {"vt", "m"});
0064   ParticleData data;
0065 
0066   while (reader.read(data)) {
0067     ActsFatras::Particle particle(ActsFatras::Barcode(data.particle_id),
0068                                   Acts::PdgParticle(data.particle_type),
0069                                   data.q * Acts::UnitConstants::e,
0070                                   data.m * Acts::UnitConstants::GeV);
0071     particle.setProcess(static_cast<ActsFatras::ProcessType>(data.process));
0072     particle.setPosition4(
0073         data.vx * Acts::UnitConstants::mm, data.vy * Acts::UnitConstants::mm,
0074         data.vz * Acts::UnitConstants::mm, data.vt * Acts::UnitConstants::mm);
0075     // Only used for direction; normalization/units do not matter
0076     particle.setDirection(data.px, data.py, data.pz);
0077     particle.setAbsoluteMomentum(std::hypot(data.px, data.py, data.pz) *
0078                                  Acts::UnitConstants::GeV);
0079     unordered.push_back(std::move(particle));
0080   }
0081 
0082   // Write ordered particles container to the EventStore
0083   SimParticleContainer particles;
0084   particles.insert(unordered.begin(), unordered.end());
0085   m_outputParticles(ctx, std::move(particles));
0086 
0087   return ProcessCode::SUCCESS;
0088 }