CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 // $Id: JetAlgoHelper.h,v 1.9 2009/07/08 00:38:29 hegner Exp $
7 
8 #include<limits>
9 #include <iostream>
10 #include <cmath>
11 
14 
15 namespace {
16 
17  struct SortObject {
18  double value;
19  unsigned index;
20  };
21 
22  inline bool so_lt (const SortObject& a, const SortObject& b) {return a.value < b.value;}
23  inline bool so_gt (const SortObject& a, const SortObject& b) {return a.value > b.value;}
24 
25  template <class T> struct GetPt {inline double getValue (const T& a) {return a.pt();}};
26  template <class T> struct GetEt {inline double getValue (const T& a) {return a.et();}};
27  template <class T> struct GetPtRef {inline double getValue (const T& a) {return a->pt();}};
28  template <class T> struct GetEtRef {inline double getValue (const T& a) {return a->et();}};
29 
30  template <class T, class GetValue>
31  inline void sortGreater (std::vector <T>* container) {
32  std::vector <SortObject> sortable (container->size());
33  bool sorted = true;
34  GetValue getter;
35  for (unsigned i = 0; i < container->size(); i++) {
36  sortable[i].value = getter.getValue ((*container)[i]);
37  sortable[i].index = i;
38  if (sorted && i && so_lt (sortable[i-1], sortable[i])) sorted = false;
39  }
40  if (!sorted) { // needs sorting
41  std::sort (sortable.begin(), sortable.end(), so_gt);
42  std::vector <T> result;
43  result.reserve(container->size());
44  for (unsigned i = 0; i < container->size(); i++) {
45  result.push_back ((*container)[sortable[i].index]);
46  }
47  container->swap (result);
48  }
49  }
50 
51  template <class T>
52  inline void sortByPt (std::vector <T>* container) {
53  sortGreater <T, GetPt<T> > (container);
54  }
55 
56  template <class T>
57  inline void sortByEt (std::vector <T>* container) {
58  sortGreater <T, GetEt<T> > (container);
59  }
60 
61 
62  template <class T>
63  inline void sortByPtRef (std::vector <T>* container) {
64  sortGreater <T, GetPtRef<T> > (container);
65  }
66 
67  template <class T>
68  inline void sortByEtRef (std::vector <T>* container) {
69  sortGreater <T, GetEtRef<T> > (container);
70  }
71 
72 }
73 
74 template <class T>
76  public:
77  int operator()(const T& a1, const T& a2) {
78  if (!a1) return 0;
79  if (!a2) return 1;
81  return comp.operator () (*a1, *a2);
82  }
83 };
84 template <class T>
86  public:
87  int operator()(const T* a1, const T* a2) {
88  if (!a1) return 0;
89  if (!a2) return 1;
91  return comp.operator () (*a1, *a2);
92  }
93 };
94 
95 template <class T>
97  public:
98  int operator()(const T& a1, const T& a2) {
99  if (!a1) return 0;
100  if (!a2) return 1;
102  return comp.operator () (*a1, *a2);
103  }
104 };
105 
106 #endif
int i
Definition: DBlmapReader.cc:9
int operator()(const T &a1, const T &a2)
Definition: JetAlgoHelper.h:98
bool sortByPt(const HepMC::GenParticle *a, const HepMC::GenParticle *b)
tuple result
Definition: query.py:137
double b
Definition: hdecay.h:120
int operator()(const T *a1, const T *a2)
Definition: JetAlgoHelper.h:87
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:77