Skip to content

Commit

Permalink
refactor!: Use Delegate for track linearizers (acts-project#2946)
Browse files Browse the repository at this point in the history
This allows removing the linearizer template from: 
- Billoir fitter
- ZScan finder
- IVF
- AMVFitter

Part of:
- acts-project#2842 

Blocked by:
- acts-project#2886
  • Loading branch information
paulgessinger authored and asalzburger committed May 21, 2024
1 parent a8eb6aa commit 8a62677
Show file tree
Hide file tree
Showing 21 changed files with 194 additions and 174 deletions.
9 changes: 2 additions & 7 deletions Core/include/Acts/Vertexing/AdaptiveMultiVertexFinder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "Acts/Utilities/Result.hpp"
#include "Acts/Vertexing/AMVFInfo.hpp"
#include "Acts/Vertexing/ImpactPointEstimator.hpp"
#include "Acts/Vertexing/TrackLinearizer.hpp"
#include "Acts/Vertexing/VertexingOptions.hpp"

#include <type_traits>
Expand All @@ -33,7 +34,6 @@ namespace Acts {
/// @tparam sfinder_t Seed finder type
template <typename vfitter_t, typename sfinder_t>
class AdaptiveMultiVertexFinder {
using Linearizer_t = typename vfitter_t::Linearizer_t;
using FitterState_t = typename vfitter_t::State;
using SeedFinderState_t = typename sfinder_t::State;

Expand All @@ -54,14 +54,12 @@ class AdaptiveMultiVertexFinder {
/// @param fitter The vertex fitter
/// @param sfinder The seed finder
/// @param ipEst ImpactPointEstimator
/// @param lin Track linearizer
/// @param bIn Input magnetic field
Config(vfitter_t fitter, sfinder_t sfinder, ImpactPointEstimator ipEst,
Linearizer_t lin, std::shared_ptr<const MagneticFieldProvider> bIn)
std::shared_ptr<const MagneticFieldProvider> bIn)
: vertexFitter(std::move(fitter)),
seedFinder(std::move(sfinder)),
ipEstimator(std::move(ipEst)),
linearizer(std::move(lin)),
bField{std::move(bIn)} {}

// Vertex fitter
Expand All @@ -73,9 +71,6 @@ class AdaptiveMultiVertexFinder {
// ImpactPointEstimator
ImpactPointEstimator ipEstimator;

// Track linearizer
Linearizer_t linearizer;

std::shared_ptr<const MagneticFieldProvider> bField;

// Max z interval used for adding tracks to fit:
Expand Down
7 changes: 3 additions & 4 deletions Core/include/Acts/Vertexing/AdaptiveMultiVertexFinder.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ auto Acts::AdaptiveMultiVertexFinder<vfitter_t, sfinder_t>::find(
fitterState.addVertexToMultiMap(vtxCandidate);

// Perform the fit
auto fitResult = m_cfg.vertexFitter.addVtxToFit(
fitterState, vtxCandidate, m_cfg.linearizer, vertexingOptions);
auto fitResult = m_cfg.vertexFitter.addVtxToFit(fitterState, vtxCandidate,
vertexingOptions);
if (!fitResult.ok()) {
return fitResult.error();
}
Expand Down Expand Up @@ -582,8 +582,7 @@ auto Acts::AdaptiveMultiVertexFinder<vfitter_t, sfinder_t>::deleteLastVertex(
}

// Do the fit with removed vertex
auto fitResult =
m_cfg.vertexFitter.fit(fitterState, m_cfg.linearizer, vertexingOptions);
auto fitResult = m_cfg.vertexFitter.fit(fitterState, vertexingOptions);
if (!fitResult.ok()) {
return fitResult.error();
}
Expand Down
29 changes: 10 additions & 19 deletions Core/include/Acts/Vertexing/AdaptiveMultiVertexFitter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "Acts/Vertexing/ImpactPointEstimator.hpp"
#include "Acts/Vertexing/LinearizerConcept.hpp"
#include "Acts/Vertexing/TrackAtVertex.hpp"
#include "Acts/Vertexing/TrackLinearizer.hpp"
#include "Acts/Vertexing/Vertex.hpp"
#include "Acts/Vertexing/VertexingError.hpp"
#include "Acts/Vertexing/VertexingOptions.hpp"
Expand All @@ -33,17 +34,8 @@ namespace Acts {
/// `Identification of b-jets and investigation of the discovery potential
/// of a Higgs boson in the WH−−>lvbb¯ channel with the ATLAS experiment`
///
///////////////////////////////////////////////////////////////////////////
///
/// @tparam linearizer_t Track linearizer type
template <typename linearizer_t>
class AdaptiveMultiVertexFitter {
static_assert(LinearizerConcept<linearizer_t>,
"Linearizer does not fulfill linearizer concept.");

public:
using Linearizer_t = linearizer_t;

/// @brief The fitter state
struct State {
State(const MagneticFieldProvider& field,
Expand All @@ -68,9 +60,6 @@ class AdaptiveMultiVertexFitter {

std::map<std::pair<InputTrack, Vertex*>, TrackAtVertex> tracksAtVerticesMap;

/// @brief Default State constructor
State() = default;

// Adds a vertex to trackToVerticesMultiMap
void addVertexToMultiMap(Vertex& vtx) {
for (auto trk : vtxInfoMap[&vtx].trackLinks) {
Expand Down Expand Up @@ -146,6 +135,8 @@ class AdaptiveMultiVertexFitter {

// Function to extract parameters from InputTrack
InputTrack::Extractor extractParameters;

TrackLinearizer trackLinearizer;
};

/// @brief Constructor for user-defined InputTrack_t type !=
Expand All @@ -164,6 +155,11 @@ class AdaptiveMultiVertexFitter {
"AdaptiveMultiVertexFitter: No function to extract parameters "
"from InputTrack_t provided.");
}

if (!m_cfg.trackLinearizer.connected()) {
throw std::invalid_argument(
"AdaptiveMultiVertexFitter: No track linearizer provided.");
}
}

/// @brief Adds a new vertex to an existing multi-vertex fit.
Expand All @@ -177,23 +173,20 @@ class AdaptiveMultiVertexFitter {
///
/// @param state Fitter state
/// @param newVertex Vertex to be added to fit
/// @param linearizer Track linearizer
/// @param vertexingOptions Vertexing options
///
/// @return Result<void> object
Result<void> addVtxToFit(State& state, Vertex& newVertex,
const Linearizer_t& linearizer,
const VertexingOptions& vertexingOptions) const;

/// @brief Performs a simultaneous fit of all vertices in
/// state.vertexCollection
///
/// @param state Fitter state
/// @param linearizer Track linearizer
/// @param vertexingOptions Vertexing options
///
/// @return Result<void> object
Result<void> fit(State& state, const Linearizer_t& linearizer,
Result<void> fit(State& state,
const VertexingOptions& vertexingOptions) const;

private:
Expand Down Expand Up @@ -241,11 +234,9 @@ class AdaptiveMultiVertexFitter {
/// and updates the vertices by calling the VertexUpdater
///
/// @param state Fitter state
/// @param linearizer The track linearizer
/// @param vertexingOptions Vertexing options
Result<void> setWeightsAndUpdate(
State& state, const Linearizer_t& linearizer,
const VertexingOptions& vertexingOptions) const;
State& state, const VertexingOptions& vertexingOptions) const;

/// @brief Collects the compatibility values of the track `trk`
/// wrt to all of its associated vertices
Expand Down
53 changes: 19 additions & 34 deletions Core/include/Acts/Vertexing/AdaptiveMultiVertexFitter.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@
#include "Acts/Vertexing/KalmanVertexUpdater.hpp"
#include "Acts/Vertexing/VertexingError.hpp"

template <typename linearizer_t>
Acts::Result<void> Acts::AdaptiveMultiVertexFitter<linearizer_t>::fit(
State& state, const linearizer_t& linearizer,
const VertexingOptions& vertexingOptions) const {
inline Acts::Result<void> Acts::AdaptiveMultiVertexFitter::fit(
State& state, const VertexingOptions& vertexingOptions) const {
// Reset annealing tool
state.annealingState = AnnealingUtility::State();

Expand Down Expand Up @@ -86,8 +84,7 @@ Acts::Result<void> Acts::AdaptiveMultiVertexFitter<linearizer_t>::fit(
} // End loop over vertex collection

// Recalculate all track weights and update vertices
auto setWeightsResult =
setWeightsAndUpdate(state, linearizer, vertexingOptions);
auto setWeightsResult = setWeightsAndUpdate(state, vertexingOptions);
if (!setWeightsResult.ok()) {
// Print vertices and associated tracks if logger is in debug mode
if (logger().doPrint(Logging::DEBUG)) {
Expand Down Expand Up @@ -115,9 +112,8 @@ Acts::Result<void> Acts::AdaptiveMultiVertexFitter<linearizer_t>::fit(
return {};
}

template <typename linearizer_t>
Acts::Result<void> Acts::AdaptiveMultiVertexFitter<linearizer_t>::addVtxToFit(
State& state, Vertex& newVertex, const linearizer_t& linearizer,
inline Acts::Result<void> Acts::AdaptiveMultiVertexFitter::addVtxToFit(
State& state, Vertex& newVertex,
const VertexingOptions& vertexingOptions) const {
if (state.vtxInfoMap[&newVertex].trackLinks.empty()) {
ACTS_ERROR(
Expand Down Expand Up @@ -181,23 +177,20 @@ Acts::Result<void> Acts::AdaptiveMultiVertexFitter<linearizer_t>::addVtxToFit(
}

// Perform fit on all added vertices
auto fitRes = fit(state, linearizer, vertexingOptions);
auto fitRes = fit(state, vertexingOptions);
if (!fitRes.ok()) {
return fitRes.error();
}

return {};
}

template <typename linearizer_t>
bool Acts::AdaptiveMultiVertexFitter<linearizer_t>::isAlreadyInList(
inline bool Acts::AdaptiveMultiVertexFitter::isAlreadyInList(
Vertex* vtx, const std::vector<Vertex*>& vertices) const {
return std::find(vertices.begin(), vertices.end(), vtx) != vertices.end();
}

template <typename linearizer_t>
Acts::Result<void>
Acts::AdaptiveMultiVertexFitter<linearizer_t>::prepareVertexForFit(
inline Acts::Result<void> Acts::AdaptiveMultiVertexFitter::prepareVertexForFit(
State& state, Vertex* vtx, const VertexingOptions& vertexingOptions) const {
// Vertex info object
auto& vtxInfo = state.vtxInfoMap[vtx];
Expand All @@ -218,9 +211,8 @@ Acts::AdaptiveMultiVertexFitter<linearizer_t>::prepareVertexForFit(
return {};
}

template <typename linearizer_t>
Acts::Result<void>
Acts::AdaptiveMultiVertexFitter<linearizer_t>::setAllVertexCompatibilities(
inline Acts::Result<void>
Acts::AdaptiveMultiVertexFitter::setAllVertexCompatibilities(
State& state, Vertex* vtx, const VertexingOptions& vertexingOptions) const {
VertexInfo& vtxInfo = state.vtxInfoMap[vtx];

Expand Down Expand Up @@ -260,11 +252,8 @@ Acts::AdaptiveMultiVertexFitter<linearizer_t>::setAllVertexCompatibilities(
return {};
}

template <typename linearizer_t>
Acts::Result<void>
Acts::AdaptiveMultiVertexFitter<linearizer_t>::setWeightsAndUpdate(
State& state, const linearizer_t& linearizer,
const VertexingOptions& vertexingOptions) const {
inline Acts::Result<void> Acts::AdaptiveMultiVertexFitter::setWeightsAndUpdate(
State& state, const VertexingOptions& vertexingOptions) const {
for (auto vtx : state.vertexCollection) {
VertexInfo& vtxInfo = state.vtxInfoMap[vtx];

Expand All @@ -288,7 +277,7 @@ Acts::AdaptiveMultiVertexFitter<linearizer_t>::setWeightsAndUpdate(
// Check if track is already linearized and whether we need to
// relinearize
if (!trkAtVtx.isLinearized || vtxInfo.relinearize) {
auto result = linearizer.linearizeTrack(
auto result = m_cfg.trackLinearizer(
m_cfg.extractParameters(trk), vtxInfo.linPoint[3],
*vtxPerigeeSurface, vertexingOptions.geoContext,
vertexingOptions.magFieldContext, state.fieldCache);
Expand Down Expand Up @@ -317,10 +306,9 @@ Acts::AdaptiveMultiVertexFitter<linearizer_t>::setWeightsAndUpdate(
return {};
}

template <typename linearizer_t>
std::vector<double> Acts::AdaptiveMultiVertexFitter<linearizer_t>::
collectTrackToVertexCompatibilities(State& state,
const InputTrack& trk) const {
inline std::vector<double>
Acts::AdaptiveMultiVertexFitter::collectTrackToVertexCompatibilities(
State& state, const InputTrack& trk) const {
// Compatibilities of trk wrt all of its associated vertices
std::vector<double> trkToVtxCompatibilities;

Expand All @@ -342,8 +330,7 @@ std::vector<double> Acts::AdaptiveMultiVertexFitter<linearizer_t>::
return trkToVtxCompatibilities;
}

template <typename linearizer_t>
bool Acts::AdaptiveMultiVertexFitter<linearizer_t>::checkSmallShift(
inline bool Acts::AdaptiveMultiVertexFitter::checkSmallShift(
State& state) const {
for (auto* vtx : state.vertexCollection) {
Vector3 diff =
Expand All @@ -357,8 +344,7 @@ bool Acts::AdaptiveMultiVertexFitter<linearizer_t>::checkSmallShift(
return true;
}

template <typename linearizer_t>
void Acts::AdaptiveMultiVertexFitter<linearizer_t>::doVertexSmoothing(
inline void Acts::AdaptiveMultiVertexFitter::doVertexSmoothing(
State& state) const {
for (const auto vtx : state.vertexCollection) {
for (const auto& trk : state.vtxInfoMap[vtx].trackLinks) {
Expand All @@ -376,8 +362,7 @@ void Acts::AdaptiveMultiVertexFitter<linearizer_t>::doVertexSmoothing(
}
}

template <typename linearizer_t>
void Acts::AdaptiveMultiVertexFitter<linearizer_t>::logDebugData(
inline void Acts::AdaptiveMultiVertexFitter::logDebugData(
const State& state, const Acts::GeometryContext& geoContext) const {
ACTS_DEBUG("Encountered an error when fitting the following "
<< state.vertexCollection.size() << " vertices:");
Expand Down
18 changes: 9 additions & 9 deletions Core/include/Acts/Vertexing/FullBilloirVertexFitter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "Acts/Utilities/Result.hpp"
#include "Acts/Vertexing/HelicalTrackLinearizer.hpp"
#include "Acts/Vertexing/LinearizerConcept.hpp"
#include "Acts/Vertexing/TrackLinearizer.hpp"
#include "Acts/Vertexing/Vertex.hpp"
#include "Acts/Vertexing/VertexingOptions.hpp"

Expand Down Expand Up @@ -44,16 +45,8 @@ namespace Acts {
/// ACTS White Paper: Cross-Covariance Matrices in the Billoir Vertex Fit
/// https://acts.readthedocs.io/en/latest/white_papers/billoir-covariances.html
/// Author(s) Russo, F
///
/// @tparam linearizer_t Track linearizer type
template <typename linearizer_t>
class FullBilloirVertexFitter {
static_assert(LinearizerConcept<linearizer_t>,
"Linearizer does not fulfill linearizer concept.");

public:
using Linearizer_t = linearizer_t;

struct State {
/// @brief The state constructor
///
Expand All @@ -70,6 +63,8 @@ class FullBilloirVertexFitter {

// Function to extract parameters from InputTrack
InputTrack::Extractor extractParameters;

TrackLinearizer trackLinearizer;
};

/// @brief Constructor for user-defined InputTrack type
Expand All @@ -87,6 +82,12 @@ class FullBilloirVertexFitter {
"No function to extract parameters "
"provided.");
}

if (!m_cfg.trackLinearizer.connected()) {
throw std::invalid_argument(
"FullBilloirVertexFitter: "
"No track linearizer provided.");
}
}

/// @brief Fit method, fitting vertex for provided tracks with constraint
Expand All @@ -98,7 +99,6 @@ class FullBilloirVertexFitter {
///
/// @return Fitted vertex
Result<Vertex> fit(const std::vector<InputTrack>& paramVector,
const linearizer_t& linearizer,
const VertexingOptions& vertexingOptions,
State& state) const;

Expand Down
7 changes: 3 additions & 4 deletions Core/include/Acts/Vertexing/FullBilloirVertexFitter.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,8 @@ struct BilloirVertex {

} // namespace Acts::detail

template <typename linearizer_t>
Acts::Result<Acts::Vertex> Acts::FullBilloirVertexFitter<linearizer_t>::fit(
const std::vector<InputTrack>& paramVector, const linearizer_t& linearizer,
inline Acts::Result<Acts::Vertex> Acts::FullBilloirVertexFitter::fit(
const std::vector<InputTrack>& paramVector,
const VertexingOptions& vertexingOptions, State& state) const {
unsigned int nTracks = paramVector.size();
double chi2 = std::numeric_limits<double>::max();
Expand Down Expand Up @@ -109,7 +108,7 @@ Acts::Result<Acts::Vertex> Acts::FullBilloirVertexFitter<linearizer_t>::fit(

const auto& trackParams = m_cfg.extractParameters(trackContainer);

auto result = linearizer.linearizeTrack(
auto result = m_cfg.trackLinearizer(
trackParams, linPoint[3], *perigeeSurface,
vertexingOptions.geoContext, vertexingOptions.magFieldContext,
state.fieldCache);
Expand Down
Loading

0 comments on commit 8a62677

Please sign in to comment.