ATLAS Offline Software
Loading...
Searching...
No Matches
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
12
15//
16#include <limits>
17
18namespace {
19
21
23inline bool
24isStateValid(const Cache& cache)
25{
26 return !cache.multiComponentState.empty();
27}
28
31doStateAssembly(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
62bool
63prepareStateForAssembly(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 const den = cache.validWeightSum + cache.invalidWeightSum;
72 double const validWeightFraction = den > 0 ? cache.validWeightSum / den : 0;
73 if (cache.invalidWeightSum > 0. &&
75 return false;
76 }
77 // Sort Multi-Component State by weights
79 cache.multiComponentState.begin(), cache.multiComponentState.end(),
81 return x.weight > y.weight;
82 });
83
84 double const 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 =
91 std::numeric_limits<double>::min());
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,
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
Definition of component parameters for use in a mixture of many components. In this regime each track...
#define y
#define x
Helper struct representing a cache of the Multicomponent state under assembly.
MultiComponentState assembledState(MultiComponentStateAssembler::Cache &&cache)
Method to return the cached state object - it performs a reweighting before returning the object base...
std::vector< ComponentParameters > MultiComponentState
void sort(typename DataModel_detail::iterator< DVL > beg, typename DataModel_detail::iterator< DVL > end)
Specialization of sort for DataVector/List.