ATLAS Offline Software
Loading...
Searching...
No Matches
BestMatcher.h
Go to the documentation of this file.
1/* emacs: this is -*- c++ -*- */
14
15
16#ifndef BESTMATCHER_H
17#define BESTMATCHER_H
18
19#include <iostream>
20#include <set>
21
22
24
25
26
27
28template<typename T, typename S=T>
29class BestMatcher : public TIDA::Associator<T,S> {
30
31protected:
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
59public:
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
87protected:
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
169protected:
170
171 double m_d;
172
173};
174
175
176
177#endif // MATCH_H
178
179
180
181
182
183
184
185
186
187
const boost::regex ref(r_ef)
static Double_t a
static Double_t t0
bool operator!=(const matched_ &a) const
Definition BestMatcher.h:52
double d() const
Definition BestMatcher.h:42
bool operator>(const matched_ &a) const
Definition BestMatcher.h:50
std::pair< int, int > m_match
Definition BestMatcher.h:56
bool operator<(const matched_ &a) const
Definition BestMatcher.h:49
matched_(double d, int i, int j)
Definition BestMatcher.h:40
bool operator==(const matched_ &a) const
Definition BestMatcher.h:51
std::pair< int, int > pair() const
Definition BestMatcher.h:44
virtual void match(const std::vector< T * > &ref, const std::vector< S * > &test)
Definition BestMatcher.h:68
virtual double distance(const T *t0, const S *t1) const =0
std::map< int, int > matcher(const std::vector< Tp * > &ref, const std::vector< Tq * > &test)
Definition BestMatcher.h:91
BestMatcher(const std::string &name, double d)
Definition BestMatcher.h:61
virtual ~BestMatcher()
Definition BestMatcher.h:65
virtual const TIDA::Track * matched(TIDA::Track *t)
Associator(const std::string &name)
double chi2(TH1 *h0, TH1 *h1)
Test for xAOD.
STL namespace.