Belle II Software  release-08-00-10
BestMatchContainer.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 <functional>
11 #include <optional>
12 
13 namespace Belle2 {
25  template <class TItem, class TEstimator >
27  public:
28 
33  typedef std::function< bool(TEstimator, TEstimator)> EstimatorComparison;
34 
43  bool add(TItem item, TEstimator est,
44  EstimatorComparison estComparison
45  = [](TEstimator currentBest, TEstimator newEst) {return newEst < currentBest;}
46  )
47  {
48  if (!hasMatch()) {
49  // not best match yet, take this one !
50  setBestMatch(item, est);
51  return true;
52  }
53 
54  if (estComparison(m_bestEstimator, est)) {
55  setBestMatch(item, est);
56  return true;
57  }
58 
59  // best match was not updated
60  return false;
61  }
62 
66  bool hasMatch() const
67  {
68  return static_cast<bool>(m_bestMatch);
69  }
70 
75  TItem const& getBestMatch() const
76  {
77  return *m_bestMatch;
78  }
79 
80  private:
81 
85  void setBestMatch(TItem item, TEstimator est)
86  {
87  m_bestMatch = item;
88  m_bestEstimator = est;
89  }
90 
92  std::optional<TItem> m_bestMatch;
93 
95  TEstimator m_bestEstimator = TEstimator();
96  };
97 
99 } //Belle2
Multiple entries can be added, but only the one will be kept, which has the best quality estimator.
std::function< bool(TEstimator, TEstimator)> EstimatorComparison
Lambda typedef for the function comparing estimators.
TEstimator m_bestEstimator
Stores the estimator value of the best match.
std::optional< TItem > m_bestMatch
Stores the best matched item.
TItem const & getBestMatch() const
bool add(TItem item, TEstimator est, EstimatorComparison estComparison=[](TEstimator currentBest, TEstimator newEst) {return newEst< currentBest;})
Add a new item with an estimator value.
void setBestMatch(TItem item, TEstimator est)
Set a new item as the best match.
Abstract base class for different kinds of events.