CMS 3D CMS Logo

JetResolutionObject.h
Go to the documentation of this file.
1 #ifndef JetResolutionObject_h
2 #define JetResolutionObject_h
3 
4 // If you want to use the JER code in standalone mode, you'll need to create a new define named 'STANDALONE'. If you use gcc for compiling, you'll need to add
5 // -DSTANDALONE to the command line
6 // In standalone mode, no reference to CMSSW exists, so the only way to retrieve resolutions and scale factors are from text files.
7 
8 #ifndef STANDALONE
10 #else
11 // Create no-op definitions of CMSSW macro
12 #define COND_SERIALIZABLE
13 #define COND_TRANSIENT
14 #endif
15 
16 #include <unordered_map>
17 #include <vector>
18 #include <string>
19 #include <tuple>
20 #include <memory>
21 #include <initializer_list>
22 
23 #ifndef STANDALONE
25 #else
26 #include <TFormula.h>
27 #endif
28 
29 enum class Variation { NOMINAL = 0, DOWN = 1, UP = 2 };
30 
31 template <typename T>
32 T clip(const T& n, const T& lower, const T& upper) {
33  return std::max(lower, std::min(n, upper));
34 }
35 
36 namespace JME {
37  template <typename T, typename U>
38  struct bimap {
39  typedef std::unordered_map<T, U> left_type;
40  typedef std::unordered_map<U, T> right_type;
41 
44 
45  bimap(std::initializer_list<typename left_type::value_type> l) {
46  for (auto& v : l) {
47  left.insert(v);
48  right.insert(typename right_type::value_type(v.second, v.first));
49  }
50  }
51 
52  bimap() {
53  // Empty
54  }
55 
56  bimap(bimap&& rhs) {
57  left = std::move(rhs.left);
58  right = std::move(rhs.right);
59  }
60  };
61 
62  enum class Binning {
63  JetPt = 0,
64  JetEta,
65  JetAbsEta,
66  JetE,
67  JetArea,
68  Mu,
69  Rho,
70  NPV,
71  };
72 
73 }; // namespace JME
74 
75 // Hash function for Binning enum class
76 namespace std {
77  template <>
78  struct hash<JME::Binning> {
80  typedef std::size_t result_type;
81 
82  hash<uint8_t> int_hash;
83 
84  result_type operator()(argument_type const& s) const { return int_hash(static_cast<uint8_t>(s)); }
85  };
86 }; // namespace std
87 
88 namespace JME {
89 
90  class JetParameters {
91  public:
92  typedef std::unordered_map<Binning, float> value_type;
93 
94  JetParameters() = default;
96  JetParameters(std::initializer_list<typename value_type::value_type> init);
97 
98  JetParameters& setJetPt(float pt);
99  JetParameters& setJetEta(float eta);
100  JetParameters& setJetE(float e);
101  JetParameters& setJetArea(float area);
102  JetParameters& setMu(float mu);
103  JetParameters& setRho(float rho);
104  JetParameters& setNPV(float npv);
105  JetParameters& set(const Binning& bin, float value);
106  JetParameters& set(const typename value_type::value_type& value);
107 
109 
110  std::vector<float> createVector(const std::vector<Binning>& binning) const;
111 
112  private:
114  };
115 
117  public:
118  struct Range {
119  float min;
120  float max;
121 
122  Range() {
123  // Empty
124  }
125 
126  Range(float min, float max) {
127  this->min = min;
128  this->max = max;
129  }
130 
131  bool is_inside(float value) const { return (value >= min) && (value < max); }
132 
134  };
135 
136  class Definition {
137  public:
139  // Empty
140  }
141 
142  Definition(const std::string& definition);
143 
144  const std::vector<std::string>& getBinsName() const { return m_bins_name; }
145 
146  const std::vector<Binning>& getBins() const { return m_bins; }
147 
148  std::string getBinName(size_t bin) const { return m_bins_name[bin]; }
149 
150  size_t nBins() const { return m_bins_name.size(); }
151 
152  const std::vector<std::string>& getVariablesName() const { return m_variables_name; }
153 
154  const std::vector<Binning>& getVariables() const { return m_variables; }
155 
157 
158  size_t nVariables() const { return m_variables.size(); }
159 
160  const std::vector<std::string>& getParametersName() const { return m_parameters_name; }
161 
162  size_t nParameters() const { return m_parameters_name.size(); }
163 
165 
166 #ifndef STANDALONE
167  const reco::FormulaEvaluator* getFormula() const { return m_formula.get(); }
168 #else
169  TFormula const* getFormula() const { return m_formula.get(); }
170 #endif
171  void init();
172 
173  private:
174  std::vector<std::string> m_bins_name;
175  std::vector<std::string> m_variables_name;
177 
178 #ifndef STANDALONE
179  std::shared_ptr<reco::FormulaEvaluator> m_formula COND_TRANSIENT;
180 #else
181  std::shared_ptr<TFormula> m_formula COND_TRANSIENT;
182 #endif
183  std::vector<Binning> m_bins COND_TRANSIENT;
184  std::vector<Binning> m_variables COND_TRANSIENT;
185  std::vector<std::string> m_parameters_name COND_TRANSIENT;
186 
188  };
189 
190  class Record {
191  public:
192  Record() {
193  // Empty
194  }
195 
196  Record(const std::string& record, const Definition& def);
197 
198  const std::vector<Range>& getBinsRange() const { return m_bins_range; }
199 
200  const std::vector<Range>& getVariablesRange() const { return m_variables_range; }
201 
202  const std::vector<float>& getParametersValues() const { return m_parameters_values; }
203 
204  size_t nVariables() const { return m_variables_range.size(); }
205 
206  size_t nParameters() const { return m_parameters_values.size(); }
207 
208  private:
209  std::vector<Range> m_bins_range;
210  std::vector<Range> m_variables_range;
211  std::vector<float> m_parameters_values;
212 
214  };
215 
216  public:
220 
221  void dump() const;
222  void saveToFile(const std::string& file) const;
223 
224  const Record* getRecord(const JetParameters& bins) const;
225  float evaluateFormula(const Record& record, const JetParameters& variables) const;
226 
227  const std::vector<Record>& getRecords() const { return m_records; }
228 
229  const Definition& getDefinition() const { return m_definition; }
230 
231  private:
233  std::vector<Record> m_records;
234 
235  bool m_valid = false;
236 
238  };
239 }; // namespace JME
240 
241 #endif
int def(FILE *, FILE *, int)
const std::vector< std::string > & getParametersName() const
const reco::FormulaEvaluator * getFormula() const
const std::vector< Range > & getBinsRange() const
JetParameters & setJetEta(float eta)
std::vector< float > createVector(const std::vector< Binning > &binning) const
std::unordered_map< Binning, float > value_type
std::string getVariableName(size_t variable) const
JetParameters & setRho(float rho)
T clip(const T &n, const T &lower, const T &upper)
result_type operator()(argument_type const &s) const
std::unordered_map< T, U > left_type
const std::vector< std::string > & getVariablesName() const
const std::vector< Range > & getVariablesRange() const
std::vector< std::string > m_parameters_name
std::shared_ptr< reco::FormulaEvaluator > m_formula
const std::vector< std::string > & getBinsName() const
std::vector< std::string > m_variables_name
std::vector< std::string > m_bins_name
std::vector< float > m_parameters_values
Definition: value.py:1
const std::vector< float > & getParametersValues() const
std::unordered_map< U, T > right_type
right_type right
const std::vector< Binning > & getVariables() const
Definition: init.py:1
bimap(bimap &&rhs)
const Definition & getDefinition() const
#define COND_TRANSIENT
Definition: Serializable.h:63
const Record * getRecord(const JetParameters &bins) const
bool is_inside(float value) const
JetParameters & setJetE(float e)
#define COND_SERIALIZABLE
Definition: Serializable.h:39
JetParameters & setMu(float mu)
const std::vector< Binning > & getBins() const
void saveToFile(const std::string &file) const
bimap(std::initializer_list< typename left_type::value_type > l)
JetParameters & setJetPt(float pt)
std::vector< Record > m_records
long double T
const std::vector< Record > & getRecords() const
float evaluateFormula(const Record &record, const JetParameters &variables) const
def move(src, dest)
Definition: eostools.py:511
JetParameters & setNPV(float npv)
JetParameters()=default
std::string getBinName(size_t bin) const
JetParameters & setJetArea(float area)
static const bimap< Binning, std::string > binning_to_string