Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-25 09:04:46

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/Root/RootParticleReader.hpp"
0010 
0011 #include "Acts/Definitions/PdgParticle.hpp"
0012 #include "Acts/Utilities/Logger.hpp"
0013 #include "ActsExamples/EventData/SimParticle.hpp"
0014 #include "ActsExamples/Framework/AlgorithmContext.hpp"
0015 #include "ActsExamples/Io/Root/RootUtility.hpp"
0016 #include "ActsFatras/EventData/GenerationProcess.hpp"
0017 #include "ActsFatras/EventData/SimulationOutcome.hpp"
0018 
0019 #include <iostream>
0020 #include <stdexcept>
0021 
0022 #include <TChain.h>
0023 
0024 namespace ActsExamples {
0025 
0026 RootParticleReader::~RootParticleReader() = default;
0027 
0028 RootParticleReader::RootParticleReader(const RootParticleReader::Config& config,
0029                                        Acts::Logging::Level level)
0030     : IReader(),
0031       m_cfg(config),
0032       m_logger(Acts::getDefaultLogger(name(), level)) {
0033   m_inputChain = std::make_unique<TChain>(m_cfg.treeName.c_str());
0034 
0035   if (m_cfg.filePath.empty()) {
0036     throw std::invalid_argument("Missing input filename");
0037   }
0038   if (m_cfg.treeName.empty()) {
0039     throw std::invalid_argument("Missing tree name");
0040   }
0041 
0042   m_outputParticles.initialize(m_cfg.outputParticles);
0043 
0044   // Set the branches
0045   m_inputChain->SetBranchAddress("event_id", &m_eventId);
0046   m_inputChain->SetBranchAddress("particle_hash", &m_particleHash.get());
0047   m_inputChain->SetBranchAddress("particle_type", &m_particleType.get());
0048   m_inputChain->SetBranchAddress("process", &m_process.get());
0049   m_inputChain->SetBranchAddress("vx", &m_vx.get());
0050   m_inputChain->SetBranchAddress("vy", &m_vy.get());
0051   m_inputChain->SetBranchAddress("vz", &m_vz.get());
0052   m_inputChain->SetBranchAddress("vt", &m_vt.get());
0053   m_inputChain->SetBranchAddress("p", &m_p.get());
0054   m_inputChain->SetBranchAddress("px", &m_px.get());
0055   m_inputChain->SetBranchAddress("py", &m_py.get());
0056   m_inputChain->SetBranchAddress("pz", &m_pz.get());
0057   m_inputChain->SetBranchAddress("m", &m_m.get());
0058   m_inputChain->SetBranchAddress("q", &m_q.get());
0059   m_inputChain->SetBranchAddress("eta", &m_eta.get());
0060   m_inputChain->SetBranchAddress("phi", &m_phi.get());
0061   m_inputChain->SetBranchAddress("pt", &m_pt.get());
0062   m_inputChain->SetBranchAddress("vertex_primary", &m_vertexPrimary.get());
0063   m_inputChain->SetBranchAddress("vertex_secondary", &m_vertexSecondary.get());
0064   m_inputChain->SetBranchAddress("particle", &m_particle.get());
0065   m_inputChain->SetBranchAddress("generation", &m_generation.get());
0066   m_inputChain->SetBranchAddress("sub_particle", &m_subParticle.get());
0067   m_inputChain->SetBranchAddress("orig_part_idx", &m_origParticleIdx.get());
0068   m_inputChain->SetBranchAddress("hf_origin", &m_hfOrigin.get());
0069 
0070   m_inputChain->SetBranchAddress("e_loss", &m_eLoss.get());
0071   m_inputChain->SetBranchAddress("total_x0", &m_pathInX0.get());
0072   m_inputChain->SetBranchAddress("total_l0", &m_pathInL0.get());
0073   m_inputChain->SetBranchAddress("number_of_hits", &m_numberOfHits.get());
0074   m_inputChain->SetBranchAddress("outcome", &m_outcome.get());
0075 
0076   auto path = m_cfg.filePath;
0077 
0078   // add file to the input chain
0079   m_inputChain->Add(path.c_str());
0080   ACTS_DEBUG("Adding File " << path << " to tree '" << m_cfg.treeName << "'.");
0081 
0082   m_events = m_inputChain->GetEntries();
0083   ACTS_DEBUG("The full chain has " << m_events << " entries.");
0084 
0085   // Sort the entry numbers of the events
0086   {
0087     // necessary to guarantee that m_inputChain->GetV1() is valid for the
0088     // entire range
0089     m_inputChain->SetEstimate(m_events + 1);
0090 
0091     m_entryNumbers.resize(m_events);
0092     m_inputChain->Draw("event_id", "", "goff");
0093     RootUtility::stableSort(m_inputChain->GetEntries(), m_inputChain->GetV1(),
0094                             m_entryNumbers.data(), false);
0095   }
0096 }
0097 
0098 std::pair<std::size_t, std::size_t> RootParticleReader::availableEvents()
0099     const {
0100   return {0u, m_events};
0101 }
0102 
0103 ProcessCode RootParticleReader::read(const AlgorithmContext& context) {
0104   ACTS_DEBUG("Trying to read recorded particles.");
0105 
0106   if (m_inputChain == nullptr || context.eventNumber >= m_events) {
0107     return ProcessCode::SUCCESS;
0108   }
0109 
0110   // lock the mutex
0111   std::lock_guard<std::mutex> lock(m_read_mutex);
0112   // now read
0113 
0114   // The particle collection to be filled
0115   SimParticleContainer particles;
0116 
0117   // Read the correct entry
0118   auto entry = m_entryNumbers.at(context.eventNumber);
0119   m_inputChain->GetEntry(entry);
0120   ACTS_DEBUG("Reading event: " << context.eventNumber
0121                                << " stored as entry: " << entry);
0122 
0123   unsigned int nParticles = m_particleType->size();
0124 
0125   for (unsigned int i = 0; i < nParticles; i++) {
0126     SimParticle p;
0127 
0128     p.setProcess(
0129         static_cast<ActsFatras::GenerationProcess>((*m_process).at(i)));
0130     p.setPdg(static_cast<Acts::PdgParticle>((*m_particleType).at(i)));
0131     p.setCharge((*m_q).at(i) * Acts::UnitConstants::e);
0132     p.setMass((*m_m).at(i) * Acts::UnitConstants::GeV);
0133     p.setParticleId(SimBarcode()
0134                         .withVertexPrimary((*m_vertexPrimary).at(i))
0135                         .withVertexSecondary((*m_vertexSecondary).at(i))
0136                         .withParticle((*m_particle).at(i))
0137                         .withGeneration((*m_generation).at(i))
0138                         .withSubParticle((*m_subParticle).at(i)));
0139 
0140     p.setOrigParticleIdx((*m_origParticleIdx).at(i));
0141     p.setHeavyFlavourOrigin(
0142         static_cast<HeavyFlavourOrigin>((*m_hfOrigin).at(i)));
0143 
0144     SimParticleState& initialState = p.initialState();
0145 
0146     initialState.setPosition4((*m_vx).at(i) * Acts::UnitConstants::mm,
0147                               (*m_vy).at(i) * Acts::UnitConstants::mm,
0148                               (*m_vz).at(i) * Acts::UnitConstants::mm,
0149                               (*m_vt).at(i) * Acts::UnitConstants::mm);
0150     // NOTE: direction is normalized inside `setDirection`
0151     initialState.setDirection((*m_px).at(i), (*m_py).at(i), (*m_pz).at(i));
0152     initialState.setAbsoluteMomentum((*m_p).at(i) * Acts::UnitConstants::GeV);
0153 
0154     SimParticleState& finalState = p.finalState();
0155 
0156     // TODO eloss cannot be read since we need the final momentum
0157     finalState.setMaterialPassed((*m_pathInX0).at(i) * Acts::UnitConstants::mm,
0158                                  (*m_pathInL0).at(i) * Acts::UnitConstants::mm);
0159     finalState.setNumberOfHits((*m_numberOfHits).at(i));
0160     finalState.setOutcome(
0161         static_cast<ActsFatras::SimulationOutcome>((*m_outcome).at(i)));
0162 
0163     particles.insert(p);
0164   }
0165 
0166   ACTS_DEBUG("Read " << particles.size() << " particles for event "
0167                      << context.eventNumber);
0168 
0169   // Write the collections to the EventStore
0170   m_outputParticles(context, std::move(particles));
0171 
0172   // Return success flag
0173   return ProcessCode::SUCCESS;
0174 }
0175 
0176 }  // namespace ActsExamples