CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
EgammaHLTPixelMatchParamObjects.h
Go to the documentation of this file.
1 //These objects allow an arbitary parameristisation to be used
2 //design:
3 // the function and parameterisation can be changing without having to change the interface
4 // or breaking backwards compatibiltiy with existing configs.
5 // This is vital for the HLT and the main driving force behind the design which otherwise
6 // could have been a lot simplier
7 //
8 //usage:
9 // The variables used are defined by an intermediate object which defines the
10 // variables x,y, and z. 1D objects only need to define x, 2D objects x,y, etc.
11 // Example is "AbsEtaNrClus" which defines x as |supercluster eta| and y as #subclusters of the supercluster.
12 // The object also defines a pass function where it determines if x (and y,z) is
13 // within the specificed xmin and xmax range. This is mainly done as individual
14 // objects can decide whether this means min<=x<max or min<=x<=max
15 //
16 // These objects are used by the binning objects. The bins can currently be 1D, 2D,
17 // or 3D based on the intermediate object. Each bin has a function with it.
18 // The function takes a ParmaType as an argument and returns a float.
19 // The function is defined by a string of format FUNCID:=ExtraConfigInfo
20 // FUNCID = function type which allows different functions to be selected
21 // while ExtraConfigInfo is any extra information needed to configure that func
22 // currently implimented types are TF1, TF2, TF3
23 // so to get a TF1 which is a pol3 just do TF1:=pol3
24 //
25 //future plans:
26 // Seperate out intermediate wrapper objects such as AbsEtaNrClus
27 // and put them in their own file. However some mechanism is needed to register them
28 // so for now this isnt done. Note we might move to the standard CMSSW of dealing
29 // with this
30 
32 
33 #include "TF1.h"
34 #include "TF2.h"
35 #include "TF3.h"
36 
37 namespace egPM {
38 
39 
40  struct AbsEtaNrClus{
41  float x;
42  size_t y;
43 
46  x = std::abs(scRef->eta());
47  y = scRef->clustersSize();
48  }
49  bool pass(float absEtaMin,float absEtaMax,size_t nrClusMin,size_t nrClusMax)const{
50  return x>=absEtaMin && x<absEtaMax && y>=nrClusMin && y<=nrClusMax;
51  }
52  };
54  float x;
55  size_t y;
56  float z;
57 
60  x = std::abs(scRef->eta());
61  y = scRef->clustersSize();
62  z = scRef->phi();
63  }
64  bool pass(float absEtaMin,float absEtaMax,size_t nrClusMin,size_t nrClusMax,
65  float phiMin,float phiMax)const{
66  return x>=absEtaMin && x<absEtaMax && y>=nrClusMin && y<=nrClusMax
67  && z>=phiMin && z < phiMax;
68  }
69  };
70 
71  struct AbsEtaNrClusEt {
72  float x;
73  size_t y;
74  float z;
75 
78  x = std::abs(scRef->eta());
79  y = scRef->clustersSize();
80  z = scRef->energy()*sin(scRef->position().Theta());
81  }
82  bool pass(float absEtaMin,float absEtaMax,size_t nrClusMin,size_t nrClusMax,
83  float etMin,float etMax)const{
84  return x>=absEtaMin && x<absEtaMax && y>=nrClusMin && y<=nrClusMax
85  && z>=etMin && z < etMax;
86  }
87  };
88 
89 
90  //these structs wrap the TF1 object
91  //also if the ParamType doesnt have a high enough dimension
92  //(ie using only one with x,y for a TF3), then the second
93  //template parameter disables the function
94  template<typename ParamType,bool=true>
95  struct TF1Wrap{
96  private:
97  TF1 func_;
98  public:
99  TF1Wrap(const std::string& funcExpr,const std::vector<double>& params):
100  func_("func",funcExpr.c_str()){
101  for(size_t paraNr=0;paraNr<params.size();paraNr++){
102  func_.SetParameter(paraNr,params[paraNr]);
103  }
104  }
105  float operator()(const ParamType& obj){
106  return func_.Eval(obj.x);
107  };
108  };
109  template<typename ParamType>
110  class TF1Wrap<ParamType,false>{
111  public:
112  TF1Wrap(const std::string& funcExpr,const std::vector<double>& params){}
113  float operator()(const ParamType& obj){return 1.;};
114  };
115 
116  template<typename ParamType,bool=true>
117  struct TF2Wrap{
118  private:
119  TF2 func_;
120  public:
121  TF2Wrap(const std::string& funcExpr,const std::vector<double>& params):
122  func_("func",funcExpr.c_str()){
123  for(size_t paraNr=0;paraNr<params.size();paraNr++){
124  func_.SetParameter(paraNr,params[paraNr]);
125  }
126  }
127  float operator()(const ParamType& obj){
128  return func_.Eval(obj.x,obj.y);
129  };
130  };
131  template<typename ParamType>
132  class TF2Wrap<ParamType,false>{
133  public:
134  TF2Wrap(const std::string& funcExpr,const std::vector<double>& params){}
135  float operator()(const ParamType& obj){return 1.;};
136  };
137 
138  template<typename ParamType,bool=true>
139  struct TF3Wrap{
140  private:
141  TF3 func_;
142  public:
143  TF3Wrap(const std::string& funcExpr,const std::vector<double>& params):
144  func_("func",funcExpr.c_str()){
145  for(size_t paraNr=0;paraNr<params.size();paraNr++){
146  func_.SetParameter(paraNr,params[paraNr]);
147  }
148  }
149  float operator()(const ParamType& obj){
150  return func_.Eval(obj.x,obj.y,obj.z);
151  };
152  };
153  template<typename ParamType>
154  class TF3Wrap<ParamType,false>{
155  public:
156  TF3Wrap(const std::string& funcExpr,const std::vector<double>& params){}
157  float operator()(const ParamType& obj){return 1.;};
158  };
159 
160  //the following functions allow for the fact that the type in the CMSSW PSet does not
161  //have floats do when it sees a float parameter, it retrieves a double
162  template<typename T>
163  struct ConfigType{
164  typedef T type;
165  };
166  template<>
167  struct ConfigType<float>{
168  typedef double type;
169  };
170  template<>
171  struct ConfigType<size_t>{
172  typedef int type;
173  };
174 
175 
176  //helper functions to figure out what dimension the ParamType is
177  //and not generate functions which require a higher dimension
178  template<typename T>
179  constexpr auto has1D(int) -> decltype(T::x,bool()){return true;}
180  template<typename T>
181  constexpr bool has1D(...){return false;}
182  template<typename T>
183  constexpr auto has2D(int) -> decltype(T::y,bool()){return true;}
184  template<typename T>
185  constexpr bool has2D(...){return false;}
186  template<typename T>
187  constexpr auto has3D(int) -> decltype(T::z,bool()){return true;}
188  template<typename T>
189  constexpr bool has3D(...){return false;}
190 
191  template<typename InputType>
192  class ParamBin {
193  public:
195  virtual ~ParamBin(){}
196  virtual bool pass(const InputType&)const=0;
197  virtual float operator()(const InputType&)const=0;
198  protected:
199  //the FUNCTYPE:=funcExpr is designed for future extensions
200  static std::pair<std::string,std::string> readFuncStr(const std::string& inStr){
201  size_t pos=inStr.find(":=");
202  if(pos!=std::string::npos) return std::make_pair(inStr.substr(0,pos),inStr.substr(pos+2));
203  else return std::make_pair(inStr,std::string(""));
204  }
205  template<typename ParamType>
206  static std::function<float(const ParamType&)> makeFunc(const edm::ParameterSet& config){
207  auto funcType = readFuncStr(config.getParameter<std::string>("funcType"));
208  auto funcParams = config.getParameter<std::vector<double> >("funcParams");
209  if(funcType.first=="TF1" && has1D<ParamType>(0)) return TF1Wrap<ParamType,has1D<ParamType>(0)>(funcType.second,funcParams);
210  else if(funcType.first=="TF2" && has2D<ParamType>(0)) return TF2Wrap<ParamType,has2D<ParamType>(0)>(funcType.second,funcParams);
211  else if(funcType.first=="TF3" && has3D<ParamType>(0)) return TF3Wrap<ParamType,has3D<ParamType>(0)>(funcType.second,funcParams);
212  else throw cms::Exception("InvalidConfig") << " type "<<funcType.first<<" is not recognised or is imcompatable with the ParamType, configuration is invalid and needs to be fixed"<<std::endl;
213  }
214  };
215 
216  template<typename InputType,typename ParamType>
217  class ParamBin1D : public ParamBin<InputType> {
218  private:
219  using XType = decltype(ParamType::x);
221  std::function<float(const ParamType&)> func_;
222  public:
224  xMin_(config.getParameter<typename ConfigType<XType>::type >("xMin")),
225  xMax_(config.getParameter<typename ConfigType<XType>::type >("xMax")),
226  func_(ParamBin<InputType>::template makeFunc<ParamType>(config))
227  {
228  }
229  bool pass(const InputType& input)const override {
230  return ParamType(input).pass(xMin_,xMax_);
231  }
232  float operator()(const InputType& input)const override{
233  if(!pass(input)) return 0;
234  else return func_(ParamType(input));
235  }
236  };
237 
238 
239 
240  template<typename InputType,typename ParamType>
241  class ParamBin2D : public ParamBin<InputType> {
242  private:
243  using XType = decltype(ParamType::x);
244  using YType = decltype(ParamType::y);
247  std::function<float(const ParamType&)> func_;
248  public:
250  xMin_(config.getParameter<typename ConfigType<XType>::type >("xMin")),
251  xMax_(config.getParameter<typename ConfigType<XType>::type >("xMax")),
252  yMin_(config.getParameter<typename ConfigType<YType>::type >("yMin")),
253  yMax_(config.getParameter<typename ConfigType<YType>::type >("yMax")),
254  func_(ParamBin<InputType>::template makeFunc<ParamType>(config))
255  {
256  }
257 
258  bool pass(const InputType& input)const override {
259  return ParamType(input).pass(xMin_,xMax_,yMin_,yMax_);
260  }
261  float operator()(const InputType& input)const override{
262  if(!pass(input)) return 0;
263  else return func_(ParamType(input));
264  }
265  };
266 
267 
268  template<typename InputType,typename ParamType>
269  class ParamBin3D : public ParamBin<InputType> {
270  using XType = decltype(ParamType::x);
271  using YType = decltype(ParamType::y);
272  using ZType = decltype(ParamType::z);
273 
277  std::function<float(const ParamType&)> func_;
278  public:
280  xMin_(config.getParameter<typename ConfigType<XType>::type >("xMin")),
281  xMax_(config.getParameter<typename ConfigType<XType>::type >("xMax")),
282  yMin_(config.getParameter<typename ConfigType<YType>::type >("yMin")),
283  yMax_(config.getParameter<typename ConfigType<YType>::type >("yMax")),
284  zMin_(config.getParameter<typename ConfigType<ZType>::type >("zMin")),
285  zMax_(config.getParameter<typename ConfigType<ZType>::type >("zMax")),
286  func_(ParamBin<InputType>::template makeFunc<ParamType>(config))
287  {
288  }
289 
290  bool pass(const InputType& input)const override {
291  return ParamType(input).pass(xMin_,xMax_,yMin_,yMax_,zMin_,zMax_);
292  }
293  float operator()(const InputType& input)const override{
294  if(!pass(input)) return 0;
295  else return func_(ParamType(input));
296  }
297  };
298 
299 
300 
301 
302  template<typename InputType>
303  class Param {
304  std::vector<std::unique_ptr<ParamBin<InputType> > > bins_;
305  public:
307  std::vector<edm::ParameterSet> binConfigs = config.getParameter<std::vector<edm::ParameterSet> >("bins");
308  for(auto& binConfig : binConfigs) bins_.emplace_back(createParamBin_(binConfig));
309  }
310  float operator()(const InputType& input)const{
311  for(auto& bin : bins_){
312  if(bin->pass(input)) return (*bin)(input);
313  }
314  return -1; //didnt find a suitable bin, just return -1 for now
315  }
316 
317  private:
318  std::unique_ptr<ParamBin<InputType> > createParamBin_(const edm::ParameterSet& config){
319  std::string type = config.getParameter<std::string>("binType");
320  if(type=="AbsEtaClus") return std::make_unique<ParamBin2D<InputType,AbsEtaNrClus>>(config);
321  else if(type=="AbsEtaClusPhi") return std::make_unique<ParamBin3D<InputType,AbsEtaNrClusPhi>>(config);
322  else if(type=="AbsEtaClusEt") return std::make_unique<ParamBin3D<InputType,AbsEtaNrClusEt>>(config);
323  else throw cms::Exception("InvalidConfig") << " type "<<type<<" is not recognised, configuration is invalid and needs to be fixed"<<std::endl;
324  }
325  };
326 }
type
Definition: HCALResponse.h:21
T getParameter(std::string const &) const
float operator()(const InputType &input) const override
decltype(ParamType::x) XType
static std::pair< std::string, std::string > readFuncStr(const std::string &inStr)
InputType
Definition: InputType.h:5
decltype(ParamType::x) XType
std::function< float(const ParamType &)> func_
ParamBin2D(const edm::ParameterSet &config)
bool pass(float absEtaMin, float absEtaMax, size_t nrClusMin, size_t nrClusMax, float etMin, float etMax) const
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
AbsEtaNrClusPhi(const reco::ElectronSeed &seed)
TF2Wrap(const std::string &funcExpr, const std::vector< double > &params)
float operator()(const ParamType &obj)
bool pass(float absEtaMin, float absEtaMax, size_t nrClusMin, size_t nrClusMax, float phiMin, float phiMax) const
virtual bool pass(const InputType &) const =0
float operator()(const InputType &input) const override
auto has3D(int) -> decltype(T::z, bool())
#define constexpr
float float float z
static std::string const input
Definition: EdmProvDump.cc:44
CaloClusterRef caloCluster() const
Definition: ElectronSeed.h:70
Param(const edm::ParameterSet &config)
AbsEtaNrClus(const reco::ElectronSeed &seed)
T x() const
Cartesian x coordinate.
ParamBin1D(const edm::ParameterSet &config)
std::function< float(const ParamType &)> func_
auto has2D(int) -> decltype(T::y, bool())
static std::function< float(const ParamType &)> makeFunc(const edm::ParameterSet &config)
TF1Wrap(const std::string &funcExpr, const std::vector< double > &params)
bool pass(const InputType &input) const override
float operator()(const ParamType &obj)
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
float operator()(const InputType &input) const override
std::unique_ptr< ParamBin< InputType > > createParamBin_(const edm::ParameterSet &config)
bool pass(const InputType &input) const override
TF1Wrap(const std::string &funcExpr, const std::vector< double > &params)
AbsEtaNrClusEt(const reco::ElectronSeed &seed)
decltype(ParamType::y) YType
REF castTo() const
Definition: RefToBase.h:286
TF3Wrap(const std::string &funcExpr, const std::vector< double > &params)
bool pass(const InputType &input) const override
float operator()(const InputType &input) const
decltype(ParamType::z) ZType
std::function< float(const ParamType &)> func_
TF3Wrap(const std::string &funcExpr, const std::vector< double > &params)
std::vector< std::unique_ptr< ParamBin< InputType > > > bins_
volatile std::atomic< bool > shutdown_flag false
auto has1D(int) -> decltype(T::x, bool())
decltype(ParamType::y) YType
bool pass(float absEtaMin, float absEtaMax, size_t nrClusMin, size_t nrClusMax) const
long double T
decltype(ParamType::x) XType
ParamBin3D(const edm::ParameterSet &config)
virtual float operator()(const InputType &) const =0
def template
Definition: svgfig.py:520
float operator()(const ParamType &obj)
TF2Wrap(const std::string &funcExpr, const std::vector< double > &params)