|
11 | 11 | #include "Acts/EventData/TrackParameters.hpp" |
12 | 12 | #include "Acts/TrackFitting/detail/GsfComponentMerging.hpp" |
13 | 13 |
|
| 14 | +#include <cstddef> |
| 15 | +#include <numeric> |
| 16 | +#include <ranges> |
| 17 | + |
14 | 18 | namespace Acts { |
15 | 19 |
|
16 | 20 | BoundTrackParameters MultiComponentBoundTrackParameters::merge( |
17 | 21 | const ComponentMergeMethod method) const { |
18 | | - const bool hasCov = std::get<2>(m_components.front()).has_value(); |
| 22 | + if (size() == 0) { |
| 23 | + throw std::logic_error( |
| 24 | + "Cannot merge MultiComponentBoundTrackParameters with zero components"); |
| 25 | + } |
| 26 | + |
| 27 | + if (size() == 1) { |
| 28 | + return BoundTrackParameters( |
| 29 | + m_surface, m_parameters[0], |
| 30 | + hasCovariance() ? std::optional(m_covariances[0]) : std::nullopt, |
| 31 | + m_particleHypothesis); |
| 32 | + } |
19 | 33 |
|
20 | | - if (!hasCov) { |
| 34 | + if (!hasCovariance()) { |
21 | 35 | const BoundVector singleParams = detail::Gsf::mergeGaussianMixtureParams( |
22 | | - m_components, |
23 | | - [](const auto& cmp) -> std::tuple<double, const BoundVector&> { |
24 | | - return {std::get<0>(cmp), std::get<1>(cmp)}; |
| 36 | + std::views::iota(std::size_t{0}, size()), |
| 37 | + [this](const std::size_t i) -> std::tuple<double, const BoundVector&> { |
| 38 | + return {m_weights[i], m_parameters[i]}; |
25 | 39 | }, |
26 | 40 | *m_surface, method); |
27 | 41 | return BoundTrackParameters(m_surface, singleParams, std::nullopt, |
28 | 42 | m_particleHypothesis); |
29 | 43 | } |
30 | 44 |
|
31 | 45 | const auto [singleParams, singleCov] = detail::Gsf::mergeGaussianMixture( |
32 | | - m_components, |
33 | | - [](const auto& cmp) |
| 46 | + std::views::iota(std::size_t{0}, size()), |
| 47 | + [this](const std::size_t i) |
34 | 48 | -> std::tuple<double, const BoundVector&, const BoundMatrix&> { |
35 | | - return {std::get<0>(cmp), std::get<1>(cmp), std::get<2>(cmp).value()}; |
| 49 | + return {m_weights[i], m_parameters[i], m_covariances[i]}; |
36 | 50 | }, |
37 | 51 | *m_surface, method); |
38 | 52 | return BoundTrackParameters(m_surface, singleParams, singleCov, |
39 | 53 | m_particleHypothesis); |
40 | 54 | } |
41 | 55 |
|
| 56 | +void MultiComponentBoundTrackParameters::reserve(const std::size_t n) { |
| 57 | + m_weights.reserve(n); |
| 58 | + m_parameters.reserve(n); |
| 59 | + if (hasCovariance()) { |
| 60 | + m_covariances.reserve(n); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +void MultiComponentBoundTrackParameters::clear() { |
| 65 | + m_weights.clear(); |
| 66 | + m_parameters.clear(); |
| 67 | + m_covariances.clear(); |
| 68 | +} |
| 69 | + |
| 70 | +void MultiComponentBoundTrackParameters::pushComponent( |
| 71 | + const double weight, const ParametersVector& params) { |
| 72 | + if (hasCovariance()) { |
| 73 | + throw std::logic_error( |
| 74 | + "Cannot push component without covariance to " |
| 75 | + "MultiComponentBoundTrackParameters with covariance"); |
| 76 | + } |
| 77 | + |
| 78 | + m_weights.push_back(weight); |
| 79 | + m_parameters.push_back(params); |
| 80 | +} |
| 81 | + |
| 82 | +void MultiComponentBoundTrackParameters::pushComponent( |
| 83 | + const double weight, const ParametersVector& params, |
| 84 | + const CovarianceMatrix& cov) { |
| 85 | + if (!hasCovariance()) { |
| 86 | + throw std::logic_error( |
| 87 | + "Cannot push component with covariance to " |
| 88 | + "MultiComponentBoundTrackParameters without covariance"); |
| 89 | + } |
| 90 | + |
| 91 | + m_weights.push_back(weight); |
| 92 | + m_parameters.push_back(params); |
| 93 | + m_covariances.push_back(cov); |
| 94 | +} |
| 95 | + |
| 96 | +void MultiComponentBoundTrackParameters::pushComponent( |
| 97 | + const double weight, const ParametersVector& params, |
| 98 | + const std::optional<CovarianceMatrix>& cov) { |
| 99 | + if (hasCovariance() != cov.has_value()) { |
| 100 | + } |
| 101 | + |
| 102 | + m_weights.push_back(weight); |
| 103 | + m_parameters.push_back(params); |
| 104 | + if (hasCovariance()) { |
| 105 | + m_covariances.push_back(*cov); |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +void MultiComponentBoundTrackParameters::normalizeWeights() { |
| 110 | + const double sumWeights = |
| 111 | + std::accumulate(m_weights.begin(), m_weights.end(), 0.0); |
| 112 | + if (sumWeights <= 0.0) { |
| 113 | + throw std::logic_error( |
| 114 | + "Cannot normalize weights of MultiComponentBoundTrackParameters: sum " |
| 115 | + "of weights is not strictly positive: " + |
| 116 | + std::to_string(sumWeights)); |
| 117 | + } |
| 118 | + for (double& weight : m_weights) { |
| 119 | + weight /= sumWeights; |
| 120 | + } |
| 121 | +} |
| 122 | + |
42 | 123 | } // namespace Acts |
0 commit comments