Belle II Software  release-05-01-25
WithWeightedItems.h
1 /**************************************************************************
2 * BASF2 (Belle Analysis Framework 2) *
3 * Copyright(C) 2015 - Belle II Collaboration *
4 * *
5 * Author: The Belle II Collaboration *
6 * Contributors: Oliver Frost *
7 * *
8 * This software is provided "as is" without any warranty. *
9 **************************************************************************/
10 #pragma once
11 
12 #include <tracking/trackFindingCDC/numerics/WithWeight.h>
13 
14 #include <vector>
15 #include <algorithm>
16 #include <numeric>
17 #include <cmath>
18 
19 namespace Belle2 {
24  namespace TrackFindingCDC {
26  template <class T, class AItem>
27  class WithWeightedItems : public T {
28 
29  private:
31  using Super = T;
32 
34  using This = WithWeightedItems<T, AItem>;
35 
36  public:
38  using T::T;
39 
41  using T::operator=;
42 
44  explicit WithWeightedItems(const T& t)
45  : T(t)
46  {
47  }
48 
50  Weight getWeight() const
51  {
52  return std::accumulate(begin(),
53  end(),
54  static_cast<Weight>(0.0),
55  [](Weight accumulatedWeight, const WithWeight<AItem>& weightedItem) {
56  return accumulatedWeight + weightedItem.getWeight();
57  });
58  }
59 
61  template <class APredicate>
62  void eraseIf(const APredicate& predicate)
63  {
64  // Sneakily hide the items instead of erasing them.
65  // auto notPredicate = [&predicate](AItem & item) { return not predicate(item); };
66  // m_itEnd = std::partition(m_items.begin(), m_items.end(), notPredicate);
67  // Properly delete use the following.
68  m_itEnd = std::remove_if(m_items.begin(), m_items.end(), predicate);
69  m_items.erase(m_itEnd, m_items.end());
70  }
71 
73  void insert(const AItem& item, Weight weight = 1.0)
74  {
75  m_itEnd = m_items.insert(end(), WithWeight<AItem>(item));
76  m_itEnd->setWeight(weight);
77  ++m_itEnd;
78  }
79 
81  template <class AMeasure>
82  void insert(const This& items, AMeasure& measure)
83  {
84  for (AItem item : items.m_items) {
85  const Weight weight = measure(item);
86  if (not std::isnan(weight)) {
87  insert(item, weight);
88  }
89  }
90  }
91 
93  typename std::vector<WithWeight<AItem>>::iterator begin()
94  {
95  return m_items.begin();
96  }
97 
99  typename std::vector<WithWeight<AItem>>::const_iterator begin() const
100  {
101  return m_items.begin();
102  }
103 
105  typename std::vector<WithWeight<AItem>>::iterator end()
106  {
107  return m_itEnd;
108  }
109 
111  typename std::vector<WithWeight<AItem>>::const_iterator end() const
112  {
113  return m_itEnd;
114  }
115 
117  size_t size() const
118  {
119  return std::distance(begin(), end());
120  }
121 
122  public:
124  void clear()
125  {
126  m_items.clear();
127  m_itEnd = m_items.end();
128  Super& super = *this;
129  clearIfApplicable(super);
130  }
131 
132  private:
134  std::vector<WithWeight<AItem>> m_items;
135 
137  typename std::vector<WithWeight<AItem>>::iterator m_itEnd{m_items.end()};
138  };
139  }
141 }
Belle2::TrackFindingCDC::WithWeightedItems::size
size_t size() const
Getter for the number of items.
Definition: WithWeightedItems.h:125
Belle2::TrackFindingCDC::WithWeightedItems::This
WithWeightedItems< T, AItem > This
Type of this class.
Definition: WithWeightedItems.h:42
Belle2::TrackFindingCDC::WithWeightedItems::m_items
std::vector< WithWeight< AItem > > m_items
Memory for the weighted items.
Definition: WithWeightedItems.h:142
Belle2::TrackFindingCDC::WithWeightedItems::eraseIf
void eraseIf(const APredicate &predicate)
Erase items from this node that satisfy the predicate.
Definition: WithWeightedItems.h:70
Belle2::TrackFindingCDC::WithWeightedItems::m_itEnd
std::vector< WithWeight< AItem > >::iterator m_itEnd
Memory for the end of the items that are not erased.
Definition: WithWeightedItems.h:145
Belle2::TrackFindingCDC::WithWeightedItems::getWeight
Weight getWeight() const
Cumulated weight of the contained items.
Definition: WithWeightedItems.h:58
Belle2::TrackFindingCDC::WithWeightedItems::insert
void insert(const AItem &item, Weight weight=1.0)
Add an item with weight.
Definition: WithWeightedItems.h:81
Belle2::TrackFindingCDC::WithWeightedItems::end
std::vector< WithWeight< AItem > >::iterator end()
End iterator of the contained items.
Definition: WithWeightedItems.h:113
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::TrackFindingCDC::WithWeightedItems::end
std::vector< WithWeight< AItem > >::const_iterator end() const
End iterator of the contained items.
Definition: WithWeightedItems.h:119
Belle2::TrackFindingCDC::WithWeight< AItem >
Belle2::TrackFindingCDC::WithWeight::getWeight
Weight getWeight() const
Getter for the weight.
Definition: WithWeight.h:66
Belle2::TrackFindingCDC::WithWeightedItems::WithWeightedItems
WithWeightedItems(const T &t)
Also forward the copy constructor form the base class object.
Definition: WithWeightedItems.h:52
Belle2::TrackFindingCDC::WithWeightedItems
A mixin class to attach a set of weighted items to a class.
Definition: WithWeightedItems.h:35
Belle2::TrackFindingCDC::WithWeightedItems::begin
std::vector< WithWeight< AItem > >::iterator begin()
Begin iterator of the contained items.
Definition: WithWeightedItems.h:101
Belle2::TrackFindingCDC::WithWeightedItems::clear
void clear()
Clear the contained items.
Definition: WithWeightedItems.h:132
Belle2::TrackFindingCDC::WithWeightedItems::Super
T Super
Type of the base class.
Definition: WithWeightedItems.h:39