Skip to content

Commit

Permalink
refactor kalmanvertexupdater
Browse files Browse the repository at this point in the history
  • Loading branch information
paulgessinger committed Feb 19, 2024
1 parent 84c039f commit 160517b
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 53 deletions.
6 changes: 1 addition & 5 deletions Core/include/Acts/Vertexing/AdaptiveMultiVertexFitter.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -291,11 +291,7 @@ inline Acts::Result<void> Acts::AdaptiveMultiVertexFitter::setWeightsAndUpdate(
// Update the vertex with the new track. The second template argument
// corresponds to the number of fitted vertex dimensions (i.e., 3 if we
// only fit spatial coordinates and 4 if we also fit time).
if (m_cfg.useTime) {
KalmanVertexUpdater::updateVertexWithTrack<4>(*vtx, trkAtVtx);
} else {
KalmanVertexUpdater::updateVertexWithTrack<3>(*vtx, trkAtVtx);
}
KalmanVertexUpdater::updateVertexWithTrack(*vtx, trkAtVtx, m_cfg.useTime ? 4 : 3);
} else {
ACTS_VERBOSE("Track weight too low. Skip track.");
}
Expand Down
17 changes: 0 additions & 17 deletions Core/include/Acts/Vertexing/KalmanVertexTrackUpdater.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,5 @@ namespace KalmanVertexTrackUpdater {
void update(TrackAtVertexRef track, const Vector4& vtxPosFull,
const SquareMatrix4& vtxCovFull, unsigned int nDimVertex);

namespace detail {

/// @brief Calculates a covariance matrix for the refitted track parameters
///
/// @param wMat W_k matrix from Ref. (1)
/// @param crossCovVP Cross-covariance matrix between vertex position and track
/// momentum
/// @param vtxCov Vertex covariance matrix
/// @param newTrkParams Refitted track parameters
template <unsigned int nDimVertex>
inline BoundMatrix calculateTrackCovariance(
const SquareMatrix3& wMat, const ActsMatrix<nDimVertex, 3>& crossCovVP,
const ActsSquareMatrix<nDimVertex>& vtxCov,
const BoundVector& newTrkParams);

} // Namespace detail

} // Namespace KalmanVertexTrackUpdater
} // Namespace Acts
37 changes: 15 additions & 22 deletions Core/include/Acts/Vertexing/KalmanVertexUpdater.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,7 @@ struct Cache {
SquareMatrix3 wMat = SquareMatrix3::Zero();
};

/// @brief Updates vertex with knowledge of new track
/// @note KalmanVertexUpdater updates the vertex when trk is added to the fit.
/// However, it does not add the track to the TrackAtVertex list. This to be
/// done manually after calling the method.
///
/// @tparam nDimVertex number of dimensions of the vertex. Can be 3 (if we only
/// fit its spatial coordinates) or 4 (if we also fit time).
///
/// @param vtx Vertex to be updated
/// @param trk Track to be used for updating the vertex
template <unsigned int nDimVertex>
void updateVertexWithTrack(Vertex& vtx, TrackAtVertex& trk);

namespace detail {
void updateVertexWithTrack(Vector4& vtxPos, SquareMatrix4& vtxCov,
std::pair<double, double>& fitQuality,
TrackAtVertexRef trk, int sign,
unsigned int nDimVertex);

// These two functions only exist so we can compile calculateUpdate in a
// compilation unit
void calculateUpdate3(const Vector4& vtxPos, const SquareMatrix4& vtxCov,
Expand All @@ -86,6 +68,19 @@ void calculateUpdate4(const Vector4& vtxPos, const SquareMatrix4& vtxCov,
Cache<4>& cache);
} // namespace detail

/// @brief Updates vertex with knowledge of new track
/// @note KalmanVertexUpdater updates the vertex when trk is added to the fit.
/// However, it does not add the track to the TrackAtVertex list. This to be
/// done manually after calling the method.
///
///
/// @param vtx Vertex to be updated
/// @param trk Track to be used for updating the vertex
/// @param nDimVertex number of dimensions of the vertex. Can be 3 (if we only
/// fit its spatial coordinates) or 4 (if we also fit time).
void updateVertexWithTrack(Vertex& vtx, TrackAtVertex& trk,
unsigned int nDimVertex);

/// @brief Calculates updated vertex position and covariance as well as the
/// updated track momentum when adding/removing linTrack. Saves the result in
/// cache.
Expand Down Expand Up @@ -119,7 +114,5 @@ void calculateUpdate(const Vector4& vtxPos, const SquareMatrix4& vtxCov,
}
}

} // Namespace KalmanVertexUpdater
} // Namespace Acts

#include "KalmanVertexUpdater.ipp"
} // namespace KalmanVertexUpdater
} // namespace Acts
10 changes: 10 additions & 0 deletions Core/src/Vertexing/KalmanVertexTrackUpdater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ namespace Acts {

namespace {

/// @brief Calculates a covariance matrix for the refitted track parameters
///
/// @tparam nDimVertex number of dimensions of the vertex. Can be 3 (if we only
/// fit its spatial coordinates) or 4 (if we also fit time).
///
/// @param wMat W_k matrix from Ref. (1)
/// @param crossCovVP Cross-covariance matrix between vertex position and track
/// momentum
/// @param vtxCov Vertex covariance matrix
/// @param newTrkParams Refitted track parameters
template <unsigned int nDimVertex>
Acts::BoundMatrix calculateTrackCovariance(
const SquareMatrix3& wMat, const ActsMatrix<nDimVertex, 3>& crossCovVP,
Expand Down
38 changes: 30 additions & 8 deletions Core/src/Vertexing/KalmanVertexUpdater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,21 @@

namespace Acts {
namespace {

/// @brief Calculates updated vertex position and covariance as well as the
/// updated track momentum when adding/removing linTrack. Saves the result in
/// cache.
///
/// @tparam nDimVertex number of dimensions of the vertex. Can be 3 (if we only
/// fit its spatial coordinates) or 4 (if we also fit time).
///
/// @param vtx Vertex
/// @param linTrack Linearized track to be added or removed
/// @param trackWeight Track weight
/// @param sign +1 (add track) or -1 (remove track)
/// @note Tracks are removed during the smoothing procedure to compute
/// the chi2 of the track wrt the updated vertex position
/// @param[out] cache A cache to store the results of this function
template <unsigned int nDimVertex>
void calculateUpdateImpl(const Vector4& vtxPos, const SquareMatrix4& vtxCov,
const Acts::LinearizedTrack& linTrack,
Expand Down Expand Up @@ -145,8 +160,14 @@ void update(Vector4& vtxPos, SquareMatrix4& vtxCov,
KalmanVertexUpdater::Cache<nDimVertex> cache;

// Calculate update and save result in cache
calculateUpdate(vtxPos, vtxCov, trk.linearizedState, trackWeight, sign,
cache);

if constexpr (nDimVertex == 3) {
calculateUpdateImpl(vtxPos, vtxCov, trk.linearizedState, trackWeight, sign,
cache);
} else if constexpr (nDimVertex == 4) {
calculateUpdateImpl(vtxPos, vtxCov, trk.linearizedState, trackWeight, sign,
cache);
}

// Get fit quality parameters wrt to old vertex
double chi2 = 0.;
Expand Down Expand Up @@ -206,19 +227,20 @@ void KalmanVertexUpdater::detail::calculateUpdate4(
calculateUpdateImpl<4>(vtxPos, vtxCov, linTrack, trackWeight, sign, cache);
}

void KalmanVertexUpdater::detail::updateVertexWithTrack(
Vector4& vtxPos, SquareMatrix4& vtxCov,
std::pair<double, double>& fitQuality, TrackAtVertexRef trk, int sign,
unsigned int nDimVertex) {
void KalmanVertexUpdater::updateVertexWithTrack(Vertex& vtx, TrackAtVertex& trk,
unsigned int nDimVertex) {
std::pair<double, double> fitQuality = vtx.fitQuality();

if (nDimVertex == 3) {
update<3>(vtxPos, vtxCov, fitQuality, trk, sign);
update<3>(vtx.fullPosition(), vtx.fullCovariance(), fitQuality, trk, 1);
} else if (nDimVertex == 4) {
update<4>(vtxPos, vtxCov, fitQuality, trk, sign);
update<4>(vtx.fullPosition(), vtx.fullCovariance(), fitQuality, trk, 1);
} else {
throw std::invalid_argument(
"The vertex dimension must either be 3 (when fitting the spatial "
"coordinates) or 4 (when fitting the spatial coordinates + time).");
}
vtx.setFitQuality(fitQuality);
}

} // namespace Acts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ BOOST_AUTO_TEST_CASE(Kalman_Vertex_Updater) {
vtx.setFullCovariance(SquareMatrix4::Identity() * 0.01);

// Update trkAtVertex with assumption of originating from vtx
KalmanVertexUpdater::updateVertexWithTrack<3>(vtx, trkAtVtx);
KalmanVertexUpdater::updateVertexWithTrack(vtx, trkAtVtx, 3);

if (debug) {
std::cout << "Old vertex position: " << vtxPos << std::endl;
Expand Down

0 comments on commit 160517b

Please sign in to comment.