CMS 3D CMS Logo

JetAlgoHelper.h
Go to the documentation of this file.
1 #ifndef JetAlgorithms_JetAlgoHelper_h
2 #define JetAlgorithms_JetAlgoHelper_h
3 
4 // Various simple tools
5 // F.Ratnikov, UMd
6 
7 #include <algorithm>
8 #include <limits>
9 #include <iostream>
10 #include <cmath>
11 #include <vector>
12 
15 
16 namespace {
17 
18  struct SortObject {
19  double value;
20  unsigned index;
21  };
22 
23  inline bool so_lt (const SortObject& a, const SortObject& b) {return a.value < b.value;}
24  inline bool so_gt (const SortObject& a, const SortObject& b) {return a.value > b.value;}
25 
26  template <class T> struct GetPt {inline double getValue (const T& a) {return a.pt();}};
27  template <class T> struct GetEt {inline double getValue (const T& a) {return a.et();}};
28  template <class T> struct GetPtRef {inline double getValue (const T& a) {return a->pt();}};
29  template <class T> struct GetEtRef {inline double getValue (const T& a) {return a->et();}};
30 
31  template <class T, class GetValue>
32  inline void sortGreater (std::vector <T>* container) {
33  std::vector <SortObject> sortable (container->size());
34  bool sorted = true;
35  GetValue getter;
36  for (unsigned i = 0; i < container->size(); i++) {
37  sortable[i].value = getter.getValue ((*container)[i]);
38  sortable[i].index = i;
39  if (sorted && i && so_lt (sortable[i-1], sortable[i])) sorted = false;
40  }
41  if (!sorted) { // needs sorting
42  std::sort (sortable.begin(), sortable.end(), so_gt);
43  std::vector <T> result;
44  result.reserve(container->size());
45  for (unsigned i = 0; i < container->size(); i++) {
46  result.push_back ((*container)[sortable[i].index]);
47  }
48  container->swap (result);
49  }
50  }
51 
52  template <class T>
53  inline void sortByPt (std::vector <T>* container) {
54  sortGreater <T, GetPt<T> > (container);
55  }
56 
57  template <class T>
58  inline void sortByEt (std::vector <T>* container) {
59  sortGreater <T, GetEt<T> > (container);
60  }
61 
62 
63  template <class T>
64  inline void sortByPtRef (std::vector <T>* container) {
65  sortGreater <T, GetPtRef<T> > (container);
66  }
67 
68  template <class T>
69  inline void sortByEtRef (std::vector <T>* container) {
70  sortGreater <T, GetEtRef<T> > (container);
71  }
72 
73 }
74 
75 template <class T>
77  public:
78  int operator()(const T& a1, const T& a2) {
79  if (!a1) return 0;
80  if (!a2) return 1;
82  return comp.operator () (*a1, *a2);
83  }
84 };
85 template <class T>
87  public:
88  int operator()(const T* a1, const T* a2) {
89  if (!a1) return 0;
90  if (!a2) return 1;
92  return comp.operator () (*a1, *a2);
93  }
94 };
95 
96 template <class T>
98  public:
99  int operator()(const T& a1, const T& a2) {
100  if (!a1) return 0;
101  if (!a2) return 1;
103  return comp.operator () (*a1, *a2);
104  }
105 };
106 
107 #endif
int operator()(const T &a1, const T &a2)
Definition: JetAlgoHelper.h:99
bool sortByPt(const HepMC::GenParticle *a, const HepMC::GenParticle *b)
PseudoJet result(const PseudoJet &jet) const override
double b
Definition: hdecay.h:120
int operator()(const T *a1, const T *a2)
Definition: JetAlgoHelper.h:88
double a
Definition: hdecay.h:121
const JetExtendedData & getValue(const Container &, const reco::JetBaseRef &)
get value for the association. Throw exception if no association found
long double T
int operator()(const T &a1, const T &a2)
Definition: JetAlgoHelper.h:78