Skip to content

Commit

Permalink
refactor!: CKF interface change to once per seed (#1705)
Browse files Browse the repository at this point in the history
Previously, the CKF accepts a vector of seeds and produces a vector of outputs. In preparation for the Track container, it's preferrable to have the loop over seeds outside this function, i.e. the CKF is invoked once per seed, and the vector accumulation occurs outside of it
  • Loading branch information
paulgessinger authored Dec 12, 2022
1 parent 20f0954 commit 23b307d
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 78 deletions.
117 changes: 48 additions & 69 deletions Core/include/Acts/TrackFinding/CombinatorialKalmanFilter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1242,21 +1242,20 @@ class CombinatorialKalmanFilter {
/// @param initialParameters The initial track parameters
/// @param tfOptions CombinatorialKalmanFilterOptions steering the track
/// finding
/// @param trajectory Optional input track state container to use
/// @param trajectory Input track state container to use
/// @note The input measurements are given in the form of @c SourceLinks.
/// It's @c calibrator_t's job to turn them into calibrated measurements
/// used in the track finding.
///
/// @return a container of track finding result for all the initial track
/// parameters
template <typename source_link_iterator_t,
typename start_parameters_container_t,
template <typename source_link_iterator_t, typename start_parameters_t,
typename parameters_t = BoundTrackParameters>
std::vector<Result<CombinatorialKalmanFilterResult<traj_t>>> findTracks(
const start_parameters_container_t& initialParameters,
Result<CombinatorialKalmanFilterResult<traj_t>> findTracks(
const start_parameters_t& initialParameters,
const CombinatorialKalmanFilterOptions<source_link_iterator_t, traj_t>&
tfOptions,
std::shared_ptr<traj_t> trajectory = {}) const {
std::shared_ptr<traj_t> trajectory) const {
const auto& logger = tfOptions.logger;

using SourceLinkAccessor =
Expand Down Expand Up @@ -1290,79 +1289,59 @@ class CombinatorialKalmanFilter {
combKalmanActor.m_extensions = tfOptions.extensions;

// Run the CombinatorialKalmanFilter.
// @todo The same target surface is used for all the initial track
// parameters, which is not necessarily the case.
std::vector<Result<CombinatorialKalmanFilterResult<traj_t>>> ckfResults;
ckfResults.reserve(initialParameters.size());
// Loop over all initial track parameters. Return the results for all
// initial track parameters including those failed ones.

if (!trajectory) {
trajectory = std::make_shared<traj_t>();
}
auto stateBuffer = std::make_shared<traj_t>();

for (size_t iseed = 0; iseed < initialParameters.size(); ++iseed) {
const auto& sParameters = initialParameters[iseed];

typename propagator_t::template action_list_t_result_t<
CurvilinearTrackParameters, Actors>
inputResult;
auto stateBuffer = std::make_shared<traj_t>();

auto& r =
inputResult.template get<CombinatorialKalmanFilterResult<traj_t>>();
typename propagator_t::template action_list_t_result_t<
CurvilinearTrackParameters, Actors>
inputResult;

r.fittedStates = trajectory;
r.stateBuffer = stateBuffer;
r.stateBuffer->clear();
auto& r =
inputResult.template get<CombinatorialKalmanFilterResult<traj_t>>();

auto result = m_propagator.template propagate(sParameters, propOptions,
std::move(inputResult));
r.fittedStates = trajectory;
r.stateBuffer = stateBuffer;
r.stateBuffer->clear();

if (!result.ok()) {
ACTS_ERROR("Propapation failed: "
<< result.error() << " " << result.error().message()
<< " with the initial parameters " << iseed << " : \n"
<< sParameters.parameters());
// Emplace back the failed result
ckfResults.emplace_back(result.error());
continue;
}
auto result = m_propagator.template propagate(
initialParameters, propOptions, std::move(inputResult));

auto& propRes = *result;

/// Get the result of the CombinatorialKalmanFilter
auto combKalmanResult = std::move(
propRes.template get<CombinatorialKalmanFilterResult<traj_t>>());

/// The propagation could already reach max step size
/// before the track finding is finished during two phases:
// -> filtering for track finding;
// -> surface targeting to get fitted parameters at target surface.
// This is regarded as a failure.
// @TODO: Implement distinguishment between the above two cases if
// necessary
if (combKalmanResult.result.ok() and not combKalmanResult.finished) {
combKalmanResult.result = Result<void>(
CombinatorialKalmanFilterError::PropagationReachesMaxSteps);
}
if (!result.ok()) {
ACTS_ERROR("Propapation failed: " << result.error() << " "
<< result.error().message()
<< " with the initial parameters: \n"
<< initialParameters.parameters());
return result.error();
}

if (!combKalmanResult.result.ok()) {
ACTS_ERROR("CombinatorialKalmanFilter failed: "
<< combKalmanResult.result.error() << " "
<< combKalmanResult.result.error().message()
<< " with the initial parameters " << iseed << " : \n"
<< sParameters.parameters());
// Emplace back the failed result
ckfResults.emplace_back(combKalmanResult.result.error());
continue;
}
auto& propRes = *result;

/// Get the result of the CombinatorialKalmanFilter
auto combKalmanResult = std::move(
propRes.template get<CombinatorialKalmanFilterResult<traj_t>>());

/// The propagation could already reach max step size
/// before the track finding is finished during two phases:
// -> filtering for track finding;
// -> surface targeting to get fitted parameters at target surface.
// This is regarded as a failure.
// @TODO: Implement distinguishment between the above two cases if
// necessary
if (combKalmanResult.result.ok() and not combKalmanResult.finished) {
combKalmanResult.result = Result<void>(
CombinatorialKalmanFilterError::PropagationReachesMaxSteps);
}

// Emplace back the successful result
ckfResults.emplace_back(std::move(combKalmanResult));
if (!combKalmanResult.result.ok()) {
ACTS_ERROR("CombinatorialKalmanFilter failed: "
<< combKalmanResult.result.error() << " "
<< combKalmanResult.result.error().message()
<< " with the initial parameters: \n"
<< initialParameters.parameters());
return combKalmanResult.result.error();
}

return ckfResults;
return combKalmanResult;
}

}; // namespace Acts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ class TrackFindingAlgorithm final : public BareAlgorithm {
using TrackFinderOptions =
Acts::CombinatorialKalmanFilterOptions<IndexSourceLinkAccessor::Iterator,
Acts::VectorMultiTrajectory>;
using TrackFinderResult = std::vector<Acts::Result<
Acts::CombinatorialKalmanFilterResult<Acts::VectorMultiTrajectory>>>;
using TrackFinderResult = Acts::Result<
Acts::CombinatorialKalmanFilterResult<Acts::VectorMultiTrajectory>>;

/// Find function that takes the above parameters
/// @note This is separated into a virtual interface to keep compilation units
Expand All @@ -44,7 +44,7 @@ class TrackFindingAlgorithm final : public BareAlgorithm {
public:
virtual ~TrackFinderFunction() = default;
virtual TrackFinderResult operator()(
const TrackParametersContainer&, const TrackFinderOptions&,
const TrackParameters&, const TrackFinderOptions&,
std::shared_ptr<Acts::VectorMultiTrajectory>) const = 0;
};

Expand Down Expand Up @@ -92,7 +92,7 @@ class TrackFindingAlgorithm final : public BareAlgorithm {
private:
template <typename source_link_accessor_container_t>
void computeSharedHits(const source_link_accessor_container_t& sourcelinks,
TrackFinderResult& /*result*/) const;
std::vector<TrackFinderResult>& result) const;

ActsExamples::ProcessCode finalize() const override;

Expand All @@ -114,7 +114,7 @@ class TrackFindingAlgorithm final : public BareAlgorithm {
template <typename source_link_accessor_container_t>
void TrackFindingAlgorithm::computeSharedHits(
const source_link_accessor_container_t& sourceLinks,
TrackFinderResult& results) const {
std::vector<TrackFinderResult>& results) const {
// Compute shared hits from all the reconstructed tracks
// Compute nSharedhits and Update ckf results
// hit index -> list of multi traj indexes [traj, meas]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,12 @@ ActsExamples::ProcessCode ActsExamples::TrackFindingAlgorithm::execute(
<< " seeds.");

auto mtj = std::make_shared<Acts::VectorMultiTrajectory>();
auto results = (*m_cfg.findTracks)(initialParameters, options, mtj);
std::vector<TrackFinderResult> results;
results.reserve(initialParameters.size());
for (std::size_t iseed = 0; iseed < initialParameters.size(); ++iseed) {
results.push_back(
(*m_cfg.findTracks)(initialParameters.at(iseed), options, mtj));
}

// Compute shared hits from all the reconstructed tracks
if (m_cfg.computeSharedHits) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ struct TrackFinderFunctionImpl
TrackFinderFunctionImpl(CKF&& f) : trackFinder(std::move(f)) {}

ActsExamples::TrackFindingAlgorithm::TrackFinderResult operator()(
const ActsExamples::TrackParametersContainer& initialParameters,
const ActsExamples::TrackParameters& initialParameters,
const ActsExamples::TrackFindingAlgorithm::TrackFinderOptions& options,
std::shared_ptr<Acts::VectorMultiTrajectory> trajectory) const override {
return trackFinder.findTracks(initialParameters, options,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,15 @@ BOOST_AUTO_TEST_CASE(ZeroFieldForward) {
&slAccessor);

// run the CKF for all initial track states
auto results = f.ckf.findTracks(f.startParameters, options);
std::vector<Acts::Result<
Acts::CombinatorialKalmanFilterResult<Acts::VectorMultiTrajectory>>>
results;
auto mtj = std::make_shared<Acts::VectorMultiTrajectory>();
for (size_t trackId = 0u; trackId < f.startParameters.size(); ++trackId) {
results.push_back(
f.ckf.findTracks(f.startParameters.at(trackId), options, mtj));
}

// There should be three track finding results with three initial track states
BOOST_CHECK_EQUAL(results.size(), 3u);

Expand Down Expand Up @@ -340,7 +348,14 @@ BOOST_AUTO_TEST_CASE(ZeroFieldBackward) {
&slAccessor);

// run the CKF for all initial track states
auto results = f.ckf.findTracks(f.endParameters, options);
std::vector<Acts::Result<
Acts::CombinatorialKalmanFilterResult<Acts::VectorMultiTrajectory>>>
results;
auto mtj = std::make_shared<Acts::VectorMultiTrajectory>();
for (size_t trackId = 0u; trackId < f.startParameters.size(); ++trackId) {
results.push_back(
f.ckf.findTracks(f.endParameters.at(trackId), options, mtj));
}
// There should be three found tracks with three initial track states
BOOST_CHECK_EQUAL(results.size(), 3u);

Expand Down

0 comments on commit 23b307d

Please sign in to comment.