00001 #ifndef JetAlgorithms_JetAlgoHelper_h
00002 #define JetAlgorithms_JetAlgoHelper_h
00003
00004
00005
00006
00007
00008 #include<limits>
00009 #include <iostream>
00010 #include <cmath>
00011
00012 #include "PhysicsTools/Utilities/interface/PtComparator.h"
00013 #include "PhysicsTools/Utilities/interface/EtComparator.h"
00014
00015 namespace {
00016
00017 struct SortObject {
00018 double value;
00019 unsigned index;
00020 };
00021
00022 inline bool so_lt (const SortObject& a, const SortObject& b) {return a.value < b.value;}
00023 inline bool so_gt (const SortObject& a, const SortObject& b) {return a.value > b.value;}
00024
00025 template <class T> struct GetPt {inline double getValue (const T& a) {return a.pt();}};
00026 template <class T> struct GetEt {inline double getValue (const T& a) {return a.et();}};
00027 template <class T> struct GetPtRef {inline double getValue (const T& a) {return a->pt();}};
00028 template <class T> struct GetEtRef {inline double getValue (const T& a) {return a->et();}};
00029
00030 template <class T, class GetValue>
00031 inline void sortGreater (std::vector <T>* container) {
00032 std::vector <SortObject> sortable (container->size());
00033 bool sorted = true;
00034 GetValue getter;
00035 for (unsigned i = 0; i < container->size(); i++) {
00036 sortable[i].value = getter.getValue ((*container)[i]);
00037 sortable[i].index = i;
00038 if (sorted && i && so_gt (sortable[i-1], sortable[i])) sorted = false;
00039 }
00040 if (!sorted) {
00041 std::sort (sortable.begin(), sortable.end(), so_gt);
00042 std::vector <T> result;
00043 result.reserve(container->size());
00044 for (unsigned i = 0; i < container->size(); i++) {
00045 result.push_back ((*container)[sortable[i].index]);
00046 }
00047 container->swap (result);
00048 }
00049 }
00050
00051 template <class T>
00052 inline void sortByPt (std::vector <T>* container) {
00053 sortGreater <T, GetPt<T> > (container);
00054 }
00055
00056 template <class T>
00057 inline void sortByEt (std::vector <T>* container) {
00058 sortGreater <T, GetEt<T> > (container);
00059 }
00060
00061
00062 template <class T>
00063 inline void sortByPtRef (std::vector <T>* container) {
00064 sortGreater <T, GetPtRef<T> > (container);
00065 }
00066
00067 template <class T>
00068 inline void sortByEtRef (std::vector <T>* container) {
00069 sortGreater <T, GetEtRef<T> > (container);
00070 }
00071
00072 }
00073
00074 template <class T>
00075 class GreaterByPtRef {
00076 public:
00077 int operator()(const T& a1, const T& a2) {
00078 if (!a1) return 0;
00079 if (!a2) return 1;
00080 NumericSafeGreaterByPt <typename T::value_type> comp;
00081 return comp.operator () (*a1, *a2);
00082 }
00083 };
00084 template <class T>
00085 class GreaterByPtPtr {
00086 public:
00087 int operator()(const T* a1, const T* a2) {
00088 if (!a1) return 0;
00089 if (!a2) return 1;
00090 NumericSafeGreaterByPt <T> comp;
00091 return comp.operator () (*a1, *a2);
00092 }
00093 };
00094
00095 template <class T>
00096 class GreaterByEtRef {
00097 public:
00098 int operator()(const T& a1, const T& a2) {
00099 if (!a1) return 0;
00100 if (!a2) return 1;
00101 NumericSafeGreaterByEt <typename T::value_type> comp;
00102 return comp.operator () (*a1, *a2);
00103 }
00104 };
00105
00106 #endif