Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:11:51

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