From 027fd9fa2ac2c6e20b770be3d747554d3b11b4e5 Mon Sep 17 00:00:00 2001 From: AJPfleger Date: Wed, 11 Oct 2023 15:46:37 +0200 Subject: [PATCH] change concept --- .../Acts/TrackFitting/KalmanFitter.hpp | 46 +++++- .../TrackFitting/detail/FitterHelpers.hpp | 93 ----------- .../detail/KalmanUpdateHelpers.hpp | 147 ++++++++++++------ 3 files changed, 140 insertions(+), 146 deletions(-) delete mode 100644 Core/include/Acts/TrackFitting/detail/FitterHelpers.hpp diff --git a/Core/include/Acts/TrackFitting/KalmanFitter.hpp b/Core/include/Acts/TrackFitting/KalmanFitter.hpp index 49abd6f8403..5e0b8979e41 100644 --- a/Core/include/Acts/TrackFitting/KalmanFitter.hpp +++ b/Core/include/Acts/TrackFitting/KalmanFitter.hpp @@ -32,7 +32,6 @@ #include "Acts/Propagator/StandardAborters.hpp" #include "Acts/Propagator/detail/PointwiseMaterialInteraction.hpp" #include "Acts/TrackFitting/KalmanFitterError.hpp" -#include "Acts/TrackFitting/detail/FitterHelpers.hpp" #include "Acts/TrackFitting/detail/KalmanUpdateHelpers.hpp" #include "Acts/TrackFitting/detail/VoidFitterComponents.hpp" #include "Acts/Utilities/CalibrationContext.hpp" @@ -717,14 +716,45 @@ class KalmanFitter { materialInteractor(surface, state, stepper, navigator, MaterialUpdateStage::PreUpdate); - auto trackStateProxyRes = detail::getTrackStateProxy( - state, stepper, *surface, *result.fittedStates, - Acts::MultiTrajectoryTraits::kInvalid, false, logger(), - freeToBoundCorrection, TrackStatePropMask::All); - if (!trackStateProxyRes.ok()) { - return trackStateProxyRes.error(); + auto fittedStates = *result.fittedStates; + + // Add a TrackState entry multi trajectory. This allocates + // storage for all components, which we will set later. + TrackStatePropMask mask = TrackStatePropMask::All; + size_t currentTrackIndex = fittedStates.addTrackState( + mask, Acts::MultiTrajectoryTraits::kInvalid); + + // now get track state proxy back + typename traj_t::TrackStateProxy trackStateProxy = + fittedStates.getTrackState(currentTrackIndex); + + // Set the trackStateProxy components with the state from the ongoing + // propagation + { + trackStateProxy.setReferenceSurface(surface->getSharedPtr()); + // Bind the transported state to the current surface + auto res = stepper.boundState(state.stepping, *surface, false, + freeToBoundCorrection); + if (!res.ok()) { + // ACTS_ERROR("Propagate to surface " << + // surface.geometryId() + // << " failed: " << + // res.error()); + return res.error(); + } + auto& [boundParams, jacobian, pathLength] = *res; + + // Fill the track state + trackStateProxy.predicted() = std::move(boundParams.parameters()); + if (boundParams.covariance().has_value()) { + trackStateProxy.predictedCovariance() = + std::move(*boundParams.covariance()); + } + + trackStateProxy.jacobian() = std::move(jacobian); + trackStateProxy.pathLength() = std::move(pathLength); } - auto& trackStateProxy = *trackStateProxyRes; + // We have predicted parameters, so calibrate the uncalibrated input // measurement extensions.calibrator(state.geoContext, *calibrationContext, diff --git a/Core/include/Acts/TrackFitting/detail/FitterHelpers.hpp b/Core/include/Acts/TrackFitting/detail/FitterHelpers.hpp deleted file mode 100644 index d846e39db35..00000000000 --- a/Core/include/Acts/TrackFitting/detail/FitterHelpers.hpp +++ /dev/null @@ -1,93 +0,0 @@ -// This file is part of the Acts project. -// -// Copyright (C) 2021-2023 CERN for the benefit of the Acts project -// -// This Source Code Form is subject to the terms of the Mozilla Public -// License, v. 2.0. If a copy of the MPL was not distributed with this -// file, You can obtain one at http://mozilla.org/MPL/2.0/. - -#pragma once - -#include "Acts/EventData/MultiTrajectory.hpp" -#include "Acts/EventData/SourceLink.hpp" -#include "Acts/EventData/detail/CorrectedTransformationFreeToBound.hpp" -#include "Acts/Surfaces/Surface.hpp" -#include "Acts/Utilities/CalibrationContext.hpp" -#include "Acts/Utilities/Result.hpp" - -namespace Acts { -namespace detail { -/// This function creates and returns a track state proxy -/// @tparam propagator_state_t The propagator state type -/// @tparam stepper_t The stepper type -/// @param state The propagator state -/// @param stepper The stepper -/// @param surface The current surface -/// @param fittedStates The Multitrajectory to that we add the state -/// @param lastTrackIndex The parent index for the new state in the MT -/// @param doCovTransport Whether to perform a covariance transport when computing the bound state or not -/// @param freeToBoundCorrection Correction for non-linearity effect during transform from free to bound (only corrected when performing CovTransport) -/// @param getOnLastTrackIndex If set, no new TrackState will be added -template -auto getTrackStateProxy(propagator_state_t &state, const stepper_t &stepper, - const Surface &surface, traj_t &fittedStates, - const size_t lastTrackIndex, bool doCovTransport, - const Logger &logger, - const FreeToBoundCorrection &freeToBoundCorrection = - FreeToBoundCorrection(false), - TrackStatePropMask mask = TrackStatePropMask::All, - bool getOnLastTrackIndex = false) - -> Result { - size_t currentTrackIndex = Acts::MultiTrajectoryTraits::kInvalid; - - if (getOnLastTrackIndex) { - if (lastTrackIndex >= fittedStates.size() || - lastTrackIndex == Acts::MultiTrajectoryTraits::kInvalid) { - ACTS_WARNING("No TrackState with index " - << lastTrackIndex << " found. Add new TrackState."); - currentTrackIndex = fittedStates.addTrackState(mask, lastTrackIndex); - } - currentTrackIndex = lastTrackIndex; - } else { - // Add a full TrackState entry multi trajectory. This allocates storage for - // all components, which we will set later. - currentTrackIndex = fittedStates.addTrackState(mask, lastTrackIndex); - } - - // now get track state proxy back - typename traj_t::TrackStateProxy trackStateProxy = - fittedStates.getTrackState(currentTrackIndex); - - trackStateProxy.setReferenceSurface(surface.getSharedPtr()); - - // Bind the transported state to the current surface - auto res = stepper.boundState(state.stepping, surface, doCovTransport, - freeToBoundCorrection); - if (!res.ok()) { - ACTS_ERROR("Propagate to surface " << surface.geometryId() - << " failed: " << res.error()); - return res.error(); - } - auto &[boundParams, jacobian, pathLength] = *res; - - // Fill the track state - if ((mask & TrackStatePropMask::Predicted) != TrackStatePropMask::None) { - trackStateProxy.predicted() = std::move(boundParams.parameters()); - } - - if (boundParams.covariance().has_value()) { - trackStateProxy.predictedCovariance() = - std::move(*boundParams.covariance()); - } - - if ((mask & TrackStatePropMask::Jacobian) != TrackStatePropMask::None) { - trackStateProxy.jacobian() = std::move(jacobian); - } - - trackStateProxy.pathLength() = std::move(pathLength); - - return trackStateProxy; -} - -} // namespace detail -} // namespace Acts diff --git a/Core/include/Acts/TrackFitting/detail/KalmanUpdateHelpers.hpp b/Core/include/Acts/TrackFitting/detail/KalmanUpdateHelpers.hpp index cdeb3f95cca..c28792b05d5 100644 --- a/Core/include/Acts/TrackFitting/detail/KalmanUpdateHelpers.hpp +++ b/Core/include/Acts/TrackFitting/detail/KalmanUpdateHelpers.hpp @@ -12,7 +12,6 @@ #include "Acts/EventData/SourceLink.hpp" #include "Acts/EventData/detail/CorrectedTransformationFreeToBound.hpp" #include "Acts/Surfaces/Surface.hpp" -#include "Acts/TrackFitting/detail/FitterHelpers.hpp" #include "Acts/Utilities/CalibrationContext.hpp" #include "Acts/Utilities/Result.hpp" @@ -43,47 +42,78 @@ auto kalmanHandleMeasurement( const size_t lastTrackIndex, bool doCovTransport, const Logger &logger, const FreeToBoundCorrection &freeToBoundCorrection = FreeToBoundCorrection( false)) -> Result { - auto trackStateProxyRes = getTrackStateProxy( - state, stepper, surface, fittedStates, lastTrackIndex, doCovTransport, - logger, freeToBoundCorrection, TrackStatePropMask::All); - if (!trackStateProxyRes.ok()) { - return trackStateProxyRes.error(); + // Add a TrackState entry multi trajectory. This allocates storage for + // all components, which we will set later. + TrackStatePropMask mask = TrackStatePropMask::All; + size_t currentTrackIndex = fittedStates.addTrackState(mask, lastTrackIndex); + + // now get track state proxy back + typename traj_t::TrackStateProxy trackStateProxy = + fittedStates.getTrackState(currentTrackIndex); + + // Set the trackStateProxy components with the state from the ongoing + // propagation + { + trackStateProxy.setReferenceSurface(surface.getSharedPtr()); + // Bind the transported state to the current surface + auto res = stepper.boundState(state.stepping, surface, doCovTransport, + freeToBoundCorrection); + if (!res.ok()) { + ACTS_ERROR("Propagate to surface " << surface.geometryId() + << " failed: " << res.error()); + return res.error(); + } + auto &[boundParams, jacobian, pathLength] = *res; + + // Fill the track state + trackStateProxy.predicted() = std::move(boundParams.parameters()); + if (boundParams.covariance().has_value()) { + trackStateProxy.predictedCovariance() = + std::move(*boundParams.covariance()); + } + + trackStateProxy.jacobian() = std::move(jacobian); + trackStateProxy.pathLength() = std::move(pathLength); } - auto &trackStateProxy = *trackStateProxyRes; + // We have predicted parameters, so calibrate the uncalibrated input // measurement extensions.calibrator(state.geoContext, calibrationContext, source_link, trackStateProxy); // Get and set the type flags - auto typeFlags = trackStateProxy.typeFlags(); - typeFlags.set(TrackStateFlag::ParameterFlag); - if (surface.surfaceMaterial() != nullptr) { - typeFlags.set(TrackStateFlag::MaterialFlag); - } + { + auto typeFlags = trackStateProxy.typeFlags(); + typeFlags.set(TrackStateFlag::ParameterFlag); + if (surface.surfaceMaterial() != nullptr) { + typeFlags.set(TrackStateFlag::MaterialFlag); + } - // Check if the state is an outlier. - // If not, run Kalman update, tag it as a - // measurement and update the stepping state. Otherwise, just tag it as - // an outlier - if (not extensions.outlierFinder(trackStateProxy)) { - // Run Kalman update - auto updateRes = extensions.updater(state.geoContext, trackStateProxy, - state.options.direction, logger); - if (!updateRes.ok()) { - ACTS_ERROR("Update step failed: " << updateRes.error()); - return updateRes.error(); + // Check if the state is an outlier. + // If not: + // - run Kalman update + // - tag it as a measurement + // - update the stepping state. + // Else, just tag it as an outlier + if (not extensions.outlierFinder(trackStateProxy)) { + // Run Kalman update + auto updateRes = extensions.updater(state.geoContext, trackStateProxy, + state.options.direction, logger); + if (!updateRes.ok()) { + ACTS_ERROR("Update step failed: " << updateRes.error()); + return updateRes.error(); + } + // Set the measurement type flag + typeFlags.set(TrackStateFlag::MeasurementFlag); + } else { + ACTS_VERBOSE( + "Filtering step successful. But measurement is determined " + "to be an outlier. Stepping state is not updated.") + // Set the outlier type flag + typeFlags.set(TrackStateFlag::OutlierFlag); + trackStateProxy.shareFrom(trackStateProxy, TrackStatePropMask::Predicted, + TrackStatePropMask::Filtered); } - // Set the measurement type flag - typeFlags.set(TrackStateFlag::MeasurementFlag); - } else { - ACTS_VERBOSE( - "Filtering step successful. But measurement is determined " - "to be an outlier. Stepping state is not updated.") - // Set the outlier type flag - typeFlags.set(TrackStateFlag::OutlierFlag); - trackStateProxy.shareFrom(trackStateProxy, TrackStatePropMask::Predicted, - TrackStatePropMask::Filtered); } return trackStateProxy; @@ -111,14 +141,46 @@ auto kalmanHandleNoMeasurement( // No source links on surface, add either hole or passive material // TrackState entry multi trajectory. No storage allocation for // uncalibrated/calibrated measurement and filtered parameter - auto trackStateProxyRes = getTrackStateProxy( - state, stepper, surface, fittedStates, lastTrackIndex, doCovTransport, - logger, freeToBoundCorrection, - ~(TrackStatePropMask::Calibrated | TrackStatePropMask::Filtered)); - if (!trackStateProxyRes.ok()) { - return trackStateProxyRes.error(); + + // Add a TrackState entry multi trajectory. This allocates storage for + // all components, which we will set later. + TrackStatePropMask mask = + ~(TrackStatePropMask::Calibrated | TrackStatePropMask::Filtered); + size_t currentTrackIndex = fittedStates.addTrackState(mask, lastTrackIndex); + + // now get track state proxy back + typename traj_t::TrackStateProxy trackStateProxy = + fittedStates.getTrackState(currentTrackIndex); + + // Set the trackStateProxy components with the state from the ongoing + // propagation + { + trackStateProxy.setReferenceSurface(surface.getSharedPtr()); + // Bind the transported state to the current surface + auto res = stepper.boundState(state.stepping, surface, doCovTransport, + freeToBoundCorrection); + if (!res.ok()) { + ACTS_ERROR("Propagate to surface " << surface.geometryId() + << " failed: " << res.error()); + return res.error(); + } + auto &[boundParams, jacobian, pathLength] = *res; + + // Fill the track state + trackStateProxy.predicted() = std::move(boundParams.parameters()); + if (boundParams.covariance().has_value()) { + trackStateProxy.predictedCovariance() = + std::move(*boundParams.covariance()); + } + + trackStateProxy.jacobian() = std::move(jacobian); + trackStateProxy.pathLength() = std::move(pathLength); + + // Set the filtered parameter index to be the same with predicted + // parameter + trackStateProxy.shareFrom(trackStateProxy, TrackStatePropMask::Predicted, + TrackStatePropMask::Filtered); } - auto &trackStateProxy = *trackStateProxyRes; // Set the track state flags auto typeFlags = trackStateProxy.typeFlags(); @@ -134,11 +196,6 @@ auto kalmanHandleNoMeasurement( ACTS_VERBOSE("Detected in-sensitive surface " << surface.geometryId()); } - // Set the filtered parameter index to be the same with predicted - // parameter - trackStateProxy.shareFrom(trackStateProxy, TrackStatePropMask::Predicted, - TrackStatePropMask::Filtered); - return trackStateProxy; }