ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RootMaterialTrackReader.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file RootMaterialTrackReader.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2017-2018 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 
11 #include <TChain.h>
12 #include <TFile.h>
13 #include <iostream>
14 
16 
19  : FW::IReader(), m_cfg(cfg), m_events(0), m_inputChain(nullptr) {
20  m_inputChain = new TChain(m_cfg.treeName.c_str());
21 
22  // Set the branches
23  m_inputChain->SetBranchAddress("v_x", &m_v_x);
24  m_inputChain->SetBranchAddress("v_y", &m_v_y);
25  m_inputChain->SetBranchAddress("v_z", &m_v_z);
26  m_inputChain->SetBranchAddress("v_px", &m_v_px);
27  m_inputChain->SetBranchAddress("v_py", &m_v_py);
28  m_inputChain->SetBranchAddress("v_pz", &m_v_pz);
29  m_inputChain->SetBranchAddress("v_phi", &m_v_phi);
30  m_inputChain->SetBranchAddress("v_eta", &m_v_eta);
31  m_inputChain->SetBranchAddress("t_X0", &m_tX0);
32  m_inputChain->SetBranchAddress("t_L0", &m_tL0);
33  m_inputChain->SetBranchAddress("mat_x", &m_step_x);
34  m_inputChain->SetBranchAddress("mat_y", &m_step_y);
35  m_inputChain->SetBranchAddress("mat_z", &m_step_z);
36  m_inputChain->SetBranchAddress("mat_step_length", &m_step_length);
37  m_inputChain->SetBranchAddress("mat_X0", &m_step_X0);
38  m_inputChain->SetBranchAddress("mat_L0", &m_step_L0);
39  m_inputChain->SetBranchAddress("mat_A", &m_step_A);
40  m_inputChain->SetBranchAddress("mat_Z", &m_step_Z);
41  m_inputChain->SetBranchAddress("mat_rho", &m_step_rho);
42 
43  // loop over the input files
44  for (auto inputFile : m_cfg.fileList) {
45  // add file to the input chain
46  m_inputChain->Add(inputFile.c_str());
47  ACTS_DEBUG("Adding File " << inputFile << " to tree '" << m_cfg.treeName
48  << "'.");
49  }
50 
51  m_events = m_inputChain->GetEntries();
52  ACTS_DEBUG("The full chain has " << m_events << " entries.");
53 }
54 
56  delete m_step_x;
57  delete m_step_y;
58  delete m_step_z;
59  delete m_step_length;
60  delete m_step_X0;
61  delete m_step_L0;
62  delete m_step_A;
63  delete m_step_Z;
64  delete m_step_rho;
65 }
66 
67 std::string FW::RootMaterialTrackReader::name() const {
68  return m_cfg.name;
69 }
70 
71 std::pair<size_t, size_t> FW::RootMaterialTrackReader::availableEvents() const {
72  return {0u, m_events};
73 }
74 
77  ACTS_DEBUG("Trying to read recorded material from tracks.");
78  // read in the material track
79  if (m_inputChain && context.eventNumber < m_events) {
80  // lock the mutex
81  std::lock_guard<std::mutex> lock(m_read_mutex);
82  // now read
83 
84  // The collection to be written
85  std::vector<Acts::RecordedMaterialTrack> mtrackCollection;
86 
87  for (size_t ib = 0; ib < m_cfg.batchSize; ++ib) {
88  // Read the correct entry: batch size * event_number + ib
89  m_inputChain->GetEntry(m_cfg.batchSize * context.eventNumber + ib);
90  ACTS_VERBOSE("Reading entry: " << m_cfg.batchSize * context.eventNumber +
91  ib);
92 
94  // Fill the position and momentum
95  rmTrack.first.first = Acts::Vector3D(m_v_x, m_v_y, m_v_z);
96  rmTrack.first.second = Acts::Vector3D(m_v_px, m_v_py, m_v_pz);
97 
98  // Fill the individual steps
99  size_t msteps = m_step_length->size();
100  ACTS_VERBOSE("Reading " << msteps << " material steps.");
101  rmTrack.second.materialInteractions.reserve(msteps);
102  rmTrack.second.materialInX0 = 0.;
103  rmTrack.second.materialInL0 = 0.;
104 
105  for (size_t is = 0; is < msteps; ++is) {
106  double mX0 = (*m_step_X0)[is];
107  double mL0 = (*m_step_L0)[is];
108  double s = (*m_step_length)[is];
109 
110  rmTrack.second.materialInX0 += s / mX0;
111  rmTrack.second.materialInL0 += s / mL0;
112 
114  Acts::MaterialInteraction mInteraction;
115  mInteraction.position =
116  Acts::Vector3D((*m_step_x)[is], (*m_step_y)[is], (*m_step_z)[is]);
118  mX0, mL0, (*m_step_A)[is], (*m_step_Z)[is], (*m_step_rho)[is], s);
119  rmTrack.second.materialInteractions.push_back(std::move(mInteraction));
120  }
121  mtrackCollection.push_back(std::move(rmTrack));
122  }
123 
124  // Write to the collection to the EventStore
125  context.eventStore.add(m_cfg.collection, std::move(mtrackCollection));
126  }
127  // Return success flag
129 }