2 Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
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"
12 #include "TrigObjectMatching/TraitDefs.h"
16 /************************************/
17 /* Public Functions */
18 /************************************/
20 template <typename T, typename U>
21 float ObjectMatching::distance(const T *t, const U *u,
22 const DistanceFunctor<T, U> *metric) const {
27 return (*metric)(t, u);
31 template <typename T, typename U>
32 float ObjectMatching::distance(const T *t, const U *u) const {
34 return this->distance(t, u, prepareMetric<T,U>());
38 template <typename T, typename U>
39 std::vector<const T*> ObjectMatching::matchToObjects(
41 const std::vector<const T*> &targetObjects,
43 const DistanceFunctor<T, U> *metric) const {
46 std::vector<const T*> matches;
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);
59 std::stable_sort(matches.begin(), matches.end(),
60 ObjectMatching::DistanceCompare<T, U>(matchObject, metric));
67 template <typename T, typename U>
68 std::vector<const T*> ObjectMatching::matchToObjects(
70 const std::vector<const T*> &targetObjects,
71 float maxDistance) const {
73 return this->matchToObjects(matchObject, targetObjects, maxDistance, prepareMetric<T,U>());
77 template <typename T, typename U>
78 std::vector<const T*> ObjectMatching::matchToObjects(
80 const DataVector<T> &targetObjects,
82 const DistanceFunctor<T, U> *metric) const {
84 std::vector<const T*> objects;
85 objects.insert(objects.end(), targetObjects.begin(), targetObjects.end());
87 return this->matchToObjects(matchObject, objects, maxDistance, metric);
91 template <typename T, typename U>
92 std::vector<const T*> ObjectMatching::matchToObjects(
94 const DataVector<T> &targetObjects,
95 float maxDistance) const {
97 return this->matchToObjects(matchObject, targetObjects, maxDistance, prepareMetric<T,U>());
101 template <typename T, typename U>
102 bool ObjectMatching::anyMatch(
103 const U* matchObject,
104 const std::vector<const T*> &targetObjects,
106 const DistanceFunctor<T, U> *metric) const {
108 if(!metric) return false;
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.))
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
126 return this->anyMatch(matchObject, targetObjects, maxDistance,
127 prepareMetric<T,U>());
130 template <typename T, typename U>
131 const T* ObjectMatching::matchToObject(
132 const U* matchObject,
133 const std::vector<const T*> &targetObjects,
135 const DistanceFunctor<T, U> *metric) const {
138 float best_dist = maxDistance;
140 best_dist = std::numeric_limits<float>::max();
142 for (const T* o : targetObjects) {
143 float dist = this->distance<T, U>(o, matchObject, metric);
144 if (dist >= 0 && dist < best_dist) {
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 {
160 return this->matchToObject(matchObject, targetObjects, maxDistance, prepareMetric<T,U>());
164 template <typename T, typename U>
165 const T* ObjectMatching::matchToObject(
166 const U* matchObject,
167 const DataVector<T> &targetObjects,
169 const DistanceFunctor<T, U> *metric) const {
172 float best_dist = maxDistance;
174 best_dist = std::numeric_limits<float>::max();
176 for (const T* o : targetObjects) {
177 float dist = this->distance<T, U>(o, matchObject, metric);
178 if (dist >= 0 && dist < best_dist) {
188 template <typename T, typename U>
189 const T* ObjectMatching::matchToObject(
190 const U* matchObject,
191 const DataVector<T> &targetObjects,
192 float maxDistance) const {
194 return this->matchToObject(matchObject, targetObjects, maxDistance, prepareMetric<T,U>());
197 template<typename T, typename U>
198 const DistanceFunctor<T,U> *ObjectMatching::prepareMetric() const {
200 // use traits to determine what the default metric should be.
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);