Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminhuth committed Jan 29, 2025
1 parent 4d37131 commit 0cc45df
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ std::shared_ptr<TrackFitterFunction> makeKalmanFitterFunction(
double reverseFilteringMomThreshold = 0.0,
Acts::FreeToBoundCorrection freeToBoundCorrection =
Acts::FreeToBoundCorrection(),
double chi2Cut = std::numeric_limits<double>::infinity(),
const Acts::Logger& logger = *Acts::getDefaultLogger("Kalman",
Acts::Logging::INFO));

Expand Down
60 changes: 59 additions & 1 deletion Examples/Algorithms/TrackFitting/src/KalmanFitterFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,60 @@ struct SimpleReverseFilteringLogic {
}
};

template <typename PredictedPars, typename PredictedCov>
double calculateChi2(const double* fullCalibrated,
const double* fullCalibratedCovariance,
PredictedPars predicted, PredictedCov predictedCovariance,
Acts::BoundSubspaceIndices projector,
unsigned int calibratedSize) {
using namespace Acts;
return visit_measurement(
calibratedSize,
[&fullCalibrated, &fullCalibratedCovariance, &predicted,
&predictedCovariance, &projector](auto N) -> double {
constexpr std::size_t kMeasurementSize = decltype(N)::value;

typename TrackStateTraits<kMeasurementSize, true>::Calibrated
calibrated{fullCalibrated};

typename TrackStateTraits<kMeasurementSize, true>::CalibratedCovariance
calibratedCovariance{fullCalibratedCovariance};

using ParametersVector = ActsVector<kMeasurementSize>;

std::span<std::uint8_t, kMeasurementSize> validSubspaceIndices(
projector.begin(), projector.begin() + kMeasurementSize);
FixedBoundSubspaceHelper<kMeasurementSize> subspaceHelper(
validSubspaceIndices);

// Get the residuals
ParametersVector res =
calibrated - subspaceHelper.projectVector(predicted);

// Get the chi2
return (res.transpose() *
(calibratedCovariance +
subspaceHelper.projectMatrix(predictedCovariance))
.inverse() *
res)
.eval()(0, 0);
});
}

struct SimpleOutlierFinder {
double chi2Cut = std::numeric_limits<double>::infinity();

bool isOutlier(
Acts::VectorMultiTrajectory::ConstTrackStateProxy trackState) const {
double chi2 = calculateChi2(
trackState.effectiveCalibrated().data(),
trackState.effectiveCalibratedCovariance().data(),
trackState.predicted(), trackState.predictedCovariance(),
trackState.projectorSubspaceIndices(), trackState.calibratedSize());
return chi2 > chi2Cut;
}
};

using namespace ActsExamples;

struct KalmanFitterFunctionImpl final : public TrackFitterFunction {
Expand All @@ -76,6 +130,7 @@ struct KalmanFitterFunctionImpl final : public TrackFitterFunction {
Acts::GainMatrixUpdater kfUpdater;
Acts::GainMatrixSmoother kfSmoother;
SimpleReverseFilteringLogic reverseFilteringLogic;
SimpleOutlierFinder outlierFinder;

bool multipleScattering = false;
bool energyLoss = false;
Expand All @@ -102,6 +157,8 @@ struct KalmanFitterFunctionImpl final : public TrackFitterFunction {
extensions.reverseFilteringLogic
.connect<&SimpleReverseFilteringLogic::doBackwardFiltering>(
&reverseFilteringLogic);
extensions.outlierFinder.connect<&SimpleOutlierFinder::isOutlier>(
&outlierFinder);

Acts::KalmanFitterOptions<Acts::VectorMultiTrajectory> kfOptions(
options.geoContext, options.magFieldContext, options.calibrationContext,
Expand Down Expand Up @@ -159,7 +216,7 @@ ActsExamples::makeKalmanFitterFunction(
std::shared_ptr<const Acts::MagneticFieldProvider> magneticField,
bool multipleScattering, bool energyLoss,
double reverseFilteringMomThreshold,
Acts::FreeToBoundCorrection freeToBoundCorrection,
Acts::FreeToBoundCorrection freeToBoundCorrection, double chi2Cut,
const Acts::Logger& logger) {
// Stepper should be copied into the fitters
const Stepper stepper(std::move(magneticField));
Expand Down Expand Up @@ -191,6 +248,7 @@ ActsExamples::makeKalmanFitterFunction(
fitterFunction->reverseFilteringLogic.momentumThreshold =
reverseFilteringMomThreshold;
fitterFunction->freeToBoundCorrection = freeToBoundCorrection;
fitterFunction->outlierFinder.chi2Cut = chi2Cut;

return fitterFunction;
}
1 change: 1 addition & 0 deletions Examples/Python/python/acts/examples/reconstruction.py
Original file line number Diff line number Diff line change
Expand Up @@ -1304,6 +1304,7 @@ def addKalmanTracks(
"reverseFilteringMomThreshold": reverseFilteringMomThreshold,
"freeToBoundCorrection": acts.examples.FreeToBoundCorrection(False),
"level": customLogLevel(),
"chi2Cut": float("inf"),
}

fitAlg = acts.examples.TrackFittingAlgorithm(
Expand Down
6 changes: 3 additions & 3 deletions Examples/Python/src/TrackFitting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,17 @@ void addTrackFitting(Context& ctx) {
std::shared_ptr<const Acts::MagneticFieldProvider> magneticField,
bool multipleScattering, bool energyLoss,
double reverseFilteringMomThreshold,
Acts::FreeToBoundCorrection freeToBoundCorrection,
Acts::FreeToBoundCorrection freeToBoundCorrection, double chi2Cut,
Logging::Level level) {
return ActsExamples::makeKalmanFitterFunction(
trackingGeometry, magneticField, multipleScattering, energyLoss,
reverseFilteringMomThreshold, freeToBoundCorrection,
reverseFilteringMomThreshold, freeToBoundCorrection, chi2Cut,
*Acts::getDefaultLogger("Kalman", level));
},
py::arg("trackingGeometry"), py::arg("magneticField"),
py::arg("multipleScattering"), py::arg("energyLoss"),
py::arg("reverseFilteringMomThreshold"),
py::arg("freeToBoundCorrection"), py::arg("level"));
py::arg("freeToBoundCorrection"), py::arg("chi2Cut"), py::arg("level"));

py::class_<MeasurementCalibrator, std::shared_ptr<MeasurementCalibrator>>(
mex, "MeasurementCalibrator");
Expand Down
1 change: 1 addition & 0 deletions Examples/Scripts/Python/truth_tracking_kalman_refitting.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def runRefittingKf(
"reverseFilteringMomThreshold": reverseFilteringMomThreshold,
"freeToBoundCorrection": acts.examples.FreeToBoundCorrection(False),
"level": acts.logging.INFO,
"chi2Cut": float("inf"),
}

s.addAlgorithm(
Expand Down

0 comments on commit 0cc45df

Please sign in to comment.