Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RootSpacepointWriter.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file RootSpacepointWriter.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2022 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
10 
16 
17 #include <ios>
18 #include <ostream>
19 #include <stdexcept>
20 #include <vector>
21 
22 #include <TFile.h>
23 #include <TTree.h>
24 
28  : WriterT(config.inputSpacepoints, "RootSpacepointWriter", level),
29  m_cfg(config) {
30  // inputParticles is already checked by base constructor
31  if (m_cfg.filePath.empty()) {
32  throw std::invalid_argument("Missing file path");
33  }
34  if (m_cfg.treeName.empty()) {
35  throw std::invalid_argument("Missing tree name");
36  }
37 
38  // open root file and create the tree
39  m_outputFile = TFile::Open(m_cfg.filePath.c_str(), m_cfg.fileMode.c_str());
40  if (m_outputFile == nullptr) {
41  throw std::ios_base::failure("Could not open '" + m_cfg.filePath + "'");
42  }
43  m_outputFile->cd();
44  m_outputTree = new TTree(m_cfg.treeName.c_str(), m_cfg.treeName.c_str());
45  if (m_outputTree == nullptr) {
46  throw std::bad_alloc();
47  }
48 
49  // setup the branches
50  m_outputTree->Branch("event_id", &m_eventId);
51  m_outputTree->Branch("measurement_id", &m_measurementId, "measurement_id/l");
52  m_outputTree->Branch("geometry_id", &m_geometryId, "geometry_id/l");
53  m_outputTree->Branch("x", &m_x);
54  m_outputTree->Branch("y", &m_y);
55  m_outputTree->Branch("z", &m_z);
56  m_outputTree->Branch("var_r", &m_var_r);
57  m_outputTree->Branch("var_z", &m_var_z);
58 }
59 
61  if (m_outputFile != nullptr) {
62  m_outputFile->Close();
63  }
64 }
65 
67  m_outputFile->cd();
68  m_outputTree->Write();
69  m_outputFile->Close();
70 
71  ACTS_VERBOSE("Wrote hits to tree '" << m_cfg.treeName << "' in '"
72  << m_cfg.filePath << "'");
73 
74  return ProcessCode::SUCCESS;
75 }
76 
78  const AlgorithmContext& ctx,
79  const ActsExamples::SimSpacePointContainer& spacepoints) {
80  // ensure exclusive access to tree/file while writing
81  std::lock_guard<std::mutex> lock(m_writeMutex);
82 
83  // Get the event number
84  m_eventId = ctx.eventNumber;
85  for (const auto& sp : spacepoints) {
86  const auto& slinkPtr = sp.sourceLinks()[0].get<IndexSourceLink>();
87  m_measurementId = slinkPtr.index();
88  m_geometryId = slinkPtr.geometryId().value();
89  // write sp position
90  m_x = sp.x() / Acts::UnitConstants::mm;
91  m_y = sp.y() / Acts::UnitConstants::mm;
92  m_z = sp.z() / Acts::UnitConstants::mm;
93  // write sp dimensions
94  m_var_r = sp.varianceR() / Acts::UnitConstants::mm;
95  m_var_z = sp.varianceZ() / Acts::UnitConstants::mm;
96  // Fill the tree
97  m_outputTree->Fill();
98  }
100 }