Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 14:27:32

0001 #include<iostream>
0002 #include<cassert>
0003 #include<tuple>
0004 #include<array>
0005 #include<utility>
0006 
0007 namespace {
0008 
0009   struct A {
0010     float chi2;
0011     int   n3d;
0012     int   nll;
0013   };
0014 
0015 
0016   constexpr float mvaVal[3] = {-.5,.5,1.};
0017   
0018   template<typename T,typename Comp>
0019   inline float cut(T val, const T * cuts, Comp comp) {
0020     for (int i=2; i>=0; --i) 
0021       if ( comp(val,cuts[i]) ) return mvaVal[i];
0022     return -1.f; 
0023   }
0024 
0025 
0026 
0027   template<typename CutT>
0028   inline float cut(A const & a, CutT const & cuts) {
0029     for (int i=2; i>=0; --i) 
0030       if ( std::get<1>(cuts)(std::get<0>(cuts)(a),std::get<2>(cuts)[i]) ) return mvaVal[i];
0031     return -1.f; 
0032   }
0033 
0034 
0035   template<class F, class...Ts, std::size_t...Is>
0036   void for_each_in_tuple(const std::tuple<Ts...> & tuple, F func, std::index_sequence<Is...>){
0037     using expander = int[];
0038     (void)expander { 0, ((void)func(std::get<Is>(tuple)), 0)... };
0039   }
0040   
0041   template<class F, class...Ts>
0042   void for_each_in_tuple(const std::tuple<Ts...> & tuple, F func){
0043     for_each_in_tuple(tuple, func, std::make_index_sequence<sizeof...(Ts)>());
0044   }
0045 
0046     
0047 
0048 
0049 
0050   float applyCuts(A const & a) {
0051     constexpr auto allCuts = std::make_tuple(
0052                          std::make_tuple(
0053                                  [](A const & a) { return a.chi2;},
0054                                  std::less_equal<>(),
0055                                  std::array<float,3>{9999.,25.,16.}
0056                                  ),
0057                          std::make_tuple(
0058                                  [](A const & a) { return a.n3d;},
0059                                  std::greater_equal<>(),
0060                                  std::array<int,3>{1,2,3}
0061                                  )
0062                          );
0063     float aret = 1;
0064     for_each_in_tuple(allCuts,[&](auto const & c) { if (aret>-1.f) aret = std::min(aret,cut(a,c));});
0065     return aret;
0066   }
0067 
0068 }
0069 
0070 int main() {
0071 
0072   A a{18.,1,1};
0073 
0074   auto cut1 = std::make_tuple(
0075                   [](A const & a) { return a.chi2;},
0076                   std::less_equal<>(),
0077                   std::array<float,3>{9999.,25.,16.}
0078                   );
0079   std::cout << std::get<1>(cut1)(std::get<0>(cut1)(a),std::get<2>(cut1)[0]) << std::endl;
0080   std::cout << std::get<1>(cut1)(std::get<0>(cut1)(a),std::get<2>(cut1)[2]) << std::endl;
0081 
0082   
0083   std::cout << applyCuts(a) << std::endl;
0084   
0085   
0086   float maxChi2[3] ={9999.,25.,16.};
0087   int min3DLayers[3] = {1,2,3}; 
0088   int maxLostLayers[3] = {99,3,3};
0089 
0090 
0091   float ret = -1.f;
0092   ret = cut(2.f,maxChi2,std::less_equal<>());
0093   assert(ret==mvaVal[2]);
0094   ret = cut(28.f,maxChi2,std::less_equal<>());
0095   assert(ret==mvaVal[0]);
0096   
0097   ret = cut(0,min3DLayers,std::greater_equal<>());
0098   assert(ret==-1);
0099   ret = cut(1,min3DLayers,std::greater_equal<>());
0100   assert(ret==mvaVal[0]);
0101   ret = cut(2,min3DLayers,std::greater_equal<>());
0102   assert(ret==mvaVal[1]);
0103   ret = cut(3,min3DLayers,std::greater_equal<>());
0104   assert(ret==mvaVal[2]);
0105   ret = cut(5,min3DLayers,std::greater_equal<>());
0106   assert(ret==mvaVal[2]);
0107   
0108   ret = cut(1,maxLostLayers,std::less_equal<>());
0109   assert(ret==mvaVal[2]);
0110       
0111   return 0;
0112 
0113 }