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 
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  std::vector<float> createVector(const std::vector<std::string>& binname) const;
112 
113  private:
115  };
116 
118  public:
119  struct Range {
120  float min;
121  float max;
122 
123  Range() {
124  // Empty
125  }
126 
127  Range(float min, float max) {
128  this->min = min;
129  this->max = max;
130  }
131 
132  bool is_inside(float value) const { return (value >= min) && (value < max); }
133 
135  };
136 
137  class Definition {
138  public:
140  // Empty
141  }
142 
143  Definition(const std::string& definition);
144 
145  const std::vector<std::string>& getBinsName() const { return m_bins_name; }
146 
147  const std::vector<Binning>& getBins() const { return m_bins; }
148 
149  std::string getBinName(size_t bin) const { return m_bins_name[bin]; }
150 
151  size_t nBins() const { return m_bins_name.size(); }
152 
153  const std::vector<std::string>& getVariablesName() const { return m_variables_name; }
154 
155  const std::vector<Binning>& getVariables() const { return m_variables; }
156 
158 
159  size_t nVariables() const { return m_variables_name.size(); }
160 
161  const std::vector<std::string>& getParametersName() const { return m_parameters_name; }
162 
163  size_t nParameters() const { return m_parameters_name.size(); }
164 
166 
167 #ifndef STANDALONE
168  const reco::FormulaEvaluator* getFormula() const { return m_formula.get(); }
169 #else
170  TFormula const* getFormula() const { return m_formula.get(); }
171 #endif
172  void init();
173 
174  private:
175  std::vector<std::string> m_bins_name;
176  std::vector<std::string> m_variables_name;
178 
179 #ifndef STANDALONE
180  std::shared_ptr<reco::FormulaEvaluator> m_formula COND_TRANSIENT;
181 #else
182  std::shared_ptr<TFormula> m_formula COND_TRANSIENT;
183 #endif
184  std::vector<Binning> m_bins COND_TRANSIENT;
185  std::vector<Binning> m_variables COND_TRANSIENT;
186  std::vector<std::string> m_parameters_name COND_TRANSIENT;
187 
189  };
190 
191  class Record {
192  public:
193  Record() {
194  // Empty
195  }
196 
197  Record(const std::string& record, const Definition& def);
198 
199  const std::vector<Range>& getBinsRange() const { return m_bins_range; }
200 
201  const std::vector<Range>& getVariablesRange() const { return m_variables_range; }
202 
203  const std::vector<float>& getParametersValues() const { return m_parameters_values; }
204 
205  size_t nVariables() const { return m_variables_range.size(); }
206 
207  size_t nParameters() const { return m_parameters_values.size(); }
208 
209  private:
210  std::vector<Range> m_bins_range;
211  std::vector<Range> m_variables_range;
212  std::vector<float> m_parameters_values;
213 
215  };
216 
217  public:
221 
222  void dump() const;
223  void saveToFile(const std::string& file) const;
224 
225  const Record* getRecord(const JetParameters& bins) const;
226  float evaluateFormula(const Record& record, const JetParameters& variables) const;
227 
228  const std::vector<Record>& getRecords() const { return m_records; }
229 
230  const Definition& getDefinition() const { return m_definition; }
231 
232  private:
234  std::vector<Record> m_records;
235 
236  bool m_valid = false;
237 
239  };
240 }; // namespace JME
241 
242 #endif
int def(FILE *, FILE *, int)
const std::vector< std::string > & getParametersName() const
const reco::FormulaEvaluator * getFormula() const
const std::vector< Range > & getBinsRange() const
JetParameters & setJetEta(float eta)
std::vector< float > createVector(const std::vector< Binning > &binning) const
std::unordered_map< Binning, float > value_type
std::string getVariableName(size_t variable) const
JetParameters & setRho(float rho)
T clip(const T &n, const T &lower, const T &upper)
result_type operator()(argument_type const &s) const
std::unordered_map< T, U > left_type
const std::vector< std::string > & getVariablesName() const
const std::vector< Range > & getVariablesRange() const
std::vector< std::string > m_parameters_name
std::shared_ptr< reco::FormulaEvaluator > m_formula
const std::vector< std::string > & getBinsName() const
std::vector< std::string > m_variables_name
std::vector< std::string > m_bins_name
std::vector< float > m_parameters_values
Definition: value.py:1
const std::vector< float > & getParametersValues() const
std::unordered_map< U, T > right_type
right_type right
const std::vector< Binning > & getVariables() const
Definition: init.py:1
bimap(bimap &&rhs)
const Definition & getDefinition() const
#define COND_TRANSIENT
Definition: Serializable.h:63
const Record * getRecord(const JetParameters &bins) const
bool is_inside(float value) const
JetParameters & setJetE(float e)
#define COND_SERIALIZABLE
Definition: Serializable.h:39
JetParameters & setMu(float mu)
const std::vector< Binning > & getBins() const
void saveToFile(const std::string &file) const
bimap(std::initializer_list< typename left_type::value_type > l)
JetParameters & setJetPt(float pt)
std::vector< Record > m_records
long double T
const std::vector< Record > & getRecords() const
float evaluateFormula(const Record &record, const JetParameters &variables) const
def move(src, dest)
Definition: eostools.py:511
JetParameters & setNPV(float npv)
JetParameters()=default
std::string getBinName(size_t bin) const
JetParameters & setJetArea(float area)
static const bimap< Binning, std::string > binning_to_string