LDMX Software
PropagatorStepWriter.cxx
1#include "Tracking/Sim/PropagatorStepWriter.h"
2
3//--- ACTS --- //
4#include <Acts/Geometry/GeometryIdentifier.hpp>
5#include <Acts/Geometry/TrackingVolume.hpp>
6#include <Acts/Propagator/ConstrainedStep.hpp>
7#include <Acts/Surfaces/Surface.hpp>
8
9#include "Acts/Geometry/DetectorElementBase.hpp"
10// mg ... I don't think these are used, and they are not defined in acts v36
11//#include "Acts/Plugins/Identification/IdentifiedDetectorElement.hpp"
12//#include "Acts/Plugins/Identification/Identifier.hpp"
13
14namespace tracking {
15namespace sim {
16
19 : m_cfg(cfg), m_outputFile(cfg.rootFile) {
20 if (m_cfg.treeName.empty()) {
21 throw std::invalid_argument("Missing tree name");
22 }
23
24 // Setup ROOT I/O
25 if (m_outputFile == nullptr) {
26 m_outputFile = TFile::Open(m_cfg.filePath.c_str(), m_cfg.fileMode.c_str());
27 if (m_outputFile == nullptr) {
28 throw std::ios_base::failure("Could not open '" + m_cfg.filePath);
29 }
30 }
31 m_outputFile->cd();
32
33 m_outputTree = new TTree(m_cfg.treeName.c_str(),
34 "TTree from RootPropagationStepsWriter");
35 if (m_outputTree == nullptr) throw std::bad_alloc();
36
37 // Set the branches
38 m_outputTree->Branch("event_nr", &m_eventNr);
39 // m_outputTree->Branch("volume_id", &m_volumeID);
40 m_outputTree->Branch("boundary_id", &m_boundaryID);
41 m_outputTree->Branch("layer_id", &m_layerID);
42 m_outputTree->Branch("approach_id", &m_approachID);
43 m_outputTree->Branch("sensitive_id", &m_sensitiveID);
44 m_outputTree->Branch("g_x", &m_x);
45 m_outputTree->Branch("g_y", &m_y);
46 m_outputTree->Branch("g_z", &m_z);
47 m_outputTree->Branch("d_x", &m_dx);
48 m_outputTree->Branch("d_y", &m_dy);
49 m_outputTree->Branch("d_z", &m_dz);
50 m_outputTree->Branch("type", &m_step_type);
51 m_outputTree->Branch("step_acc", &m_step_acc);
52 m_outputTree->Branch("step_act", &m_step_act);
53 m_outputTree->Branch("step_abt", &m_step_abt);
54 m_outputTree->Branch("step_usr", &m_step_usr);
55 m_outputTree->Branch("hit_x", &m_hit_x);
56 m_outputTree->Branch("hit_y", &m_hit_y);
57 m_outputTree->Branch("hit_z", &m_hit_z);
58 m_outputTree->Branch("start_pos", &m_start_pos);
59 m_outputTree->Branch("start_mom", &m_start_mom);
60
61} // constructor
62
65 if (m_cfg.rootFile == nullptr) {
66 m_outputFile->cd();
67 m_outputTree->Write();
68 m_outputFile->Close();
69 }
70} // destructor
71
72bool PropagatorStepWriter::WriteSteps(
73 framework::Event& event,
74 const std::vector<PropagationSteps>& stepCollection,
75 const std::vector<ldmx::Measurement>& measurements,
76 const Acts::Vector3& start_pos, const Acts::Vector3& start_mom) {
77 // Exclusive access to the tree while writing
78 std::lock_guard<std::mutex> lock(m_writeMutex);
79
80 m_outputFile->cd();
81
82 // we get the event number
83 m_eventNr = event.getEventNumber();
84
85 // fill the hits
86 m_hit_x.clear();
87 m_hit_y.clear();
88 m_hit_z.clear();
89 // m_hit_erru.clear();
90 // m_hit_errv.clear();
91
92 m_start_pos.clear();
93 m_start_mom.clear();
94
95 for (auto& meas : measurements) {
96 m_hit_x.push_back(meas.getGlobalPosition()[0]);
97 m_hit_y.push_back(meas.getGlobalPosition()[1]);
98 m_hit_z.push_back(meas.getGlobalPosition()[2]);
99 }
100
101 for (unsigned int i = 0; i < 3; i++) {
102 m_start_pos.push_back(start_pos(i));
103 m_start_mom.push_back(start_mom(i));
104 }
105
106 // loop over the step vector of each test propagation in this
107 for (auto& steps : stepCollection) {
108 // clear the vectors for each collection
109 // m_volumeID.clear();
110 m_boundaryID.clear();
111 m_layerID.clear();
112 m_approachID.clear();
113 m_sensitiveID.clear();
114 m_x.clear();
115 m_y.clear();
116 m_z.clear();
117 m_dx.clear();
118 m_dy.clear();
119 m_dz.clear();
120 m_step_type.clear();
121 m_step_acc.clear();
122 m_step_act.clear();
123 m_step_abt.clear();
124 m_step_usr.clear();
125
126 // loop over single steps
127 for (auto& step : steps) {
128 // the identification of the step
129 // Acts::GeometryIdentifier::Value volumeID = 0;
130 Acts::GeometryIdentifier::Value boundaryID = 0;
131 Acts::GeometryIdentifier::Value layerID = 0;
132 Acts::GeometryIdentifier::Value approachID = 0;
133 Acts::GeometryIdentifier::Value sensitiveID = 0;
134 // get the identification from the surface first
135 if (step.surface) {
136 auto geoID = step.surface->geometryId();
137 // volumeID = geoID.volume();
138 boundaryID = geoID.boundary();
139 layerID = geoID.layer();
140 approachID = geoID.approach();
141 sensitiveID = geoID.sensitive();
142 }
143 // a current volume overwrites the surface tagged one
144 // mg ... v36 Step does not include volume
145 // if (step.volume) {
146 // volumeID = step.volume->geometryId().volume();
147 // }
148 // now fill
149 m_sensitiveID.push_back(sensitiveID);
150 m_approachID.push_back(approachID);
151 m_layerID.push_back(layerID);
152 m_boundaryID.push_back(boundaryID);
153 // m_volumeID.push_back(volumeID);
154
155 // kinematic information
156 m_x.push_back(step.position.x());
157 m_y.push_back(step.position.y());
158 m_z.push_back(step.position.z());
159 auto direction = step.momentum.normalized();
160 m_dx.push_back(direction.x());
161 m_dy.push_back(direction.y());
162 m_dz.push_back(direction.z());
163
164 // double accuracy =
165 // step.stepSize.value(Acts::ConstrainedStep::accuracy());
166 double accuracy = step.stepSize.accuracy();
167 double actor = step.stepSize.value(Acts::ConstrainedStep::actor);
168 double aborter = step.stepSize.value(Acts::ConstrainedStep::aborter);
169 double user = step.stepSize.value(Acts::ConstrainedStep::user);
170 double act2 = actor * actor;
171 double acc2 = accuracy * accuracy;
172 double abo2 = aborter * aborter;
173 double usr2 = user * user;
174
175 // todo - fold with direction
176 if (act2 < acc2 && act2 < abo2 && act2 < usr2) {
177 m_step_type.push_back(0);
178 } else if (acc2 < abo2 && acc2 < usr2) {
179 m_step_type.push_back(1);
180 } else if (abo2 < usr2) {
181 m_step_type.push_back(2);
182 } else {
183 m_step_type.push_back(3);
184 }
185
186 // step size information
187 m_step_acc.push_back(accuracy);
188 m_step_act.push_back(actor);
189 m_step_abt.push_back(aborter);
190 m_step_usr.push_back(user);
191 }
192 m_outputTree->Fill();
193 }
194 return true;
195}
196} // namespace sim
197} // namespace tracking
Implements an event buffer system for storing event data.
Definition Event.h:41
std::vector< int > m_boundaryID
boundary identifier
PropagatorStepWriter(const Config &cfg)
Constructor with.
std::vector< int > m_step_type
step type
std::vector< float > m_step_act
actor check
std::vector< float > m_hit_x
hit location X
TFile * m_outputFile
the output file name
std::vector< int > m_sensitiveID
surface identifier
std::vector< float > m_dz
global direction z
std::vector< float > m_hit_z
hit location Z
std::vector< int > m_approachID
surface identifier
std::vector< float > m_dx
global direction x
std::vector< float > m_start_pos
start position of the particle propagated
std::vector< float > m_dy
global direction y
std::vector< int > m_layerID
layer identifier if
std::vector< float > m_start_mom
start momentum of the particle propagated
std::vector< float > m_step_acc
accuracy
std::vector< float > m_step_abt
aborter
std::mutex m_writeMutex
protect multi-threaded writes
std::vector< float > m_hit_y
hit location Y
Config m_cfg
the configuration object
The measurement calibrator can be a function or a class/struct able to retrieve the sim hits containe...
std::string filePath
path of the output file
std::string treeName
name of the output tree