Belle II Software development
WithWeightedItems.h
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8#pragma once
9
10#include <tracking/trackingUtilities/numerics/WithWeight.h>
11
12#include <vector>
13#include <algorithm>
14#include <numeric>
15#include <cmath>
16
17namespace Belle2 {
22 namespace TrackFindingCDC {
24 template <class T, class AItem>
25 class WithWeightedItems : public T {
26
27 private:
29 using Super = T;
30
33
34 public:
36 using T::T;
37
39 using T::operator=;
40
42 explicit WithWeightedItems(const T& t)
43 : T(t)
44 {
45 }
46
48 TrackingUtilities::Weight getWeight() const
49 {
50 return m_weightSum;
51 }
52
54 template <class APredicate>
55 void eraseIf(const APredicate& predicate)
56 {
57 // Sneakily hide the items instead of erasing them.
58 // auto notPredicate = [&predicate](AItem & item) { return not predicate(item); };
59 // m_itEnd = std::partition(m_items.begin(), m_items.end(), notPredicate);
60 // Properly delete use the following.
61 m_itEnd = std::remove_if(m_items.begin(), m_items.end(), predicate);
62 if (m_itEnd != m_items.end()) {
63 m_items.erase(m_itEnd, m_items.end());
64 // Recompute the cumulated weight in the same order as a fresh accumulation
65 m_weightSum = std::accumulate(begin(),
66 end(),
67 static_cast<TrackingUtilities::Weight>(0.0),
68 [](TrackingUtilities::Weight accumulatedWeight, const TrackingUtilities::WithWeight<AItem>& weightedItem) {
69 return accumulatedWeight + weightedItem.getWeight();
70 });
71 }
72 }
73
75 void insert(const AItem& item, TrackingUtilities::Weight weight = 1.0)
76 {
77 // The end marker always coincides with the end of the item vector, hence append.
78 m_items.emplace_back(item, weight);
79 m_itEnd = m_items.end();
80 m_weightSum += weight;
81 }
82
84 template <class AMeasure>
85 void insert(const This& items, AMeasure& measure)
86 {
87 for (AItem item : items.m_items) {
88 const TrackingUtilities::Weight weight = measure(item);
89 if (not std::isnan(weight)) {
90 insert(item, weight);
91 }
92 }
93 }
94
96 typename std::vector<TrackingUtilities::WithWeight<AItem>>::iterator begin()
97 {
98 return m_items.begin();
99 }
100
102 typename std::vector<TrackingUtilities::WithWeight<AItem>>::const_iterator begin() const
103 {
104 return m_items.begin();
105 }
106
108 typename std::vector<TrackingUtilities::WithWeight<AItem>>::iterator end()
109 {
110 return m_itEnd;
111 }
112
114 typename std::vector<TrackingUtilities::WithWeight<AItem>>::const_iterator end() const
115 {
116 return m_itEnd;
117 }
118
120 size_t size() const
121 {
122 return std::distance(begin(), end());
123 }
124
125 public:
127 void clear()
128 {
129 m_items.clear();
130 m_itEnd = m_items.end();
131 m_weightSum = 0.0;
132 Super& super = *this;
133 TrackingUtilities::clearIfApplicable(super);
134 }
135
136 private:
138 std::vector<TrackingUtilities::WithWeight<AItem>> m_items;
139
141 typename std::vector<TrackingUtilities::WithWeight<AItem>>::iterator m_itEnd{m_items.end()};
142
144 TrackingUtilities::Weight m_weightSum = 0.0;
145 };
146 }
148}
WithWeightedItems< T, AItem > This
Type of this class.
std::vector< TrackingUtilities::WithWeight< AItem > >::const_iterator end() const
End iterator of the contained items.
size_t size() const
Getter for the number of items.
std::vector< TrackingUtilities::WithWeight< AItem > > m_items
Memory for the weighted items.
TrackingUtilities::Weight getWeight() const
Cumulated weight of the contained items.
void insert(const AItem &item, TrackingUtilities::Weight weight=1.0)
Add an item with weight.
void eraseIf(const APredicate &predicate)
Erase items from this node that satisfy the predicate.
WithWeightedItems(const T &t)
Also forward the copy constructor form the base class object.
std::vector< TrackingUtilities::WithWeight< AItem > >::const_iterator begin() const
Begin iterator of the contained items.
std::vector< TrackingUtilities::WithWeight< AItem > >::iterator end()
End iterator of the contained items.
TrackingUtilities::Weight m_weightSum
Cumulated weight of the contained items, kept up to date on insertion and removal.
std::vector< TrackingUtilities::WithWeight< AItem > >::iterator begin()
Begin iterator of the contained items.
void insert(const This &items, AMeasure &measure)
Add the items from another item range assigning a weight from the predicate.
void clear()
Clear the contained items.
std::vector< TrackingUtilities::WithWeight< AItem > >::iterator m_itEnd
Memory for the end of the items that are not erased.
A mixin class to attach a weight to an object.
Definition WithWeight.h:24
Weight getWeight() const
Getter for the weight.
Definition WithWeight.h:56
Abstract base class for different kinds of events.