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 
12 
13 #include <iostream>
14 
15 #include <TChain.h>
16 #include <TFile.h>
17 
20  : ActsExamples::IReader(), m_cfg(cfg), m_events(0), m_inputChain(nullptr) {
21  m_inputChain = new TChain(m_cfg.treeName.c_str());
22 
23  // Set the branches
24  m_inputChain->SetBranchAddress("v_x", &m_v_x);
25  m_inputChain->SetBranchAddress("v_y", &m_v_y);
26  m_inputChain->SetBranchAddress("v_z", &m_v_z);
27  m_inputChain->SetBranchAddress("v_px", &m_v_px);
28  m_inputChain->SetBranchAddress("v_py", &m_v_py);
29  m_inputChain->SetBranchAddress("v_pz", &m_v_pz);
30  m_inputChain->SetBranchAddress("v_phi", &m_v_phi);
31  m_inputChain->SetBranchAddress("v_eta", &m_v_eta);
32  m_inputChain->SetBranchAddress("t_X0", &m_tX0);
33  m_inputChain->SetBranchAddress("t_L0", &m_tL0);
34  m_inputChain->SetBranchAddress("mat_x", &m_step_x);
35  m_inputChain->SetBranchAddress("mat_y", &m_step_y);
36  m_inputChain->SetBranchAddress("mat_z", &m_step_z);
37  m_inputChain->SetBranchAddress("mat_dx", &m_step_dx);
38  m_inputChain->SetBranchAddress("mat_dy", &m_step_dy);
39  m_inputChain->SetBranchAddress("mat_dz", &m_step_dz);
40  m_inputChain->SetBranchAddress("mat_step_length", &m_step_length);
41  m_inputChain->SetBranchAddress("mat_X0", &m_step_X0);
42  m_inputChain->SetBranchAddress("mat_L0", &m_step_L0);
43  m_inputChain->SetBranchAddress("mat_A", &m_step_A);
44  m_inputChain->SetBranchAddress("mat_Z", &m_step_Z);
45  m_inputChain->SetBranchAddress("mat_rho", &m_step_rho);
46 
47  // loop over the input files
48  for (auto inputFile : m_cfg.fileList) {
49  // add file to the input chain
50  m_inputChain->Add(inputFile.c_str());
51  ACTS_DEBUG("Adding File " << inputFile << " to tree '" << m_cfg.treeName
52  << "'.");
53  }
54 
55  m_events = m_inputChain->GetEntries();
56  ACTS_DEBUG("The full chain has " << m_events << " entries.");
57 }
58 
60  delete m_step_x;
61  delete m_step_y;
62  delete m_step_z;
63  delete m_step_length;
64  delete m_step_X0;
65  delete m_step_L0;
66  delete m_step_A;
67  delete m_step_Z;
68  delete m_step_rho;
69 }
70 
72  return m_cfg.name;
73 }
74 
75 std::pair<size_t, size_t>
77  return {0u, m_events};
78 }
79 
81  const ActsExamples::AlgorithmContext& context) {
82  ACTS_DEBUG("Trying to read recorded material from tracks.");
83  // read in the material track
84  if (m_inputChain && context.eventNumber < m_events) {
85  // lock the mutex
86  std::lock_guard<std::mutex> lock(m_read_mutex);
87  // now read
88 
89  // The collection to be written
90  std::vector<Acts::RecordedMaterialTrack> mtrackCollection;
91 
92  for (size_t ib = 0; ib < m_cfg.batchSize; ++ib) {
93  // Read the correct entry: batch size * event_number + ib
94  m_inputChain->GetEntry(m_cfg.batchSize * context.eventNumber + ib);
95  ACTS_VERBOSE("Reading entry: " << m_cfg.batchSize * context.eventNumber +
96  ib);
97 
99  // Fill the position and momentum
100  rmTrack.first.first = Acts::Vector3D(m_v_x, m_v_y, m_v_z);
101  rmTrack.first.second = Acts::Vector3D(m_v_px, m_v_py, m_v_pz);
102 
103  // Fill the individual steps
104  size_t msteps = m_step_length->size();
105  ACTS_VERBOSE("Reading " << msteps << " material steps.");
106  rmTrack.second.materialInteractions.reserve(msteps);
107  rmTrack.second.materialInX0 = 0.;
108  rmTrack.second.materialInL0 = 0.;
109 
110  for (size_t is = 0; is < msteps; ++is) {
111  double mX0 = (*m_step_X0)[is];
112  double mL0 = (*m_step_L0)[is];
113  double s = (*m_step_length)[is];
114 
115  rmTrack.second.materialInX0 += s / mX0;
116  rmTrack.second.materialInL0 += s / mL0;
117 
119  Acts::MaterialInteraction mInteraction;
120  mInteraction.position =
121  Acts::Vector3D((*m_step_x)[is], (*m_step_y)[is], (*m_step_z)[is]);
122  mInteraction.direction = Acts::Vector3D(
123  (*m_step_dx)[is], (*m_step_dy)[is], (*m_step_dz)[is]);
124  mInteraction.materialSlab = Acts::MaterialSlab(
125  Acts::Material::fromMassDensity(mX0, mL0, (*m_step_A)[is],
126  (*m_step_Z)[is], (*m_step_rho)[is]),
127  s);
128  rmTrack.second.materialInteractions.push_back(std::move(mInteraction));
129  }
130  mtrackCollection.push_back(std::move(rmTrack));
131  }
132 
133  // Write to the collection to the EventStore
134  context.eventStore.add(m_cfg.collection, std::move(mtrackCollection));
135  }
136  // Return success flag
138 }