Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-30 09:26:08

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/RootPropagationSummaryWriter.hpp"
0010 
0011 #include "Acts/Utilities/VectorHelpers.hpp"
0012 #include "ActsExamples/Framework/AlgorithmContext.hpp"
0013 #include "ActsExamples/Propagation/PropagationAlgorithm.hpp"
0014 
0015 #include <ios>
0016 #include <ostream>
0017 #include <stdexcept>
0018 
0019 #include <TFile.h>
0020 #include <TTree.h>
0021 
0022 namespace ActsExamples {
0023 
0024 RootPropagationSummaryWriter::RootPropagationSummaryWriter(
0025     const RootPropagationSummaryWriter::Config& cfg, Acts::Logging::Level level)
0026     : WriterT(cfg.inputSummaryCollection, "RootPropagationSummaryWriter",
0027               level),
0028       m_cfg(cfg),
0029       m_outputFile(cfg.rootFile) {
0030   // An input collection name and tree name must be specified
0031   if (m_cfg.inputSummaryCollection.empty()) {
0032     throw std::invalid_argument("Missing input collection");
0033   }
0034   if (m_cfg.treeName.empty()) {
0035     throw std::invalid_argument("Missing tree name");
0036   }
0037 
0038   // Setup ROOT I/O
0039   if (m_outputFile == nullptr) {
0040     m_outputFile = TFile::Open(m_cfg.filePath.c_str(), m_cfg.fileMode.c_str());
0041     if (m_outputFile == nullptr) {
0042       throw std::ios_base::failure("Could not open '" + m_cfg.filePath + "'");
0043     }
0044   }
0045   m_outputFile->cd();
0046 
0047   m_outputTree = new TTree(m_cfg.treeName.c_str(),
0048                            "TTree from RootPropagationSummaryWriter");
0049   if (m_outputTree == nullptr) {
0050     throw std::bad_alloc();
0051   }
0052 
0053   // Set the branches
0054   m_outputTree->Branch("event_nr", &m_eventNr);
0055   m_outputTree->Branch("track_nr", &m_trackNr);
0056 
0057   m_outputTree->Branch("d0", &m_d0);
0058   m_outputTree->Branch("z0", &m_z0);
0059   m_outputTree->Branch("phi", &m_phi);
0060   m_outputTree->Branch("theta", &m_theta);
0061   m_outputTree->Branch("qOverP", &m_qOverP);
0062   m_outputTree->Branch("t", &m_t);
0063 
0064   m_outputTree->Branch("eta", &m_eta);
0065   m_outputTree->Branch("pt", &m_pt);
0066   m_outputTree->Branch("p", &m_p);
0067 
0068   m_outputTree->Branch("nSensitives", &m_nSensitives);
0069   m_outputTree->Branch("nMaterials", &m_nMaterials);
0070   m_outputTree->Branch("nPortals", &m_nPortals);
0071 
0072   m_outputTree->Branch("nAttemptedSteps", &m_nAttemptedSteps);
0073   m_outputTree->Branch("nRejectedSteps", &m_nRejectedSteps);
0074   m_outputTree->Branch("nSuccessfulSteps", &m_nSuccessfulSteps);
0075   m_outputTree->Branch("nReverseSteps", &m_nReverseSteps);
0076   m_outputTree->Branch("pathLength", &m_pathLength);
0077   m_outputTree->Branch("absolutePathLength", &m_absolutePathLength);
0078   if (m_cfg.storePropagationTime) {
0079     m_outputTree->Branch("propagationTime", &m_propagationTime);
0080   }
0081 
0082   m_outputTree->Branch("nRenavigations", &m_nRenavigations);
0083   m_outputTree->Branch("nVolumeSwitches", &m_nVolumeSwitches);
0084 }
0085 
0086 RootPropagationSummaryWriter::~RootPropagationSummaryWriter() {
0087   if (m_outputFile != nullptr) {
0088     m_outputFile->Close();
0089   }
0090 }
0091 
0092 ProcessCode RootPropagationSummaryWriter::finalize() {
0093   // Write the tree
0094   m_outputFile->cd();
0095   m_outputTree->Write();
0096   /// Close the file if it's yours
0097   if (m_cfg.rootFile == nullptr) {
0098     m_outputFile->Close();
0099   }
0100 
0101   ACTS_VERBOSE("Wrote particles to tree '" << m_cfg.treeName << "' in '"
0102                                            << m_cfg.filePath << "'");
0103 
0104   return ProcessCode::SUCCESS;
0105 }
0106 
0107 ProcessCode RootPropagationSummaryWriter::writeT(
0108     const AlgorithmContext& context, const PropagationSummaries& summaries) {
0109   // Exclusive access to the tree while writing
0110   std::scoped_lock lock(m_writeMutex);
0111 
0112   // Get the event number
0113   m_eventNr = static_cast<int>(context.eventNumber);
0114 
0115   // Loop over the step vector of each test propagation in this
0116   for (const auto& [trackNr, summary] : Acts::enumerate(summaries)) {
0117     // Set the track number
0118     m_trackNr = static_cast<int>(trackNr);
0119 
0120     // Set the initial trajectory parameters
0121     const auto& startParameters = summary.startParameters;
0122     m_d0 = static_cast<float>(startParameters.parameters()[Acts::eBoundLoc0]);
0123     m_z0 = static_cast<float>(startParameters.parameters()[Acts::eBoundLoc1]);
0124     m_phi = static_cast<float>(startParameters.parameters()[Acts::eBoundDir0]);
0125     m_theta =
0126         static_cast<float>(startParameters.parameters()[Acts::eBoundDir1]);
0127     m_qOverP =
0128         static_cast<float>(startParameters.parameters()[Acts::eBoundQOverP]);
0129     m_t = static_cast<float>(startParameters.parameters()[Acts::eBoundTime]);
0130 
0131     // Derived initial trajectory parameters
0132     m_eta = static_cast<float>(
0133         Acts::VectorHelpers::eta(startParameters.direction()));
0134     m_pt = static_cast<float>(startParameters.transverseMomentum());
0135     m_p = static_cast<float>(startParameters.absoluteMomentum());
0136 
0137     m_nMaterials = 0;
0138     m_nSensitives = 0;
0139     m_nPortals = 0;
0140 
0141     // Loop over the steps & count for the statistics
0142     std::ranges::for_each(summary.steps, [&](const auto& step) {
0143       // Check if the step is a sensitive step
0144       if (step.geoID.sensitive() > 0) {
0145         m_nSensitives++;
0146       }
0147       // Check if the step is a portal step
0148       if (step.geoID.boundary() > 0) {
0149         m_nPortals++;
0150       }
0151 
0152       if (step.surface != nullptr) {
0153         // Check if the step is a material step
0154         if (step.surface->hasMaterial()) {
0155           m_nMaterials++;
0156         }
0157       }
0158     });
0159 
0160     // Stepper statistics
0161     m_nAttemptedSteps = summary.statistics.stepping.nAttemptedSteps;
0162     m_nRejectedSteps = summary.statistics.stepping.nRejectedSteps;
0163     m_nSuccessfulSteps = summary.statistics.stepping.nSuccessfulSteps;
0164     m_nReverseSteps = summary.statistics.stepping.nReverseSteps;
0165     m_pathLength = summary.statistics.stepping.pathLength;
0166     m_absolutePathLength = summary.statistics.stepping.absolutePathLength;
0167     m_propagationTime = summary.propagationTime;
0168 
0169     // Navigator statistics
0170     m_nRenavigations = summary.statistics.navigation.nRenavigations;
0171     m_nVolumeSwitches = summary.statistics.navigation.nVolumeSwitches;
0172 
0173     m_outputTree->Fill();
0174   }
0175 
0176   return ProcessCode::SUCCESS;
0177 }
0178 
0179 }  // namespace ActsExamples