LDMX Software
PropagatorStepWriter.cxx
1#include "Tracking/Sim/PropagatorStepWriter.h"
2
3//--- ACTS --- //
4#include <Acts/Geometry/GeometryIdentifier.hpp>
5#include <Acts/Propagator/ConstrainedStep.hpp>
6
7#include "Framework/Exception/Exception.h"
8// mg ... I don't think these are used, and they are not defined in acts v36
9// #include "Acts/Plugins/Identification/IdentifiedDetectorElement.hpp"
10// #include "Acts/Plugins/Identification/Identifier.hpp"
11
12namespace tracking {
13namespace sim {
14
17 : m_cfg_(cfg), m_output_file_(cfg.root_file_) {
18 if (m_cfg_.tree_name_.empty()) {
19 EXCEPTION_RAISE("InvalidArgument", "Missing tree name");
20 }
21
22 // Setup ROOT I/O
23 if (m_output_file_ == nullptr) {
25 TFile::Open(m_cfg_.file_path_.c_str(), m_cfg_.file_mode_.c_str());
26 if (m_output_file_ == nullptr) {
27 throw std::ios_base::failure("Could not open '" + m_cfg_.file_path_);
28 }
29 }
30 m_output_file_->cd();
31
32 m_output_tree_ = new TTree(m_cfg_.tree_name_.c_str(),
33 "TTree from RootPropagationStepsWriter");
34 if (m_output_tree_ == nullptr) throw std::bad_alloc();
35
36 // Set the branches
37 m_output_tree_->Branch("event_nr", &m_event_nr_);
38 // m_outputTree->Branch("volume_id", &m_volumeID);
39 m_output_tree_->Branch("boundary_id", &m_boundary_id_);
40 m_output_tree_->Branch("layer_id", &m_layer_id_);
41 m_output_tree_->Branch("approach_id", &m_approach_id_);
42 m_output_tree_->Branch("sensitive_id", &m_sensitive_id_);
43 m_output_tree_->Branch("g_x", &m_x_);
44 m_output_tree_->Branch("g_y", &m_y_);
45 m_output_tree_->Branch("g_z", &m_z_);
46 m_output_tree_->Branch("d_x", &m_dx_);
47 m_output_tree_->Branch("d_y", &m_dy_);
48 m_output_tree_->Branch("d_z", &m_dz_);
49 m_output_tree_->Branch("type", &m_step_type_);
50 m_output_tree_->Branch("step_acc", &m_step_acc_);
51 m_output_tree_->Branch("step_act", &m_step_act_);
52 m_output_tree_->Branch("step_abt", &m_step_abt_);
53 m_output_tree_->Branch("step_usr", &m_step_usr_);
54 m_output_tree_->Branch("hit_x", &m_hit_x_);
55 m_output_tree_->Branch("hit_y", &m_hit_y_);
56 m_output_tree_->Branch("hit_z", &m_hit_z_);
57 m_output_tree_->Branch("start_pos", &m_start_pos_);
58 m_output_tree_->Branch("start_mom", &m_start_mom_);
59
60} // constructor
61
64 if (m_cfg_.root_file_ == nullptr) {
65 m_output_file_->cd();
66 m_output_tree_->Write();
67 m_output_file_->Close();
68 }
69} // destructor
70
71bool PropagatorStepWriter::writeSteps(
72 framework::Event& event,
73 const std::vector<PropagationSteps>& stepCollection,
74 const std::vector<ldmx::Measurement>& measurements,
75 const Acts::Vector3& start_pos, const Acts::Vector3& start_mom) {
76 // Exclusive access to the tree while writing
77 std::lock_guard<std::mutex> lock(m_write_mutex_);
78
79 m_output_file_->cd();
80
81 // we get the event number
82 m_event_nr_ = event.getEventNumber();
83
84 // fill the hits_
85 m_hit_x_.clear();
86 m_hit_y_.clear();
87 m_hit_z_.clear();
88 // m_hit_erru.clear();
89 // m_hit_errv.clear();
90
91 m_start_pos_.clear();
92 m_start_mom_.clear();
93
94 for (auto& meas : measurements) {
95 m_hit_x_.push_back(meas.getGlobalPosition()[0]);
96 m_hit_y_.push_back(meas.getGlobalPosition()[1]);
97 m_hit_z_.push_back(meas.getGlobalPosition()[2]);
98 }
99
100 for (unsigned int i = 0; i < 3; i++) {
101 m_start_pos_.push_back(start_pos(i));
102 m_start_mom_.push_back(start_mom(i));
103 }
104
105 // loop over the step vector of each test propagation in this
106 for (auto& steps : stepCollection) {
107 // clear the vectors for each collection
108 // m_volumeID.clear();
109 m_boundary_id_.clear();
110 m_layer_id_.clear();
111 m_approach_id_.clear();
112 m_sensitive_id_.clear();
113 m_x_.clear();
114 m_y_.clear();
115 m_z_.clear();
116 m_dx_.clear();
117 m_dy_.clear();
118 m_dz_.clear();
119 m_step_type_.clear();
120 m_step_acc_.clear();
121 m_step_act_.clear();
122 m_step_abt_.clear();
123 m_step_usr_.clear();
124
125 // loop over single steps
126 for (auto& step : steps) {
127 // the identification of the step
128 // Acts::GeometryIdentifier::Value volumeID = 0;
129 Acts::GeometryIdentifier::Value boundary_id = 0;
130 Acts::GeometryIdentifier::Value layer_id = 0;
131 Acts::GeometryIdentifier::Value approach_id = 0;
132 Acts::GeometryIdentifier::Value sensitive_id = 0;
133 // get the identification from the surface first
134 if (step.surface) {
135 auto geo_id = step.surface->geometryId();
136 // volumeID = geoID.volume();
137 boundary_id = geo_id.boundary();
138 layer_id = geo_id.layer();
139 approach_id = geo_id.approach();
140 sensitive_id = geo_id.sensitive();
141 }
142 // a current volume overwrites the surface tagged one
143 // mg ... v36 Step does not include volume
144 // if (step.volume) {
145 // volumeID = step.volume->geometryId().volume();
146 // }
147 // now fill
148 m_sensitive_id_.push_back(sensitive_id);
149 m_approach_id_.push_back(approach_id);
150 m_layer_id_.push_back(layer_id);
151 m_boundary_id_.push_back(boundary_id);
152 // m_volumeID.push_back(volumeID);
153
154 // kinematic information
155 m_x_.push_back(step.position.x());
156 m_y_.push_back(step.position.y());
157 m_z_.push_back(step.position.z());
158 auto direction = step.momentum.normalized();
159 m_dx_.push_back(direction.x());
160 m_dy_.push_back(direction.y());
161 m_dz_.push_back(direction.z());
162
163 // double accuracy =
164 // step.stepSize.value(Acts::ConstrainedStep::accuracy());
165 double accuracy = step.stepSize.accuracy();
166 double actor = step.stepSize.value(Acts::ConstrainedStep::Type::Actor);
167 double aborter =
168 step.stepSize.value(Acts::ConstrainedStep::Type::Navigator);
169 double user = step.stepSize.value(Acts::ConstrainedStep::Type::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_output_tree_->Fill();
193 }
194 return true;
195}
196} // namespace sim
197} // namespace tracking
Implements an event buffer system for storing event data.
Definition Event.h:40
std::vector< float > m_dx_
global direction x_
PropagatorStepWriter(const Config &cfg)
Constructor with.
std::vector< float > m_x_
global x_
std::vector< float > m_start_mom_
start momentum of the particle propagated
std::vector< float > m_start_pos_
start position of the particle propagated
TFile * m_output_file_
the output file name
std::vector< float > m_hit_x_
hit location X
std::vector< float > m_z_
global z_
std::vector< float > m_y_
global y_
std::mutex m_write_mutex_
protect multi-threaded writes
std::vector< float > m_step_abt_
aborter
std::vector< float > m_hit_y_
hit location Y
std::vector< float > m_hit_z_
hit location Z
std::vector< int > m_approach_id_
surface identifier
Config m_cfg_
the configuration object
std::vector< int > m_sensitive_id_
surface identifier
std::vector< float > m_dz_
global direction z_
std::vector< float > m_dy_
global direction y_
std::vector< float > m_step_act_
actor check
std::vector< int > m_boundary_id_
boundary identifier
std::vector< int > m_layer_id_
layer identifier if
std::vector< float > m_step_acc_
accuracy
std::vector< int > m_step_type_
step type
The measurement calibrator can be a function or a class/struct able to retrieve the sim hits containe...
std::string tree_name_
name of the output tree
std::string file_path_
path of the output file