Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-10-25 09:58:45

0001 #ifndef RecoBTag_FeatureTools_sorting_modules_h
0002 #define RecoBTag_FeatureTools_sorting_modules_h
0003 
0004 #include <algorithm>
0005 #include <cmath>
0006 #include <vector>
0007 
0008 #include "FWCore/Utilities/interface/isFinite.h"
0009 
0010 namespace btagbtvdeep {
0011 
0012   /*
0013  * the std::sort function needs a strict ordering.
0014  * means if your_function(a,b) is true, than your_function(b,a) must be false for all a and b
0015  */
0016 
0017   template <class T>
0018   class SortingClass {
0019   public:
0020     SortingClass() : sortValA(0), sortValB(0), sortValC(0), t_(0) {}
0021 
0022     SortingClass(const T& t, float sortA, float sortB = 0, float sortC = 0)
0023         : t_(t), sortValA(sortA), sortValB(sortB), sortValC(sortC) {}
0024 
0025     const T& get() const { return t_; }
0026 
0027     enum compareResult { cmp_smaller, cmp_greater, cmp_invalid };
0028 
0029     static inline compareResult compare(const SortingClass& a, const SortingClass& b, int validx = 0) {
0030       float vala = a.sortValA;
0031       float valb = b.sortValA;
0032       if (validx == 1) {
0033         vala = a.sortValB;
0034         valb = b.sortValB;
0035       } else if (validx == 2) {
0036         vala = a.sortValC;
0037         valb = b.sortValC;
0038       }
0039       if (edm::isFinite(vala) && edm::isFinite(valb) && valb != vala) {
0040         if (vala > valb)
0041           return cmp_greater;
0042         else
0043           return cmp_smaller;
0044       }
0045       if (edm::isFinite(vala) && !edm::isFinite(valb))
0046         return cmp_greater;
0047       if (!edm::isFinite(vala) && edm::isFinite(valb))
0048         return cmp_smaller;
0049       return cmp_invalid;
0050     }
0051 
0052     //hierarchical sort
0053     static bool compareByABC(const SortingClass& a, const SortingClass& b) {
0054       compareResult tmpres = compare(a, b, 0);
0055       if (tmpres == cmp_smaller)
0056         return true;
0057       if (tmpres == cmp_greater)
0058         return false;
0059 
0060       tmpres = compare(a, b, 1);
0061       if (tmpres == cmp_smaller)
0062         return true;
0063       if (tmpres == cmp_greater)
0064         return false;
0065 
0066       tmpres = compare(a, b, 2);
0067       if (tmpres == cmp_smaller)
0068         return true;
0069       if (tmpres == cmp_greater)
0070         return false;
0071 
0072       return false;
0073     }
0074 
0075     static bool compareByABCInv(const SortingClass& a, const SortingClass& b) { return compareByABC(b, a); }
0076 
0077   private:
0078     T t_;
0079     float sortValA, sortValB, sortValC;
0080   };
0081 
0082   std::vector<std::size_t> invertSortingVector(const std::vector<SortingClass<std::size_t> >& in);
0083 
0084 }  // namespace btagbtvdeep
0085 #endif  //RecoBTag_FeatureTools_sorting_modules_h