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 {
30  NOMINAL = 0,
31  DOWN = 1,
32  UP = 2
33 };
34 
35 template <typename T>
36 T clip(const T& n, const T& lower, const T& upper) {
37  return std::max(lower, std::min(n, upper));
38 }
39 
40 namespace JME {
41  template <typename T, typename U>
42  struct bimap {
43  typedef std::unordered_map<T, U> left_type;
44  typedef std::unordered_map<U, T> right_type;
45 
46  left_type left;
47  right_type right;
48 
49  bimap(std::initializer_list<typename left_type::value_type> l) {
50  for (auto& v: l) {
51  left.insert(v);
52  right.insert(typename right_type::value_type(v.second, v.first));
53  }
54  }
55 
56  bimap() {
57  // Empty
58  }
59 
60  bimap(bimap&& rhs) {
61  left = std::move(rhs.left);
62  right = std::move(rhs.right);
63  }
64  };
65 
66  enum class Binning {
67  JetPt = 0,
68  JetEta,
69  JetAbsEta,
70  JetE,
71  JetArea,
72  Mu,
73  Rho,
74  NPV,
75  };
76 
77 };
78 
79 // Hash function for Binning enum class
80 namespace std {
81  template<>
82  struct hash<JME::Binning> {
84  typedef std::size_t result_type;
85 
86  hash<uint8_t> int_hash;
87 
88  result_type operator()(argument_type const& s) const {
89  return int_hash(static_cast<uint8_t>(s));
90  }
91  };
92 };
93 
94 namespace JME {
95 
96  class JetParameters {
97  public:
98  typedef std::unordered_map<Binning, float> value_type;
99 
100  JetParameters() = default;
102  JetParameters(std::initializer_list<typename value_type::value_type> init);
103 
104 
105  JetParameters& setJetPt(float pt);
106  JetParameters& setJetEta(float eta);
107  JetParameters& setJetE(float e);
108  JetParameters& setJetArea(float area);
109  JetParameters& setMu(float mu);
110  JetParameters& setRho(float rho);
111  JetParameters& setNPV(float npv);
112  JetParameters& set(const Binning& bin, float value);
113  JetParameters& set(const typename value_type::value_type& value);
114 
116 
117  std::vector<float> createVector(const std::vector<Binning>& binning) const;
118 
119  private:
120  value_type m_values;
121  };
122 
123 
125 
126  public:
127 
128  struct Range {
129  float min;
130  float max;
131 
132  Range() {
133  // Empty
134  }
135 
136  Range(float min, float max) {
137  this->min = min;
138  this->max = max;
139  }
140 
141  bool is_inside(float value) const {
142  return (value >= min) && (value <= max);
143  }
144 
146  };
147 
148  class Definition {
149 
150  public:
152  // Empty
153  }
154 
155  Definition(const std::string& definition);
156 
157  const std::vector<std::string>& getBinsName() const {
158  return m_bins_name;
159  }
160 
161  const std::vector<Binning>& getBins() const {
162  return m_bins;
163  }
164 
165  std::string getBinName(size_t bin) const {
166  return m_bins_name[bin];
167  }
168 
169  size_t nBins() const {
170  return m_bins_name.size();
171  }
172 
173  const std::vector<std::string>& getVariablesName() const {
174  return m_variables_name;
175  }
176 
177  const std::vector<Binning>& getVariables() const {
178  return m_variables;
179  }
180 
182  return m_variables_name[variable];
183  }
184 
185  size_t nVariables() const {
186  return m_variables.size();
187  }
188 
189  const std::vector<std::string>& getParametersName() const { return m_parameters_name; }
190 
191  size_t nParameters() const { return m_parameters_name.size(); }
192 
194  return m_formula_str;
195  }
196 
197 #ifndef STANDALONE
199  return m_formula.get();
200  }
201 #else
202  TFormula const * getFormula() const {
203  return m_formula.get();
204  }
205 #endif
206  void init();
207 
208  private:
209  std::vector<std::string> m_bins_name;
210  std::vector<std::string> m_variables_name;
212 
213 #ifndef STANDALONE
214  std::shared_ptr<reco::FormulaEvaluator> m_formula COND_TRANSIENT;
215 #else
216  std::shared_ptr<TFormula> m_formula COND_TRANSIENT;
217 #endif
218  std::vector<Binning> m_bins COND_TRANSIENT;
219  std::vector<Binning> m_variables COND_TRANSIENT;
220  std::vector<std::string> m_parameters_name COND_TRANSIENT;
221 
223  };
224 
225  class Record {
226  public:
227  Record() {
228  // Empty
229  }
230 
231  Record(const std::string& record, const Definition& def);
232 
233  const std::vector<Range>& getBinsRange() const {
234  return m_bins_range;
235  }
236 
237  const std::vector<Range>& getVariablesRange() const {
238  return m_variables_range;
239  }
240 
241  const std::vector<float>& getParametersValues() const {
242  return m_parameters_values;
243  }
244 
245  size_t nVariables() const {
246  return m_variables_range.size();
247  }
248 
249  size_t nParameters() const {
250  return m_parameters_values.size();
251  }
252 
253  private:
254  std::vector<Range> m_bins_range;
255  std::vector<Range> m_variables_range;
256  std::vector<float> m_parameters_values;
257 
259  };
260 
261  public:
265 
266  void dump() const;
267  void saveToFile(const std::string& file) const;
268 
269  const Record* getRecord(const JetParameters& bins) const;
270  float evaluateFormula(const Record& record, const JetParameters& variables) const;
271 
272  const std::vector<Record>& getRecords() const {
273  return m_records;
274  }
275 
276  const Definition& getDefinition() const {
277  return m_definition;
278  }
279 
280  private:
282  std::vector<Record> m_records;
283 
284  bool m_valid = false;
285 
287  };
288 };
289 
290 #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
JetCorrectorParameters::Record record
Definition: classes.h:7
int init
Definition: HydjetWrapper.h:67
const std::vector< std::string > & getVariablesName() const
std::unordered_map< T, U > left_type
const std::vector< Range > & getVariablesRange() const
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
const reco::FormulaEvaluator * getFormula() const
std::string getBinName(size_t bin) const
const int mu
Definition: Constants.h:22
std::vector< float > m_parameters_values
Definition: value.py:1
T min(T a, T b)
Definition: MathUtil.h:58
std::unordered_map< U, T > right_type
right_type right
bin
set the eta bin as selection string.
Definition: init.py:1
bimap(bimap &&rhs)
const std::vector< float > & getParametersValues() const
#define COND_TRANSIENT
Definition: Serializable.h:61
Definition: L1GtObject.h:30
bool is_inside(float value) const
#define COND_SERIALIZABLE
Definition: Serializable.h:38
bimap(std::initializer_list< typename left_type::value_type > l)
std::vector< Record > m_records
long double T
JetCorrectorParameters::Definitions def
Definition: classes.h:6
const std::vector< Range > & getBinsRange() const
def move(src, dest)
Definition: eostools.py:511
const std::vector< std::string > & getParametersName() const
static const bimap< Binning, std::string > binning_to_string