CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/PhysicsTools/PatUtils/interface/GenericOverlapFinder.h

Go to the documentation of this file.
00001 #ifndef PhysicsTools_PatUtils_GenericOverlapFinder_h
00002 #define PhysicsTools_PatUtils_GenericOverlapFinder_h
00003 
00004 #include "DataFormats/Math/interface/deltaR.h"
00005 
00006 #include <memory>
00007 #include <vector>
00008 #include <algorithm>
00009 
00010 namespace pat {
00011 
00015     typedef std::vector< std::pair<size_t, size_t> > OverlapList;
00016 
00023     template<typename Comparator>
00024     struct OverlapDistance {
00025         public:
00026             OverlapDistance() {}
00027             OverlapDistance(const Comparator &comp) : comp_(comp) {}
00028             template<typename T1, typename T2>
00029             double operator()(const T1 &t1, const T2 &t2) const {
00030                 return 1.0 - comp_(t1,t2);
00031             }
00032         private:
00033             Comparator comp_;
00034     }; //struct
00035 
00038     struct OverlapByDeltaR {
00039         public:
00040             OverlapByDeltaR(double deltaR) :  scale_(1.0/(deltaR*deltaR)) {}
00041             template<typename T1, typename T2>
00042             double operator()(const T1 &t1, const T2 &t2) const {
00043                 return deltaR2(t1,t2) * scale_;
00044             }
00045         private:
00046             double scale_;
00047     }; //struct
00048 
00049 
00050     template <typename Distance>
00051     class GenericOverlapFinder {
00052 
00053         public:
00054 
00055            GenericOverlapFinder() {}
00056            GenericOverlapFinder(const Distance &dist) : distance_(dist) {}
00057             
00061             template <typename Collection, typename OtherCollection>
00062             std::auto_ptr< OverlapList >
00063             find(const Collection &items, const OtherCollection &other) const ;
00064 
00065         private:
00066             Distance distance_;
00067 
00068     }; // class
00069 }
00070 
00071 template<typename Distance>
00072 template<typename Collection, typename OtherCollection>
00073 std::auto_ptr< pat::OverlapList >
00074 pat::GenericOverlapFinder<Distance>::find(const Collection &items, const OtherCollection &other) const 
00075 {
00076     size_t size = items.size(), size2 = other.size();
00077 
00078     std::auto_ptr< OverlapList > ret(new OverlapList());
00079     
00080     for (size_t ie = 0; ie < size; ++ie) {
00081         double dmin   = 1.0;
00082         size_t match = 0;
00083 
00084         for (size_t je = 0; je < size2; ++je) {
00085             double dist = distance_(items[ie], other[je]);
00086             if (dist < dmin) { match = je; dmin = dist;  }
00087         }
00088         
00089         if (dmin < 1.0) {
00090             ret->push_back(std::make_pair(ie,match));
00091         }
00092     }
00093 
00094     return ret;
00095 }
00096 
00097 
00098 #endif