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
btag::Matching::isMatched2nd
bool isMatched2nd(index_type index)
Definition: Matching.h:94
testProducerWithPsetDescEmpty_cfi.i2
i2
Definition: testProducerWithPsetDescEmpty_cfi.py:46
mps_fire.i
i
Definition: mps_fire.py:428
funct::false
false
Definition: Factorize.h:29
btag::Matching::matrix
SimpleMatrix< Delta > matrix
Definition: Matching.h:97
btag::SimpleMatrix::col
size_type col(size_type index) const
Definition: SimpleMatrix.h:30
btag::Matching::isMatched1st
bool isMatched1st(index_type index)
Definition: Matching.h:93
btag::SimpleMatrix::size
size_type size() const
Definition: SimpleMatrix.h:21
btag::Matching::matched1
std::vector< bool > matched1
Definition: Matching.h:98
min
T min(T a, T b)
Definition: MathUtil.h:58
btag::Matching::match
std::vector< Match > match()
Definition: Matching.h:91
cuy.col
col
Definition: cuy.py:1009
testProducerWithPsetDescEmpty_cfi.i1
i1
Definition: testProducerWithPsetDescEmpty_cfi.py:45
oniaPATMuonsWithTrigger_cff.matches
matches
Definition: oniaPATMuonsWithTrigger_cff.py:77
btag::Matching::Match::index1
index_type index1
Definition: Matching.h:33
btag::Matching::Comparator::operator()
bool operator()(index_type i1, index_type i2) const
Definition: Matching.h:47
btag::Matching::match
std::vector< Match > match(SortComparator sortComparator=SortComparator(), CutCriterion cutCriterion=CutCriterion())
Definition: Matching.h:59
btag::Matching::AlwaysTrue
Definition: Matching.h:53
btag::Matching::delta
Delta delta(Match match) const
Definition: Matching.h:38
btag::Matching::matched2
std::vector< bool > matched2
Definition: Matching.h:98
btag::SimpleMatrix::rows
size_type rows() const
Definition: SimpleMatrix.h:19
btag::Matching::index_type
SimpleMatrix< Delta >::size_type index_type
Definition: Matching.h:15
btag::Matching::Comparator::Comparator
Comparator(const SimpleMatrix< Delta > &matrix, SortComparator &sort)
Definition: Matching.h:45
btag::Matching::AlwaysTrue::operator()
bool operator()(Delta dummy)
Definition: Matching.h:54
btag::SimpleMatrix::cols
size_type cols() const
Definition: SimpleMatrix.h:20
btag::SimpleMatrix::row
size_type row(size_type index) const
Definition: SimpleMatrix.h:29
btag::Matching::match
std::vector< Match > match()
Definition: Matching.h:87
btag::Matching::Match::index_type
Matching::index_type index_type
Definition: Matching.h:29
jetUpdater_cfi.sort
sort
Definition: jetUpdater_cfi.py:29
btag::Matching::Matching
Matching(const V1 &v1, const V2 &v2, Separation separation)
Definition: Matching.h:18
btag::Matching::Comparator
Definition: Matching.h:42
btag::Matching
Definition: Matching.h:13
btag::Matching::Match::Match
Match(index_type i1, index_type i2)
Definition: Matching.h:31
MultipleCompare.Match
def Match(required, got)
Definition: MultipleCompare.py:75
SimpleMatrix.h
btag::Matching::Comparator::matrix
const SimpleMatrix< Delta > & matrix
Definition: Matching.h:49
btag::Matching::Match::index2
index_type index2
Definition: Matching.h:33
AlignmentPI::index
index
Definition: AlignmentPayloadInspectorHelper.h:46
btag::Matching::Match
Definition: Matching.h:28
btag::Matching::Comparator::sort
SortComparator & sort
Definition: Matching.h:50
mps_fire.result
result
Definition: mps_fire.py:311
dummy
Definition: DummySelector.h:38
dqmiolumiharvest.j
j
Definition: dqmiolumiharvest.py:66
btag::Matching::delta
Delta delta(index_type index1, index_type index2) const
Definition: Matching.h:36
btag
Definition: Matching.h:10
btag::Matching::Comparator::index_type
Matching::index_type index_type
Definition: Matching.h:43
btag::SimpleMatrix
Definition: SimpleMatrix.h:10
findQualityFiles.size
size
Write out results.
Definition: findQualityFiles.py:443