Belle II Software  release-05-01-25
BestMatchContainer.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2016 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Thomas Hauth *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 #pragma once
11 
12 #include <functional>
13 
14 namespace Belle2 {
26  template <class TItem, class TEstimator >
27  class BestMatchContainer {
28  public:
29 
34  typedef std::function< bool(TEstimator, TEstimator)> EstimatorComparison;
35 
42  bool add(TItem const& item, TEstimator est,
43  EstimatorComparison estComparison
44  = [](TEstimator currentBest, TEstimator newEst) {return newEst < currentBest;}
45  )
46  {
47  if (!hasMatch()) {
48  // not best match yet, take this one !
49  setBestMatch(item , est);
50  return true;
51  }
52 
53  if (estComparison(m_bestEstimator, est)) {
54  setBestMatch(item, est);
55  return true;
56  }
57 
58  // best match was not updated
59  return false;
60  }
61 
65  bool hasMatch() const
66  {
67  return static_cast<bool>(m_bestMatch);
68  }
69 
74  TItem const& getBestMatch() const
75  {
76  return *m_bestMatch;
77  }
78 
79  private:
80 
84  void setBestMatch(TItem const& item, TEstimator est)
85  {
86  m_bestMatch = item;
87  m_bestEstimator = est;
88  }
89 
91  std::optional<TItem> m_bestMatch;
92 
94  TEstimator m_bestEstimator = TEstimator();
95  };
96 
98 } //Belle2
Belle2::BestMatchContainer::m_bestEstimator
TEstimator m_bestEstimator
Stores the estimator value of the best match.
Definition: BestMatchContainer.h:102
Belle2::BestMatchContainer::getBestMatch
TItem const & getBestMatch() const
Definition: BestMatchContainer.h:82
Belle2::BestMatchContainer::hasMatch
bool hasMatch() const
Definition: BestMatchContainer.h:73
Belle2::BestMatchContainer::m_bestMatch
std::optional< TItem > m_bestMatch
Stores the best matched item.
Definition: BestMatchContainer.h:99
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::BestMatchContainer::setBestMatch
void setBestMatch(TItem const &item, TEstimator est)
Set a new item as the best match.
Definition: BestMatchContainer.h:92
Belle2::BestMatchContainer::add
bool add(TItem const &item, TEstimator est, EstimatorComparison estComparison=[](TEstimator currentBest, TEstimator newEst) {return newEst< currentBest;})
Add a new item with an estimator value.
Definition: BestMatchContainer.h:50
Belle2::BestMatchContainer::EstimatorComparison
std::function< bool(TEstimator, TEstimator)> EstimatorComparison
Lambda typedef for the function comparing estimators.
Definition: BestMatchContainer.h:42