LDMX Software
TrackExtrapolatorTool.h
1#pragma once
2
3#include <iostream>
4#include <optional>
5
6#include "Acts/Definitions/TrackParametrization.hpp"
7#include "Acts/EventData/ParticleHypothesis.hpp"
8#include "Acts/Geometry/GeometryContext.hpp"
9#include "Acts/MagneticField/MagneticFieldContext.hpp"
10#include "Acts/Propagator/ActorList.hpp"
11#include "Acts/Propagator/MaterialInteractor.hpp"
12#include "Acts/Propagator/detail/SteppingLogger.hpp"
13#include "Acts/Utilities/TrackHelpers.hpp"
14#include "Tracking/Event/Track.h"
15#include "Tracking/Sim/TrackingUtils.h"
16
17using ActionList =
18 Acts::ActorList<Acts::detail::SteppingLogger, Acts::MaterialInteractor,
19 Acts::EndOfWorldReached>;
20
21namespace tracking {
22namespace reco {
23
24template <class propagator_t>
26 public:
27 // The geometry context should be already in the propagator options...
28 TrackExtrapolatorTool(propagator_t propagator,
29 const Acts::GeometryContext& gctx,
30 const Acts::MagneticFieldContext& mctx)
31 : propagator_(std::move(propagator)), gctx_(gctx), mctx_(mctx) {}
32
38 void setDebug(bool debug) { debug_ = debug; }
39 void setMaxStepSize(double step) { max_step_size_ = step; }
40 void setPathLimit(double limit) { path_limit_ = limit; }
41
50 using PropagatorOptions = typename propagator_t::template Options<ActionList>;
51
52 std::optional<Acts::BoundTrackParameters> extrapolate(
53 const Acts::BoundTrackParameters pars,
54 const std::shared_ptr<Acts::Surface>& target_surface) {
55 auto intersection = target_surface->intersect(gctx_, pars.position(gctx_),
56 pars.direction());
57
58 PropagatorOptions p_options(gctx_, mctx_);
59 if (max_step_size_ > 0) p_options.stepping.maxStepSize = max_step_size_;
60 if (path_limit_ > 0) p_options.pathLimit = path_limit_;
61
62 p_options.direction = intersection[0].pathLength() >= 0
63 ? Acts::Direction::Forward()
64 : Acts::Direction::Backward();
65
66 auto result = propagator_.propagate(pars, *target_surface, p_options);
67
68 // CHECK THE EXTRAPOLATION COVARIANCE MATRIX
69
70 if (debug_) {
71 if (result.ok()) {
72 std::cout << "INITIAL COV MATRIX\n";
73 std::cout << (*(pars.covariance())) << std::endl;
74
75 std::cout << "FINAL COV MATRIX\n";
76 auto opt_pars = *result->endParameters;
77 std::cout << *(opt_pars.covariance()) << std::endl;
78 }
79 }
80
81 if (result.ok())
82 return *result->endParameters;
83 else
84 return std::nullopt;
85 } // end of extrapolate()
86
97 template <class track_t>
98 std::optional<Acts::BoundTrackParameters> extrapolate(
99 track_t track, const std::shared_ptr<Acts::Surface>& target_surface) {
100 if (debug_) {
101 std::cout << "[TrackExtrapolatorTool] extrapolate START\n";
102 std::cout << "[TrackExtrapolatorTool] track.nTrackStates() = "
103 << track.nTrackStates() << std::endl;
104 std::cout << "[TrackExtrapolatorTool] target_surface = "
105 << target_surface.get() << std::endl;
106 }
107
108 if (track.nTrackStates() == 0) {
109 return std::nullopt;
110 }
111
112 // Use ACTS's built-in helper to find the measurement track state
113 // (first or last) that is closest to the target surface. This correctly
114 // handles holes and material-only states which lack filtered parameters.
115 auto state_result = Acts::findTrackStateForExtrapolation(
116 gctx_, track, *target_surface,
117 Acts::TrackExtrapolationStrategy::firstOrLast);
118
119 if (!state_result.ok()) {
120 return std::nullopt;
121 }
122
123 const auto& ts = state_result->first;
124 const auto& surface = ts.referenceSurface();
125
126 Acts::BoundVector params;
127 Acts::BoundMatrix cov;
128
129 if (ts.hasSmoothed()) {
130 if (debug_)
131 std::cout << "[TrackExtrapolatorTool] Using smoothed parameters\n";
132 params = ts.smoothed();
133 cov = ts.smoothedCovariance();
134 } else if (ts.hasFiltered()) {
135 if (debug_)
136 std::cout << "[TrackExtrapolatorTool] Using filtered parameters\n";
137 params = ts.filtered();
138 cov = ts.filteredCovariance();
139 } else {
140 return std::nullopt;
141 }
142
143 if (debug_) {
144 std::cout << "Surface::"
145 << surface.localToGlobalTransform(gctx_).translation()
146 << std::endl;
147 std::cout << "HasSmoothed::" << ts.hasSmoothed() << std::endl;
148 std::cout << "Parameters::" << params.transpose() << std::endl;
149 }
150
151 auto part_hypo{Acts::ParticleHypothesis::electron()};
152 Acts::BoundTrackParameters sp(surface.getSharedPtr(), params, cov,
153 part_hypo);
154 if (debug_)
155 std::cout << "[TrackExtrapolatorTool] calling extrapolate(BTP)...\n";
156 auto result = extrapolate(sp, target_surface);
157 if (debug_) std::cout << "[TrackExtrapolatorTool] extrapolate DONE\n";
158 return result;
159 }
160
170 template <class track_t>
171 std::optional<Acts::BoundTrackParameters> extrapolateToEcal(
172 track_t track, const std::shared_ptr<Acts::Surface>& target_surface) {
173 // get last track state on the track.
174 // Now.. I'm taking whatever it is. I'm not checking here if it is a
175 // measurement.
176
177 auto& tsc = track.container().trackStateContainer();
178 auto begin = track.trackStates().begin();
179 auto ts_last = *begin;
180 const auto& surface = (ts_last).referenceSurface();
181 const auto& smoothed = (ts_last).smoothed();
182 const auto& cov = (ts_last).smoothedCovariance();
183
184 // Get the BoundTrackStateParameters
185 // assume electron for now
186 auto part_hypo{Acts::ParticleHypothesis::electron()};
187
188 Acts::BoundTrackParameters state_parameters(surface.getSharedPtr(),
189 smoothed, cov, part_hypo);
190
191 // One can also use directly the extrapolate method
192 PropagatorOptions p_options(gctx_, mctx_);
193 auto result =
194 propagator_.propagate(state_parameters, *target_surface, p_options);
195
196 if (result.ok())
197 return *result->endParameters;
198 else
199 return std::nullopt;
200 }
201
212 template <class track_t>
213 bool trackStateAtSurface(track_t track,
214 const std::shared_ptr<Acts::Surface>& target_surface,
216 ldmx::TrackStateType type) {
217 if (debug_) {
218 std::cout << "[TrackExtrapolatorTool] trackStateAtSurface START\n";
219 std::cout << "[TrackExtrapolatorTool] target_surface = "
220 << target_surface.get() << std::endl;
221 std::cout << "[TrackExtrapolatorTool] track.nTrackStates() = "
222 << track.nTrackStates() << std::endl;
223 std::cout << "[TrackExtrapolatorTool] TrackStateType = "
224 << static_cast<int>(type) << std::endl;
225 std::cout << "[TrackExtrapolatorTool] About to call extrapolate...\n";
226 }
227
228 auto opt_pars = extrapolate(track, target_surface);
229
230 if (debug_) {
231 std::cout << "[TrackExtrapolatorTool] extrapolate returned, "
232 "opt_pars.has_value() = "
233 << opt_pars.has_value() << std::endl;
234 }
235
236 if (opt_pars) {
237 if (debug_) {
238 Acts::Vector3 surf_loc =
239 target_surface->localToGlobalTransform(gctx_).translation();
240 std::cout << "[TrackExtrapolatorTool] Surface location: ("
241 << surf_loc(0) << ", " << surf_loc(1) << ", " << surf_loc(2)
242 << ")\n";
243 }
244
245 ts = tracking::sim::utils::makeTrackState(gctx_, *opt_pars, type);
246 if (debug_)
247 std::cout << "[TrackExtrapolatorTool] trackStateAtSurface SUCCESS\n";
248 return true;
249 } else {
250 if (debug_)
251 std::cout << "[TrackExtrapolatorTool] trackStateAtSurface FAILED - "
252 "opt_pars is empty\n";
253 return false;
254 }
255 }
256
257 private:
258 propagator_t propagator_;
259 Acts::GeometryContext gctx_;
260 Acts::MagneticFieldContext mctx_;
261 bool debug_{false};
262 double max_step_size_{-1};
263 double path_limit_{-1};
264};
265
266} // namespace reco
267} // namespace tracking
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.
typename propagator_t::template Options< ActionList > PropagatorOptions
Method to extrapolate to a target surface given a set of BoundTrackParameters.
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...