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 
190  return m_formula_str;
191  }
192 
193 #ifndef STANDALONE
195  return m_formula.get();
196  }
197 #else
198  TFormula const * getFormula() const {
199  return m_formula.get();
200  }
201 #endif
202  void init();
203 
204  private:
205  std::vector<std::string> m_bins_name;
206  std::vector<std::string> m_variables_name;
208 
209 #ifndef STANDALONE
210  std::shared_ptr<reco::FormulaEvaluator> m_formula COND_TRANSIENT;
211 #else
212  std::shared_ptr<TFormula> m_formula COND_TRANSIENT;
213 #endif
214  std::vector<Binning> m_bins COND_TRANSIENT;
215  std::vector<Binning> m_variables COND_TRANSIENT;
216 
218  };
219 
220  class Record {
221  public:
222  Record() {
223  // Empty
224  }
225 
226  Record(const std::string& record, const Definition& def);
227 
228  const std::vector<Range>& getBinsRange() const {
229  return m_bins_range;
230  }
231 
232  const std::vector<Range>& getVariablesRange() const {
233  return m_variables_range;
234  }
235 
236  const std::vector<float>& getParametersValues() const {
237  return m_parameters_values;
238  }
239 
240  size_t nVariables() const {
241  return m_variables_range.size();
242  }
243 
244  size_t nParameters() const {
245  return m_parameters_values.size();
246  }
247 
248  private:
249  std::vector<Range> m_bins_range;
250  std::vector<Range> m_variables_range;
251  std::vector<float> m_parameters_values;
252 
254  };
255 
256  public:
260 
261  void dump() const;
262  void saveToFile(const std::string& file) const;
263 
264  const Record* getRecord(const JetParameters& bins) const;
265  float evaluateFormula(const Record& record, const JetParameters& variables) const;
266 
267  const std::vector<Record>& getRecords() const {
268  return m_records;
269  }
270 
271  const Definition& getDefinition() const {
272  return m_definition;
273  }
274 
275  private:
277  std::vector<Record> m_records;
278 
279  bool m_valid = false;
280 
282  };
283 };
284 
285 #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
static const bimap< Binning, std::string > binning_to_string