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