Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RootAthenaNTupleReader.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file RootAthenaNTupleReader.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2017-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 
19 
20 #include <cstdint>
21 #include <iostream>
22 #include <optional>
23 #include <stdexcept>
24 
25 #include <TChain.h>
26 #include <TMathBase.h>
27 
31  : ActsExamples::IReader(),
32  m_cfg(config),
33  m_logger(Acts::getDefaultLogger(name(), level)) {
34  if (m_cfg.inputFilePath.empty()) {
35  throw std::invalid_argument("Missing input filename");
36  }
37  if (m_cfg.inputTreeName.empty()) {
38  throw std::invalid_argument("Missing tree name");
39  }
40 
45 
46  m_inputChain = new TChain(m_cfg.inputTreeName.c_str());
47 
48  // unused event identifier
49  std::int32_t eventNumber = 0;
50 
51  // Set the branches
52  m_inputChain->SetBranchAddress("EventNumber", &eventNumber);
53  m_inputChain->SetBranchAddress("track_d0", &m_branches.track_d0);
54  m_inputChain->SetBranchAddress("track_z0", &m_branches.track_z0);
55  m_inputChain->SetBranchAddress("track_theta", &m_branches.track_theta);
56  m_inputChain->SetBranchAddress("track_phi", &m_branches.track_phi);
57  m_inputChain->SetBranchAddress("track_qOverP", &m_branches.track_qOverP);
58  m_inputChain->SetBranchAddress("track_t", &m_branches.track_t);
59  m_inputChain->SetBranchAddress("track_z", &m_branches.track_z);
60 
61  // Covariance stuff
62  m_inputChain->SetBranchAddress("track_var_d0", &m_branches.track_var_d0);
63  m_inputChain->SetBranchAddress("track_var_z0", &m_branches.track_var_z0);
64  m_inputChain->SetBranchAddress("track_var_phi", &m_branches.track_var_phi);
65  m_inputChain->SetBranchAddress("track_var_theta",
67  m_inputChain->SetBranchAddress("track_var_qOverP",
69  m_inputChain->SetBranchAddress("track_cov_d0z0", &m_branches.track_cov_d0z0);
70  m_inputChain->SetBranchAddress("track_cov_d0phi",
72  m_inputChain->SetBranchAddress("track_cov_d0theta",
74  m_inputChain->SetBranchAddress("track_cov_d0qOverP",
76  m_inputChain->SetBranchAddress("track_cov_z0phi",
78  m_inputChain->SetBranchAddress("track_cov_z0theta",
80  m_inputChain->SetBranchAddress("track_cov_z0qOverP",
82  m_inputChain->SetBranchAddress("track_cov_phitheta",
84  m_inputChain->SetBranchAddress("track_cov_phiqOverP",
86  m_inputChain->SetBranchAddress("track_cov_tehtaqOverP",
88 
89  // Truth vertex
90  m_inputChain->SetBranchAddress("truthvertex_x", &m_branches.truthvertex_x);
91  m_inputChain->SetBranchAddress("truthvertex_y", &m_branches.truthvertex_y);
92  m_inputChain->SetBranchAddress("truthvertex_z", &m_branches.truthvertex_z);
93  m_inputChain->SetBranchAddress("truthvertex_t", &m_branches.truthvertex_t);
94 
95  m_inputChain->SetBranchAddress("recovertex_x", &m_branches.recovertex_x);
96  m_inputChain->SetBranchAddress("recovertex_y", &m_branches.recovertex_y);
97  m_inputChain->SetBranchAddress("recovertex_z", &m_branches.recovertex_z);
98  m_inputChain->SetBranchAddress("truthvertex_tracks_idx",
100 
101  m_inputChain->SetBranchAddress("beamspot_x", &m_branches.beamspot_x);
102  m_inputChain->SetBranchAddress("beamspot_y", &m_branches.beamspot_y);
103  m_inputChain->SetBranchAddress("beamspot_z", &m_branches.beamspot_z);
104  m_inputChain->SetBranchAddress("beamspot_sigX", &m_branches.beamspot_sigX);
105  m_inputChain->SetBranchAddress("beamspot_sigY", &m_branches.beamspot_sigY);
106  m_inputChain->SetBranchAddress("beamspot_sigZ", &m_branches.beamspot_sigZ);
107 
108  auto path = m_cfg.inputFilePath;
109 
110  // add file to the input chain
111  m_inputChain->Add(path.c_str());
112  ACTS_DEBUG("Adding File " << path << " to tree '" << m_cfg.inputTreeName
113  << "'.");
114 
115  m_events = m_inputChain->GetEntries();
116  ACTS_DEBUG("The full chain has " << m_events << " entries.");
117 
118  {
119  // The entry numbers for accessing events in increased order (there could be
120  // multiple entries corresponding to one event number)
121  std::vector<long long> m_entryNumbers;
122  // If the events are not in order, get the entry numbers for ordered events
123  m_entryNumbers.resize(m_events);
124  m_inputChain->Draw("EventNumber", "", "goff");
125  // Sort to get the entry numbers of the ordered events
126  TMath::Sort(m_inputChain->GetEntries(), m_inputChain->GetV1(),
127  m_entryNumbers.data(), false);
128  }
129 }
130 
132  const ActsExamples::AlgorithmContext& context) {
133  ACTS_DEBUG("Trying to read track parameters from ntuple.");
134 
135  Acts::Vector3 pos(0, 0, 0);
136  std::shared_ptr<Acts::PerigeeSurface> surface =
137  Acts::Surface::makeShared<Acts::PerigeeSurface>(pos);
138 
139  if (context.eventNumber >= m_events) {
140  ACTS_ERROR("event out of bounds");
141  return ProcessCode::ABORT;
142  }
143 
144  std::lock_guard<std::mutex> lock(m_read_mutex);
145 
146  auto entry = context.eventNumber;
147  m_inputChain->GetEntry(entry);
148  ACTS_INFO("Reading event: " << context.eventNumber
149  << " stored as entry: " << entry);
150 
151  const unsigned int nTracks = m_branches.track_d0.size();
152  const unsigned int nTruthVtx = m_branches.truthvertex_z.size();
153  const unsigned int nRecoVtx = m_branches.recovertex_z.size();
154 
155  ACTS_DEBUG("nTracks = " << nTracks);
156  ACTS_DEBUG("nTruthVtx = " << nTruthVtx);
157  ACTS_DEBUG("nRecoVtx = " << nRecoVtx);
158 
159  TrackParametersContainer trackContainer;
160  trackContainer.reserve(nTracks);
161 
162  for (unsigned int i = 0; i < nTracks; i++) {
163  Acts::BoundVector params;
164 
165  params[Acts::BoundIndices::eBoundLoc0] = m_branches.track_d0[i];
166  params[Acts::BoundIndices::eBoundLoc1] = m_branches.track_z0[i];
167  params[Acts::BoundIndices::eBoundPhi] = m_branches.track_phi[i];
168  params[Acts::BoundIndices::eBoundTheta] = m_branches.track_theta[i];
169  params[Acts::BoundIndices::eBoundQOverP] = m_branches.track_qOverP[i];
170  params[Acts::BoundIndices::eBoundTime] = m_branches.track_t[i];
171 
172  // Construct and fill covariance matrix
174 
175  // Variances
177  m_branches.track_var_d0[i];
179  m_branches.track_var_z0[i];
181  m_branches.track_var_phi[i];
183  m_branches.track_var_theta[i];
185  m_branches.track_var_qOverP[i];
186 
188  m_branches.track_cov_d0z0[i];
190  m_branches.track_cov_d0phi[i];
192  m_branches.track_cov_d0theta[i];
194  m_branches.track_cov_d0qOverP[i];
196  m_branches.track_cov_z0phi[i];
198  m_branches.track_cov_z0theta[i];
200  m_branches.track_cov_z0qOverP[i];
202  m_branches.track_cov_phitheta[i];
204  m_branches.track_cov_phiqOverP[i];
206  m_branches.track_cov_tehtaqOverP[i];
207 
209  m_branches.track_cov_d0z0[i];
211  m_branches.track_cov_d0phi[i];
213  m_branches.track_cov_d0theta[i];
215  m_branches.track_cov_d0qOverP[i];
217  m_branches.track_cov_z0phi[i];
219  m_branches.track_cov_z0theta[i];
221  m_branches.track_cov_z0qOverP[i];
223  m_branches.track_cov_phitheta[i];
225  m_branches.track_cov_phiqOverP[i];
227  m_branches.track_cov_tehtaqOverP[i];
228 
229  // TODO we do not have a hypothesis at hand here. defaulting to pion
230  Acts::BoundTrackParameters tc(surface, params, cov,
232  trackContainer.push_back(tc);
233  }
234 
235  std::vector<Acts::Vector4> truthVertexContainer;
236  for (unsigned int i = 0; i < nTruthVtx; i++) {
237  Acts::Vector4 vtx(m_branches.truthvertex_x[i], m_branches.truthvertex_y[i],
238  m_branches.truthvertex_z[i], m_branches.truthvertex_t[i]);
239  truthVertexContainer.push_back(vtx);
240  }
241  std::vector<Acts::Vector4> recoVertexContainer;
242  for (unsigned int i = 0; i < nRecoVtx; i++) {
243  Acts::Vector4 vtx(m_branches.recovertex_x[i], m_branches.recovertex_y[i],
244  m_branches.recovertex_z[i], 0);
245  recoVertexContainer.push_back(vtx);
246  }
247 
248  Acts::Vertex<Acts::BoundTrackParameters> beamspotConstraint;
249  Acts::Vector3 beamspotPos;
250  Acts::SquareMatrix3 beamspotCov;
251 
252  beamspotPos << m_branches.beamspot_x, m_branches.beamspot_y,
253  m_branches.beamspot_z;
254  beamspotCov << m_branches.beamspot_sigX * m_branches.beamspot_sigX, 0, 0, 0,
255  m_branches.beamspot_sigY * m_branches.beamspot_sigY, 0, 0, 0,
256  m_branches.beamspot_sigZ * m_branches.beamspot_sigZ;
257 
258  beamspotConstraint.setPosition(beamspotPos);
259  beamspotConstraint.setCovariance(beamspotCov);
260 
261  m_outputTrackParameters(context, std::move(trackContainer));
262  m_outputTruthVtxParameters(context, std::move(truthVertexContainer));
263  m_outputRecoVtxParameters(context, std::move(recoVertexContainer));
264  m_outputBeamspotConstraint(context, std::move(beamspotConstraint));
265 
266  // Return success flag
267  return ProcessCode::SUCCESS;
268 }