CMS 3D CMS Logo

sorting_modules.h
Go to the documentation of this file.
1 #ifndef RecoBTag_FeatureTools_sorting_modules_h
2 #define RecoBTag_FeatureTools_sorting_modules_h
3 
4 #include <algorithm>
5 #include <cmath>
6 #include <vector>
7 
9 
10 namespace btagbtvdeep{
11 
12 /*
13  * the std::sort function needs a strict ordering.
14  * means if your_function(a,b) is true, than your_function(b,a) must be false for all a and b
15  */
16 
17 template <class T>
19 
20  public:
21 
23 
24  SortingClass(const T& t, float sortA, float sortB=0, float sortC=0) :
25  t_(t), sortValA(sortA), sortValB(sortB), sortValC(sortC) {}
26 
27  const T& get() const {return t_;}
28 
30 
31  static inline compareResult compare(const SortingClass& a, const SortingClass& b,int validx=0){
32  float vala=a.sortValA;
33  float valb=b.sortValA;
34  if(validx==1){
35  vala=a.sortValB;
36  valb=b.sortValB;
37  }else if(validx==2){
38  vala=a.sortValC;
39  valb=b.sortValC;
40  }
41  if(edm::isFinite(vala) && edm::isFinite(valb) && valb!=vala){
42  if(vala>valb) return cmp_greater;
43  else return cmp_smaller;
44  }
45  if(edm::isFinite(vala) && !edm::isFinite(valb))
46  return cmp_greater;
47  if(!edm::isFinite(vala) && edm::isFinite(valb))
48  return cmp_smaller;
49  return cmp_invalid;
50  }
51 
52  //hierarchical sort
53  static bool compareByABC(const SortingClass& a, const SortingClass& b){
54 
55  compareResult tmpres=compare(a,b,0);
56  if(tmpres==cmp_smaller) return true;
57  if(tmpres==cmp_greater) return false;
58 
59  tmpres=compare(a,b,1);
60  if(tmpres==cmp_smaller) return true;
61  if(tmpres==cmp_greater) return false;
62 
63  tmpres=compare(a,b,2);
64  if(tmpres==cmp_smaller) return true;
65  if(tmpres==cmp_greater) return false;
66 
67  return false;
68 
69  }
70 
71  static bool compareByABCInv(const SortingClass& a, const SortingClass& b){
72  return compareByABC(b,a);
73  }
74 
75  private:
76 
77  T t_;
79 };
80 
81 std::vector<std::size_t> invertSortingVector(const std::vector<SortingClass<std::size_t> > & in);
82 
83 }
84 #endif //RecoBTag_FeatureTools_sorting_modules_h
static bool compareByABCInv(const SortingClass &a, const SortingClass &b)
constexpr bool isFinite(T x)
SortingClass(const T &t, float sortA, float sortB=0, float sortC=0)
static bool compareByABC(const SortingClass &a, const SortingClass &b)
double b
Definition: hdecay.h:120
static compareResult compare(const SortingClass &a, const SortingClass &b, int validx=0)
std::vector< std::size_t > invertSortingVector(const std::vector< SortingClass< std::size_t > > &in)
double a
Definition: hdecay.h:121
long double T