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) 2022 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/CsvParticleWriter.hpp"
0010 
0011 #include "ActsExamples/Framework/AlgorithmContext.hpp"
0012 #include "ActsExamples/Utilities/Paths.hpp"
0013 #include "ActsFatras/EventData/Barcode.hpp"
0014 #include "ActsFatras/EventData/Particle.hpp"
0015 #include <Acts/Definitions/Units.hpp>
0016 
0017 #include <stdexcept>
0018 #include <vector>
0019 
0020 #include <dfe/dfe_io_dsv.hpp>
0021 
0022 #include "CsvOutputData.hpp"
0023 
0024 ActsExamples::CsvParticleWriter::CsvParticleWriter(
0025     const ActsExamples::CsvParticleWriter::Config& cfg,
0026     Acts::Logging::Level lvl)
0027     : WriterT(cfg.inputParticles, "CsvParticleWriter", lvl), m_cfg(cfg) {
0028   // inputParticles is already checked by base constructor
0029   if (m_cfg.outputStem.empty()) {
0030     throw std::invalid_argument("Missing output filename stem");
0031   }
0032 }
0033 
0034 ActsExamples::ProcessCode ActsExamples::CsvParticleWriter::writeT(
0035     const ActsExamples::AlgorithmContext& ctx,
0036     const SimParticleContainer& particles) {
0037   auto pathParticles = perEventFilepath(
0038       m_cfg.outputDir, m_cfg.outputStem + ".csv", ctx.eventNumber);
0039   dfe::NamedTupleCsvWriter<ParticleData> writer(pathParticles,
0040                                                 m_cfg.outputPrecision);
0041 
0042   ParticleData data;
0043   for (const auto& particle : particles) {
0044     data.particle_id = particle.particleId().value();
0045     data.particle_type = particle.pdg();
0046     data.process = static_cast<decltype(data.process)>(particle.process());
0047     data.vx = particle.position().x() / Acts::UnitConstants::mm;
0048     data.vy = particle.position().y() / Acts::UnitConstants::mm;
0049     data.vz = particle.position().z() / Acts::UnitConstants::mm;
0050     data.vt = particle.time() / Acts::UnitConstants::mm;
0051     const auto p = particle.absoluteMomentum() / Acts::UnitConstants::GeV;
0052     data.px = p * particle.direction().x();
0053     data.py = p * particle.direction().y();
0054     data.pz = p * particle.direction().z();
0055     data.m = particle.mass() / Acts::UnitConstants::GeV;
0056     data.q = particle.charge() / Acts::UnitConstants::e;
0057     writer.append(data);
0058   }
0059 
0060   return ProcessCode::SUCCESS;
0061 }