ATLAS Offline Software
BestMatcher.h
Go to the documentation of this file.
1 /* emacs: this is -*- c++ -*- */
16 #ifndef BESTMATCHER_H
17 #define BESTMATCHER_H
18 
19 #include <iostream>
20 #include <set>
21 
22 
24 
25 
26 
27 
28 template<typename T, typename S=T>
29 class BestMatcher : public TIDA::Associator<T,S> {
30 
31 protected:
32 
35 
36  class matched_ {
37 
38  public:
39 
40  matched_(double d, int i, int j) : m_d(d), m_match(std::pair<int,int>(i,j)) { }
41 
42  double d() const { return m_d; }
43 
44  std::pair<int, int> pair() const { return m_match; }
45 
46  int first() const { return m_match.first; }
47  int second() const { return m_match.second; }
48 
49  bool operator<(const matched_& a) const { return d()<a.d(); }
50  bool operator>(const matched_& a) const { return d()>a.d(); }
51  bool operator==(const matched_& a) const { return d()==a.d(); }
52  bool operator!=(const matched_& a) const { return d()!=a.d(); }
53 
54  private:
55  double m_d;
56  std::pair<int, int> m_match;
57  };
58 
59 public:
60 
61  BestMatcher(const std::string& name, double d) :
62  TIDA::Associator<T,S>(name), m_d(d)
63  { }
64 
65  virtual ~BestMatcher() { }
66 
67 
68  virtual void match(const std::vector<T*>& ref, const std::vector<S*>& test )
69  {
70  this->clear();
71 
72  std::map<int,int> matched = matcher( ref, test);
73 
74  std::map<int,int>::iterator mitr = matched.begin();
75  while ( mitr!=matched.end() ) {
76  this->mmatched.insert( typename TIDA::Associator<T,S>::map_type::value_type( ref[mitr->first], test[mitr->second]) );
77  this->mrevmatched.insert( typename TIDA::Associator<S,T>::map_type::value_type( test[mitr->second], ref[mitr->first] ) );
78  mitr++;
79  }
80 
81  }
82 
83 
84  virtual double distance( const T* t0, const S* t1 ) const = 0;
85 
86 
87 protected:
88 
89 
90  template<typename Tp, typename Tq>
91  std::map<int, int> matcher( const std::vector<Tp*>& ref, const std::vector<Tq*>& test) {
92 
98 
99  std::multiset<matched_> m;
100 
101  for (unsigned int i=0 ; i<ref.size() ; i++ ) {
102 
103  for (unsigned int j=0 ; j<test.size() ; j++ ) {
104  double d = distance(ref[i], test[j]);
105  if ( d<m_d ){
106  m.insert( matched_(d, i, j) );
107  }
108  }
109  }
110 
114 
115  std::vector<bool> refused( ref.size(), false );
116  std::vector<bool> testused( test.size(), false );
117 
122  std::multiset<matched_> unique;
123 
124  typename std::multiset<matched_>::iterator mitr = m.begin();
125 
126  double chi2 = 0;
127 
128  for ( ; mitr!=m.end() ; mitr++ ) {
129 
130  int rind = mitr->first();
131  int tind = mitr->second();
132 
133  if ( refused[rind] ) continue;
134  if ( testused[tind] ) continue;
135 
136  refused[rind] = true;
137  testused[tind] = true;
138 
139  unique.insert( *mitr );
140 
141  chi2 += (mitr->d()*mitr->d());
142 
143  }
144 
145 
146  // std::cout << "chi2 of matches " << chi2 << std::endl;
147 
151 
152  // std::cout << "\nmatched" << std::endl;
153 
154  std::map<int, int> matches;
155 
156 
157  mitr = unique.begin();
158  while ( mitr!=unique.end() ) {
159  matches.insert( std::map<int, int>::value_type( mitr->first(), mitr->second() ) );
160  // std::cout << "\tbest match " << *mitr << "\t" << ref[mitr->first()] << "\t" << test[mitr->second()] << std::endl;
161  mitr++;
162  }
163 
164  return matches;
165 
166  }
167 
168 
169 protected:
170 
171  double m_d;
172 
173 };
174 
175 
176 
177 #endif // MATCH_H
178 
179 
180 
181 
182 
183 
184 
185 
186 
187 
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
TIDA::Associator
Definition: TIDAAssociator.h:24
TIDA
Test for xAOD.
Definition: Filter_AcceptAll.h:22
BestMatcher::matched_::m_match
std::pair< int, int > m_match
Definition: BestMatcher.h:56
TIDAAssociator.h
python.SystemOfUnits.m
int m
Definition: SystemOfUnits.py:91
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
hist_file_dump.d
d
Definition: hist_file_dump.py:137
TIDA::Associator< T, T >::matched
const map_type & matched() const
Definition: TIDAAssociator.h:59
BestMatcher::matched_::operator<
bool operator<(const matched_ &a) const
Definition: BestMatcher.h:49
ALFA_EventTPCnv_Dict::t0
std::vector< ALFA_RawData_p1 > t0
Definition: ALFA_EventTPCnvDict.h:42
ALFA_EventTPCnv_Dict::t1
std::vector< ALFA_RawDataCollection_p1 > t1
Definition: ALFA_EventTPCnvDict.h:43
TIDA::Associator< T, T >::mmatched
map_type mmatched
Definition: TIDAAssociator.h:73
BestMatcher::match
virtual void match(const std::vector< T * > &ref, const std::vector< S * > &test)
Definition: BestMatcher.h:68
BestMatcher::~BestMatcher
virtual ~BestMatcher()
Definition: BestMatcher.h:65
TrigInDetValidation_Base.test
test
Definition: TrigInDetValidation_Base.py:144
JetTiledMap::S
@ S
Definition: TiledEtaPhiMap.h:44
BestMatcher::matched_
internal class to store the matching paramter for a pair, and in the indices of the pair
Definition: BestMatcher.h:36
BestMatcher::matcher
std::map< int, int > matcher(const std::vector< Tp * > &ref, const std::vector< Tq * > &test)
Definition: BestMatcher.h:91
BestMatcher::distance
virtual double distance(const T *t0, const S *t1) const =0
BestMatcher::matched_::second
int second() const
Definition: BestMatcher.h:47
BestMatcher::BestMatcher
BestMatcher(const std::string &name, double d)
Definition: BestMatcher.h:61
TIDA::Associator< T, T >::clear
void clear()
Definition: TIDAAssociator.h:63
BestMatcher::matched_::pair
std::pair< int, int > pair() const
Definition: BestMatcher.h:44
lumiFormat.i
int i
Definition: lumiFormat.py:92
BestMatcher::matched_::matched_
matched_(double d, int i, int j)
Definition: BestMatcher.h:40
chi2
double chi2(TH1 *h0, TH1 *h1)
Definition: comparitor.cxx:522
BestMatcher::matched_::m_d
double m_d
Definition: BestMatcher.h:55
BestMatcher::matched_::operator>
bool operator>(const matched_ &a) const
Definition: BestMatcher.h:50
BestMatcher::matched_::operator==
bool operator==(const matched_ &a) const
Definition: BestMatcher.h:51
BestMatcher::matched_::first
int first() const
Definition: BestMatcher.h:46
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
BestMatcher
Definition: BestMatcher.h:29
a
TList * a
Definition: liststreamerinfos.cxx:10
BestMatcher::matched_::d
double d() const
Definition: BestMatcher.h:42
ref
const boost::regex ref(r_ef)
TIDA::Associator< T, T >::Associator
Associator(const std::string &name)
Definition: TIDAAssociator.h:33
TIDA::Associator< T, T >::mrevmatched
rmap_type mrevmatched
Definition: TIDAAssociator.h:74
BestMatcher::matched_::operator!=
bool operator!=(const matched_ &a) const
Definition: BestMatcher.h:52
BestMatcher::m_d
double m_d
Definition: BestMatcher.h:171
TSU::T
unsigned long long T
Definition: L1TopoDataTypes.h:35