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 { NOMINAL = 0, DOWN = 1, UP = 2 };
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 
42  left_type left;
43  right_type right;
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 }; // namespace JME
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 
84  result_type operator()(argument_type const& s) const { return int_hash(static_cast<uint8_t>(s)); }
85  };
86 }; // namespace std
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  JetParameters& setJetPt(float pt);
99  JetParameters& setJetEta(float eta);
100  JetParameters& setJetE(float e);
101  JetParameters& setJetArea(float area);
102  JetParameters& setMu(float mu);
103  JetParameters& setRho(float rho);
104  JetParameters& setNPV(float npv);
105  JetParameters& set(const Binning& bin, float value);
106  JetParameters& set(const typename value_type::value_type& value);
107 
109 
110  std::vector<float> createVector(const std::vector<Binning>& binning) const;
111 
112  private:
113  value_type m_values;
114  };
115 
117  public:
118  struct Range {
119  float min;
120  float max;
121 
122  Range() {
123  // Empty
124  }
125 
126  Range(float min, float max) {
127  this->min = min;
128  this->max = max;
129  }
130 
131  bool is_inside(float value) const { return (value >= min) && (value <= max); }
132 
134  };
135 
136  class Definition {
137  public:
139  // Empty
140  }
141 
142  Definition(const std::string& definition);
143 
144  const std::vector<std::string>& getBinsName() const { return m_bins_name; }
145 
146  const std::vector<Binning>& getBins() const { return m_bins; }
147 
148  std::string getBinName(size_t bin) const { return m_bins_name[bin]; }
149 
150  size_t nBins() const { return m_bins_name.size(); }
151 
152  const std::vector<std::string>& getVariablesName() const { return m_variables_name; }
153 
154  const std::vector<Binning>& getVariables() const { return m_variables; }
155 
156  std::string getVariableName(size_t variable) const { return m_variables_name[variable]; }
157 
158  size_t nVariables() const { return m_variables.size(); }
159 
160  std::string getFormulaString() const { return m_formula_str; }
161 
162 #ifndef STANDALONE
163  const reco::FormulaEvaluator* getFormula() const { return m_formula.get(); }
164 #else
165  TFormula const* getFormula() const { return m_formula.get(); }
166 #endif
167  void init();
168 
169  private:
170  std::vector<std::string> m_bins_name;
171  std::vector<std::string> m_variables_name;
173 
174 #ifndef STANDALONE
175  std::shared_ptr<reco::FormulaEvaluator> m_formula COND_TRANSIENT;
176 #else
177  std::shared_ptr<TFormula> m_formula COND_TRANSIENT;
178 #endif
179  std::vector<Binning> m_bins COND_TRANSIENT;
180  std::vector<Binning> m_variables COND_TRANSIENT;
181 
183  };
184 
185  class Record {
186  public:
187  Record() {
188  // Empty
189  }
190 
191  Record(const std::string& record, const Definition& def);
192 
193  const std::vector<Range>& getBinsRange() const { return m_bins_range; }
194 
195  const std::vector<Range>& getVariablesRange() const { return m_variables_range; }
196 
197  const std::vector<float>& getParametersValues() const { return m_parameters_values; }
198 
199  size_t nVariables() const { return m_variables_range.size(); }
200 
201  size_t nParameters() const { return m_parameters_values.size(); }
202 
203  private:
204  std::vector<Range> m_bins_range;
205  std::vector<Range> m_variables_range;
206  std::vector<float> m_parameters_values;
207 
209  };
210 
211  public:
215 
216  void dump() const;
217  void saveToFile(const std::string& file) const;
218 
219  const Record* getRecord(const JetParameters& bins) const;
220  float evaluateFormula(const Record& record, const JetParameters& variables) const;
221 
222  const std::vector<Record>& getRecords() const { return m_records; }
223 
224  const Definition& getDefinition() const { return m_definition; }
225 
226  private:
228  std::vector<Record> m_records;
229 
230  bool m_valid = false;
231 
233  };
234 }; // namespace JME
235 
236 #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:64
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
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
Definition: init.py:1
bimap(bimap &&rhs)
const std::vector< float > & getParametersValues() const
#define COND_TRANSIENT
Definition: Serializable.h:62
Definition: L1GtObject.h:29
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