File indexing completed on 2026-11-29 09:46:18
0001
0002
0003
0004
0005
0006
0007
0008
0009 #include "ActsExamples/Io/Root/RootVertexReader.hpp"
0010
0011 #include "Acts/Utilities/Logger.hpp"
0012 #include "ActsExamples/EventData/SimParticle.hpp"
0013 #include "ActsExamples/Framework/AlgorithmContext.hpp"
0014 #include "ActsExamples/Io/Root/RootUtility.hpp"
0015 #include "ActsFatras/EventData/GenerationProcess.hpp"
0016
0017 #include <cstdint>
0018 #include <iostream>
0019 #include <stdexcept>
0020
0021 #include <TChain.h>
0022 #include <TMathBase.h>
0023
0024 namespace ActsExamples {
0025
0026 RootVertexReader::~RootVertexReader() = default;
0027
0028 RootVertexReader::RootVertexReader(const RootVertexReader::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_outputVertices.initialize(m_cfg.outputVertices);
0043
0044
0045 m_inputChain->SetBranchAddress("event_id", &m_eventId);
0046 m_inputChain->SetBranchAddress("process", &m_process.get());
0047 m_inputChain->SetBranchAddress("vx", &m_vx.get());
0048 m_inputChain->SetBranchAddress("vy", &m_vy.get());
0049 m_inputChain->SetBranchAddress("vz", &m_vz.get());
0050 m_inputChain->SetBranchAddress("vt", &m_vt.get());
0051 if (m_inputChain->GetBranch("incoming_particles") != nullptr) {
0052 m_hasCombinedIncoming = true;
0053 m_incomingParticles.allocate();
0054 m_inputChain->SetBranchAddress("incoming_particles",
0055 &m_incomingParticles.get());
0056 } else {
0057 m_hasCombinedIncoming = false;
0058 m_incomingParticlesVertexPrimary.allocate();
0059 m_incomingParticlesVertexSecondary.allocate();
0060 m_incomingParticlesParticle.allocate();
0061 m_incomingParticlesGeneration.allocate();
0062 m_incomingParticlesSubParticle.allocate();
0063 m_inputChain->SetBranchAddress("incoming_particles_vertex_primary",
0064 &m_incomingParticlesVertexPrimary.get());
0065 m_inputChain->SetBranchAddress("incoming_particles_vertex_secondary",
0066 &m_incomingParticlesVertexSecondary.get());
0067 m_inputChain->SetBranchAddress("incoming_particles_particle",
0068 &m_incomingParticlesParticle.get());
0069 m_inputChain->SetBranchAddress("incoming_particles_generation",
0070 &m_incomingParticlesGeneration.get());
0071 m_inputChain->SetBranchAddress("incoming_particles_sub_particle",
0072 &m_incomingParticlesSubParticle.get());
0073 }
0074
0075 if (m_inputChain->GetBranch("outgoing_particles") != nullptr) {
0076 m_hasCombinedOutgoing = true;
0077 m_outgoingParticles.allocate();
0078 m_inputChain->SetBranchAddress("outgoing_particles",
0079 &m_outgoingParticles.get());
0080 } else {
0081 m_hasCombinedOutgoing = false;
0082 m_outgoingParticlesVertexPrimary.allocate();
0083 m_outgoingParticlesVertexSecondary.allocate();
0084 m_outgoingParticlesParticle.allocate();
0085 m_outgoingParticlesGeneration.allocate();
0086 m_outgoingParticlesSubParticle.allocate();
0087 m_inputChain->SetBranchAddress("outgoing_particles_vertex_primary",
0088 &m_outgoingParticlesVertexPrimary.get());
0089 m_inputChain->SetBranchAddress("outgoing_particles_vertex_secondary",
0090 &m_outgoingParticlesVertexSecondary.get());
0091 m_inputChain->SetBranchAddress("outgoing_particles_particle",
0092 &m_outgoingParticlesParticle.get());
0093 m_inputChain->SetBranchAddress("outgoing_particles_generation",
0094 &m_outgoingParticlesGeneration.get());
0095 m_inputChain->SetBranchAddress("outgoing_particles_sub_particle",
0096 &m_outgoingParticlesSubParticle.get());
0097 }
0098 m_inputChain->SetBranchAddress("vertex_primary", &m_vertexPrimary.get());
0099 m_inputChain->SetBranchAddress("vertex_secondary", &m_vertexSecondary.get());
0100 m_inputChain->SetBranchAddress("generation", &m_generation.get());
0101
0102 auto path = m_cfg.filePath;
0103
0104
0105 m_inputChain->Add(path.c_str());
0106 ACTS_DEBUG("Adding File " << path << " to tree '" << m_cfg.treeName << "'.");
0107
0108 m_events = m_inputChain->GetEntries();
0109 ACTS_DEBUG("The full chain has " << m_events << " entries.");
0110
0111
0112 {
0113
0114
0115 m_inputChain->SetEstimate(m_events + 1);
0116
0117 m_entryNumbers.resize(m_events);
0118 m_inputChain->Draw("event_id", "", "goff");
0119 RootUtility::stableSort(m_inputChain->GetEntries(), m_inputChain->GetV1(),
0120 m_entryNumbers.data(), false);
0121 }
0122 }
0123
0124 std::pair<std::size_t, std::size_t> RootVertexReader::availableEvents() const {
0125 return {0u, m_events};
0126 }
0127
0128 ProcessCode RootVertexReader::read(const AlgorithmContext& context) {
0129 ACTS_DEBUG("Trying to read recorded vertices.");
0130
0131 if (m_inputChain == nullptr || context.eventNumber >= m_events) {
0132 return ProcessCode::SUCCESS;
0133 }
0134
0135
0136 std::lock_guard<std::mutex> lock(m_read_mutex);
0137
0138
0139
0140 SimVertexContainer vertices;
0141
0142
0143 auto entry = m_entryNumbers.at(context.eventNumber);
0144 m_inputChain->GetEntry(entry);
0145 ACTS_DEBUG("Reading event: " << context.eventNumber
0146 << " stored as entry: " << entry);
0147
0148 unsigned int nVertices = m_vx->size();
0149
0150 for (unsigned int i = 0; i < nVertices; i++) {
0151 SimVertex v;
0152
0153 v.id = SimVertexBarcode()
0154 .withVertexPrimary((*m_vertexPrimary)[i])
0155 .withVertexSecondary((*m_vertexSecondary)[i])
0156 .withGeneration((*m_generation)[i]);
0157
0158 v.process = static_cast<ActsFatras::GenerationProcess>((*m_process)[i]);
0159 v.position4 = Acts::Vector4((*m_vx)[i] * Acts::UnitConstants::mm,
0160 (*m_vy)[i] * Acts::UnitConstants::mm,
0161 (*m_vz)[i] * Acts::UnitConstants::mm,
0162 (*m_vt)[i] * Acts::UnitConstants::mm);
0163
0164
0165 if (m_hasCombinedIncoming && m_incomingParticles.hasValue()) {
0166 for (auto& barcodeComponents : (*m_incomingParticles)[i]) {
0167 v.incoming.insert(SimBarcode().withData(barcodeComponents));
0168 }
0169 } else if (m_incomingParticlesVertexPrimary.hasValue()) {
0170 const auto& incomingPrimaries = (*m_incomingParticlesVertexPrimary)[i];
0171 const auto& incomingSecondaries =
0172 (*m_incomingParticlesVertexSecondary)[i];
0173 const auto& incomingParticles = (*m_incomingParticlesParticle)[i];
0174 const auto& incomingGenerations = (*m_incomingParticlesGeneration)[i];
0175 const auto& incomingSubParticles = (*m_incomingParticlesSubParticle)[i];
0176 for (std::size_t j = 0; j < incomingPrimaries.size(); ++j) {
0177 v.incoming.insert(
0178 SimBarcode()
0179 .withVertexPrimary(static_cast<SimBarcode::PrimaryVertexId>(
0180 incomingPrimaries[j]))
0181 .withVertexSecondary(static_cast<SimBarcode::SecondaryVertexId>(
0182 incomingSecondaries[j]))
0183 .withParticle(
0184 static_cast<SimBarcode::ParticleId>(incomingParticles[j]))
0185 .withGeneration(static_cast<SimBarcode::GenerationId>(
0186 incomingGenerations[j]))
0187 .withSubParticle(static_cast<SimBarcode::SubParticleId>(
0188 incomingSubParticles[j])));
0189 }
0190 }
0191
0192
0193 if (m_hasCombinedOutgoing && m_outgoingParticles.hasValue()) {
0194 for (auto& barcodeComponents : (*m_outgoingParticles)[i]) {
0195 v.outgoing.insert(SimBarcode().withData(barcodeComponents));
0196 }
0197 } else if (m_outgoingParticlesVertexPrimary.hasValue()) {
0198 const auto& outgoingPrimaries = (*m_outgoingParticlesVertexPrimary)[i];
0199 const auto& outgoingSecondaries =
0200 (*m_outgoingParticlesVertexSecondary)[i];
0201 const auto& outgoingParticles = (*m_outgoingParticlesParticle)[i];
0202 const auto& outgoingGenerations = (*m_outgoingParticlesGeneration)[i];
0203 const auto& outgoingSubParticles = (*m_outgoingParticlesSubParticle)[i];
0204 for (std::size_t j = 0; j < outgoingPrimaries.size(); ++j) {
0205 v.outgoing.insert(
0206 SimBarcode()
0207 .withVertexPrimary(static_cast<SimBarcode::PrimaryVertexId>(
0208 outgoingPrimaries[j]))
0209 .withVertexSecondary(static_cast<SimBarcode::SecondaryVertexId>(
0210 outgoingSecondaries[j]))
0211 .withParticle(
0212 static_cast<SimBarcode::ParticleId>(outgoingParticles[j]))
0213 .withGeneration(static_cast<SimBarcode::GenerationId>(
0214 outgoingGenerations[j]))
0215 .withSubParticle(static_cast<SimBarcode::SubParticleId>(
0216 outgoingSubParticles[j])));
0217 }
0218 }
0219
0220 vertices.insert(v);
0221 }
0222
0223 ACTS_DEBUG("Read " << vertices.size() << " vertices for event "
0224 << context.eventNumber);
0225
0226
0227 m_outputVertices(context, std::move(vertices));
0228
0229
0230 return ProcessCode::SUCCESS;
0231 }
0232
0233 }