CMS 3D CMS Logo

VarRangeCut.h
Go to the documentation of this file.
1 #ifndef DQMOffline_Trigger_VarRangeCut_h
2 #define DQMOffline_Trigger_VarRangeCut_h
3 
4 
5 //********************************************************************************
6 //
7 // Description:
8 // A object containing a minimal set of selection cuts.
9 // These selection cuts are intended to be simple selections on kinematic variables.
10 // Currently these are implimented as simple allowed ranges X<var<Y which are ORed together
11 // So for example we may want to have an eta cut of 0<|eta|<1.4442 || 1.556<|eta|<2.5
12 //
13 // Implimentation:
14 // std::function holds the function which generates the variable from the object
15 // the name of the variable is also stored so we can determine if we should not apply
16 // a given selection cut
17 //
18 // Author : Sam Harper , RAL, May 2017
19 //
20 //***********************************************************************************
21 
24 
26 
27 #include <boost/algorithm/string.hpp>
28 
29 template<typename ObjType>
30 class VarRangeCut {
31 public:
32  explicit VarRangeCut(const edm::ParameterSet& config);
34 
35  bool operator()(const ObjType& obj)const;
36  const std::string& varName()const{return varName_;}
37 
38 private:
40  std::function<float(const ObjType&)> varFunc_;
41  std::vector<std::pair<float,float> > allowedRanges_;
42 };
43 
44 template<typename ObjType>
46 {
47  varName_ = config.getParameter<std::string>("rangeVar");
48  varFunc_ = hltdqm::getUnaryFuncFloat<ObjType>(varName_);
49  auto ranges = config.getParameter<std::vector<std::string> >("allowedRanges");
50  for(auto range: ranges){
51  std::vector<std::string> splitRange;
52  boost::split(splitRange,range,boost::is_any_of(":"));
53  if(splitRange.size()!=2) throw cms::Exception("ConfigError") <<"in VarRangeCut::VarRangeCut range "<<range<<" is not of format X:Y"<<std::endl;
54  allowedRanges_.push_back({std::stof(splitRange[0]),std::stof(splitRange[1])});
55  }
56 }
57 
58 template<typename ObjType>
60 {
62  desc.add<std::string>("rangeVar","");
63  desc.add<std::vector<std::string> >("allowedRanges",std::vector<std::string>());
64  return desc;
65 }
66 
67 template<typename ObjType>
68 bool VarRangeCut<ObjType>::operator()(const ObjType& obj)const
69 {
70  if(!varFunc_) return true; //auto pass if we dont specify a variable function
71  else{
72  float varVal = varFunc_(obj);
73  for(auto& range : allowedRanges_){
74  if(varVal>=range.first && varVal<range.second) return true;
75  }
76  return false;
77  }
78 }
79 
80 #endif
T getParameter(std::string const &) const
static edm::ParameterSetDescription makePSetDescription()
Definition: VarRangeCut.h:59
Definition: config.py:1
std::string varName_
Definition: VarRangeCut.h:39
ParameterDescriptionBase * add(U const &iLabel, T const &value)
string ranges
Definition: diffTwoXMLs.py:78
bool operator()(const ObjType &obj) const
Definition: VarRangeCut.h:68
const std::string & varName() const
Definition: VarRangeCut.h:36
std::vector< std::pair< float, float > > allowedRanges_
Definition: VarRangeCut.h:41
std::function< float(const ObjType &)> varFunc_
Definition: VarRangeCut.h:40
double split
Definition: MVATrainer.cc:139
VarRangeCut(const edm::ParameterSet &config)
Definition: VarRangeCut.h:45