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/CsvSimHitReader.hpp"
0010 
0011 #include "Acts/Definitions/Units.hpp"
0012 #include "Acts/Geometry/GeometryIdentifier.hpp"
0013 #include "ActsExamples/EventData/SimHit.hpp"
0014 #include "ActsExamples/Framework/AlgorithmContext.hpp"
0015 #include "ActsExamples/Utilities/Paths.hpp"
0016 #include "ActsFatras/EventData/Barcode.hpp"
0017 #include "ActsFatras/EventData/Hit.hpp"
0018 
0019 #include <array>
0020 #include <stdexcept>
0021 
0022 #include <dfe/dfe_io_dsv.hpp>
0023 
0024 #include "CsvOutputData.hpp"
0025 
0026 ActsExamples::CsvSimHitReader::CsvSimHitReader(
0027     const ActsExamples::CsvSimHitReader::Config& config,
0028     Acts::Logging::Level level)
0029     : m_cfg(config),
0030       // TODO check that all files (hits,cells,truth) exists
0031       m_eventsRange(
0032           determineEventFilesRange(m_cfg.inputDir, m_cfg.inputStem + ".csv")),
0033       m_logger(Acts::getDefaultLogger("CsvSimHitReader", level)) {
0034   if (m_cfg.inputStem.empty()) {
0035     throw std::invalid_argument("Missing input filename stem");
0036   }
0037   if (m_cfg.outputSimHits.empty()) {
0038     throw std::invalid_argument("Missing simulated hits output collection");
0039   }
0040 
0041   m_outputSimHits.initialize(m_cfg.outputSimHits);
0042 }
0043 
0044 std::string ActsExamples::CsvSimHitReader::CsvSimHitReader::name() const {
0045   return "CsvSimHitReader";
0046 }
0047 
0048 std::pair<std::size_t, std::size_t>
0049 ActsExamples::CsvSimHitReader::availableEvents() const {
0050   return m_eventsRange;
0051 }
0052 
0053 ActsExamples::ProcessCode ActsExamples::CsvSimHitReader::read(
0054     const ActsExamples::AlgorithmContext& ctx) {
0055   auto path = perEventFilepath(m_cfg.inputDir, m_cfg.inputStem + ".csv",
0056                                ctx.eventNumber);
0057 
0058   dfe::NamedTupleCsvReader<SimHitData> reader(path);
0059 
0060   SimHitContainer::sequence_type unordered;
0061   SimHitData data;
0062 
0063   ACTS_DEBUG("start to read hits ");
0064   while (reader.read(data)) {
0065     ACTS_DEBUG("found a sim hit");
0066     const auto geometryId = Acts::GeometryIdentifier(data.geometry_id);
0067     // TODO validate geo id consistency
0068     const auto particleId = ActsFatras::Barcode(data.particle_id);
0069 
0070     ActsFatras::Hit::Vector4 pos4{
0071         data.tx * Acts::UnitConstants::mm,
0072         data.ty * Acts::UnitConstants::mm,
0073         data.tz * Acts::UnitConstants::mm,
0074         data.tt * Acts::UnitConstants::mm,
0075     };
0076     ActsFatras::Hit::Vector4 mom4{
0077         data.tpx * Acts::UnitConstants::GeV,
0078         data.tpy * Acts::UnitConstants::GeV,
0079         data.tpz * Acts::UnitConstants::GeV,
0080         data.te * Acts::UnitConstants::GeV,
0081     };
0082     ActsFatras::Hit::Vector4 delta4{
0083         data.deltapx * Acts::UnitConstants::GeV,
0084         data.deltapy * Acts::UnitConstants::GeV,
0085         data.deltapz * Acts::UnitConstants::GeV,
0086         data.deltae * Acts::UnitConstants::GeV,
0087     };
0088 
0089     ActsFatras::Hit hit(geometryId, particleId, pos4, mom4, mom4 + delta4,
0090                         data.index);
0091     unordered.push_back(std::move(hit));
0092   }
0093 
0094   // write the ordered data to the EventStore (according to geometry_id).
0095   SimHitContainer simHits;
0096   simHits.insert(unordered.begin(), unordered.end());
0097   m_outputSimHits(ctx, std::move(simHits));
0098 
0099   return ActsExamples::ProcessCode::SUCCESS;
0100 }