Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GsfMixtureReduction.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file GsfMixtureReduction.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2023 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
10 
12 
13 template <typename proj_t, typename angle_desc_t>
14 void reduceWithKLDistanceImpl(std::vector<Acts::GsfComponent> &cmpCache,
15  std::size_t maxCmpsAfterMerge, const proj_t &proj,
16  const angle_desc_t &desc) {
17  Acts::detail::SymmetricKLDistanceMatrix distances(cmpCache, proj);
18 
19  auto remainingComponents = cmpCache.size();
20 
21  while (remainingComponents > maxCmpsAfterMerge) {
22  const auto [minI, minJ] = distances.minDistancePair();
23 
24  // Set one component and compute associated distances
25  cmpCache[minI] =
26  mergeComponents(cmpCache[minI], cmpCache[minJ], proj, desc);
27  distances.recomputeAssociatedDistances(minI, cmpCache, proj);
28 
29  // Set weight of the other component to -1 so we can remove it later and
30  // mask its distances
31  proj(cmpCache[minJ]).weight = -1.0;
32  distances.maskAssociatedDistances(minJ);
33 
34  remainingComponents--;
35  }
36 
37  // Remove all components which are labeled with weight -1
38  std::sort(cmpCache.begin(), cmpCache.end(),
39  [&](const auto &a, const auto &b) {
40  return proj(a).weight < proj(b).weight;
41  });
42  cmpCache.erase(
43  std::remove_if(cmpCache.begin(), cmpCache.end(),
44  [&](const auto &a) { return proj(a).weight == -1.0; }),
45  cmpCache.end());
46 
47  assert(cmpCache.size() == maxCmpsAfterMerge && "size mismatch");
48 }
49 
50 namespace Acts {
51 
52 void reduceMixtureWithKLDistance(std::vector<Acts::GsfComponent> &cmpCache,
53  std::size_t maxCmpsAfterMerge,
54  const Surface &surface) {
55  if (cmpCache.size() <= maxCmpsAfterMerge) {
56  return;
57  }
58 
59  auto proj = [](auto &a) -> decltype(auto) { return a; };
60 
61  // We must differ between surface types, since there can be different
62  // local coordinates
63  detail::angleDescriptionSwitch(surface, [&](const auto &desc) {
64  reduceWithKLDistanceImpl(cmpCache, maxCmpsAfterMerge, proj, desc);
65  });
66 }
67 
68 } // namespace Acts