CMS 3D CMS Logo

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 
35 
36 #include "TF1.h"
37 #include "TF2.h"
38 #include "TF3.h"
39 
40 namespace egPM {
41 
42 
43  struct AbsEtaNrClus{
44  float x;
45  size_t y;
46 
49  x = std::abs(scRef->eta());
50  y = scRef->clustersSize();
51  }
52  bool pass(float absEtaMin,float absEtaMax,size_t nrClusMin,size_t nrClusMax)const{
53  return x>=absEtaMin && x<absEtaMax && y>=nrClusMin && y<=nrClusMax;
54  }
55  };
57  float x;
58  size_t y;
59  float z;
60 
63  x = std::abs(scRef->eta());
64  y = scRef->clustersSize();
65  z = scRef->phi();
66  }
67  bool pass(float absEtaMin,float absEtaMax,size_t nrClusMin,size_t nrClusMax,
68  float phiMin,float phiMax)const{
69  return x>=absEtaMin && x<absEtaMax && y>=nrClusMin && y<=nrClusMax
70  && z>=phiMin && z < phiMax;
71  }
72  };
73 
74  struct AbsEtaNrClusEt {
75  float x;
76  size_t y;
77  float z;
78 
81  x = std::abs(scRef->eta());
82  y = scRef->clustersSize();
83  z = scRef->energy()*sin(scRef->position().Theta());
84  }
85  bool pass(float absEtaMin,float absEtaMax,size_t nrClusMin,size_t nrClusMax,
86  float etMin,float etMax)const{
87  return x>=absEtaMin && x<absEtaMax && y>=nrClusMin && y<=nrClusMax
88  && z>=etMin && z < etMax;
89  }
90  };
91 
92 
93  //these structs wrap the TF1 object
94  //also if the ParamType doesnt have a high enough dimension
95  //(ie using only one with x,y for a TF3), then the second
96  //template parameter disables the function
97  template<typename ParamType,bool=true>
98  struct TF1Wrap{
99  private:
100  TF1 func_;
101  public:
102  TF1Wrap(const std::string& funcExpr,const std::vector<double>& params):
103  func_("func",funcExpr.c_str()){
104  for(size_t paraNr=0;paraNr<params.size();paraNr++){
105  func_.SetParameter(paraNr,params[paraNr]);
106  }
107  }
108  float operator()(const ParamType& obj){
109  return func_.Eval(obj.x);
110  };
111  };
112  template<typename ParamType>
113  class TF1Wrap<ParamType,false>{
114  public:
115  TF1Wrap(const std::string& funcExpr,const std::vector<double>& params){}
116  float operator()(const ParamType& obj){return 1.;};
117  };
118 
119  template<typename ParamType,bool=true>
120  struct TF2Wrap{
121  private:
122  TF2 func_;
123  public:
124  TF2Wrap(const std::string& funcExpr,const std::vector<double>& params):
125  func_("func",funcExpr.c_str()){
126  for(size_t paraNr=0;paraNr<params.size();paraNr++){
127  func_.SetParameter(paraNr,params[paraNr]);
128  }
129  }
130  float operator()(const ParamType& obj){
131  return func_.Eval(obj.x,obj.y);
132  };
133  };
134  template<typename ParamType>
135  class TF2Wrap<ParamType,false>{
136  public:
137  TF2Wrap(const std::string& funcExpr,const std::vector<double>& params){}
138  float operator()(const ParamType& obj){return 1.;};
139  };
140 
141  template<typename ParamType,bool=true>
142  struct TF3Wrap{
143  private:
144  TF3 func_;
145  public:
146  TF3Wrap(const std::string& funcExpr,const std::vector<double>& params):
147  func_("func",funcExpr.c_str()){
148  for(size_t paraNr=0;paraNr<params.size();paraNr++){
149  func_.SetParameter(paraNr,params[paraNr]);
150  }
151  }
152  float operator()(const ParamType& obj){
153  return func_.Eval(obj.x,obj.y,obj.z);
154  };
155  };
156  template<typename ParamType>
157  class TF3Wrap<ParamType,false>{
158  public:
159  TF3Wrap(const std::string& funcExpr,const std::vector<double>& params){}
160  float operator()(const ParamType& obj){return 1.;};
161  };
162 
163  //the following functions allow for the fact that the type in the CMSSW PSet does not
164  //have floats do when it sees a float parameter, it retrieves a double
165  template<typename T>
166  struct ConfigType{
167  typedef T type;
168  };
169  template<>
170  struct ConfigType<float>{
171  typedef double type;
172  };
173  template<>
174  struct ConfigType<size_t>{
175  typedef int type;
176  };
177 
178 
179  //helper functions to figure out what dimension the ParamType is
180  //and not generate functions which require a higher dimension
181  template<typename T>
182  constexpr auto has1D(int) -> decltype(T::x,bool()){return true;}
183  template<typename T>
184  constexpr bool has1D(...){return false;}
185  template<typename T>
186  constexpr auto has2D(int) -> decltype(T::y,bool()){return true;}
187  template<typename T>
188  constexpr bool has2D(...){return false;}
189  template<typename T>
190  constexpr auto has3D(int) -> decltype(T::z,bool()){return true;}
191  template<typename T>
192  constexpr bool has3D(...){return false;}
193 
194  template<typename InputType>
195  class ParamBin {
196  public:
198  virtual ~ParamBin(){}
199  virtual bool pass(const InputType&)const=0;
200  virtual float operator()(const InputType&)const=0;
201  protected:
202  //the FUNCTYPE:=funcExpr is designed for future extensions
203  static std::pair<std::string,std::string> readFuncStr(const std::string& inStr){
204  size_t pos=inStr.find(":=");
205  if(pos!=std::string::npos) return std::make_pair(inStr.substr(0,pos),inStr.substr(pos+2));
206  else return std::make_pair(inStr,std::string(""));
207  }
208  template<typename ParamType>
209  static std::function<float(const ParamType&)> makeFunc(const edm::ParameterSet& config){
210  auto funcType = readFuncStr(config.getParameter<std::string>("funcType"));
211  auto funcParams = config.getParameter<std::vector<double> >("funcParams");
212  if(funcType.first=="TF1" && has1D<ParamType>(0)) return TF1Wrap<ParamType,has1D<ParamType>(0)>(funcType.second,funcParams);
213  else if(funcType.first=="TF2" && has2D<ParamType>(0)) return TF2Wrap<ParamType,has2D<ParamType>(0)>(funcType.second,funcParams);
214  else if(funcType.first=="TF3" && has3D<ParamType>(0)) return TF3Wrap<ParamType,has3D<ParamType>(0)>(funcType.second,funcParams);
215  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;
216  }
217  };
218 
219  template<typename InputType,typename ParamType>
220  class ParamBin1D : public ParamBin<InputType> {
221  private:
222  using XType = decltype(ParamType::x);
223  XType xMin_,xMax_;
224  std::function<float(const ParamType&)> func_;
225  public:
227  xMin_(config.getParameter<typename ConfigType<XType>::type >("xMin")),
228  xMax_(config.getParameter<typename ConfigType<XType>::type >("xMax")),
229  func_(ParamBin<InputType>::template makeFunc<ParamType>(config))
230  {
231  }
232  bool pass(const InputType& input)const override {
233  return ParamType(input).pass(xMin_,xMax_);
234  }
235  float operator()(const InputType& input)const override{
236  if(!pass(input)) return 0;
237  else return func_(ParamType(input));
238  }
239  };
240 
241 
242 
243  template<typename InputType,typename ParamType>
244  class ParamBin2D : public ParamBin<InputType> {
245  private:
246  using XType = decltype(ParamType::x);
247  using YType = decltype(ParamType::y);
248  XType xMin_,xMax_;
249  YType yMin_,yMax_;
250  std::function<float(const ParamType&)> func_;
251  public:
253  xMin_(config.getParameter<typename ConfigType<XType>::type >("xMin")),
254  xMax_(config.getParameter<typename ConfigType<XType>::type >("xMax")),
255  yMin_(config.getParameter<typename ConfigType<YType>::type >("yMin")),
256  yMax_(config.getParameter<typename ConfigType<YType>::type >("yMax")),
257  func_(ParamBin<InputType>::template makeFunc<ParamType>(config))
258  {
259  }
260 
261  bool pass(const InputType& input)const override {
262  return ParamType(input).pass(xMin_,xMax_,yMin_,yMax_);
263  }
264  float operator()(const InputType& input)const override{
265  if(!pass(input)) return 0;
266  else return func_(ParamType(input));
267  }
268  };
269 
270 
271  template<typename InputType,typename ParamType>
272  class ParamBin3D : public ParamBin<InputType> {
273  using XType = decltype(ParamType::x);
274  using YType = decltype(ParamType::y);
275  using ZType = decltype(ParamType::z);
276 
277  XType xMin_,xMax_;
278  YType yMin_,yMax_;
279  ZType zMin_,zMax_;
280  std::function<float(const ParamType&)> func_;
281  public:
283  xMin_(config.getParameter<typename ConfigType<XType>::type >("xMin")),
284  xMax_(config.getParameter<typename ConfigType<XType>::type >("xMax")),
285  yMin_(config.getParameter<typename ConfigType<YType>::type >("yMin")),
286  yMax_(config.getParameter<typename ConfigType<YType>::type >("yMax")),
287  zMin_(config.getParameter<typename ConfigType<ZType>::type >("zMin")),
288  zMax_(config.getParameter<typename ConfigType<ZType>::type >("zMax")),
289  func_(ParamBin<InputType>::template makeFunc<ParamType>(config))
290  {
291  }
292 
293  bool pass(const InputType& input)const override {
294  return ParamType(input).pass(xMin_,xMax_,yMin_,yMax_,zMin_,zMax_);
295  }
296  float operator()(const InputType& input)const override{
297  if(!pass(input)) return 0;
298  else return func_(ParamType(input));
299  }
300  };
301 
302 
303 
304 
305  template<typename InputType>
306  class Param {
307  std::vector<std::unique_ptr<ParamBin<InputType> > > bins_;
308  public:
310  std::vector<edm::ParameterSet> binConfigs = config.getParameter<std::vector<edm::ParameterSet> >("bins");
311  for(auto& binConfig : binConfigs) bins_.emplace_back(createParamBin_(binConfig));
312  }
313  float operator()(const InputType& input)const{
314  for(auto& bin : bins_){
315  if(bin->pass(input)) return (*bin)(input);
316  }
317  return -1; //didnt find a suitable bin, just return -1 for now
318  }
319 
320  private:
321  std::unique_ptr<ParamBin<InputType> > createParamBin_(const edm::ParameterSet& config){
322  std::string type = config.getParameter<std::string>("binType");
323  if(type=="AbsEtaClus") return std::make_unique<ParamBin2D<InputType,AbsEtaNrClus>>(config);
324  else if(type=="AbsEtaClusPhi") return std::make_unique<ParamBin3D<InputType,AbsEtaNrClusPhi>>(config);
325  else if(type=="AbsEtaClusEt") return std::make_unique<ParamBin3D<InputType,AbsEtaNrClusEt>>(config);
326  else throw cms::Exception("InvalidConfig") << " type "<<type<<" is not recognised, configuration is invalid and needs to be fixed"<<std::endl;
327  }
328  };
329 }
type
Definition: HCALResponse.h:21
T getParameter(std::string const &) const
decltype(ParamType::x) XType
static std::pair< std::string, std::string > readFuncStr(const std::string &inStr)
float operator()(const InputType &input) const override
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)
const CaloClusterRef & caloCluster() const
Definition: ElectronSeed.h:94
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
Definition: config.py:1
auto has3D(int) -> decltype(T::z, bool())
float float float z
static std::string const input
Definition: EdmProvDump.cc:48
Param(const edm::ParameterSet &config)
AbsEtaNrClus(const reco::ElectronSeed &seed)
bool pass(const InputType &input) const override
bool pass(const InputType &input) const override
config
Definition: looper.py:291
ParamBin1D(const edm::ParameterSet &config)
std::function< float(const ParamType &)> func_
float operator()(const InputType &input) const override
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)
float operator()(const ParamType &obj)
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
def template(fileName, svg, replaceme="REPLACEME")
Definition: svgfig.py:521
std::unique_ptr< ParamBin< InputType > > createParamBin_(const edm::ParameterSet &config)
TF1Wrap(const std::string &funcExpr, const std::vector< double > &params)
bin
set the eta bin as selection string.
AbsEtaNrClusEt(const reco::ElectronSeed &seed)
decltype(ParamType::y) YType
REF castTo() const
Definition: RefToBase.h:289
bool pass(const InputType &input) const override
TF3Wrap(const std::string &funcExpr, const std::vector< double > &params)
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_
float operator()(const InputType &input) const override
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)
#define constexpr
float operator()(const ParamType &obj)
TF2Wrap(const std::string &funcExpr, const std::vector< double > &params)