test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 #include <TFormula.h>
24 
25 enum class Variation {
26  NOMINAL = 0,
27  DOWN = 1,
28  UP = 2
29 };
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 };
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 
85  return int_hash(static_cast<uint8_t>(s));
86  }
87  };
88 };
89 
90 namespace JME {
91 
92  class JetParameters {
93  public:
94  typedef std::unordered_map<Binning, float> value_type;
95 
96  JetParameters() = default;
98  JetParameters(std::initializer_list<typename value_type::value_type> init);
99 
100 
101  JetParameters& setJetPt(float pt);
102  JetParameters& setJetEta(float eta);
103  JetParameters& setJetE(float e);
104  JetParameters& setJetArea(float area);
105  JetParameters& setMu(float mu);
106  JetParameters& setRho(float rho);
107  JetParameters& setNPV(float npv);
108  JetParameters& set(const Binning& bin, float value);
109  JetParameters& set(const typename value_type::value_type& value);
110 
112 
113  std::vector<float> createVector(const std::vector<Binning>& binning) const;
114 
115  private:
117  };
118 
119 
121 
122  public:
123 
124  struct Range {
125  float min;
126  float max;
127 
128  Range() {
129  // Empty
130  }
131 
132  Range(float min, float max) {
133  this->min = min;
134  this->max = max;
135  }
136 
137  bool is_inside(float value) const {
138  return (value >= min) && (value <= max);
139  }
140 
142  };
143 
144  class Definition {
145 
146  public:
148  // Empty
149  }
150 
151  Definition(const std::string& definition);
152 
153  const std::vector<std::string>& getBinsName() const {
154  return m_bins_name;
155  }
156 
157  const std::vector<Binning>& getBins() const {
158  return m_bins;
159  }
160 
161  std::string getBinName(size_t bin) const {
162  return m_bins_name[bin];
163  }
164 
165  size_t nBins() const {
166  return m_bins_name.size();
167  }
168 
169  const std::vector<std::string>& getVariablesName() const {
170  return m_variables_name;
171  }
172 
173  const std::vector<Binning>& getVariables() const {
174  return m_variables;
175  }
176 
177  std::string getVariableName(size_t variable) const {
178  return m_variables_name[variable];
179  }
180 
181  size_t nVariables() const {
182  return m_variables.size();
183  }
184 
186  return m_formula_str;
187  }
188 
189  TFormula* getFormula() const {
190  return m_formula.get();
191  }
192 
193  void init();
194 
195  private:
196  std::vector<std::string> m_bins_name;
197  std::vector<std::string> m_variables_name;
199 
200  std::shared_ptr<TFormula> m_formula COND_TRANSIENT;
201 
202  std::vector<Binning> m_bins COND_TRANSIENT;
203  std::vector<Binning> m_variables COND_TRANSIENT;
204 
206  };
207 
208  class Record {
209  public:
210  Record() {
211  // Empty
212  }
213 
214  Record(const std::string& record, const Definition& def);
215 
216  const std::vector<Range>& getBinsRange() const {
217  return m_bins_range;
218  }
219 
220  const std::vector<Range>& getVariablesRange() const {
221  return m_variables_range;
222  }
223 
224  const std::vector<float>& getParametersValues() const {
225  return m_parameters_values;
226  }
227 
228  size_t nVariables() const {
229  return m_variables_range.size();
230  }
231 
232  size_t nParameters() const {
233  return m_parameters_values.size();
234  }
235 
236  private:
237  std::vector<Range> m_bins_range;
238  std::vector<Range> m_variables_range;
239  std::vector<float> m_parameters_values;
240 
242  };
243 
244  public:
248 
249  void dump() const;
250  void saveToFile(const std::string& file) const;
251 
252  const Record* getRecord(const JetParameters& bins) const;
253  float evaluateFormula(const Record& record, const JetParameters& variables) const;
254 
255  const std::vector<Record>& getRecords() const {
256  return m_records;
257  }
258 
259  const Definition& getDefinition() const {
260  return m_definition;
261  }
262 
263  private:
265  std::vector<Record> m_records;
266 
267  bool m_valid = false;
268 
270  };
271 };
272 
273 #endif
const std::vector< Binning > & getVariables() const
const Record * getRecord(const JetParameters &bins) const
JetParameters & setJetEta(float eta)
std::unordered_map< Binning, float > value_type
JetParameters & setRho(float rho)
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
JetParameters & set(const Binning &bin, float value)
const std::vector< std::string > & getVariablesName() const
std::unordered_map< T, U > left_type
const std::vector< Range > & getVariablesRange() const
void saveToFile(const std::string &file) const
std::string getVariableName(size_t variable) const
float evaluateFormula(const Record &record, const JetParameters &variables) 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
def move
Definition: eostools.py:510
std::vector< std::string > m_bins_name
std::string getBinName(size_t bin) const
std::shared_ptr< TFormula > m_formula
const int mu
Definition: Constants.h:22
std::vector< float > m_parameters_values
T min(T a, T b)
Definition: MathUtil.h:58
std::vector< float > createVector(const std::vector< Binning > &binning) const
std::unordered_map< U, T > right_type
right_type right
bimap(bimap &&rhs)
const std::vector< float > & getParametersValues() const
#define COND_TRANSIENT
Definition: Serializable.h:60
bool is_inside(float value) const
JetParameters & setJetE(float e)
#define COND_SERIALIZABLE
Definition: Serializable.h:37
JetParameters & setMu(float mu)
tuple filename
Definition: lut2db_cfg.py:20
bimap(std::initializer_list< typename left_type::value_type > l)
JetParameters & setJetPt(float pt)
std::vector< Record > m_records
long double T
JetCorrectorParameters::Definitions def
Definition: classes.h:6
const std::vector< Range > & getBinsRange() const
JetParameters & setNPV(float npv)
JetParameters()=default
JetParameters & setJetArea(float area)
static const bimap< Binning, std::string > binning_to_string