Commit 01cccc41 authored by Joerg Stelzer's avatar Joerg Stelzer Committed by Graeme Stewart
Browse files

Remove ARA support (TrigObjectMatching-00-01-39)

parent 115278a8
/*
Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
*/
#ifndef CLUSTER_DISTANCE_FUNCTOR_H
#define CLUSTER_DISTANCE_FUNCTOR_H
#include "TrigObjectMatching/DistanceFunctor.h"
class TrigEMCluster;
class TrigElectron;
class CaloCluster;
class EmTau_ROI;
/**
* @brief helper namespace for calculating deltaR for unknown
* object types
*/
// What is the type of cluster returned by a given object?
namespace TrigMatch {
template<class T> struct clusterTraits { typedef CaloCluster type; };
template<> struct clusterTraits<TrigElectron> { typedef TrigEMCluster type; };
template<> struct clusterTraits<EmTau_ROI> { typedef EmTau_ROI type; };
}
namespace ClusterDistanceHelper {
// what the deltaR is between two clusters
template<class T, class U>
float deltaR(const T* t, const U* u) {
return DeltaRDistanceFunctor<T,U>::deltaR(t, u);
}
// how to get the cluster for various types
template <class T>
const typename TrigMatch::clusterTraits<T>::type *getCluster(const T* t) { return t->cluster(); }
inline const EmTau_ROI *getCluster(const EmTau_ROI* t) { return t; }
}
/**
* @brief The ClusterDistanceFunctor is a templated distance functor
* used for matching two objects based upon their cluster rather
* than their eta and phi directly.
*/
template<class T, class U>
class ClusterDistanceFunctor : public DistanceFunctor<T,U> {
public:
ClusterDistanceFunctor() { };
virtual ~ClusterDistanceFunctor() { };
private:
virtual float calculateDistance(const T *t, const U *u) const {
if(!t || !u)
return -1.;
return ClusterDistanceHelper::deltaR(ClusterDistanceHelper::getCluster(t),
ClusterDistanceHelper::getCluster(u));
}
};
#endif
/*
Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
*/
#ifndef DISTANCE_FUNCTOR_H
#define DISTANCE_FUNCTOR_H
#include <math.h>
template <typename T, typename U>
class DistanceFunctor {
public:
float operator()(const T *t, const U *u) const {
return calculateDistance(t, u);
}
float distance(const T *t, const U *u) const {
return (*this)(t, u);
}
virtual ~DistanceFunctor() {;}
private:
virtual float calculateDistance(const T *t, const U *u) const = 0;
}; // end class DistanceFunctor
template <typename T, typename U>
class DeltaRDistanceFunctor : virtual public DistanceFunctor<T, U> {
public:
virtual ~DeltaRDistanceFunctor() {;}
static float deltaR(const T *t, const U *u) {
if(!t || !u)
return -1.;
float phiDiff = t->phi() - u->phi();
while(phiDiff < -M_PI) phiDiff += 2. * M_PI;
while(phiDiff >= M_PI) phiDiff -= 2. * M_PI;
float etaDiff = t->eta() - u->eta();
return sqrt(phiDiff * phiDiff + etaDiff * etaDiff);
}
private:
virtual float calculateDistance(const T *t, const U *u) const {
return DeltaRDistanceFunctor<T,U>::deltaR(t, u);
}
}; // end class DeltaRDistanceFunctor
#endif
/*
Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
*/
#ifndef OBJECT_MATCHING_H
#define OBJECT_MATCHING_H
#include "TrigObjectMatching/DistanceFunctor.h"
#include "DataModel/DataVector.h"
#include "CxxUtils/fpcompare.h"
#include <vector>
/**
* @brief ObjectMatching is a base class that performs generic
* matching of objects using the distance functor framework
*/
class ObjectMatching {
public:
ObjectMatching() { ; };
virtual ~ObjectMatching() { ; };
/**
* @brief distance returns the distance between the two supplied objects
* using metric, if available, or the default metric, if not.
*/
template <typename T, typename U>
float distance(
const T *t,
const U *u,
const DistanceFunctor<T, U> *metric);
// default metric version - uses deltaR matching and so no metric
// need be supplied
template <typename T, typename U>
float distance(
const T *t,
const U *u);
/**
*
* @brief matchToObjects returns the objects of type T from the supplied
* vector that match to matchObject, sorted by distance from
* the matchObject according to metric.
*/
template <typename T, typename U>
std::vector<const T*> matchToObjects(
const U* matchObject,
const std::vector<const T*> &targetObjects,
float maxDistance,
const DistanceFunctor<T, U> *metric);
// default metric version - uses deltaR matching and so no metric
// need be supplied
template <typename T, typename U>
std::vector<const T*> matchToObjects(
const U* matchObject,
const std::vector<const T*> &targetObjects,
float maxDistance = 0.1);
// similar matching functions for data vectors instead of std vectors
template <typename T, typename U>
std::vector<const T*> matchToObjects(
const U* matchObject,
const DataVector<T> &targetObjects,
float maxDistance,
const DistanceFunctor<T, U> *metric);
template <typename T, typename U>
std::vector<const T*> matchToObjects(
const U* matchObject,
const DataVector<T> &targetObjects,
float maxDistance = 0.1);
template <typename T, typename U>
bool anyMatch(
const U* matchObject,
const std::vector<const T*> &targetObjects,
float maxDistance,
const DistanceFunctor<T, U> *metric);
template <typename T, typename U>
bool anyMatch(
const U* matchObject,
const std::vector<const T*> &targetObjects,
float maxDistance);
/**
*
* @brief matchToObject returns the object of type T from the suppled
* vector that best matches matchObject according to the distance
* metric, if available, or if not, according the the default
* metric.
*/
template <typename T, typename U>
const T* matchToObject(
const U *matchObject,
const std::vector<const T*> &targetObjects,
float maxDistance,
const DistanceFunctor<T, U> *metric);
// default metric version - uses deltaR matching and so no metric
// need be supplied
template <typename T, typename U>
const T* matchToObject(
const U *matchObject,
const std::vector<const T*> &targetObjects,
float maxDistance = 0.1);
// similar functions for data vectors rather than std::vectors
template <typename T, typename U>
const T* matchToObject(
const U *matchObject,
const DataVector<T> &targetObjects,
float maxDistance,
const DistanceFunctor<T, U> *metric);
template <typename T, typename U>
const T* matchToObject(
const U *matchObject,
const DataVector<T> &targetObjects,
float maxDistance = 0.1);
protected:
// returns factory held default metric (deltaR)
template <typename T, typename U>
const DistanceFunctor<T,U> *prepareMetric();
private:
// functor for sorting by distance relative to a base object
// with a given metric
template <typename T, typename U>
class DistanceCompare {
public:
DistanceCompare(const U *baseObject, const DistanceFunctor<T, U> *m) {
m_metric = m;
m_baseObject = baseObject;
}
virtual bool operator()(const T *x, const T *y) {
// see Control/CxxUtils/fpcompare.h for explanation of
// why this is required
return CxxUtils::fpcompare::less((*m_metric)(x,m_baseObject),
(*m_metric)(y,m_baseObject));
};
private:
const U *m_baseObject;
const DistanceFunctor<T, U> *m_metric;
};
}; // end ObjectMatching declaration
#include "TrigObjectMatching/ObjectMatching.icc"
#endif
/*
Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
*/
#include "TrigParticle/TrigElectronContainer.h"
#include "TrigParticle/TrigPhotonContainer.h"
#include "TrigMuonEvent/TrigMuonEFContainer.h"
#include "tauEvent/TauJetContainer.h"
#include "JetEvent/JetCollection.h"
#include "egammaEvent/egammaContainer.h"
#include "TrigObjectMatching/TraitDefs.h"
#include "boost/foreach.hpp"
#include <memory>
/************************************/
/* Public Functions */
/************************************/
template <typename T, typename U>
float ObjectMatching::distance(const T *t, const U *u,
const DistanceFunctor<T, U> *metric) {
if(!metric)
return -1.;
return (*metric)(t, u);
}
template <typename T, typename U>
float ObjectMatching::distance(const T *t, const U *u) {
return this->distance(t, u, prepareMetric<T,U>());
}
template <typename T, typename U>
std::vector<const T*> ObjectMatching::matchToObjects(
const U* matchObject,
const std::vector<const T*> &targetObjects,
float maxDistance,
const DistanceFunctor<T, U> *metric) {
// record matches
std::vector<const T*> matches;
if(!metric)
return matches;
typename std::vector<const T*>::const_iterator iter;
for(iter = targetObjects.begin(); iter != targetObjects.end(); ++iter) {
float dist = this->distance<T, U>(*iter, matchObject, metric);
if(dist >= 0. && (dist < maxDistance || maxDistance < 0.))
matches.push_back(*iter);
}
// sort the matches
std::stable_sort(matches.begin(), matches.end(),
ObjectMatching::DistanceCompare<T, U>(matchObject, metric));
// return the vector
return matches;
}
template <typename T, typename U>
std::vector<const T*> ObjectMatching::matchToObjects(
const U* matchObject,
const std::vector<const T*> &targetObjects,
float maxDistance) {
return this->matchToObjects(matchObject, targetObjects, maxDistance, prepareMetric<T,U>());
}
template <typename T, typename U>
std::vector<const T*> ObjectMatching::matchToObjects(
const U* matchObject,
const DataVector<T> &targetObjects,
float maxDistance,
const DistanceFunctor<T, U> *metric) {
std::vector<const T*> objects;
objects.insert(objects.end(), targetObjects.begin(), targetObjects.end());
return this->matchToObjects(matchObject, objects, maxDistance, metric);
}
template <typename T, typename U>
std::vector<const T*> ObjectMatching::matchToObjects(
const U* matchObject,
const DataVector<T> &targetObjects,
float maxDistance) {
return this->matchToObjects(matchObject, targetObjects, maxDistance, prepareMetric<T,U>());
}
template <typename T, typename U>
bool ObjectMatching::anyMatch(
const U* matchObject,
const std::vector<const T*> &targetObjects,
float maxDistance,
const DistanceFunctor<T, U> *metric) {
if(!metric) return false;
typename std::vector<const T*>::const_iterator iter;
for(iter = targetObjects.begin(); iter != targetObjects.end(); ++iter) {
float dist = this->distance<T, U>(*iter, matchObject, metric);
if(dist >= 0. && (dist < maxDistance || maxDistance < 0.))
return true;
}
return false;
}
template <typename T, typename U>
bool ObjectMatching::anyMatch(
const U* matchObject,
const std::vector<const T*> &targetObjects,
float maxDistance)
{
return this->anyMatch(matchObject, targetObjects, maxDistance,
prepareMetric<T,U>());
}
template <typename T, typename U>
const T* ObjectMatching::matchToObject(
const U* matchObject,
const std::vector<const T*> &targetObjects,
float maxDistance,
const DistanceFunctor<T, U> *metric) {
const T *best = 0;
float best_dist = maxDistance;
if (best_dist < 0)
best_dist = std::numeric_limits<float>::max();
BOOST_FOREACH (const T* o, targetObjects) {
float dist = this->distance<T, U>(o, matchObject, metric);
if (dist >= 0 && dist < best_dist) {
best = o;
best_dist = dist;
}
}
return best;
}
template <typename T, typename U>
const T* ObjectMatching::matchToObject(
const U* matchObject,
const std::vector<const T*> &targetObjects,
float maxDistance) {
return this->matchToObject(matchObject, targetObjects, maxDistance, prepareMetric<T,U>());
}
template <typename T, typename U>
const T* ObjectMatching::matchToObject(
const U* matchObject,
const DataVector<T> &targetObjects,
float maxDistance,
const DistanceFunctor<T, U> *metric) {
const T *best = 0;
float best_dist = maxDistance;
if (best_dist < 0)
best_dist = std::numeric_limits<float>::max();
BOOST_FOREACH (const T* o, targetObjects) {
float dist = this->distance<T, U>(o, matchObject, metric);
if (dist >= 0 && dist < best_dist) {
best = o;
best_dist = dist;
}
}
return best;
}
template <typename T, typename U>
const T* ObjectMatching::matchToObject(
const U* matchObject,
const DataVector<T> &targetObjects,
float maxDistance) {
return this->matchToObject(matchObject, targetObjects, maxDistance, prepareMetric<T,U>());
}
template<typename T, typename U>
const DistanceFunctor<T,U> *ObjectMatching::prepareMetric() {
// use traits to determine what the default metric should be.
// note that the static keyword prevents multiple initialization,
// so only one of each metric will be produced per run
static const std::auto_ptr<const typename TrigMatch::MetricTraits<T,U>::type > metric(new typename TrigMatch::MetricTraits<T,U>::type);
return metric.get();
}
// Dear emacs, this is -*- c++ -*-
/*
Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
*/
#ifndef TRIG_OBJECT_MATCHING_TRAIT_DEFS_H
#define TRIG_OBJECT_MATCHING_TRAIT_DEFS_H
#include "DataModel/DataVector.h"
#include "TrigObjectMatching/TrigMuonEFInfoDistanceFunctor.h"
#include "TrigObjectMatching/ClusterDistanceFunctor.h"
namespace TrigMatch {
// Here we define the trait classes. The basic problem is
// this:
//
// Some classes are attached to the navigation as basic objects
// for example - MuonFeature
// Other classes are attached to the navigation as containers of
// objects for example - egamma's are attached as egammaContainers
// Still other classes are not attached at all, but can be gotten
// from the ancestor method in the TDT, such as the L1 items
// We need to make this complexity invisible to the user, so they
// can just call whatever they want to match to and not worry
// about how it interacts with the navigation
// We solve this with traits classes (thanks to Scott Snyder for
// the suggestion) which tell us whether or not a class is a
// container and if it is, allows us to map from the class to
// the container class and similarly for ancest style objects
struct DirectAttached {}; // identifies objects as attached directly
struct AncestorAttached {}; // identifies objects as attached via ancest
// by default, we assume a class is attached directly
template<typename T>
struct ClassTraits {
typedef DirectAttached type;
};
}
// now we specialize for the container classes
// This macro specializes the definition of the
// ContainerForClass trait class
#define SPECIALIZE_CONTAINER_FOR_CLASS(x,y) \
namespace TrigMatch { \
template<> \
struct ClassTraits<x> { \
typedef y type; \
}; \
}
// This macro does the forward declaration
// for objects without a namespace
#define DECLARE_ATTACHED_CONTAINER(x,y) \
class x; \
class y; \
SPECIALIZE_CONTAINER_FOR_CLASS(x,y)
// This macro does the forward declaration
// for objects with the same namespace
// as the container
#define DECLARE_ATTACHED_CONTAINER_NAMESPACE(a,x,y) \
namespace a { \
class x; \
class y; \
} \
SPECIALIZE_CONTAINER_FOR_CLASS(a::x,a::y)
// This macro does the forward declaration
// for objects with a namespace, but not namespace
// on the container
#define DECLARE_ATTACHED_CONTAINER_OBJECT_NAMESPACE(a,x,y) \
namespace a { \
class x; \
} \
class y; \
SPECIALIZE_CONTAINER_FOR_CLASS(a::x,y)
// This macro does the forward declaration
// for objects with a namespace, and containers
// that are a typedef
#define DECLARE_ATTACHED_CONTAINER_TYPEDEF(a, x, y) \