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_output_file_(cfg.root_file_) {
20 if (m_cfg_.tree_name_.empty()) {
21 throw std::invalid_argument("Missing tree name");
22 }
23
24 // Setup ROOT I/O
25 if (m_output_file_ == nullptr) {
27 TFile::Open(m_cfg_.file_path_.c_str(), m_cfg_.file_mode_.c_str());
28 if (m_output_file_ == nullptr) {
29 throw std::ios_base::failure("Could not open '" + m_cfg_.file_path_);
30 }
31 }
32 m_output_file_->cd();
33
34 m_output_tree_ = new TTree(m_cfg_.tree_name_.c_str(),
35 "TTree from RootPropagationStepsWriter");
36 if (m_output_tree_ == nullptr) throw std::bad_alloc();
37
38 // Set the branches
39 m_output_tree_->Branch("event_nr", &m_event_nr_);
40 // m_outputTree->Branch("volume_id", &m_volumeID);
41 m_output_tree_->Branch("boundary_id", &m_boundary_id_);
42 m_output_tree_->Branch("layer_id", &m_layer_id_);
43 m_output_tree_->Branch("approach_id", &m_approach_id_);
44 m_output_tree_->Branch("sensitive_id", &m_sensitive_id_);
45 m_output_tree_->Branch("g_x", &m_x_);
46 m_output_tree_->Branch("g_y", &m_y_);
47 m_output_tree_->Branch("g_z", &m_z_);
48 m_output_tree_->Branch("d_x", &m_dx_);
49 m_output_tree_->Branch("d_y", &m_dy_);
50 m_output_tree_->Branch("d_z", &m_dz_);
51 m_output_tree_->Branch("type", &m_step_type_);
52 m_output_tree_->Branch("step_acc", &m_step_acc_);
53 m_output_tree_->Branch("step_act", &m_step_act_);
54 m_output_tree_->Branch("step_abt", &m_step_abt_);
55 m_output_tree_->Branch("step_usr", &m_step_usr_);
56 m_output_tree_->Branch("hit_x", &m_hit_x_);
57 m_output_tree_->Branch("hit_y", &m_hit_y_);
58 m_output_tree_->Branch("hit_z", &m_hit_z_);
59 m_output_tree_->Branch("start_pos", &m_start_pos_);
60 m_output_tree_->Branch("start_mom", &m_start_mom_);
61
62} // constructor
63
66 if (m_cfg_.root_file_ == nullptr) {
67 m_output_file_->cd();
68 m_output_tree_->Write();
69 m_output_file_->Close();
70 }
71} // destructor
72
73bool PropagatorStepWriter::writeSteps(
74 framework::Event& event,
75 const std::vector<PropagationSteps>& stepCollection,
76 const std::vector<ldmx::Measurement>& measurements,
77 const Acts::Vector3& start_pos, const Acts::Vector3& start_mom) {
78 // Exclusive access to the tree while writing
79 std::lock_guard<std::mutex> lock(m_write_mutex_);
80
81 m_output_file_->cd();
82
83 // we get the event number
84 m_event_nr_ = event.getEventNumber();
85
86 // fill the hits_
87 m_hit_x_.clear();
88 m_hit_y_.clear();
89 m_hit_z_.clear();
90 // m_hit_erru.clear();
91 // m_hit_errv.clear();
92
93 m_start_pos_.clear();
94 m_start_mom_.clear();
95
96 for (auto& meas : measurements) {
97 m_hit_x_.push_back(meas.getGlobalPosition()[0]);
98 m_hit_y_.push_back(meas.getGlobalPosition()[1]);
99 m_hit_z_.push_back(meas.getGlobalPosition()[2]);
100 }
101
102 for (unsigned int i = 0; i < 3; i++) {
103 m_start_pos_.push_back(start_pos(i));
104 m_start_mom_.push_back(start_mom(i));
105 }
106
107 // loop over the step vector of each test propagation in this
108 for (auto& steps : stepCollection) {
109 // clear the vectors for each collection
110 // m_volumeID.clear();
111 m_boundary_id_.clear();
112 m_layer_id_.clear();
113 m_approach_id_.clear();
114 m_sensitive_id_.clear();
115 m_x_.clear();
116 m_y_.clear();
117 m_z_.clear();
118 m_dx_.clear();
119 m_dy_.clear();
120 m_dz_.clear();
121 m_step_type_.clear();
122 m_step_acc_.clear();
123 m_step_act_.clear();
124 m_step_abt_.clear();
125 m_step_usr_.clear();
126
127 // loop over single steps
128 for (auto& step : steps) {
129 // the identification of the step
130 // Acts::GeometryIdentifier::Value volumeID = 0;
131 Acts::GeometryIdentifier::Value boundary_id = 0;
132 Acts::GeometryIdentifier::Value layer_id = 0;
133 Acts::GeometryIdentifier::Value approach_id = 0;
134 Acts::GeometryIdentifier::Value sensitive_id = 0;
135 // get the identification from the surface first
136 if (step.surface) {
137 auto geo_id = step.surface->geometryId();
138 // volumeID = geoID.volume();
139 boundary_id = geo_id.boundary();
140 layer_id = geo_id.layer();
141 approach_id = geo_id.approach();
142 sensitive_id = geo_id.sensitive();
143 }
144 // a current volume overwrites the surface tagged one
145 // mg ... v36 Step does not include volume
146 // if (step.volume) {
147 // volumeID = step.volume->geometryId().volume();
148 // }
149 // now fill
150 m_sensitive_id_.push_back(sensitive_id);
151 m_approach_id_.push_back(approach_id);
152 m_layer_id_.push_back(layer_id);
153 m_boundary_id_.push_back(boundary_id);
154 // m_volumeID.push_back(volumeID);
155
156 // kinematic information
157 m_x_.push_back(step.position.x());
158 m_y_.push_back(step.position.y());
159 m_z_.push_back(step.position.z());
160 auto direction = step.momentum.normalized();
161 m_dx_.push_back(direction.x());
162 m_dy_.push_back(direction.y());
163 m_dz_.push_back(direction.z());
164
165 // double accuracy =
166 // step.stepSize.value(Acts::ConstrainedStep::accuracy());
167 double accuracy = step.stepSize.accuracy();
168 double actor = step.stepSize.value(Acts::ConstrainedStep::actor);
169 double aborter = step.stepSize.value(Acts::ConstrainedStep::aborter);
170 double user = step.stepSize.value(Acts::ConstrainedStep::user);
171 double act2 = actor * actor;
172 double acc2 = accuracy * accuracy;
173 double abo2 = aborter * aborter;
174 double usr2 = user * user;
175
176 // todo - fold with direction
177 if (act2 < acc2 && act2 < abo2 && act2 < usr2) {
178 m_step_type_.push_back(0);
179 } else if (acc2 < abo2 && acc2 < usr2) {
180 m_step_type_.push_back(1);
181 } else if (abo2 < usr2) {
182 m_step_type_.push_back(2);
183 } else {
184 m_step_type_.push_back(3);
185 }
186
187 // step size information
188 m_step_acc_.push_back(accuracy);
189 m_step_act_.push_back(actor);
190 m_step_abt_.push_back(aborter);
191 m_step_usr_.push_back(user);
192 }
193 m_output_tree_->Fill();
194 }
195 return true;
196}
197} // namespace sim
198} // namespace tracking
Implements an event buffer system for storing event data.
Definition Event.h:42
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