CMS 3D CMS Logo

Matching.h
Go to the documentation of this file.
1 #ifndef RecoBTag_Analysis_Matching_h
2 #define RecoBTag_Analysis_Matching_h
3 
4 #include <functional>
5 #include <algorithm>
6 #include <vector>
7 #include <set>
8 
9 #include "SimpleMatrix.h"
10 
11 namespace btag {
12 
13 template<typename Delta>
14 class Matching {
15  public:
17 
18  template<typename V1, typename V2, class Separation>
19  Matching(const V1 &v1, const V2 &v2, Separation separation) :
20  matrix(v1.size(), v2.size()),
21  matched1(v1.size(), false),
22  matched2(v2.size(), false)
23  {
24  index_type i = 0;
25  for(typename V1::const_iterator iter1 = v1.begin();
26  iter1 != v1.end(); ++iter1, ++i) {
27  index_type j = 0;
28  for(typename V2::const_iterator iter2 = v2.begin();
29  iter2 != v2.end(); ++iter2, ++j)
30  matrix(i, j) = separation(*iter1, *iter2);
31 
32  }
33  }
34 
35  struct Match {
36  typedef typename Matching::index_type index_type;
37 
38  inline Match(index_type i1, index_type i2) :
39  index1(i1), index2(i2) {}
40 
41  index_type index1, index2;
42  };
43 
44  inline Delta delta(index_type index1, index_type index2) const
45  { return matrix(index1, index2); }
46 
47  inline Delta delta(Match match) const
48  { return matrix(match.index1, match.index2); }
49 
50  private:
51  template<class SortComparator>
52  struct Comparator : public std::binary_function<Delta, Delta, bool> {
53  typedef typename Matching::index_type index_type;
54 
56  SortComparator &sort) :
57  matrix(matrix), sort(sort) {}
58 
59  inline bool operator () (index_type i1, index_type i2) const
60  { return sort(matrix[i1], matrix[i2]); }
61 
63  SortComparator &sort;
64  };
65 
66  struct AlwaysTrue : public std::unary_function<Delta, bool> {
67  inline bool operator () (Delta dummy) { return true; }
68  };
69 
70  public:
71  template<class SortComparator, class CutCriterion>
72  std::vector<Match> match(
73  SortComparator sortComparator = SortComparator(),
74  CutCriterion cutCriterion = CutCriterion())
75  {
76  std::vector<index_type> matches(matrix.size());
77  for(index_type i = 0; i != matrix.size(); ++i)
78  matches[i] = i;
79 
80  std::sort(matches.begin(), matches.end(),
81  Comparator<SortComparator>(matrix, sortComparator));
82 
83  std::vector<Match> result;
84  result.reserve(std::min(matrix.rows(), matrix.cols()));
85  for(typename std::vector<index_type>::const_iterator iter =
86  matches.begin(); iter != matches.end(); ++iter) {
87 
88  index_type row = matrix.row(*iter);
89  index_type col = matrix.col(*iter);
90  if (matched1[row] || matched2[col])
91  continue;
92 
93  if (!cutCriterion(matrix[*iter]))
94  continue;
95 
96  matched1[row] = true;
97  matched2[col] = true;
98  result.push_back(Match(row, col));
99  }
100 
101  return result;
102  }
103 
104  template<class SortComparator>
105  inline std::vector<Match> match()
106  { return match<SortComparator, AlwaysTrue>(); }
107 
108  inline std::vector<Match> match()
109  { return match<std::less<Delta>, AlwaysTrue>(); }
110 
111  inline bool isMatched1st(index_type index) { return matched1[index]; }
112  inline bool isMatched2nd(index_type index) { return matched2[index]; }
113 
114  private:
116  std::vector<bool> matched1, matched2;
117 };
118 
119 } // namespace btag
120 
121 #endif // GeneratorEvent_Analysis_Matching_h
size
Write out results.
Matching::index_type index_type
Definition: Matching.h:53
Match(index_type i1, index_type i2)
Definition: Matching.h:38
Comparator(const SimpleMatrix< Delta > &matrix, SortComparator &sort)
Definition: Matching.h:55
Matching(const V1 &v1, const V2 &v2, Separation separation)
Definition: Matching.h:19
Definition: Matching.h:11
Delta delta(index_type index1, index_type index2) const
Definition: Matching.h:44
Matching::index_type index_type
Definition: Matching.h:36
size_type size() const
Definition: SimpleMatrix.h:21
index_type index1
Definition: Matching.h:41
std::vector< Match > match()
Definition: Matching.h:108
T min(T a, T b)
Definition: MathUtil.h:58
SimpleMatrix< Delta > matrix
Definition: Matching.h:115
Delta delta(Match match) const
Definition: Matching.h:47
bool isMatched2nd(index_type index)
Definition: Matching.h:112
index_type index2
Definition: Matching.h:41
bool isMatched1st(index_type index)
Definition: Matching.h:111
std::vector< bool > matched1
Definition: Matching.h:116
const SimpleMatrix< Delta > & matrix
Definition: Matching.h:62
std::vector< Match > match(SortComparator sortComparator=SortComparator(), CutCriterion cutCriterion=CutCriterion())
Definition: Matching.h:72
col
Definition: cuy.py:1008
SimpleMatrix< Delta >::size_type index_type
Definition: Matching.h:16
std::vector< bool > matched2
Definition: Matching.h:116
std::vector< Match > match()
Definition: Matching.h:105
SortComparator & sort
Definition: Matching.h:63