ATLAS Offline Software
MultiComponentStateAssembler.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3 */
4 
15 //
16 #include <limits>
17 
18 namespace {
19 
20 using namespace Trk::MultiComponentStateAssembler;
21 
23 inline bool
24 isStateValid(const Cache& cache)
25 {
26  return !cache.multiComponentState.empty();
27 }
28 
31 doStateAssembly(Cache&& cache, const double newWeight)
32 {
33  if (!isStateValid(cache)) {
34  return {};
35  }
36  const size_t cacheSize = cache.multiComponentState.size();
37  if (cache.validWeightSum <= 0.) {
38  if (!cache.multiComponentState.empty()) {
39  const double fixedWeights = 1. / static_cast<double>(cacheSize);
40  for (auto& component : cache.multiComponentState) {
41  component.weight = fixedWeights;
42  }
43  }
45  std::move(cache.multiComponentState);
46  // Reset the cache before leaving
47  return assembledState;
48  }
49 
51  std::move(cache.multiComponentState);
52  const double scalingFactor =
53  cache.validWeightSum > 0. ? newWeight / cache.validWeightSum : 1.;
54  for (auto& component : assembledState) {
55  component.weight *= scalingFactor;
56  }
57  // Reset the cache before leaving
58  return assembledState;
59 }
60 
62 bool
63 prepareStateForAssembly(Cache& cache)
64 {
65  // Protect against empty state
66  if (!isStateValid(cache)) {
67  return false;
68  }
69 
70  // Check for minimum fraction of valid states
71  double den = cache.validWeightSum + cache.invalidWeightSum;
72  double validWeightFraction = den > 0 ? cache.validWeightSum / den : 0;
73  if (cache.invalidWeightSum > 0. &&
75  return false;
76  }
77  // Sort Multi-Component State by weights
78  std::sort(
79  cache.multiComponentState.begin(), cache.multiComponentState.end(),
81  return x.weight > y.weight;
82  });
83 
84  double totalWeight(cache.validWeightSum + cache.invalidWeightSum);
85  if (totalWeight != 0.) {
86 
87  // ordered in descending order
88  // return the 1st element where (element<value)
89  const double minimumWeight =
92 
93  const Trk::ComponentParameters dummySmallestWeight = {nullptr, minimumWeight};
94 
95  auto lower_than = std::upper_bound(
96  cache.multiComponentState.begin(), cache.multiComponentState.end(),
97  dummySmallestWeight,
98  [](const Trk::ComponentParameters& x,
99  const Trk::ComponentParameters& y) { return x.weight > y.weight; });
100 
101  // reverse iterate , so as to delete removing the last
102  auto lower_than_reverse = std::make_reverse_iterator(lower_than);
103  for (auto itr = cache.multiComponentState.rbegin();
104  itr != lower_than_reverse;
105  ++itr) {
106  cache.multiComponentState.erase(itr.base() - 1);
107  }
108  }
109  // Now recheck to make sure the state is now still valid
110  if (!isStateValid(cache)) {
111  return false;
112  }
113 
114  return true;
115 }
116 
117 } // end anonymous namespace
118 
122 {
123  if (!prepareStateForAssembly(cache)) {
124  return {};
125  }
126  double totalWeight = cache.validWeightSum;
127  if (cache.invalidWeightSum > 0. || cache.validWeightSum <= 0.) {
128  totalWeight = cache.validWeightSum + cache.invalidWeightSum;
129  }
130 
131  return doStateAssembly(std::move(cache), totalWeight);
132 }
133 
Trk::y
@ y
Definition: ParamDefs.h:62
Trk::MultiComponentStateAssembler
Helper struct representing a cache of the Multicomponent state under assembly.
Definition: MultiComponentStateAssembler.h:34
max
#define max(a, b)
Definition: cfImp.cxx:41
Trk::MultiComponentStateAssembler::assembledState
MultiComponentState assembledState(MultiComponentStateAssembler::Cache &&cache)
Method to return the cached state object - it performs a reweighting before returning the object base...
Definition: MultiComponentStateAssembler.cxx:120
Trk::MultiComponentStateAssembler::Cache
Definition: MultiComponentStateAssembler.h:35
Trk::MultiComponentStateAssembler::Cache::minimumValidFraction
static constexpr double minimumValidFraction
Definition: MultiComponentStateAssembler.h:42
MultiComponentStateAssembler.h
Trk::MultiComponentState
std::vector< ComponentParameters > MultiComponentState
Definition: ComponentParameters.h:27
min
#define min(a, b)
Definition: cfImp.cxx:40
Trk::MultiComponentStateAssembler::Cache::minimumFractionalWeight
static constexpr double minimumFractionalWeight
Definition: MultiComponentStateAssembler.h:43
Trk::MultiComponentStateAssembler::Cache::invalidWeightSum
double invalidWeightSum
Definition: MultiComponentStateAssembler.h:41
Trk::ComponentParameters
Definition: ComponentParameters.h:22
ComponentParameters.h
Definition of component parameters for use in a mixture of many components. In this regime each track...
Trk::MultiComponentStateAssembler::Cache::multiComponentState
Trk::MultiComponentState multiComponentState
Definition: MultiComponentStateAssembler.h:39
std::sort
void sort(typename std::reverse_iterator< DataModel_detail::iterator< DVL > > beg, typename std::reverse_iterator< DataModel_detail::iterator< DVL > > end, const Compare &comp)
Specialization of sort for DataVector/List.
Definition: DVL_algorithms.h:623
Trk::x
@ x
Definition: ParamDefs.h:61
Trk::MultiComponentStateAssembler::Cache::validWeightSum
double validWeightSum
Definition: MultiComponentStateAssembler.h:40