Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RootParticleWriter.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file RootParticleWriter.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2017 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 
17 
18 #include <algorithm>
19 #include <ios>
20 #include <ostream>
21 #include <stdexcept>
22 
23 #include <TFile.h>
24 #include <TTree.h>
25 
29  : WriterT(cfg.inputParticles, "RootParticleWriter", lvl), m_cfg(cfg) {
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("particle_id", &m_particleId);
52  m_outputTree->Branch("particle_type", &m_particleType);
53  m_outputTree->Branch("process", &m_process);
54  m_outputTree->Branch("vx", &m_vx);
55  m_outputTree->Branch("vy", &m_vy);
56  m_outputTree->Branch("vz", &m_vz);
57  m_outputTree->Branch("vt", &m_vt);
58  m_outputTree->Branch("px", &m_px);
59  m_outputTree->Branch("py", &m_py);
60  m_outputTree->Branch("pz", &m_pz);
61  m_outputTree->Branch("m", &m_m);
62  m_outputTree->Branch("q", &m_q);
63  m_outputTree->Branch("eta", &m_eta);
64  m_outputTree->Branch("phi", &m_phi);
65  m_outputTree->Branch("pt", &m_pt);
66  m_outputTree->Branch("p", &m_p);
67  m_outputTree->Branch("vertex_primary", &m_vertexPrimary);
68  m_outputTree->Branch("vertex_secondary", &m_vertexSecondary);
69  m_outputTree->Branch("particle", &m_particle);
70  m_outputTree->Branch("generation", &m_generation);
71  m_outputTree->Branch("sub_particle", &m_subParticle);
72 }
73 
75  if (m_outputFile != nullptr) {
76  m_outputFile->Close();
77  }
78 }
79 
81  m_outputFile->cd();
82  m_outputTree->Write();
83  m_outputFile->Close();
84 
85  ACTS_INFO("Wrote particles to tree '" << m_cfg.treeName << "' in '"
86  << m_cfg.filePath << "'");
87 
88  return ProcessCode::SUCCESS;
89 }
90 
92  const AlgorithmContext& ctx, const SimParticleContainer& particles) {
93  // ensure exclusive access to tree/file while writing
94  std::lock_guard<std::mutex> lock(m_writeMutex);
95 
96  m_eventId = ctx.eventNumber;
97  for (const auto& particle : particles) {
98  m_particleId.push_back(particle.particleId().value());
99  m_particleType.push_back(particle.pdg());
100  m_process.push_back(static_cast<uint32_t>(particle.process()));
101  // position
102  m_vx.push_back(Acts::clampValue<float>(particle.fourPosition().x() /
104  m_vy.push_back(Acts::clampValue<float>(particle.fourPosition().y() /
106  m_vz.push_back(Acts::clampValue<float>(particle.fourPosition().z() /
108  m_vt.push_back(Acts::clampValue<float>(particle.fourPosition().w() /
110  // momentum
111  const auto p = particle.absoluteMomentum() / Acts::UnitConstants::GeV;
112  m_p.push_back(Acts::clampValue<float>(p));
113  m_px.push_back(Acts::clampValue<float>(p * particle.direction().x()));
114  m_py.push_back(Acts::clampValue<float>(p * particle.direction().y()));
115  m_pz.push_back(Acts::clampValue<float>(p * particle.direction().z()));
116  // particle constants
117  m_m.push_back(
118  Acts::clampValue<float>(particle.mass() / Acts::UnitConstants::GeV));
119  m_q.push_back(
120  Acts::clampValue<float>(particle.charge() / Acts::UnitConstants::e));
121  // derived kinematic quantities
122  m_eta.push_back(Acts::clampValue<float>(
123  Acts::VectorHelpers::eta(particle.direction())));
124  m_phi.push_back(Acts::clampValue<float>(
125  Acts::VectorHelpers::phi(particle.direction())));
126  m_pt.push_back(Acts::clampValue<float>(
127  p * Acts::VectorHelpers::perp(particle.direction())));
128  // decoded barcode components
129  m_vertexPrimary.push_back(particle.particleId().vertexPrimary());
130  m_vertexSecondary.push_back(particle.particleId().vertexSecondary());
131  m_particle.push_back(particle.particleId().particle());
132  m_generation.push_back(particle.particleId().generation());
133  m_subParticle.push_back(particle.particleId().subParticle());
134  }
135 
136  m_outputTree->Fill();
137 
138  m_particleId.clear();
139  m_particleType.clear();
140  m_process.clear();
141  m_vx.clear();
142  m_vy.clear();
143  m_vz.clear();
144  m_vt.clear();
145  m_p.clear();
146  m_px.clear();
147  m_py.clear();
148  m_pz.clear();
149  m_m.clear();
150  m_q.clear();
151  m_eta.clear();
152  m_phi.clear();
153  m_pt.clear();
154  m_vertexPrimary.clear();
155  m_vertexSecondary.clear();
156  m_particle.clear();
157  m_generation.clear();
158  m_subParticle.clear();
159 
160  return ProcessCode::SUCCESS;
161 }