Belle II Software  release-06-02-00
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 
12 namespace Belle2 {
24  template <class TItem, class TEstimator >
26  public:
27 
32  typedef std::function< bool(TEstimator, TEstimator)> EstimatorComparison;
33 
40  bool add(TItem item, TEstimator est,
41  EstimatorComparison estComparison
42  = [](TEstimator currentBest, TEstimator newEst) {return newEst < currentBest;}
43  )
44  {
45  if (!hasMatch()) {
46  // not best match yet, take this one !
47  setBestMatch(item , est);
48  return true;
49  }
50 
51  if (estComparison(m_bestEstimator, est)) {
52  setBestMatch(item, est);
53  return true;
54  }
55 
56  // best match was not updated
57  return false;
58  }
59 
63  bool hasMatch() const
64  {
65  return static_cast<bool>(m_bestMatch);
66  }
67 
72  TItem const& getBestMatch() const
73  {
74  return *m_bestMatch;
75  }
76 
77  private:
78 
82  void setBestMatch(TItem item, TEstimator est)
83  {
84  m_bestMatch = item;
85  m_bestEstimator = est;
86  }
87 
89  std::optional<TItem> m_bestMatch;
90 
92  TEstimator m_bestEstimator = TEstimator();
93  };
94 
96 } //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.