ATLAS Offline Software
ObjectMatching.icc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #include "TrigParticle/TrigElectronContainer.h"
6 #include "TrigParticle/TrigPhotonContainer.h"
7 #include "TrigMuonEvent/TrigMuonEFContainer.h"
8 #include "tauEvent/TauJetContainer.h"
9 #include "JetEvent/JetCollection.h"
10 #include "egammaEvent/egammaContainer.h"
11 
12 #include "TrigObjectMatching/TraitDefs.h"
13 
14 #include <memory>
15 
16 /************************************/
17 /* Public Functions */
18 /************************************/
19 
20 template <typename T, typename U>
21 float ObjectMatching::distance(const T *t, const U *u,
22  const DistanceFunctor<T, U> *metric) const {
23 
24  if(!metric)
25  return -1.;
26 
27  return (*metric)(t, u);
28 
29 }
30 
31 template <typename T, typename U>
32 float ObjectMatching::distance(const T *t, const U *u) const {
33 
34  return this->distance(t, u, prepareMetric<T,U>());
35 
36 }
37 
38 template <typename T, typename U>
39 std::vector<const T*> ObjectMatching::matchToObjects(
40  const U* matchObject,
41  const std::vector<const T*> &targetObjects,
42  float maxDistance,
43  const DistanceFunctor<T, U> *metric) const {
44 
45  // record matches
46  std::vector<const T*> matches;
47 
48  if(!metric)
49  return matches;
50 
51  typename std::vector<const T*>::const_iterator iter;
52  for(iter = targetObjects.begin(); iter != targetObjects.end(); ++iter) {
53  float dist = this->distance<T, U>(*iter, matchObject, metric);
54  if(dist >= 0. && (dist < maxDistance || maxDistance < 0.))
55  matches.push_back(*iter);
56  }
57 
58  // sort the matches
59  std::stable_sort(matches.begin(), matches.end(),
60  ObjectMatching::DistanceCompare<T, U>(matchObject, metric));
61 
62  // return the vector
63  return matches;
64 
65 }
66 
67 template <typename T, typename U>
68 std::vector<const T*> ObjectMatching::matchToObjects(
69  const U* matchObject,
70  const std::vector<const T*> &targetObjects,
71  float maxDistance) const {
72 
73  return this->matchToObjects(matchObject, targetObjects, maxDistance, prepareMetric<T,U>());
74 
75 }
76 
77 template <typename T, typename U>
78 std::vector<const T*> ObjectMatching::matchToObjects(
79  const U* matchObject,
80  const DataVector<T> &targetObjects,
81  float maxDistance,
82  const DistanceFunctor<T, U> *metric) const {
83 
84  std::vector<const T*> objects;
85  objects.insert(objects.end(), targetObjects.begin(), targetObjects.end());
86 
87  return this->matchToObjects(matchObject, objects, maxDistance, metric);
88 
89 }
90 
91 template <typename T, typename U>
92 std::vector<const T*> ObjectMatching::matchToObjects(
93  const U* matchObject,
94  const DataVector<T> &targetObjects,
95  float maxDistance) const {
96 
97  return this->matchToObjects(matchObject, targetObjects, maxDistance, prepareMetric<T,U>());
98 
99 }
100 
101 template <typename T, typename U>
102 bool ObjectMatching::anyMatch(
103  const U* matchObject,
104  const std::vector<const T*> &targetObjects,
105  float maxDistance,
106  const DistanceFunctor<T, U> *metric) const {
107 
108  if(!metric) return false;
109 
110  typename std::vector<const T*>::const_iterator iter;
111  for(iter = targetObjects.begin(); iter != targetObjects.end(); ++iter) {
112  float dist = this->distance<T, U>(*iter, matchObject, metric);
113  if(dist >= 0. && (dist < maxDistance || maxDistance < 0.))
114  return true;
115  }
116 
117  return false;
118 }
119 
120 template <typename T, typename U>
121 bool ObjectMatching::anyMatch(
122  const U* matchObject,
123  const std::vector<const T*> &targetObjects,
124  float maxDistance) const
125 {
126  return this->anyMatch(matchObject, targetObjects, maxDistance,
127  prepareMetric<T,U>());
128 }
129 
130 template <typename T, typename U>
131 const T* ObjectMatching::matchToObject(
132  const U* matchObject,
133  const std::vector<const T*> &targetObjects,
134  float maxDistance,
135  const DistanceFunctor<T, U> *metric) const {
136 
137  const T *best = 0;
138  float best_dist = maxDistance;
139  if (best_dist < 0)
140  best_dist = std::numeric_limits<float>::max();
141 
142  for (const T* o : targetObjects) {
143  float dist = this->distance<T, U>(o, matchObject, metric);
144  if (dist >= 0 && dist < best_dist) {
145  best = o;
146  best_dist = dist;
147  }
148  }
149 
150  return best;
151 
152 }
153 
154 template <typename T, typename U>
155 const T* ObjectMatching::matchToObject(
156  const U* matchObject,
157  const std::vector<const T*> &targetObjects,
158  float maxDistance) const {
159 
160  return this->matchToObject(matchObject, targetObjects, maxDistance, prepareMetric<T,U>());
161 
162 }
163 
164 template <typename T, typename U>
165 const T* ObjectMatching::matchToObject(
166  const U* matchObject,
167  const DataVector<T> &targetObjects,
168  float maxDistance,
169  const DistanceFunctor<T, U> *metric) const {
170 
171  const T *best = 0;
172  float best_dist = maxDistance;
173  if (best_dist < 0)
174  best_dist = std::numeric_limits<float>::max();
175 
176  for (const T* o : targetObjects) {
177  float dist = this->distance<T, U>(o, matchObject, metric);
178  if (dist >= 0 && dist < best_dist) {
179  best = o;
180  best_dist = dist;
181  }
182  }
183 
184  return best;
185 
186 }
187 
188 template <typename T, typename U>
189 const T* ObjectMatching::matchToObject(
190  const U* matchObject,
191  const DataVector<T> &targetObjects,
192  float maxDistance) const {
193 
194  return this->matchToObject(matchObject, targetObjects, maxDistance, prepareMetric<T,U>());
195 
196 }
197 template<typename T, typename U>
198 const DistanceFunctor<T,U> *ObjectMatching::prepareMetric() const {
199 
200  // use traits to determine what the default metric should be.
201 
202  // note that the static keyword prevents multiple initialization,
203  // so only one of each metric will be produced per run
204  static const std::unique_ptr<const typename TrigMatch::MetricTraits<T,U>::type > metric(new typename TrigMatch::MetricTraits<T,U>::type);
205 
206  return metric.get();
207 
208 }