Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-03-28 07:45:34

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 CERN for the benefit of the ACTS project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
0008 
0009 #include "Acts/EventData/MultiComponentTrackParameters.hpp"
0010 
0011 #include "Acts/TrackFitting/detail/GsfComponentMerging.hpp"
0012 
0013 #include <cstddef>
0014 #include <numeric>
0015 #include <ranges>
0016 
0017 namespace Acts {
0018 
0019 BoundTrackParameters MultiComponentBoundTrackParameters::merge(
0020     const ComponentMergeMethod method) const {
0021   if (empty()) {
0022     throw std::logic_error(
0023         "Cannot merge MultiComponentBoundTrackParameters with zero components");
0024   }
0025 
0026   if (size() == 1) {
0027     return BoundTrackParameters(
0028         m_surface, m_parameters[0],
0029         hasCovariance() ? std::optional(m_covariances[0]) : std::nullopt,
0030         m_particleHypothesis);
0031   }
0032 
0033   if (!hasCovariance()) {
0034     const BoundVector singleParams = detail::Gsf::mergeGaussianMixtureParams(
0035         std::views::iota(std::size_t{0}, size()),
0036         [this](const std::size_t i) -> std::tuple<double, const BoundVector&> {
0037           return {m_weights[i], m_parameters[i]};
0038         },
0039         *m_surface, method);
0040     return BoundTrackParameters(m_surface, singleParams, std::nullopt,
0041                                 m_particleHypothesis);
0042   }
0043 
0044   const auto [singleParams, singleCov] = detail::Gsf::mergeGaussianMixture(
0045       std::views::iota(std::size_t{0}, size()),
0046       [this](const std::size_t i)
0047           -> std::tuple<double, const BoundVector&, const BoundMatrix&> {
0048         return {m_weights[i], m_parameters[i], m_covariances[i]};
0049       },
0050       *m_surface, method);
0051   return BoundTrackParameters(m_surface, singleParams, singleCov,
0052                               m_particleHypothesis);
0053 }
0054 
0055 void MultiComponentBoundTrackParameters::reserve(const std::size_t n) {
0056   m_weights.reserve(n);
0057   m_parameters.reserve(n);
0058   if (hasCovariance()) {
0059     m_covariances.reserve(n);
0060   }
0061 }
0062 
0063 void MultiComponentBoundTrackParameters::clear() {
0064   m_weights.clear();
0065   m_parameters.clear();
0066   m_covariances.clear();
0067 }
0068 
0069 void MultiComponentBoundTrackParameters::pushComponent(
0070     const double weight, const ParametersVector& params) {
0071   if (hasCovariance()) {
0072     throw std::logic_error(
0073         "Cannot push component without covariance to "
0074         "MultiComponentBoundTrackParameters with covariance");
0075   }
0076 
0077   m_weights.push_back(weight);
0078   m_parameters.push_back(params);
0079 }
0080 
0081 void MultiComponentBoundTrackParameters::pushComponent(
0082     const double weight, const ParametersVector& params,
0083     const CovarianceMatrix& cov) {
0084   if (!hasCovariance()) {
0085     throw std::logic_error(
0086         "Cannot push component with covariance to "
0087         "MultiComponentBoundTrackParameters without covariance");
0088   }
0089 
0090   m_weights.push_back(weight);
0091   m_parameters.push_back(params);
0092   m_covariances.push_back(cov);
0093 }
0094 
0095 void MultiComponentBoundTrackParameters::pushComponent(
0096     const double weight, const ParametersVector& params,
0097     const std::optional<CovarianceMatrix>& cov) {
0098   if (hasCovariance() != cov.has_value()) {
0099   }
0100 
0101   m_weights.push_back(weight);
0102   m_parameters.push_back(params);
0103   if (hasCovariance()) {
0104     m_covariances.push_back(*cov);
0105   }
0106 }
0107 
0108 void MultiComponentBoundTrackParameters::normalizeWeights() {
0109   const double sumWeights =
0110       std::accumulate(m_weights.begin(), m_weights.end(), 0.0);
0111   if (sumWeights <= 0.0) {
0112     throw std::logic_error(
0113         "Cannot normalize weights of MultiComponentBoundTrackParameters: sum "
0114         "of weights is not strictly positive: " +
0115         std::to_string(sumWeights));
0116   }
0117   for (double& weight : m_weights) {
0118     weight /= sumWeights;
0119   }
0120 }
0121 
0122 }  // namespace Acts