CMS 3D CMS Logo

FunctionDefs.h
Go to the documentation of this file.
1 #ifndef DQMOffline_Trigger_FunctionDefs_h
2 #define DQMOffline_Trigger_FunctionDefs_h
3 
4 //***************************************************************************
5 //
6 // Description:
7 // These are the functions we wish to access via strings
8 // The function returns a std::function holding the function
9 // Have to admit, I'm not happy with the implimentation but fine
10 // no time to do something better
11 //
12 // There are various issues. The main is the awkward specialisations
13 // needed for the different types. Its a nasty hack. Probably
14 // can do it cleaner with ROOT dictionaries
15 //
16 // Useage:
17 // 1) define any extra functions you need at the top (seed scEtaFunc as example)
18 // 2) generic functions applicable to all normal objects are set in
19 // getUnaryFuncFloat (if they are floats, other types will need seperate
20 // functions which can be done with this as example
21 // 3) object specific functions are done with getUnaryFuncExtraFloat
22 // by specialising that function approprately for the object
23 // 4) user should simply call getUnaryFuncFloat()
24 //
25 // Author: Sam Harper (RAL) , 2017
26 //
27 //***************************************************************************
28 
30 
31 #include <vector>
32 #include <functional>
33 
36 
37 namespace hltdqm {
38  //here we define needed functions that otherwise dont exist
39  template <typename ObjType>
40  float scEtaFunc(const ObjType& obj) {
41  return obj.superCluster()->eta();
42  }
43 
44  //additional functions specific to a given type (specialised later on)
45  template <typename ObjType>
46  std::function<float(const ObjType&)> getUnaryFuncExtraFloat(const std::string& varName) {
47  std::function<float(const ObjType&)> varFunc;
48  return varFunc;
49  }
50 
51  //the generic function to call
52  template <typename ObjType>
53  std::function<float(const ObjType&)> getUnaryFuncFloat(const std::string& varName) {
54  std::function<float(const ObjType&)> varFunc;
55  if (varName == "et")
56  varFunc = &ObjType::et;
57  else if (varName == "pt")
58  varFunc = &ObjType::pt;
59  else if (varName == "eta")
60  varFunc = &ObjType::eta;
61  else if (varName == "phi")
62  varFunc = &ObjType::phi;
63  else
64  varFunc = getUnaryFuncExtraFloat<ObjType>(varName);
65  //check if we never set varFunc and throw an error for anything but an empty input string
66  if (!varFunc && !varName.empty()) {
67  throw cms::Exception("ConfigError")
68  << "var " << varName << " not recognised " << __FILE__ << "," << __LINE__ << std::endl;
69  }
70  return varFunc;
71  }
72 
73  template <>
74  std::function<float(const reco::GsfElectron&)> getUnaryFuncExtraFloat<reco::GsfElectron>(const std::string& varName);
75  template <>
76  std::function<float(const reco::Photon&)> getUnaryFuncExtraFloat<reco::Photon>(const std::string& varName);
77 
78 } // namespace hltdqm
79 
80 #endif
float scEtaFunc(const ObjType &obj)
Definition: FunctionDefs.h:40
std::function< float(const ObjType &)> getUnaryFuncFloat(const std::string &varName)
Definition: FunctionDefs.h:53
std::function< float(const ObjType &)> getUnaryFuncExtraFloat(const std::string &varName)
Definition: FunctionDefs.h:46