File indexing completed on 2025-01-18 09:11:57
0001
0002
0003
0004
0005
0006
0007
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/ParticleOutcome.hpp"
0017 #include "ActsFatras/EventData/ProcessType.hpp"
0018
0019 #include <iostream>
0020 #include <stdexcept>
0021
0022 #include <TChain.h>
0023
0024 namespace ActsExamples {
0025
0026 RootParticleReader::RootParticleReader(const RootParticleReader::Config& config,
0027 Acts::Logging::Level level)
0028 : IReader(),
0029 m_cfg(config),
0030 m_logger(Acts::getDefaultLogger(name(), level)) {
0031 m_inputChain = std::make_unique<TChain>(m_cfg.treeName.c_str());
0032
0033 if (m_cfg.filePath.empty()) {
0034 throw std::invalid_argument("Missing input filename");
0035 }
0036 if (m_cfg.treeName.empty()) {
0037 throw std::invalid_argument("Missing tree name");
0038 }
0039
0040 m_outputParticles.initialize(m_cfg.outputParticles);
0041
0042
0043 m_inputChain->SetBranchAddress("event_id", &m_eventId);
0044 m_inputChain->SetBranchAddress("particle_id", &m_particleId);
0045 m_inputChain->SetBranchAddress("particle_type", &m_particleType);
0046 m_inputChain->SetBranchAddress("process", &m_process);
0047 m_inputChain->SetBranchAddress("vx", &m_vx);
0048 m_inputChain->SetBranchAddress("vy", &m_vy);
0049 m_inputChain->SetBranchAddress("vz", &m_vz);
0050 m_inputChain->SetBranchAddress("vt", &m_vt);
0051 m_inputChain->SetBranchAddress("p", &m_p);
0052 m_inputChain->SetBranchAddress("px", &m_px);
0053 m_inputChain->SetBranchAddress("py", &m_py);
0054 m_inputChain->SetBranchAddress("pz", &m_pz);
0055 m_inputChain->SetBranchAddress("m", &m_m);
0056 m_inputChain->SetBranchAddress("q", &m_q);
0057 m_inputChain->SetBranchAddress("eta", &m_eta);
0058 m_inputChain->SetBranchAddress("phi", &m_phi);
0059 m_inputChain->SetBranchAddress("pt", &m_pt);
0060 m_inputChain->SetBranchAddress("vertex_primary", &m_vertexPrimary);
0061 m_inputChain->SetBranchAddress("vertex_secondary", &m_vertexSecondary);
0062 m_inputChain->SetBranchAddress("particle", &m_particle);
0063 m_inputChain->SetBranchAddress("generation", &m_generation);
0064 m_inputChain->SetBranchAddress("sub_particle", &m_subParticle);
0065
0066 m_inputChain->SetBranchAddress("e_loss", &m_eLoss);
0067 m_inputChain->SetBranchAddress("total_x0", &m_pathInX0);
0068 m_inputChain->SetBranchAddress("total_l0", &m_pathInL0);
0069 m_inputChain->SetBranchAddress("number_of_hits", &m_numberOfHits);
0070 m_inputChain->SetBranchAddress("outcome", &m_outcome);
0071
0072 auto path = m_cfg.filePath;
0073
0074
0075 m_inputChain->Add(path.c_str());
0076 ACTS_DEBUG("Adding File " << path << " to tree '" << m_cfg.treeName << "'.");
0077
0078 m_events = m_inputChain->GetEntries();
0079 ACTS_DEBUG("The full chain has " << m_events << " entries.");
0080
0081
0082 {
0083
0084
0085 m_inputChain->SetEstimate(m_events + 1);
0086
0087 m_entryNumbers.resize(m_events);
0088 m_inputChain->Draw("event_id", "", "goff");
0089 RootUtility::stableSort(m_inputChain->GetEntries(), m_inputChain->GetV1(),
0090 m_entryNumbers.data(), false);
0091 }
0092 }
0093
0094 std::pair<std::size_t, std::size_t> RootParticleReader::availableEvents()
0095 const {
0096 return {0u, m_events};
0097 }
0098
0099 RootParticleReader::~RootParticleReader() {
0100 delete m_particleId;
0101 delete m_particleType;
0102 delete m_process;
0103 delete m_vx;
0104 delete m_vy;
0105 delete m_vz;
0106 delete m_vt;
0107 delete m_p;
0108 delete m_px;
0109 delete m_py;
0110 delete m_pz;
0111 delete m_m;
0112 delete m_q;
0113 delete m_eta;
0114 delete m_phi;
0115 delete m_pt;
0116 delete m_vertexPrimary;
0117 delete m_vertexSecondary;
0118 delete m_particle;
0119 delete m_generation;
0120 delete m_subParticle;
0121
0122 delete m_eLoss;
0123 delete m_pathInX0;
0124 delete m_pathInL0;
0125 delete m_numberOfHits;
0126 delete m_outcome;
0127 }
0128
0129 ProcessCode RootParticleReader::read(const AlgorithmContext& context) {
0130 ACTS_DEBUG("Trying to read recorded particles.");
0131
0132 if (m_inputChain == nullptr || context.eventNumber >= m_events) {
0133 return ProcessCode::SUCCESS;
0134 }
0135
0136
0137 std::lock_guard<std::mutex> lock(m_read_mutex);
0138
0139
0140
0141 SimParticleContainer particles;
0142
0143
0144 auto entry = m_entryNumbers.at(context.eventNumber);
0145 m_inputChain->GetEntry(entry);
0146 ACTS_DEBUG("Reading event: " << context.eventNumber
0147 << " stored as entry: " << entry);
0148
0149 unsigned int nParticles = m_particleId->size();
0150
0151 for (unsigned int i = 0; i < nParticles; i++) {
0152 SimParticle p;
0153
0154 p.setProcess(static_cast<ActsFatras::ProcessType>((*m_process).at(i)));
0155 p.setPdg(static_cast<Acts::PdgParticle>((*m_particleType).at(i)));
0156 p.setCharge((*m_q).at(i) * Acts::UnitConstants::e);
0157 p.setMass((*m_m).at(i) * Acts::UnitConstants::GeV);
0158 p.setParticleId((*m_particleId).at(i));
0159
0160 SimParticleState& initialState = p.initial();
0161
0162 initialState.setPosition4((*m_vx).at(i) * Acts::UnitConstants::mm,
0163 (*m_vy).at(i) * Acts::UnitConstants::mm,
0164 (*m_vz).at(i) * Acts::UnitConstants::mm,
0165 (*m_vt).at(i) * Acts::UnitConstants::mm);
0166
0167 initialState.setDirection((*m_px).at(i), (*m_py).at(i), (*m_pz).at(i));
0168 initialState.setAbsoluteMomentum((*m_p).at(i) * Acts::UnitConstants::GeV);
0169
0170 SimParticleState& finalState = p.final();
0171
0172
0173 finalState.setMaterialPassed((*m_pathInX0).at(i) * Acts::UnitConstants::mm,
0174 (*m_pathInL0).at(i) * Acts::UnitConstants::mm);
0175 finalState.setNumberOfHits((*m_numberOfHits).at(i));
0176 finalState.setOutcome(
0177 static_cast<ActsFatras::ParticleOutcome>((*m_outcome).at(i)));
0178
0179 particles.insert(p);
0180 }
0181
0182 ACTS_DEBUG("Read " << particles.size() << " particles for event "
0183 << context.eventNumber);
0184
0185
0186 m_outputParticles(context, std::move(particles));
0187
0188
0189 return ProcessCode::SUCCESS;
0190 }
0191
0192 }