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