Skip to content

Commit

Permalink
feat: Refitting algorithm (#1940)
Browse files Browse the repository at this point in the history
So far, real refitting of tracks was not possible in the examples framework. The direct-fitter option of the `ActsExamples::TrackFittingAlgorithm` was broken due to the fact, that only measurement surfaces were added to the surface sequence, and thus e.g. the traversed material could differ.

This adds `ActsExamples::RefittingAlgorithm`, that uses the new `Acts::TrackContainer` infrastructure to enable a very simple interface: Just a input container and a output container.

Furthermore, the `TrackFittingAlgorithm` looses the `directFit` option. To make this work, also the `TrackFitterFunction` had to be refactored and was pulled out of the `TrackFittingAlgorithm`.
  • Loading branch information
benjaminhuth committed Apr 13, 2023
1 parent f5644a7 commit 08ef3cc
Show file tree
Hide file tree
Showing 20 changed files with 500 additions and 256 deletions.
32 changes: 21 additions & 11 deletions Core/include/Acts/EventData/MultiTrajectory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,27 @@ class TrackStateProxy {
projectorBitset.to_ullong();
}

/// Get the projector bitset, a compressed form of a projection matrix
/// @note This is mainly to copy explicitly a projector from one state
/// to another. Use the `projector` or `effectiveProjector` method if
/// you want to access the matrix.
/// @return The projector bitset
ProjectorBitset projectorBitset() const {
assert(has<hashString("projector")>());
return component<ProjectorBitset, hashString("projector")>();
}

/// Set the projector bitset, a compressed form of a projection matrix
/// @param proj The projector bitset
///
/// @note This is mainly to copy explicitly a projector from one state
/// to another. If you have a projection matrix, set it with `setProjector`.
template <bool RO = ReadOnly, typename = std::enable_if_t<!RO>>
void setProjectorBitset(ProjectorBitset proj) {
assert(has<hashString("projector")>());
component<ProjectorBitset, hashString("projector")>() = proj;
}

/// Uncalibrated measurement in the form of a source link. Const version
/// @return The uncalibrated measurement source link
SourceLink getUncalibratedSourceLink() const;
Expand Down Expand Up @@ -950,17 +971,6 @@ class TrackStateProxy {
hashString("referenceSurface")>();
}

ProjectorBitset projectorBitset() const {
assert(has<hashString("projector")>());
return component<ProjectorBitset, hashString("projector")>();
}

template <bool RO = ReadOnly, typename = std::enable_if_t<!RO>>
void setProjectorBitset(ProjectorBitset proj) {
assert(has<hashString("projector")>());
component<ProjectorBitset, hashString("projector")>() = proj;
}

TransitiveConstPointer<ConstIf<MultiTrajectory<Trajectory>, ReadOnly>> m_traj;
IndexType m_istate;

Expand Down
2 changes: 2 additions & 0 deletions Examples/Algorithms/TrackFitting/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
add_library(
ActsExamplesTrackFitting SHARED
src/RefittingCalibrator.cpp
src/SurfaceSortingAlgorithm.cpp
src/TrackFittingAlgorithm.cpp
src/KalmanFitterFunction.cpp
src/RefittingAlgorithm.cpp
src/GsfFitterFunction.cpp)
target_include_directories(
ActsExamplesTrackFitting
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// This file is part of the Acts project.
//
// Copyright (C) 2019-2021 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 "ActsExamples/Framework/DataHandle.hpp"
#include "ActsExamples/Framework/IAlgorithm.hpp"
#include "ActsExamples/TrackFitting/TrackFitterFunction.hpp"

namespace ActsExamples {

class RefittingAlgorithm final : public IAlgorithm {
public:
struct Config {
/// The intput track collection
std::string inputTracks;
/// Output fitted tracks collection.
std::string outputTracks;
/// Type erased fitter function.
std::shared_ptr<TrackFitterFunction> fit;
/// Pick a single track for debugging (-1 process all tracks)
int pickTrack = -1;
};

/// Constructor of the fitting algorithm
///
/// @param config is the config struct to configure the algorihtm
/// @param level is the logging level
RefittingAlgorithm(Config config, Acts::Logging::Level level);

/// Framework execute method of the fitting algorithm
///
/// @param ctx is the algorithm context that holds event-wise information
/// @return a process code to steer the algporithm flow
ActsExamples::ProcessCode execute(const AlgorithmContext& ctx) const final;

/// Get readonly access to the config parameters
const Config& config() const { return m_cfg; }

private:
Config m_cfg;

ReadDataHandle<ConstTrackContainer> m_inputTracks{this, "InputTracks"};
WriteDataHandle<ConstTrackContainer> m_outputTracks{this, "OutputTracks"};
};

} // namespace ActsExamples
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// This file is part of the Acts project.
//
// Copyright (C) 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/VectorMultiTrajectory.hpp"
#include "Acts/Surfaces/Surface.hpp"

namespace ActsExamples {

struct RefittingCalibrator {
using Proxy =
Acts::MultiTrajectory<Acts::VectorMultiTrajectory>::TrackStateProxy;
using ConstProxy = Acts::MultiTrajectory<
Acts::ConstVectorMultiTrajectory>::ConstTrackStateProxy;

struct RefittingSourceLink {
ConstProxy state;

Acts::GeometryIdentifier geometryId() const {
return state.referenceSurface().geometryId();
}
};

void calibrate(const Acts::GeometryContext& /*gctx*/, Proxy trackState) const;
};

} // namespace ActsExamples
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// This file is part of the Acts project.
//
// Copyright (C) 2019-2021 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/SourceLink.hpp"
#include "Acts/EventData/TrackParameters.hpp"
#include "Acts/EventData/VectorMultiTrajectory.hpp"
#include "Acts/EventData/VectorTrackContainer.hpp"
#include "Acts/Geometry/GeometryContext.hpp"
#include "Acts/Geometry/TrackingGeometry.hpp"
#include "Acts/MagneticField/MagneticFieldContext.hpp"
#include "Acts/MagneticField/MagneticFieldProvider.hpp"
#include "Acts/Propagator/MultiEigenStepperLoop.hpp"
#include "Acts/Propagator/Propagator.hpp"
#include "Acts/TrackFitting/BetheHeitlerApprox.hpp"
#include "Acts/Utilities/CalibrationContext.hpp"
#include "ActsExamples/EventData/Measurement.hpp"
#include "ActsExamples/EventData/Track.hpp"
#include "ActsExamples/TrackFitting/RefittingCalibrator.hpp"

namespace ActsExamples {

/// Fit function that takes the above parameters and runs a fit
/// @note This is separated into a virtual interface to keep compilation units
/// small.
class TrackFitterFunction {
public:
using TrackFitterResult = Acts::Result<TrackContainer::TrackProxy>;

struct GeneralFitterOptions {
std::reference_wrapper<const Acts::GeometryContext> geoContext;
std::reference_wrapper<const Acts::MagneticFieldContext> magFieldContext;
std::reference_wrapper<const Acts::CalibrationContext> calibrationContext;
const Acts::Surface* referenceSurface = nullptr;
Acts::PropagatorPlainOptions propOptions;
};

virtual ~TrackFitterFunction() = default;

virtual TrackFitterResult operator()(const std::vector<Acts::SourceLink>&,
const TrackParameters&,
const GeneralFitterOptions&,
const MeasurementCalibrator&,
TrackContainer&) const = 0;

virtual TrackFitterResult operator()(const std::vector<Acts::SourceLink>&,
const TrackParameters&,
const GeneralFitterOptions&,
const RefittingCalibrator&,
const std::vector<const Acts::Surface*>&,
TrackContainer&) const = 0;
};

/// Makes a fitter function object for the Kalman Filter
///
std::shared_ptr<TrackFitterFunction> makeKalmanFitterFunction(
std::shared_ptr<const Acts::TrackingGeometry> trackingGeometry,
std::shared_ptr<const Acts::MagneticFieldProvider> magneticField,
bool multipleScattering = true, bool energyLoss = true,
double reverseFilteringMomThreshold = 0.0,
Acts::FreeToBoundCorrection freeToBoundCorrection =
Acts::FreeToBoundCorrection(),
const Acts::Logger& logger = *Acts::getDefaultLogger("Kalman",
Acts::Logging::INFO));

/// This type is used in the Examples framework for the Bethe-Heitler
/// approximation
using BetheHeitlerApprox = Acts::Experimental::AtlasBetheHeitlerApprox<6, 5>;

/// Makes a fitter function object for the GSF
///
/// @param trackingGeometry the trackingGeometry for the propagator
/// @param magneticField the magnetic field for the propagator
/// @param betheHeitlerApprox The object that encapsulates the approximation.
/// @param maxComponents number of maximum components in the track state
/// @param abortOnError wether to call std::abort on an error
/// @param disableAllMaterialHandling run the GSF like a KF (no energy loss,
/// always 1 component, ...)
/// @param logger a logger instance
std::shared_ptr<TrackFitterFunction> makeGsfFitterFunction(
std::shared_ptr<const Acts::TrackingGeometry> trackingGeometry,
std::shared_ptr<const Acts::MagneticFieldProvider> magneticField,
BetheHeitlerApprox betheHeitlerApprox, std::size_t maxComponents,
double weightCutoff, Acts::FinalReductionMethod finalReductionMethod,
bool abortOnError, bool disableAllMaterialHandling,
const Acts::Logger& logger);

} // namespace ActsExamples
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,13 @@

#pragma once

#include "Acts/EventData/VectorMultiTrajectory.hpp"
#include "Acts/EventData/VectorTrackContainer.hpp"
#include "Acts/EventData/detail/CorrectedTransformationFreeToBound.hpp"
#include "Acts/Geometry/TrackingGeometry.hpp"
#include "Acts/Propagator/MultiEigenStepperLoop.hpp"
#include "Acts/TrackFitting/KalmanFitter.hpp"
#include "ActsExamples/EventData/IndexSourceLink.hpp"
#include "ActsExamples/EventData/Measurement.hpp"
#include "ActsExamples/EventData/ProtoTrack.hpp"
#include "ActsExamples/EventData/Track.hpp"
#include "ActsExamples/Framework/DataHandle.hpp"
#include "ActsExamples/Framework/IAlgorithm.hpp"
#include "ActsExamples/MagneticField/MagneticField.hpp"

#include <functional>
#include <memory>
#include <vector>
#include "ActsExamples/TrackFitting/TrackFitterFunction.hpp"

namespace Acts {
class TrackingGeometry;
Expand All @@ -34,49 +24,9 @@ namespace ActsExamples {

class TrackFittingAlgorithm final : public IAlgorithm {
public:
// All track fitter functions must return the same type. For now this is the
// KalmanFitterResult, but maybe in the future it makes sense to generalize
// this

using TrackContainer =
Acts::TrackContainer<Acts::VectorTrackContainer,
Acts::VectorMultiTrajectory, std::shared_ptr>;

using TrackFitterResult = Acts::Result<TrackContainer::TrackProxy>;

/// General options that do not depend on the fitter type, but need to be
/// handed over by the algorithm
struct GeneralFitterOptions {
std::reference_wrapper<const Acts::GeometryContext> geoContext;
std::reference_wrapper<const Acts::MagneticFieldContext> magFieldContext;
std::reference_wrapper<const Acts::CalibrationContext> calibrationContext;
std::reference_wrapper<const MeasurementCalibrator> calibrator;
const Acts::Surface* referenceSurface = nullptr;
Acts::PropagatorPlainOptions propOptions;
};

/// Fit function that takes the above parameters and runs a fit
/// @note This is separated into a virtual interface to keep compilation units
/// small.
class TrackFitterFunction {
public:
virtual ~TrackFitterFunction() = default;
virtual TrackFitterResult operator()(const std::vector<Acts::SourceLink>&,
const TrackParameters&,
const GeneralFitterOptions&,
TrackContainer&) const = 0;

virtual TrackFitterResult operator()(
const std::vector<Acts::SourceLink>&, const TrackParameters&,
const GeneralFitterOptions&, const std::vector<const Acts::Surface*>&,
TrackContainer&) const = 0;
};

struct Config {
/// Input measurements collection.
std::string inputMeasurements;
/// Boolean determining to use DirectNavigator or standard Navigator
bool directNavigation = false;
/// Input source links collection.
std::string inputSourceLinks;
/// Input proto tracks collection, i.e. groups of hit indices.
Expand All @@ -88,8 +38,6 @@ class TrackFittingAlgorithm final : public IAlgorithm {
/// Type erased fitter function.
std::shared_ptr<TrackFitterFunction> fit;
/// Tracking geometry for surface lookup
std::shared_ptr<const Acts::TrackingGeometry> trackingGeometry;
/// Pick a single track for debugging (-1 process all tracks)
int pickTrack = -1;
};

Expand Down

0 comments on commit 08ef3cc

Please sign in to comment.