LDMX Software
TrackExtrapolatorTool.h
1#pragma once
2
3#include <iostream>
4#include <iterator>
5#include <optional>
6
7#include "Acts/Definitions/TrackParametrization.hpp"
8#include "Acts/EventData/TrackContainer.hpp"
9#include "Acts/EventData/TrackProxy.hpp"
10#include "Acts/Geometry/GeometryContext.hpp"
11#include "Acts/MagneticField/MagneticFieldContext.hpp"
12#include "Acts/Propagator/AbortList.hpp"
13#include "Acts/Propagator/ActionList.hpp"
14#include "Acts/Propagator/MaterialInteractor.hpp"
15#include "Acts/Propagator/Propagator.hpp"
16#include "Acts/Propagator/detail/SteppingLogger.hpp"
17#include "Tracking/Event/Track.h"
18#include "Tracking/Sim/TrackingUtils.h"
19
20using ActionList =
21 Acts::ActionList<Acts::detail::SteppingLogger, Acts::MaterialInteractor>;
22using AbortList = Acts::AbortList<Acts::EndOfWorldReached>;
23
24namespace tracking {
25namespace reco {
26
27template <class propagator_t>
29 public:
30 // The geometry context should be already in the propagator options...
31 TrackExtrapolatorTool(propagator_t propagator,
32 const Acts::GeometryContext& gctx,
33 const Acts::MagneticFieldContext& mctx)
34 : propagator_(std::move(propagator)) {
35 gctx_ = gctx;
36 mctx_ = mctx;
37 }
38
44 void setDebug(bool debug) { debug_ = debug; }
45 void setMaxStepSize(double step) { max_step_size_ = step; }
46 void setPathLimit(double limit) { path_limit_ = limit; }
47
57 typename propagator_t::template Options<ActionList, AbortList>;
58
59 std::optional<Acts::BoundTrackParameters> extrapolate(
60 const Acts::BoundTrackParameters pars,
61 const std::shared_ptr<Acts::Surface>& target_surface) {
62 auto intersection = target_surface->intersect(gctx_, pars.position(gctx_),
63 pars.direction());
64
65 PropagatorOptions p_options(gctx_, mctx_);
66 if (max_step_size_ > 0) p_options.stepping.maxStepSize = max_step_size_;
67 if (path_limit_ > 0) p_options.pathLimit = path_limit_;
68
69 p_options.direction = intersection.intersections()[0].pathLength() >= 0
70 ? Acts::Direction::Forward
71 : Acts::Direction::Backward;
72
73 auto result = propagator_.propagate(pars, *target_surface, p_options);
74
75 // CHECK THE EXTRAPOLATION COVARIANCE MATRIX
76
77 if (debug_) {
78 if (result.ok()) {
79 std::cout << "INITIAL COV MATRIX\n";
80 std::cout << (*(pars.covariance())) << std::endl;
81
82 std::cout << "FINAL COV MATRIX\n";
83 auto opt_pars = *result->endParameters;
84 std::cout << *(opt_pars.covariance()) << std::endl;
85 }
86 }
87
88 if (result.ok())
89 return *result->endParameters;
90 else
91 return std::nullopt;
92 } // end of extrapolate()
93
104 template <class track_t>
105 std::optional<Acts::BoundTrackParameters> extrapolate(
106 track_t track, const std::shared_ptr<Acts::Surface>& target_surface) {
107 if (debug_) {
108 std::cout << "[TrackExtrapolatorTool] extrapolate START\n";
109 std::cout << "[TrackExtrapolatorTool] track.nTrackStates() = "
110 << track.nTrackStates() << std::endl;
111 std::cout << "[TrackExtrapolatorTool] target_surface = "
112 << target_surface.get() << std::endl;
113 }
114
115 // get first and last track state on surface
116 if (debug_)
117 std::cout
118 << "[TrackExtrapolatorTool] Getting outermost track state...\n";
119 auto outermost = *(track.trackStatesReversed().begin());
120 if (debug_)
121 std::cout
122 << "[TrackExtrapolatorTool] Getting innermost track state...\n";
123 auto begin = track.trackStatesReversed().begin();
124 std::advance(begin, track.nTrackStates() - 1);
125 auto innermost = *begin;
126 if (debug_)
127 std::cout << "[TrackExtrapolatorTool] Got innermost and outermost "
128 "track states\n";
129
130 // I'm checking which track state is closer to the origin of the target
131 // surface to decide from where to start the extrapolation to the surface. I
132 // use the coordinate along the beam axis.
133 if (debug_)
134 std::cout << "[TrackExtrapolatorTool] Calculating distances...\n";
135 double first_dis = std::abs(
136 innermost.referenceSurface().transform(gctx_).translation()(0) -
137 target_surface->transform(gctx_).translation()(0));
138
139 double last_dis = std::abs(
140 outermost.referenceSurface().transform(gctx_).translation()(0) -
141 target_surface->transform(gctx_).translation()(0));
142 if (debug_)
143 std::cout << "[TrackExtrapolatorTool] first_dis = " << first_dis
144 << ", last_dis = " << last_dis << std::endl;
145
146 // This is the track state to use for the extrapolation
147
148 const auto& ts = first_dis < last_dis ? innermost : outermost;
149 if (debug_) std::cout << "[TrackExtrapolatorTool] Selected track state\n";
150
151 // Get the BoundTrackStateParameters
152
153 if (debug_)
154 std::cout << "[TrackExtrapolatorTool] Getting reference surface...\n";
155 const auto& surface = ts.referenceSurface();
156 if (debug_)
157 std::cout << "[TrackExtrapolatorTool] Checking hasSmoothed...\n";
158 bool has_smoothed = ts.hasSmoothed();
159 if (debug_)
160 std::cout << "[TrackExtrapolatorTool] has_smoothed = " << has_smoothed
161 << std::endl;
162
163 // Use smoothed parameters if available, otherwise use filtered
164 Acts::BoundVector params;
165 Acts::BoundMatrix cov;
166
167 if (has_smoothed) {
168 if (debug_)
169 std::cout << "[TrackExtrapolatorTool] Using smoothed parameters...\n";
170 params = ts.smoothed();
171 cov = ts.smoothedCovariance();
172 } else {
173 if (debug_)
174 std::cout << "[TrackExtrapolatorTool] Using filtered parameters...\n";
175 params = ts.filtered();
176 cov = ts.filteredCovariance();
177 }
178 if (debug_)
179 std::cout << "[TrackExtrapolatorTool] Got all track state components\n";
180
181 if (debug_) {
182 std::cout << "Surface::" << surface.transform(gctx_).translation()
183 << std::endl;
184 std::cout << "HasSmoothed::" << has_smoothed << std::endl;
185 std::cout << "Parameters::" << params.transpose() << std::endl;
186 }
187 // mg Aug 2024 ... v36 takes the particle...assume electron
188 if (debug_)
189 std::cout
190 << "[TrackExtrapolatorTool] Creating BoundTrackParameters...\n";
191 auto part_hypo{Acts::SinglyChargedParticleHypothesis::electron()};
192 Acts::BoundTrackParameters sp(surface.getSharedPtr(), params, cov,
193 part_hypo);
194 if (debug_)
195 std::cout << "[TrackExtrapolatorTool] BoundTrackParameters created, "
196 "calling extrapolate(BTP)...\n";
197 auto result = extrapolate(sp, target_surface);
198 if (debug_) std::cout << "[TrackExtrapolatorTool] extrapolate DONE\n";
199 return result;
200 }
201
211 template <class track_t>
212 std::optional<Acts::BoundTrackParameters> extrapolateToEcal(
213 track_t track, const std::shared_ptr<Acts::Surface>& target_surface) {
214 // get last track state on the track.
215 // Now.. I'm taking whatever it is. I'm not checking here if it is a
216 // measurement.
217
218 auto& tsc = track.container().trackStateContainer();
219 auto begin = track.trackStates().begin();
220 auto ts_last = *begin;
221 const auto& surface = (ts_last).referenceSurface();
222 const auto& smoothed = (ts_last).smoothed();
223 const auto& cov = (ts_last).smoothedCovariance();
224
225 // Get the BoundTrackStateParameters
226 // assume electron for now
227 auto part_hypo{Acts::SinglyChargedParticleHypothesis::electron()};
228
229 Acts::BoundTrackParameters state_parameters(surface.getSharedPtr(),
230 smoothed, cov, part_hypo);
231
232 // One can also use directly the extrapolate method
233 PropagatorOptions p_options(gctx_, mctx_);
234 auto result =
235 propagator_.propagate(state_parameters, *target_surface, p_options);
236
237 if (result.ok())
238 return *result->endParameters;
239 else
240 return std::nullopt;
241 }
242
253 template <class track_t>
254 bool trackStateAtSurface(track_t track,
255 const std::shared_ptr<Acts::Surface>& target_surface,
257 ldmx::TrackStateType type) {
258 if (debug_) {
259 std::cout << "[TrackExtrapolatorTool] trackStateAtSurface START\n";
260 std::cout << "[TrackExtrapolatorTool] target_surface = "
261 << target_surface.get() << std::endl;
262 std::cout << "[TrackExtrapolatorTool] track.nTrackStates() = "
263 << track.nTrackStates() << std::endl;
264 std::cout << "[TrackExtrapolatorTool] TrackStateType = "
265 << static_cast<int>(type) << std::endl;
266 std::cout << "[TrackExtrapolatorTool] About to call extrapolate...\n";
267 }
268
269 auto opt_pars = extrapolate(track, target_surface);
270
271 if (debug_) {
272 std::cout << "[TrackExtrapolatorTool] extrapolate returned, "
273 "opt_pars.has_value() = "
274 << opt_pars.has_value() << std::endl;
275 }
276
277 if (opt_pars) {
278 if (debug_) {
279 Acts::Vector3 surf_loc = target_surface->transform(gctx_).translation();
280 std::cout << "[TrackExtrapolatorTool] Surface location: ("
281 << surf_loc(0) << ", " << surf_loc(1) << ", " << surf_loc(2)
282 << ")\n";
283 }
284
285 ts = tracking::sim::utils::makeTrackState(gctx_, *opt_pars, type);
286 if (debug_)
287 std::cout << "[TrackExtrapolatorTool] trackStateAtSurface SUCCESS\n";
288 return true;
289 } else {
290 if (debug_)
291 std::cout << "[TrackExtrapolatorTool] trackStateAtSurface FAILED - "
292 "opt_pars is empty\n";
293 return false;
294 }
295 }
296
297 private:
298 propagator_t propagator_;
299 Acts::GeometryContext gctx_;
300 Acts::MagneticFieldContext mctx_;
301 bool debug_{false};
302 double max_step_size_{-1};
303 double path_limit_{-1};
304};
305
306} // namespace reco
307} // namespace tracking
typename propagator_t::template Options< ActionList, AbortList > PropagatorOptions
Method to extrapolate to a target surface given a set of BoundTrackParameters.
std::optional< Acts::BoundTrackParameters > extrapolate(track_t track, const std::shared_ptr< Acts::Surface > &target_surface)
Method to extrapolate to a target surface given a track The method computes which track state is clos...
std::optional< Acts::BoundTrackParameters > extrapolateToEcal(track_t track, const std::shared_ptr< Acts::Surface > &target_surface)
Create an ldmx::TrackState to the extrapolated position.
bool trackStateAtSurface(track_t track, const std::shared_ptr< Acts::Surface > &target_surface, ldmx::Track::TrackState &ts, ldmx::TrackStateType type)
Create an ldmx::TrackState to the extrapolated position.
void setDebug(bool debug)
Turn on/off internal debug flag.
The measurement calibrator can be a function or a class/struct able to retrieve the sim hits containe...