Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:34:52

0001 #ifndef JetAlgorithms_JetAlgoHelper_h
0002 #define JetAlgorithms_JetAlgoHelper_h
0003 
0004 // Various simple tools
0005 // F.Ratnikov, UMd
0006 
0007 #include <algorithm>
0008 #include <limits>
0009 #include <iostream>
0010 #include <cmath>
0011 #include <vector>
0012 
0013 #include "CommonTools/Utils/interface/PtComparator.h"
0014 #include "CommonTools/Utils/interface/EtComparator.h"
0015 
0016 namespace {
0017 
0018   struct SortObject {
0019     double value;
0020     unsigned index;
0021   };
0022 
0023   inline bool so_lt(const SortObject& a, const SortObject& b) { return a.value < b.value; }
0024   inline bool so_gt(const SortObject& a, const SortObject& b) { return a.value > b.value; }
0025 
0026   template <class T>
0027   struct GetPt {
0028     inline double getValue(const T& a) { return a.pt(); }
0029   };
0030   template <class T>
0031   struct GetEt {
0032     inline double getValue(const T& a) { return a.et(); }
0033   };
0034   template <class T>
0035   struct GetPtRef {
0036     inline double getValue(const T& a) { return a->pt(); }
0037   };
0038   template <class T>
0039   struct GetEtRef {
0040     inline double getValue(const T& a) { return a->et(); }
0041   };
0042 
0043   template <class T, class GetValue>
0044   inline void sortGreater(std::vector<T>* container) {
0045     std::vector<SortObject> sortable(container->size());
0046     bool sorted = true;
0047     GetValue getter;
0048     for (unsigned i = 0; i < container->size(); i++) {
0049       sortable[i].value = getter.getValue((*container)[i]);
0050       sortable[i].index = i;
0051       if (sorted && i && so_lt(sortable[i - 1], sortable[i]))
0052         sorted = false;
0053     }
0054     if (!sorted) {  // needs sorting
0055       std::sort(sortable.begin(), sortable.end(), so_gt);
0056       std::vector<T> result;
0057       result.reserve(container->size());
0058       for (unsigned i = 0; i < container->size(); i++) {
0059         result.push_back((*container)[sortable[i].index]);
0060       }
0061       container->swap(result);
0062     }
0063   }
0064 
0065   template <class T>
0066   inline void sortByPt(std::vector<T>* container) {
0067     sortGreater<T, GetPt<T> >(container);
0068   }
0069 
0070   template <class T>
0071   inline void sortByEt(std::vector<T>* container) {
0072     sortGreater<T, GetEt<T> >(container);
0073   }
0074 
0075   template <class T>
0076   inline void sortByPtRef(std::vector<T>* container) {
0077     sortGreater<T, GetPtRef<T> >(container);
0078   }
0079 
0080   template <class T>
0081   inline void sortByEtRef(std::vector<T>* container) {
0082     sortGreater<T, GetEtRef<T> >(container);
0083   }
0084 
0085 }  // namespace
0086 
0087 template <class T>
0088 class GreaterByPtRef {
0089 public:
0090   int operator()(const T& a1, const T& a2) {
0091     if (!a1)
0092       return 0;
0093     if (!a2)
0094       return 1;
0095     NumericSafeGreaterByPt<typename T::value_type> comp;
0096     return comp.operator()(*a1, *a2);
0097   }
0098 };
0099 template <class T>
0100 class GreaterByPtPtr {
0101 public:
0102   int operator()(const T* a1, const T* a2) {
0103     if (!a1)
0104       return 0;
0105     if (!a2)
0106       return 1;
0107     NumericSafeGreaterByPt<T> comp;
0108     return comp.operator()(*a1, *a2);
0109   }
0110 };
0111 
0112 template <class T>
0113 class GreaterByEtRef {
0114 public:
0115   int operator()(const T& a1, const T& a2) {
0116     if (!a1)
0117       return 0;
0118     if (!a2)
0119       return 1;
0120     NumericSafeGreaterByEt<typename T::value_type> comp;
0121     return comp.operator()(*a1, *a2);
0122   }
0123 };
0124 
0125 #endif