Skip to content

Commit e1949a9

Browse files
Merge branch 'main' into feature/bind-measurement-maps
2 parents 56a3656 + 9b0d2c4 commit e1949a9

9 files changed

Lines changed: 943 additions & 13 deletions

File tree

Core/include/Acts/TrackFitting/KalmanFitter.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ class KalmanFitter {
288288
/// Broadcast the result_type
289289
using result_type = KalmanFitterResult<traj_t>;
290290

291-
/// The target surface aboter
291+
/// The target surface aborter
292292
SurfaceReached targetReached{std::numeric_limits<double>::lowest()};
293293

294294
/// Allows retrieving measurements for a surface

Core/include/Acts/TrackFitting/ReferenceTrajectoryBuilder.hpp

Lines changed: 614 additions & 0 deletions
Large diffs are not rendered by default.

Examples/Algorithms/TrackFitting/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ acts_add_library(
33
src/RefittingCalibrator.cpp
44
src/TrackFittingAlgorithm.cpp
55
src/KalmanFitterFunction.cpp
6+
src/KalmanReferenceTrajectoryFitterFunction.cpp
67
src/RefittingAlgorithm.cpp
78
src/GsfFitterFunction.cpp
89
src/GlobalChiSquareFitterFunction.cpp

Examples/Algorithms/TrackFitting/include/ActsExamples/TrackFitting/TrackFitterFunction.hpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,26 @@ std::shared_ptr<TrackFitterFunction> makeKalmanFitterFunction(
7878
bool multipleScattering = true, bool energyLoss = true,
7979
double reverseFilteringMomThreshold = 0.0,
8080
double reverseFilteringCovarianceScaling = 100.0,
81-
Acts::FreeToBoundCorrection freeToBoundCorrection =
81+
const Acts::FreeToBoundCorrection& freeToBoundCorrection =
8282
Acts::FreeToBoundCorrection(),
8383
double chi2Cut = std::numeric_limits<double>::infinity(),
8484
bool useJosephFormulation = false,
8585
const Acts::Logger& logger = *Acts::getDefaultLogger("Kalman",
8686
Acts::Logging::INFO));
8787

88+
/// Makes a reference trajectory fitter function object using the Kalman Filter
89+
///
90+
std::shared_ptr<TrackFitterFunction>
91+
makeKalmanReferenceTrajectoryFitterFunction(
92+
std::shared_ptr<const Acts::TrackingGeometry> trackingGeometry,
93+
std::shared_ptr<const Acts::MagneticFieldProvider> magneticField,
94+
bool multipleScattering = true, bool energyLoss = true,
95+
const Acts::FreeToBoundCorrection& freeToBoundCorrection =
96+
Acts::FreeToBoundCorrection(),
97+
bool useJosephFormulation = false,
98+
const Acts::Logger& logger = *Acts::getDefaultLogger(
99+
"KalmanReferenceTrajectory", Acts::Logging::INFO));
100+
88101
/// Available algorithms for the mixture reduction
89102
enum class MixtureReductionAlgorithm { weightCut, KLDistance, KLDistanceNaive };
90103

@@ -117,15 +130,15 @@ std::shared_ptr<TrackFitterFunction> makeGsfFitterFunction(
117130
/// @param magneticField the magnetic field for the propagator
118131
/// @param multipleScattering bool
119132
/// @param energyLoss bool
120-
/// @param freeToBoundCorrection bool
133+
/// @param freeToBoundCorrection the correction for free to bound state transformations
121134
/// @param nUpdateMax max number of iterations during the fit
122135
/// @param relChi2changeCutOff Check for convergence (abort condition). Set to 0 to skip.
123136
/// @param logger a logger instance
124137
std::shared_ptr<TrackFitterFunction> makeGlobalChiSquareFitterFunction(
125138
std::shared_ptr<const Acts::TrackingGeometry> trackingGeometry,
126139
std::shared_ptr<const Acts::MagneticFieldProvider> magneticField,
127140
bool multipleScattering = true, bool energyLoss = true,
128-
Acts::FreeToBoundCorrection freeToBoundCorrection =
141+
const Acts::FreeToBoundCorrection& freeToBoundCorrection =
129142
Acts::FreeToBoundCorrection(),
130143
std::size_t nUpdateMax = 5, double relChi2changeCutOff = 1e-7,
131144
const Acts::Logger& logger = *Acts::getDefaultLogger("Gx2f",

Examples/Algorithms/TrackFitting/src/GlobalChiSquareFitterFunction.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,9 @@ ActsExamples::makeGlobalChiSquareFitterFunction(
123123
std::shared_ptr<const Acts::TrackingGeometry> trackingGeometry,
124124
std::shared_ptr<const Acts::MagneticFieldProvider> magneticField,
125125
bool multipleScattering, bool energyLoss,
126-
Acts::FreeToBoundCorrection freeToBoundCorrection, std::size_t nUpdateMax,
127-
double relChi2changeCutOff, const Acts::Logger& logger) {
126+
const Acts::FreeToBoundCorrection& freeToBoundCorrection,
127+
std::size_t nUpdateMax, double relChi2changeCutOff,
128+
const Acts::Logger& logger) {
128129
// Stepper should be copied into the fitters
129130
const Stepper stepper(std::move(magneticField));
130131

Examples/Algorithms/TrackFitting/src/KalmanFitterFunction.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ std::shared_ptr<TrackFitterFunction> ActsExamples::makeKalmanFitterFunction(
165165
bool multipleScattering, bool energyLoss,
166166
double reverseFilteringMomThreshold,
167167
double reverseFilteringCovarianceScaling,
168-
Acts::FreeToBoundCorrection freeToBoundCorrection, double chi2Cut,
168+
const Acts::FreeToBoundCorrection& freeToBoundCorrection, double chi2Cut,
169169
bool useJosephFormulation, const Acts::Logger& logger) {
170170
// Stepper should be copied into the fitters
171171
const Stepper stepper(std::move(magneticField));
Lines changed: 270 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,270 @@
1+
// This file is part of the ACTS project.
2+
//
3+
// Copyright (C) 2016 CERN for the benefit of the ACTS project
4+
//
5+
// This Source Code Form is subject to the terms of the Mozilla Public
6+
// License, v. 2.0. If a copy of the MPL was not distributed with this
7+
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
8+
9+
#include "Acts/EventData/MultiTrajectory.hpp"
10+
#include "Acts/EventData/TrackContainer.hpp"
11+
#include "Acts/EventData/VectorMultiTrajectory.hpp"
12+
#include "Acts/EventData/VectorTrackContainer.hpp"
13+
#include "Acts/EventData/detail/CorrectedTransformationFreeToBound.hpp"
14+
#include "Acts/Geometry/GeometryIdentifier.hpp"
15+
#include "Acts/Propagator/DirectNavigator.hpp"
16+
#include "Acts/Propagator/Navigator.hpp"
17+
#include "Acts/Propagator/Propagator.hpp"
18+
#include "Acts/Propagator/SympyStepper.hpp"
19+
#include "Acts/TrackFitting/GainMatrixUpdater.hpp"
20+
#include "Acts/TrackFitting/MbfSmoother.hpp"
21+
#include "Acts/TrackFitting/ReferenceTrajectoryBuilder.hpp"
22+
#include "Acts/Utilities/Logger.hpp"
23+
#include "Acts/Utilities/TrackHelpers.hpp"
24+
#include "ActsExamples/EventData/IndexSourceLink.hpp"
25+
#include "ActsExamples/EventData/MeasurementCalibration.hpp"
26+
#include "ActsExamples/EventData/Track.hpp"
27+
#include "ActsExamples/TrackFitting/RefittingCalibrator.hpp"
28+
#include "ActsExamples/TrackFitting/TrackFitterFunction.hpp"
29+
30+
#include <memory>
31+
#include <utility>
32+
#include <vector>
33+
34+
namespace {
35+
36+
using Stepper = Acts::SympyStepper;
37+
using Propagator = Acts::Propagator<Stepper, Acts::Navigator>;
38+
using ReferenceTrajectoryBuilder =
39+
Acts::Experimental::ReferenceTrajectoryBuilder<Propagator,
40+
Acts::VectorMultiTrajectory>;
41+
using DirectPropagator = Acts::Propagator<Stepper, Acts::DirectNavigator>;
42+
using DirectReferenceTrajectoryBuilder =
43+
Acts::Experimental::ReferenceTrajectoryBuilder<DirectPropagator,
44+
Acts::VectorMultiTrajectory>;
45+
46+
using TrackContainer =
47+
Acts::TrackContainer<Acts::VectorTrackContainer,
48+
Acts::VectorMultiTrajectory, std::shared_ptr>;
49+
50+
using namespace ActsExamples;
51+
52+
struct KalmanReferenceTrajectoryFitterFunctionImpl final
53+
: public TrackFitterFunction {
54+
Propagator extrapolator;
55+
56+
ReferenceTrajectoryBuilder referenceTrajectoryBuilder;
57+
DirectReferenceTrajectoryBuilder directReferenceTrajectoryBuilder;
58+
59+
Acts::GainMatrixUpdater kfUpdater;
60+
Acts::MbfSmoother kfSmoother;
61+
62+
bool multipleScattering = false;
63+
bool energyLoss = false;
64+
Acts::FreeToBoundCorrection freeToBoundCorrection;
65+
66+
IndexSourceLink::SurfaceAccessor slSurfaceAccessor;
67+
68+
std::unique_ptr<const Acts::Logger> m_smootherLogger;
69+
std::unique_ptr<const Acts::Logger> m_extrapolationLogger;
70+
71+
KalmanReferenceTrajectoryFitterFunctionImpl(
72+
Propagator&& ext, ReferenceTrajectoryBuilder&& rtBuilder,
73+
DirectReferenceTrajectoryBuilder&& dRtBuilder,
74+
const Acts::TrackingGeometry& trkGeo, const Acts::Logger& logger)
75+
: extrapolator(std::move(ext)),
76+
referenceTrajectoryBuilder(std::move(rtBuilder)),
77+
directReferenceTrajectoryBuilder(std::move(dRtBuilder)),
78+
slSurfaceAccessor{trkGeo} {
79+
m_smootherLogger = logger.cloneWithSuffix("Smoother");
80+
m_extrapolationLogger = logger.cloneWithSuffix("Extrapolation");
81+
}
82+
83+
auto makeReferenceTrajectoryBuilderOptions(
84+
const GeneralFitterOptions& options) const {
85+
Acts::Experimental::ReferenceTrajectoryBuilderOptions<
86+
Acts::VectorMultiTrajectory>
87+
rtOptions(options.geoContext, options.magFieldContext,
88+
options.propOptions, nullptr);
89+
90+
rtOptions.multipleScattering = multipleScattering;
91+
rtOptions.energyLoss = energyLoss;
92+
rtOptions.freeToBoundCorrection = freeToBoundCorrection;
93+
94+
return rtOptions;
95+
}
96+
97+
TrackFitterResult operator()(const std::vector<Acts::SourceLink>& sourceLinks,
98+
const TrackParameters& initialParameters,
99+
const GeneralFitterOptions& options,
100+
const MeasurementCalibratorAdapter& calibrator,
101+
TrackContainer& tracks) const override {
102+
const auto referenceTrajectoryBuilderOptions =
103+
makeReferenceTrajectoryBuilderOptions(options);
104+
105+
Acts::SourceLinkSurfaceAccessor sourceLinkAccessorDelegate;
106+
sourceLinkAccessorDelegate
107+
.connect<&IndexSourceLink::SurfaceAccessor::operator()>(
108+
&slSurfaceAccessor);
109+
110+
ReferenceTrajectoryBuilder::Calibrator calibratorDelegate;
111+
calibratorDelegate.connect<&MeasurementCalibratorAdapter::calibrate>(
112+
&calibrator);
113+
114+
ReferenceTrajectoryBuilder::Updater updaterDelegate;
115+
updaterDelegate.connect<
116+
&Acts::GainMatrixUpdater::operator()<Acts::VectorMultiTrajectory>>(
117+
&kfUpdater);
118+
119+
auto buildResult = referenceTrajectoryBuilder.build(
120+
initialParameters, referenceTrajectoryBuilderOptions, tracks);
121+
if (!buildResult.ok()) {
122+
return buildResult.error();
123+
}
124+
auto track = *buildResult;
125+
126+
referenceTrajectoryBuilder.attachSourceLinks(track, sourceLinks,
127+
sourceLinkAccessorDelegate);
128+
129+
referenceTrajectoryBuilder.calibrateMeasurements(options.geoContext,
130+
options.calibrationContext,
131+
track, calibratorDelegate);
132+
133+
auto fitterResult = referenceTrajectoryBuilder.filter(
134+
options.geoContext, track, updaterDelegate);
135+
if (!fitterResult.ok()) {
136+
return fitterResult.error();
137+
}
138+
139+
auto smoothRes =
140+
kfSmoother(options.geoContext, tracks.trackStateContainer(),
141+
track.tipIndex(), *m_smootherLogger);
142+
if (!smoothRes.ok()) {
143+
return smoothRes.error();
144+
}
145+
146+
if (!track.hasReferenceSurface() && options.referenceSurface != nullptr) {
147+
typename Propagator::template Options<> extrapolationOptions(
148+
options.geoContext, options.magFieldContext);
149+
auto extrapolationResult = extrapolateTrackToReferenceSurface(
150+
track, *options.referenceSurface, extrapolator, extrapolationOptions,
151+
Acts::TrackExtrapolationStrategy::first, *m_extrapolationLogger);
152+
153+
if (!extrapolationResult.ok()) {
154+
return extrapolationResult.error();
155+
}
156+
}
157+
158+
Acts::calculateTrackQuantities(track);
159+
160+
return TrackFitterResult::success(track);
161+
}
162+
163+
TrackFitterResult operator()(
164+
const std::vector<Acts::SourceLink>& sourceLinks,
165+
const TrackParameters& initialParameters,
166+
const GeneralFitterOptions& options,
167+
const RefittingCalibrator& calibrator,
168+
const std::vector<const Acts::Surface*>& surfaceSequence,
169+
TrackContainer& tracks) const override {
170+
const auto referenceTrajectoryBuilderOptions =
171+
makeReferenceTrajectoryBuilderOptions(options);
172+
173+
Acts::SourceLinkSurfaceAccessor sourceLinkAccessorDelegate;
174+
sourceLinkAccessorDelegate.connect<&RefittingCalibrator::accessSurface>();
175+
176+
DirectReferenceTrajectoryBuilder::Calibrator calibratorDelegate;
177+
calibratorDelegate.connect<&RefittingCalibrator::calibrate>(&calibrator);
178+
179+
DirectReferenceTrajectoryBuilder::Updater updaterDelegate;
180+
updaterDelegate.connect<
181+
&Acts::GainMatrixUpdater::operator()<Acts::VectorMultiTrajectory>>(
182+
&kfUpdater);
183+
184+
auto buildResult = directReferenceTrajectoryBuilder.build(
185+
initialParameters, referenceTrajectoryBuilderOptions, surfaceSequence,
186+
tracks);
187+
if (!buildResult.ok()) {
188+
return buildResult.error();
189+
}
190+
auto track = *buildResult;
191+
192+
directReferenceTrajectoryBuilder.attachSourceLinks(
193+
track, sourceLinks, sourceLinkAccessorDelegate);
194+
195+
directReferenceTrajectoryBuilder.calibrateMeasurements(
196+
options.geoContext, options.calibrationContext, track,
197+
calibratorDelegate);
198+
199+
directReferenceTrajectoryBuilder.filter(options.geoContext, track,
200+
updaterDelegate);
201+
202+
auto smoothRes = kfSmoother(options.geoContext,
203+
tracks.trackStateContainer(), track.tipIndex());
204+
if (!smoothRes.ok()) {
205+
return smoothRes.error();
206+
}
207+
208+
if (!track.hasReferenceSurface() && options.referenceSurface != nullptr) {
209+
typename Propagator::template Options<> extrapolationOptions(
210+
options.geoContext, options.magFieldContext);
211+
auto extrapolationResult = extrapolateTrackToReferenceSurface(
212+
track, *options.referenceSurface, extrapolator, extrapolationOptions,
213+
Acts::TrackExtrapolationStrategy::first);
214+
215+
if (!extrapolationResult.ok()) {
216+
return extrapolationResult.error();
217+
}
218+
}
219+
220+
Acts::calculateTrackQuantities(track);
221+
222+
return TrackFitterResult::success(track);
223+
}
224+
};
225+
226+
} // namespace
227+
228+
std::shared_ptr<TrackFitterFunction>
229+
ActsExamples::makeKalmanReferenceTrajectoryFitterFunction(
230+
std::shared_ptr<const Acts::TrackingGeometry> trackingGeometry,
231+
std::shared_ptr<const Acts::MagneticFieldProvider> magneticField,
232+
bool multipleScattering, bool energyLoss,
233+
const Acts::FreeToBoundCorrection& freeToBoundCorrection,
234+
bool useJosephFormulation, const Acts::Logger& logger) {
235+
const Stepper stepper(std::move(magneticField));
236+
237+
const auto& geo = *trackingGeometry;
238+
Acts::Navigator::Config cfg{std::move(trackingGeometry)};
239+
cfg.resolvePassive = false;
240+
cfg.resolveMaterial = true;
241+
cfg.resolveSensitive = true;
242+
Acts::Navigator navigator(cfg, logger.cloneWithSuffix("Navigator"));
243+
Propagator propagator(stepper, navigator,
244+
logger.cloneWithSuffix("Propagator"));
245+
ReferenceTrajectoryBuilder referenceTrajectoryBuilder(
246+
std::move(propagator),
247+
logger.cloneWithSuffix("ReferenceTrajectoryBuilder"));
248+
249+
Acts::DirectNavigator directNavigator{
250+
logger.cloneWithSuffix("DirectNavigator")};
251+
DirectPropagator directPropagator(stepper, std::move(directNavigator),
252+
logger.cloneWithSuffix("DirectPropagator"));
253+
DirectReferenceTrajectoryBuilder directReferenceTrajectoryBuilder(
254+
std::move(directPropagator),
255+
logger.cloneWithSuffix("DirectReferenceTrajectoryBuilder"));
256+
257+
Propagator extrapolator(stepper, navigator,
258+
logger.cloneWithSuffix("Extrapolator"));
259+
260+
auto fitterFunction =
261+
std::make_shared<KalmanReferenceTrajectoryFitterFunctionImpl>(
262+
std::move(extrapolator), std::move(referenceTrajectoryBuilder),
263+
std::move(directReferenceTrajectoryBuilder), geo, logger);
264+
fitterFunction->multipleScattering = multipleScattering;
265+
fitterFunction->energyLoss = energyLoss;
266+
fitterFunction->freeToBoundCorrection = freeToBoundCorrection;
267+
fitterFunction->kfUpdater = Acts::GainMatrixUpdater(useJosephFormulation);
268+
269+
return fitterFunction;
270+
}

Python/Examples/python/reconstruction.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1488,6 +1488,7 @@ def addKalmanTracks(
14881488
calibrator: acts.examples.MeasurementCalibrator = acts.examples.makePassThroughCalibrator(),
14891489
linkForward: bool = False,
14901490
useJosephFormulation: bool = False,
1491+
useReferenceTrajectory: bool = False,
14911492
logLevel: Optional[acts.logging.Level] = None,
14921493
) -> None:
14931494
customLogLevel = acts.examples.defaultLogging(s, logLevel)
@@ -1502,6 +1503,21 @@ def addKalmanTracks(
15021503
"chi2Cut": float("inf"),
15031504
"useJosephFormulation": useJosephFormulation,
15041505
}
1506+
fitFunction = acts.examples.makeKalmanFitterFunction(
1507+
trackingGeometry, field, **kalmanOptions
1508+
)
1509+
1510+
if useReferenceTrajectory:
1511+
kalmanOptions = {
1512+
"multipleScattering": multipleScattering,
1513+
"energyLoss": energyLoss,
1514+
"freeToBoundCorrection": acts.examples.FreeToBoundCorrection(False),
1515+
"level": customLogLevel(),
1516+
"useJosephFormulation": useJosephFormulation,
1517+
}
1518+
fitFunction = acts.examples.makeKalmanReferenceTrajectoryFitterFunction(
1519+
trackingGeometry, field, **kalmanOptions
1520+
)
15051521

15061522
fitAlg = acts.examples.TrackFittingAlgorithm(
15071523
level=customLogLevel(),
@@ -1511,9 +1527,7 @@ def addKalmanTracks(
15111527
inputClusters=clusters if clusters is not None else "",
15121528
outputTracks="kf_tracks",
15131529
pickTrack=-1,
1514-
fit=acts.examples.makeKalmanFitterFunction(
1515-
trackingGeometry, field, **kalmanOptions
1516-
),
1530+
fit=fitFunction,
15171531
calibrator=calibrator,
15181532
linkForward=linkForward,
15191533
)

0 commit comments

Comments
 (0)