Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:11:58

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/RootSpacepointWriter.hpp"
0010 
0011 #include "Acts/Definitions/Units.hpp"
0012 #include "Acts/EventData/SourceLink.hpp"
0013 #include "Acts/Geometry/GeometryIdentifier.hpp"
0014 #include "ActsExamples/EventData/IndexSourceLink.hpp"
0015 #include "ActsExamples/Framework/AlgorithmContext.hpp"
0016 
0017 #include <ios>
0018 #include <ostream>
0019 #include <stdexcept>
0020 #include <vector>
0021 
0022 #include <TFile.h>
0023 #include <TTree.h>
0024 
0025 ActsExamples::RootSpacepointWriter::RootSpacepointWriter(
0026     const ActsExamples::RootSpacepointWriter::Config& config,
0027     Acts::Logging::Level level)
0028     : WriterT(config.inputSpacepoints, "RootSpacepointWriter", level),
0029       m_cfg(config) {
0030   // inputParticles is already checked by base constructor
0031   if (m_cfg.filePath.empty()) {
0032     throw std::invalid_argument("Missing file path");
0033   }
0034   if (m_cfg.treeName.empty()) {
0035     throw std::invalid_argument("Missing tree name");
0036   }
0037 
0038   // open root file and create the tree
0039   m_outputFile = TFile::Open(m_cfg.filePath.c_str(), m_cfg.fileMode.c_str());
0040   if (m_outputFile == nullptr) {
0041     throw std::ios_base::failure("Could not open '" + m_cfg.filePath + "'");
0042   }
0043   m_outputFile->cd();
0044   m_outputTree = new TTree(m_cfg.treeName.c_str(), m_cfg.treeName.c_str());
0045   if (m_outputTree == nullptr) {
0046     throw std::bad_alloc();
0047   }
0048 
0049   // setup the branches
0050   m_outputTree->Branch("event_id", &m_eventId);
0051   m_outputTree->Branch("measurement_id", &m_measurementId, "measurement_id/l");
0052   m_outputTree->Branch("geometry_id", &m_geometryId, "geometry_id/l");
0053   m_outputTree->Branch("x", &m_x);
0054   m_outputTree->Branch("y", &m_y);
0055   m_outputTree->Branch("z", &m_z);
0056   m_outputTree->Branch("var_r", &m_var_r);
0057   m_outputTree->Branch("var_z", &m_var_z);
0058 }
0059 
0060 ActsExamples::RootSpacepointWriter::~RootSpacepointWriter() {
0061   if (m_outputFile != nullptr) {
0062     m_outputFile->Close();
0063   }
0064 }
0065 
0066 ActsExamples::ProcessCode ActsExamples::RootSpacepointWriter::finalize() {
0067   m_outputFile->cd();
0068   m_outputTree->Write();
0069   m_outputFile->Close();
0070 
0071   ACTS_VERBOSE("Wrote hits to tree '" << m_cfg.treeName << "' in '"
0072                                       << m_cfg.filePath << "'");
0073 
0074   return ProcessCode::SUCCESS;
0075 }
0076 
0077 ActsExamples::ProcessCode ActsExamples::RootSpacepointWriter::writeT(
0078     const AlgorithmContext& ctx,
0079     const ActsExamples::SimSpacePointContainer& spacepoints) {
0080   // ensure exclusive access to tree/file while writing
0081   std::lock_guard<std::mutex> lock(m_writeMutex);
0082 
0083   // Get the event number
0084   m_eventId = ctx.eventNumber;
0085   for (const auto& sp : spacepoints) {
0086     const auto& slinkPtr = sp.sourceLinks()[0].get<IndexSourceLink>();
0087     m_measurementId = slinkPtr.index();
0088     m_geometryId = slinkPtr.geometryId().value();
0089     // write sp position
0090     m_x = sp.x() / Acts::UnitConstants::mm;
0091     m_y = sp.y() / Acts::UnitConstants::mm;
0092     m_z = sp.z() / Acts::UnitConstants::mm;
0093     // write sp dimensions
0094     m_var_r = sp.varianceR() / Acts::UnitConstants::mm;
0095     m_var_z = sp.varianceZ() / Acts::UnitConstants::mm;
0096     // Fill the tree
0097     m_outputTree->Fill();
0098   }
0099   return ActsExamples::ProcessCode::SUCCESS;
0100 }