Belle II Software  release-08-01-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 /* C++ headers. */
11 #include <functional>
12 #include <optional>
13 
14 namespace Belle2 {
26  template <class TItem, class TEstimator >
28  public:
29 
34  typedef std::function< bool(TEstimator, TEstimator)> EstimatorComparison;
35 
44  bool add(TItem item, TEstimator est,
45  EstimatorComparison estComparison
46  = [](TEstimator currentBest, TEstimator newEst) {return newEst < currentBest;}
47  )
48  {
49  if (!hasMatch()) {
50  // not best match yet, take this one !
51  setBestMatch(item, est);
52  return true;
53  }
54 
55  if (estComparison(m_bestEstimator, est)) {
56  setBestMatch(item, est);
57  return true;
58  }
59 
60  // best match was not updated
61  return false;
62  }
63 
67  bool hasMatch() const
68  {
69  return static_cast<bool>(m_bestMatch);
70  }
71 
76  TItem const& getBestMatch() const
77  {
78  return *m_bestMatch;
79  }
80 
81  private:
82 
86  void setBestMatch(TItem item, TEstimator est)
87  {
88  m_bestMatch = item;
89  m_bestEstimator = est;
90  }
91 
93  std::optional<TItem> m_bestMatch;
94 
96  TEstimator m_bestEstimator = TEstimator();
97  };
98 
100 } //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.