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 
112  private:
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 
157 
158  size_t nVariables() const { return m_variables.size(); }
159 
160  const std::vector<std::string>& getParametersName() const { return m_parameters_name; }
161 
162  size_t nParameters() const { return m_parameters_name.size(); }
163 
165 
166 #ifndef STANDALONE
167  const reco::FormulaEvaluator* getFormula() const { return m_formula.get(); }
168 #else
169  TFormula const* getFormula() const { return m_formula.get(); }
170 #endif
171  void init();
172 
173  private:
174  std::vector<std::string> m_bins_name;
175  std::vector<std::string> m_variables_name;
177 
178 #ifndef STANDALONE
179  std::shared_ptr<reco::FormulaEvaluator> m_formula COND_TRANSIENT;
180 #else
181  std::shared_ptr<TFormula> m_formula COND_TRANSIENT;
182 #endif
183  std::vector<Binning> m_bins COND_TRANSIENT;
184  std::vector<Binning> m_variables COND_TRANSIENT;
185  std::vector<std::string> m_parameters_name COND_TRANSIENT;
186 
188  };
189 
190  class Record {
191  public:
192  Record() {
193  // Empty
194  }
195 
196  Record(const std::string& record, const Definition& def);
197 
198  const std::vector<Range>& getBinsRange() const { return m_bins_range; }
199 
200  const std::vector<Range>& getVariablesRange() const { return m_variables_range; }
201 
202  const std::vector<float>& getParametersValues() const { return m_parameters_values; }
203 
204  size_t nVariables() const { return m_variables_range.size(); }
205 
206  size_t nParameters() const { return m_parameters_values.size(); }
207 
208  private:
209  std::vector<Range> m_bins_range;
210  std::vector<Range> m_variables_range;
211  std::vector<float> m_parameters_values;
212 
214  };
215 
216  public:
220 
221  void dump() const;
222  void saveToFile(const std::string& file) const;
223 
224  const Record* getRecord(const JetParameters& bins) const;
225  float evaluateFormula(const Record& record, const JetParameters& variables) const;
226 
227  const std::vector<Record>& getRecords() const { return m_records; }
228 
229  const Definition& getDefinition() const { return m_definition; }
230 
231  private:
233  std::vector<Record> m_records;
234 
235  bool m_valid = false;
236 
238  };
239 }; // namespace JME
240 
241 #endif
JME::JetParameters::value_type
std::unordered_map< Binning, float > value_type
Definition: JetResolutionObject.h:92
JME::JetResolutionObject::Record::m_variables_range
std::vector< Range > m_variables_range
Definition: JetResolutionObject.h:210
Variation
Variation
Definition: JetResolutionObject.h:29
JME::JetParameters::setJetEta
JetParameters & setJetEta(float eta)
Definition: JetResolutionObject.cc:69
JME::JetResolutionObject::Record::getParametersValues
const std::vector< float > & getParametersValues() const
Definition: JetResolutionObject.h:202
JME::JetResolutionObject::Range::Range
Range(float min, float max)
Definition: JetResolutionObject.h:126
JME::Binning::NPV
JME::JetParameters::setNPV
JetParameters & setNPV(float npv)
Definition: JetResolutionObject.cc:90
JME::JetResolutionObject
Definition: JetResolutionObject.h:116
JME::JetResolutionObject::Definition::m_variables_name
std::vector< std::string > m_variables_name
Definition: JetResolutionObject.h:175
dqmiodumpmetadata.n
n
Definition: dqmiodumpmetadata.py:28
JME::JetResolutionObject::Range
Definition: JetResolutionObject.h:118
JME::Binning
Binning
Definition: JetResolutionObject.h:62
JME
Definition: JetResolutionObject.h:36
JME::bimap::right_type
std::unordered_map< U, T > right_type
Definition: JetResolutionObject.h:40
DiDispStaMuonMonitor_cfi.pt
pt
Definition: DiDispStaMuonMonitor_cfi.py:39
clip
T clip(const T &n, const T &lower, const T &upper)
Definition: JetResolutionObject.h:32
amptDefaultParameters_cff.mu
mu
Definition: amptDefaultParameters_cff.py:16
L1TEGammaDiff_cfi.variables
variables
Definition: L1TEGammaDiff_cfi.py:5
JME::Binning::JetEta
min
T min(T a, T b)
Definition: MathUtil.h:58
cond::hash
Definition: Time.h:19
std::hash< JME::Binning >::result_type
std::size_t result_type
Definition: JetResolutionObject.h:80
std::hash< JME::Binning >::int_hash
hash< uint8_t > int_hash
Definition: JetResolutionObject.h:82
JME::Binning::JetArea
JME::JetResolutionObject::Record::nParameters
size_t nParameters() const
Definition: JetResolutionObject.h:206
COND_SERIALIZABLE
#define COND_SERIALIZABLE
Definition: Serializable.h:39
GlobalPosition_Frontier_DevDB_cff.record
record
Definition: GlobalPosition_Frontier_DevDB_cff.py:10
COND_TRANSIENT
#define COND_TRANSIENT
Definition: Serializable.h:63
JME::JetResolutionObject::getRecord
const Record * getRecord(const JetParameters &bins) const
Definition: JetResolutionObject.cc:364
JME::JetParameters
Definition: JetResolutionObject.h:90
JME::JetResolutionObject::Definition::getBins
const std::vector< Binning > & getBins() const
Definition: JetResolutionObject.h:146
Variation::UP
JME::JetResolutionObject::evaluateFormula
float evaluateFormula(const Record &record, const JetParameters &variables) const
Definition: JetResolutionObject.cc:394
JME::JetResolutionObject::Range::max
float max
Definition: JetResolutionObject.h:120
findQualityFiles.v
v
Definition: findQualityFiles.py:179
JME::JetParameters::setJetArea
JetParameters & setJetArea(float area)
Definition: JetResolutionObject.cc:80
JME::bimap::left
left_type left
Definition: JetResolutionObject.h:42
JME::JetResolutionObject::m_records
std::vector< Record > m_records
Definition: JetResolutionObject.h:233
JME::bimap
Definition: JetResolutionObject.h:38
JME::JetResolutionObject::Definition::getVariables
const std::vector< Binning > & getVariables() const
Definition: JetResolutionObject.h:154
JME::JetResolutionObject::Definition::getFormulaString
std::string getFormulaString() const
Definition: JetResolutionObject.h:164
JME::JetParameters::m_values
value_type m_values
Definition: JetResolutionObject.h:113
JME::JetResolutionObject::Definition::m_variables
std::vector< Binning > m_variables
Definition: JetResolutionObject.h:184
JME::bimap::right
right_type right
Definition: JetResolutionObject.h:43
JME::JetParameters::set
JetParameters & set(const Binning &bin, float value)
Definition: JetResolutionObject.cc:100
alignCSCRings.s
s
Definition: alignCSCRings.py:92
JME::JetResolutionObject::Definition::m_formula_str
std::string m_formula_str
Definition: JetResolutionObject.h:176
reco::FormulaEvaluator
Definition: FormulaEvaluator.h:67
JME::JetResolutionObject::Record::nVariables
size_t nVariables() const
Definition: JetResolutionObject.h:204
JME::JetParameters::setJetPt
JetParameters & setJetPt(float pt)
Definition: JetResolutionObject.cc:64
JME::JetResolutionObject::getDefinition
const Definition & getDefinition() const
Definition: JetResolutionObject.h:229
JME::JetResolutionObject::getRecords
const std::vector< Record > & getRecords() const
Definition: JetResolutionObject.h:227
PVValHelper::eta
Definition: PVValidationHelpers.h:69
JME::bimap::left_type
std::unordered_map< T, U > left_type
Definition: JetResolutionObject.h:39
JME::JetResolutionObject::Definition::Definition
Definition()
Definition: JetResolutionObject.h:138
JME::JetResolutionObject::Record::m_bins_range
std::vector< Range > m_bins_range
Definition: JetResolutionObject.h:209
JME::JetResolutionObject::Definition::getFormula
const reco::FormulaEvaluator * getFormula() const
Definition: JetResolutionObject.h:167
Variation::NOMINAL
JME::JetParameters::binning_to_string
static const bimap< Binning, std::string > binning_to_string
Definition: JetResolutionObject.h:108
JME::JetResolutionObject::Definition::getBinName
std::string getBinName(size_t bin) const
Definition: JetResolutionObject.h:148
JME::JetResolutionObject::Range::Range
Range()
Definition: JetResolutionObject.h:122
corrVsCorr.filename
filename
Definition: corrVsCorr.py:123
JME::bimap::bimap
bimap()
Definition: JetResolutionObject.h:52
JME::JetResolutionObject::Definition::getVariableName
std::string getVariableName(size_t variable) const
Definition: JetResolutionObject.h:156
JME::JetResolutionObject::Range::is_inside
bool is_inside(float value) const
Definition: JetResolutionObject.h:131
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
JME::JetResolutionObject::Definition::nBins
size_t nBins() const
Definition: JetResolutionObject.h:150
JME::Binning::JetE
JME::JetResolutionObject::dump
void dump() const
Definition: JetResolutionObject.cc:283
Binning
Definition: PPSAlignmentConfig.h:81
JME::JetParameters::setRho
JetParameters & setRho(float rho)
Definition: JetResolutionObject.cc:95
JME::JetResolutionObject::Record::Record
Record()
Definition: JetResolutionObject.h:192
JME::bimap::bimap
bimap(std::initializer_list< typename left_type::value_type > l)
Definition: JetResolutionObject.h:45
listHistos.binning
binning
Definition: listHistos.py:43
SiStripPI::max
Definition: SiStripPayloadInspectorHelper.h:169
FormulaEvaluator.h
JME::JetResolutionObject::Record
Definition: JetResolutionObject.h:190
FrontierConditions_GlobalTag_cff.file
file
Definition: FrontierConditions_GlobalTag_cff.py:13
Serializable.h
JME::JetResolutionObject::Definition::nVariables
size_t nVariables() const
Definition: JetResolutionObject.h:158
JME::JetResolutionObject::Record::m_parameters_values
std::vector< float > m_parameters_values
Definition: JetResolutionObject.h:211
value
Definition: value.py:1
JME::JetResolutionObject::Record::getBinsRange
const std::vector< Range > & getBinsRange() const
Definition: JetResolutionObject.h:198
JME::JetResolutionObject::Definition::nParameters
size_t nParameters() const
Definition: JetResolutionObject.h:162
reco::JetExtendedAssociation::value_type
Container::value_type value_type
Definition: JetExtendedAssociation.h:30
JME::Binning::JetAbsEta
JME::JetResolutionObject::Definition::getParametersName
const std::vector< std::string > & getParametersName() const
Definition: JetResolutionObject.h:160
JME::Binning::Mu
cmsLHEtoEOSManager.l
l
Definition: cmsLHEtoEOSManager.py:204
newFWLiteAna.bin
bin
Definition: newFWLiteAna.py:161
std::hash< JME::Binning >::operator()
result_type operator()(argument_type const &s) const
Definition: JetResolutionObject.h:84
JME::Binning::Rho
JME::JetParameters::JetParameters
JetParameters()=default
taus_updatedMVAIds_cff.variable
variable
Definition: taus_updatedMVAIds_cff.py:33
eostools.move
def move(src, dest)
Definition: eostools.py:511
std
Definition: JetResolutionObject.h:76
JME::JetParameters::createVector
std::vector< float > createVector(const std::vector< Binning > &binning) const
Definition: JetResolutionObject.cc:116
init
Definition: init.py:1
JME::JetResolutionObject::JetResolutionObject
JetResolutionObject()
Definition: JetResolutionObject.cc:279
T
long double T
Definition: Basic3DVectorLD.h:48
JME::JetResolutionObject::Definition::getVariablesName
const std::vector< std::string > & getVariablesName() const
Definition: JetResolutionObject.h:152
JME::JetResolutionObject::Range::min
float min
Definition: JetResolutionObject.h:119
Variation::DOWN
JME::JetResolutionObject::Definition::getBinsName
const std::vector< std::string > & getBinsName() const
Definition: JetResolutionObject.h:144
pileupCalc.upper
upper
Definition: pileupCalc.py:214
JME::JetResolutionObject::Definition::m_parameters_name
std::vector< std::string > m_parameters_name
Definition: JetResolutionObject.h:185
JME::JetResolutionObject::Definition
Definition: JetResolutionObject.h:136
JME::JetResolutionObject::m_definition
Definition m_definition
Definition: JetResolutionObject.h:232
JME::JetResolutionObject::Definition::init
void init()
Definition: JetResolutionObject.cc:186
JME::Binning::JetPt
JME::JetParameters::setJetE
JetParameters & setJetE(float e)
Definition: JetResolutionObject.cc:75
spu::def
int def(FILE *, FILE *, int)
Definition: SherpackUtilities.cc:14
JME::JetResolutionObject::Definition::m_bins
std::vector< Binning > m_bins
Definition: JetResolutionObject.h:183
JME::JetResolutionObject::Definition::m_bins_name
std::vector< std::string > m_bins_name
Definition: JetResolutionObject.h:174
trigObjTnPSource_cfi.bins
bins
Definition: trigObjTnPSource_cfi.py:20
std::hash< JME::Binning >::argument_type
JME::Binning argument_type
Definition: JetResolutionObject.h:79
JME::JetParameters::setMu
JetParameters & setMu(float mu)
Definition: JetResolutionObject.cc:85
JME::JetResolutionObject::saveToFile
void saveToFile(const std::string &file) const
Definition: JetResolutionObject.cc:327
JME::JetResolutionObject::m_valid
bool m_valid
Definition: JetResolutionObject.h:235
JME::JetResolutionObject::Definition::m_formula
std::shared_ptr< reco::FormulaEvaluator > m_formula
Definition: JetResolutionObject.h:179
custom_jme_cff.area
area
Definition: custom_jme_cff.py:140
JME::JetResolutionObject::Record::getVariablesRange
const std::vector< Range > & getVariablesRange() const
Definition: JetResolutionObject.h:200
JME::bimap::bimap
bimap(bimap &&rhs)
Definition: JetResolutionObject.h:56
MillePedeFileConverter_cfg.e
e
Definition: MillePedeFileConverter_cfg.py:37