Back to home page

sPhenix code displayed by LXR

 
 

    


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

0001 // This file is part of the Acts project.
0002 //
0003 // Copyright (C) 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/Csv/CsvSimHitWriter.hpp"
0010 
0011 #include "Acts/Definitions/Algebra.hpp"
0012 #include "Acts/Definitions/Common.hpp"
0013 #include "Acts/Definitions/Units.hpp"
0014 #include "Acts/Geometry/GeometryIdentifier.hpp"
0015 #include "ActsExamples/EventData/SimHit.hpp"
0016 #include "ActsExamples/Framework/AlgorithmContext.hpp"
0017 #include "ActsExamples/Utilities/Paths.hpp"
0018 #include "ActsFatras/EventData/Barcode.hpp"
0019 #include "ActsFatras/EventData/Hit.hpp"
0020 
0021 #include <stdexcept>
0022 #include <vector>
0023 
0024 #include <dfe/dfe_io_dsv.hpp>
0025 
0026 #include "CsvOutputData.hpp"
0027 
0028 ActsExamples::CsvSimHitWriter::CsvSimHitWriter(
0029     const ActsExamples::CsvSimHitWriter::Config& config,
0030     Acts::Logging::Level level)
0031     : WriterT(config.inputSimHits, "CsvSimHitWriter", level), m_cfg(config) {
0032   // inputSimHits is already checked by base constructor
0033   if (m_cfg.outputStem.empty()) {
0034     throw std::invalid_argument("Missing output filename stem");
0035   }
0036 }
0037 
0038 ActsExamples::ProcessCode ActsExamples::CsvSimHitWriter::writeT(
0039     const AlgorithmContext& ctx, const ActsExamples::SimHitContainer& simHits) {
0040   // open per-event file for all simhit components
0041   std::string pathSimHit = perEventFilepath(
0042       m_cfg.outputDir, m_cfg.outputStem + ".csv", ctx.eventNumber);
0043 
0044   dfe::NamedTupleCsvWriter<SimHitData> writerSimHit(pathSimHit,
0045                                                     m_cfg.outputPrecision);
0046 
0047   // CsvOutputData struct
0048   SimHitData simhit;
0049   // Write data from internal impl. to output-side struct
0050   for (const auto& simHit : simHits) {
0051     // local simhit information in global coord.
0052     const Acts::Vector4& globalPos4 = simHit.fourPosition();
0053     const Acts::Vector4& momentum4Before = simHit.momentum4Before();
0054 
0055     simhit.geometry_id = simHit.geometryId().value();
0056     simhit.particle_id = simHit.particleId().value();
0057     // hit position
0058     simhit.tx = globalPos4[Acts::ePos0] / Acts::UnitConstants::mm;
0059     simhit.ty = globalPos4[Acts::ePos1] / Acts::UnitConstants::mm;
0060     simhit.tz = globalPos4[Acts::ePos2] / Acts::UnitConstants::mm;
0061     simhit.tt = globalPos4[Acts::eTime] / Acts::UnitConstants::mm;
0062     // particle four-momentum before interaction
0063     simhit.tpx = momentum4Before[Acts::eMom0] / Acts::UnitConstants::GeV;
0064     simhit.tpy = momentum4Before[Acts::eMom1] / Acts::UnitConstants::GeV;
0065     simhit.tpz = momentum4Before[Acts::eMom2] / Acts::UnitConstants::GeV;
0066     simhit.te = momentum4Before[Acts::eEnergy] / Acts::UnitConstants::GeV;
0067     // particle four-momentum change due to interaction
0068     const auto delta4 = simHit.momentum4After() - momentum4Before;
0069     simhit.deltapx = delta4[Acts::eMom0] / Acts::UnitConstants::GeV;
0070     simhit.deltapy = delta4[Acts::eMom1] / Acts::UnitConstants::GeV;
0071     simhit.deltapz = delta4[Acts::eMom2] / Acts::UnitConstants::GeV;
0072     simhit.deltae = delta4[Acts::eEnergy] / Acts::UnitConstants::GeV;
0073     // TODO write hit index along the particle trajectory
0074     simhit.index = simHit.index();
0075     writerSimHit.append(simhit);
0076   }  // end simHit loop
0077 
0078   return ActsExamples::ProcessCode::SUCCESS;
0079 }