Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:11:31

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/TrackFitting/GsfMixtureReduction.hpp"
0010 
0011 #include "Acts/TrackFitting/detail/SymmetricKlDistanceMatrix.hpp"
0012 
0013 #include <algorithm>
0014 
0015 template <typename proj_t, typename angle_desc_t>
0016 void reduceWithKLDistanceImpl(std::vector<Acts::GsfComponent> &cmpCache,
0017                               std::size_t maxCmpsAfterMerge, const proj_t &proj,
0018                               const angle_desc_t &desc) {
0019   Acts::detail::SymmetricKLDistanceMatrix distances(cmpCache, proj);
0020 
0021   auto remainingComponents = cmpCache.size();
0022 
0023   while (remainingComponents > maxCmpsAfterMerge) {
0024     const auto [minI, minJ] = distances.minDistancePair();
0025 
0026     // Set one component and compute associated distances
0027     cmpCache[minI] =
0028         mergeComponents(cmpCache[minI], cmpCache[minJ], proj, desc);
0029     distances.recomputeAssociatedDistances(minI, cmpCache, proj);
0030 
0031     // Set weight of the other component to -1 so we can remove it later and
0032     // mask its distances
0033     proj(cmpCache[minJ]).weight = -1.0;
0034     distances.maskAssociatedDistances(minJ);
0035 
0036     remainingComponents--;
0037   }
0038 
0039   // Remove all components which are labeled with weight -1
0040   std::ranges::sort(cmpCache, {},
0041                     [&](const auto &c) { return proj(c).weight; });
0042   cmpCache.erase(
0043       std::remove_if(cmpCache.begin(), cmpCache.end(),
0044                      [&](const auto &a) { return proj(a).weight == -1.0; }),
0045       cmpCache.end());
0046 
0047   assert(cmpCache.size() == maxCmpsAfterMerge && "size mismatch");
0048 }
0049 
0050 namespace Acts {
0051 
0052 void reduceMixtureLargestWeights(std::vector<GsfComponent> &cmpCache,
0053                                  std::size_t maxCmpsAfterMerge,
0054                                  const Surface & /*surface*/) {
0055   if (cmpCache.size() <= maxCmpsAfterMerge) {
0056     return;
0057   }
0058 
0059   std::nth_element(
0060       cmpCache.begin(), cmpCache.begin() + maxCmpsAfterMerge, cmpCache.end(),
0061       [](const auto &a, const auto &b) { return a.weight > b.weight; });
0062   cmpCache.resize(maxCmpsAfterMerge);
0063 }
0064 
0065 void reduceMixtureWithKLDistance(std::vector<Acts::GsfComponent> &cmpCache,
0066                                  std::size_t maxCmpsAfterMerge,
0067                                  const Surface &surface) {
0068   if (cmpCache.size() <= maxCmpsAfterMerge) {
0069     return;
0070   }
0071 
0072   auto proj = [](auto &a) -> decltype(auto) { return a; };
0073 
0074   // We must differ between surface types, since there can be different
0075   // local coordinates
0076   detail::angleDescriptionSwitch(surface, [&](const auto &desc) {
0077     reduceWithKLDistanceImpl(cmpCache, maxCmpsAfterMerge, proj, desc);
0078   });
0079 }
0080 
0081 }  // namespace Acts