DESY Hbb Analysis Framework
JetResolutionObject.h
Go to the documentation of this file.
1 #ifndef Analysis_Tools_JetResolutionObject_h
2 #define Analysis_Tools_JetResolutionObject_h
3 
4 // Original code: CMSSW_10_2_22 - CondFormats/JetMETObjects/interface/JetResolutionObject.h
5 
6 // 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
7 // -DSTANDALONE to the command line
8 // In standalone mode, no reference to CMSSW exists, so the only way to retrieve resolutions and scale factors are from text files.
9 
10 // Create no-op definitions of CMSSW macro
11 #define COND_SERIALIZABLE
12 #define COND_TRANSIENT
13 
14 #include <unordered_map>
15 #include <vector>
16 #include <string>
17 #include <tuple>
18 #include <memory>
19 #include <initializer_list>
20 
21 #include <TFormula.h>
22 
23 enum class Variation {
24  NOMINAL = 0,
25  DOWN = 1,
26  UP = 2
27 };
28 
29 template <typename T>
30 T clip(const T& n, const T& lower, const T& upper) {
31  return std::max(lower, std::min(n, upper));
32 }
33 
34 namespace JME {
35  template <typename T, typename U>
36  struct bimap {
37  typedef std::unordered_map<T, U> left_type;
38  typedef std::unordered_map<U, T> right_type;
39 
40  left_type left;
41  right_type right;
42 
43  bimap(std::initializer_list<typename left_type::value_type> l) {
44  for (auto& v: l) {
45  left.insert(v);
46  right.insert(typename right_type::value_type(v.second, v.first));
47  }
48  }
49 
50  bimap() {
51  // Empty
52  }
53 
54  bimap(bimap&& rhs) {
55  left = std::move(rhs.left);
56  right = std::move(rhs.right);
57  }
58  };
59 
60  enum class Binning {
61  JetPt = 0,
62  JetEta,
63  JetAbsEta,
64  JetE,
65  JetArea,
66  Mu,
67  Rho,
68  NPV,
69  };
70 
71 };
72 
73 // Hash function for Binning enum class
74 namespace std {
75  template<>
76  struct hash<JME::Binning> {
78  typedef std::size_t result_type;
79 
80  hash<uint8_t> int_hash;
81 
82  result_type operator()(argument_type const& s) const {
83  return int_hash(static_cast<uint8_t>(s));
84  }
85  };
86 };
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 
99  JetParameters& setJetPt(float pt);
100  JetParameters& setJetEta(float eta);
101  JetParameters& setJetE(float e);
102  JetParameters& setJetArea(float area);
103  JetParameters& setMu(float mu);
104  JetParameters& setRho(float rho);
105  JetParameters& setNPV(float npv);
106  JetParameters& set(const Binning& bin, float value);
107  JetParameters& set(const typename value_type::value_type& value);
108 
110 
111  std::vector<float> createVector(const std::vector<Binning>& binning) const;
112 
113  private:
114  value_type m_values;
115  };
116 
117 
119 
120  public:
121 
122  struct Range {
123  float min;
124  float max;
125 
126  Range() {
127  // Empty
128  }
129 
130  Range(float min, float max) {
131  this->min = min;
132  this->max = max;
133  }
134 
135  bool is_inside(float value) const {
136  return (value >= min) && (value <= max);
137  }
138 
140  };
141 
142  class Definition {
143 
144  public:
146  // Empty
147  }
148 
149  Definition(const std::string& definition);
150 
151  const std::vector<std::string>& getBinsName() const {
152  return m_bins_name;
153  }
154 
155  const std::vector<Binning>& getBins() const {
156  return m_bins;
157  }
158 
159  std::string getBinName(size_t bin) const {
160  return m_bins_name[bin];
161  }
162 
163  size_t nBins() const {
164  return m_bins_name.size();
165  }
166 
167  const std::vector<std::string>& getVariablesName() const {
168  return m_variables_name;
169  }
170 
171  const std::vector<Binning>& getVariables() const {
172  return m_variables;
173  }
174 
175  std::string getVariableName(size_t variable) const {
176  return m_variables_name[variable];
177  }
178 
179  size_t nVariables() const {
180  return m_variables.size();
181  }
182 
183  const std::vector<std::string>& getParametersName() const { return m_parameters_name; }
184 
185  size_t nParameters() const { return m_parameters_name.size(); }
186 
187  std::string getFormulaString() const {
188  return m_formula_str;
189  }
190 
191  TFormula const * getFormula() const {
192  return m_formula.get();
193  }
194  void init();
195 
196  private:
197  std::vector<std::string> m_bins_name;
198  std::vector<std::string> m_variables_name;
199  std::string m_formula_str;
200 
201  std::shared_ptr<TFormula> m_formula COND_TRANSIENT;
202  std::vector<Binning> m_bins COND_TRANSIENT;
203  std::vector<Binning> m_variables COND_TRANSIENT;
204  std::vector<std::string> m_parameters_name COND_TRANSIENT;
205 
207  };
208 
209  class Record {
210  public:
211  Record() {
212  // Empty
213  }
214 
215  Record(const std::string& record, const Definition& def);
216 
217  const std::vector<Range>& getBinsRange() const {
218  return m_bins_range;
219  }
220 
221  const std::vector<Range>& getVariablesRange() const {
222  return m_variables_range;
223  }
224 
225  const std::vector<float>& getParametersValues() const {
226  return m_parameters_values;
227  }
228 
229  size_t nVariables() const {
230  return m_variables_range.size();
231  }
232 
233  size_t nParameters() const {
234  return m_parameters_values.size();
235  }
236 
237  private:
238  std::vector<Range> m_bins_range;
239  std::vector<Range> m_variables_range;
240  std::vector<float> m_parameters_values;
241 
243  };
244 
245  public:
246  JetResolutionObject(const std::string& filename);
247  JetResolutionObject(const JetResolutionObject& filename);
249 
250  void dump() const;
251  void saveToFile(const std::string& file) const;
252 
253  const Record* getRecord(const JetParameters& bins) const;
254  float evaluateFormula(const Record& record, const JetParameters& variables) const;
255 
256  const std::vector<Record>& getRecords() const {
257  return m_records;
258  }
259 
260  const Definition& getDefinition() const {
261  return m_definition;
262  }
263 
264  private:
266  std::vector<Record> m_records;
267 
268  bool m_valid = false;
269 
271  };
272 };
273 
274 #endif
const std::vector< Binning > & getVariables() const
std::unordered_map< Binning, float > value_type
T clip(const T &n, const T &lower, const T &upper)
const std::vector< Binning > & getBins() const
std::vector< std::string > m_parameters_name COND_TRANSIENT
const std::vector< std::string > & getVariablesName() const
std::unordered_map< T, U > left_type
const std::vector< Range > & getVariablesRange() const
std::shared_ptr< TFormula > m_formula COND_TRANSIENT
std::string getVariableName(size_t variable) const
const std::vector< Record > & getRecords() const
std::vector< std::string > m_variables_name
const Definition & getDefinition() const
result_type operator()(argument_type const &s) const
const std::vector< std::string > & getBinsName() const
std::vector< std::string > m_bins_name
std::string getBinName(size_t bin) const
std::vector< float > m_parameters_values
float T
Definition: PlotsCompare.cc:18
std::unordered_map< U, T > right_type
right_type right
bimap(bimap &&rhs)
const std::vector< float > & getParametersValues() const
std::vector< Binning > m_bins COND_TRANSIENT
bool is_inside(float value) const
bimap(std::initializer_list< typename left_type::value_type > l)
std::vector< Record > m_records
const std::vector< Range > & getBinsRange() const
std::vector< Binning > m_variables COND_TRANSIENT
const std::vector< std::string > & getParametersName() const
static const bimap< Binning, std::string > binning_to_string