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