-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Factorize track selector tool from algorithm (#2267)
This separates out the track selection logic itself from the `TrackSelectorAlgorithm` (formerly called just `TrackSelector`). This is in preparation for #2206.
- Loading branch information
1 parent
f118922
commit c0b9fcc
Showing
9 changed files
with
255 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// 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 <cmath> | ||
#include <limits> | ||
|
||
namespace Acts { | ||
|
||
/// Class which performs filtering of tracks. It accepts an input and an output | ||
/// track container and uses the built-in copy facility to copy tracks into the | ||
/// output container. | ||
class TrackSelector { | ||
public: | ||
struct Config { | ||
// Minimum/maximum local positions. | ||
double loc0Min = -std::numeric_limits<double>::infinity(); | ||
double loc0Max = std::numeric_limits<double>::infinity(); | ||
double loc1Min = -std::numeric_limits<double>::infinity(); | ||
double loc1Max = std::numeric_limits<double>::infinity(); | ||
// Minimum/maximum track time. | ||
double timeMin = -std::numeric_limits<double>::infinity(); | ||
double timeMax = std::numeric_limits<double>::infinity(); | ||
// Direction cuts. | ||
double phiMin = -std::numeric_limits<double>::infinity(); | ||
double phiMax = std::numeric_limits<double>::infinity(); | ||
double etaMin = -std::numeric_limits<double>::infinity(); | ||
double etaMax = std::numeric_limits<double>::infinity(); | ||
double absEtaMin = 0.0; | ||
double absEtaMax = std::numeric_limits<double>::infinity(); | ||
// Momentum cuts. | ||
double ptMin = 0.0; | ||
double ptMax = std::numeric_limits<double>::infinity(); | ||
|
||
std::size_t minMeasurements = 0; | ||
}; | ||
|
||
/// Constructor from a config object | ||
/// @param config is the configuration object | ||
TrackSelector(const Config& config) : m_cfg(config) {} | ||
|
||
/// Select tracks from an input container and copy them into an output | ||
/// container | ||
/// @tparam input_tracks_t is the type of the input track container | ||
/// @tparam output_tracks_t is the type of the output track container | ||
/// @param inputTracks is the input track container | ||
/// @param outputTracks is the output track container | ||
template <typename input_tracks_t, typename output_tracks_t> | ||
void selectTracks(const input_tracks_t& inputTracks, | ||
output_tracks_t& outputTracks) const; | ||
|
||
/// Helper function to check if a track is valid | ||
/// @tparam track_proxy_t is the type of the track proxy | ||
/// @param track is the track proxy | ||
/// @return true if the track is valid | ||
template <typename track_proxy_t> | ||
bool isValidTrack(const track_proxy_t& track) const; | ||
|
||
/// Get readonly access to the config parameters | ||
/// @return the config object | ||
const Config& config() const { return m_cfg; } | ||
|
||
private: | ||
Config m_cfg; | ||
}; | ||
|
||
template <typename input_tracks_t, typename output_tracks_t> | ||
void TrackSelector::selectTracks(const input_tracks_t& inputTracks, | ||
output_tracks_t& outputTracks) const { | ||
for (auto track : inputTracks) { | ||
if (!isValidTrack(track)) { | ||
continue; | ||
} | ||
auto destProxy = outputTracks.getTrack(outputTracks.addTrack()); | ||
destProxy.copyFrom(track, false); | ||
destProxy.tipIndex() = track.tipIndex(); | ||
} | ||
} | ||
|
||
template <typename track_proxy_t> | ||
bool TrackSelector::isValidTrack(const track_proxy_t& track) const { | ||
auto checkMin = [](auto x, auto min) { return min <= x; }; | ||
auto within = [](double x, double min, double max) { | ||
return (min <= x) and (x < max); | ||
}; | ||
const auto theta = track.theta(); | ||
const auto eta = -std::log(std::tan(theta / 2)); | ||
return within(track.transverseMomentum(), m_cfg.ptMin, m_cfg.ptMax) and | ||
within(std::abs(eta), m_cfg.absEtaMin, m_cfg.absEtaMax) and | ||
within(eta, m_cfg.etaMin, m_cfg.etaMax) and | ||
within(track.phi(), m_cfg.phiMin, m_cfg.phiMax) and | ||
within(track.loc0(), m_cfg.loc0Min, m_cfg.loc0Max) and | ||
within(track.loc1(), m_cfg.loc1Min, m_cfg.loc1Max) and | ||
within(track.time(), m_cfg.timeMin, m_cfg.timeMax) and | ||
checkMin(track.nMeasurements(), m_cfg.minMeasurements); | ||
} | ||
|
||
} // namespace Acts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
Examples/Algorithms/Utilities/src/TrackSelectorAlgorithm.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// This file is part of the Acts project. | ||
// | ||
// Copyright (C) 2019-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/. | ||
|
||
#include "ActsExamples/Utilities/TrackSelectorAlgorithm.hpp" | ||
|
||
#include "Acts/EventData/TrackContainer.hpp" | ||
#include "Acts/EventData/TrackProxy.hpp" | ||
#include "Acts/EventData/VectorMultiTrajectory.hpp" | ||
#include "Acts/EventData/VectorTrackContainer.hpp" | ||
#include "ActsExamples/EventData/Track.hpp" | ||
|
||
#include <cmath> | ||
#include <memory> | ||
#include <stdexcept> | ||
#include <utility> | ||
|
||
namespace ActsExamples { | ||
struct AlgorithmContext; | ||
} // namespace ActsExamples | ||
|
||
ActsExamples::TrackSelectorAlgorithm::TrackSelectorAlgorithm( | ||
const Config& config, Acts::Logging::Level level) | ||
: IAlgorithm("TrackSelector", level), | ||
m_cfg(config), | ||
m_selector(config.selectorConfig) { | ||
if (m_cfg.inputTracks.empty()) { | ||
throw std::invalid_argument("Input track collection is empty"); | ||
} | ||
|
||
if (m_cfg.outputTracks.empty()) { | ||
throw std::invalid_argument("Output track collection is empty"); | ||
} | ||
|
||
m_inputTrackContainer.initialize(m_cfg.inputTracks); | ||
m_outputTrackContainer.initialize(m_cfg.outputTracks); | ||
} | ||
|
||
ActsExamples::ProcessCode ActsExamples::TrackSelectorAlgorithm::execute( | ||
const ActsExamples::AlgorithmContext& ctx) const { | ||
ACTS_VERBOSE("Reading tracks from: " << m_cfg.inputTracks); | ||
|
||
const auto& inputTracks = m_inputTrackContainer(ctx); | ||
|
||
std::shared_ptr<Acts::ConstVectorMultiTrajectory> trackStateContainer = | ||
inputTracks.trackStateContainerHolder(); | ||
|
||
auto trackContainer = std::make_shared<Acts::VectorTrackContainer>(); | ||
|
||
// temporary empty track state container: we don't change the original one, | ||
// but we need one for filtering | ||
auto tempTrackStateContainer = | ||
std::make_shared<Acts::VectorMultiTrajectory>(); | ||
|
||
TrackContainer filteredTracks{trackContainer, tempTrackStateContainer}; | ||
filteredTracks.ensureDynamicColumns(inputTracks); | ||
|
||
ACTS_DEBUG("Track container size before filtering: " << inputTracks.size()); | ||
|
||
m_selector.selectTracks(inputTracks, filteredTracks); | ||
|
||
ACTS_DEBUG("Track container size after filtering: " << filteredTracks.size()); | ||
|
||
ConstTrackContainer outputTracks{ | ||
std::make_shared<Acts::ConstVectorTrackContainer>( | ||
std::move(*trackContainer)), | ||
trackStateContainer}; | ||
|
||
m_outputTrackContainer(ctx, std::move(outputTracks)); | ||
|
||
return ProcessCode::SUCCESS; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.