00001 #ifndef JetResolutionObject_h
00002 #define JetResolutionObject_h
00003
00004
00005
00006
00007 #define STANDALONE
00008
00009 #ifndef STANDALONE
00010 #include "CondFormats/Serialization/interface/Serializable.h"
00011 #else
00012
00013 #define COND_SERIALIZABLE
00014 #define COND_TRANSIENT
00015 #endif
00016
00017 #include <unordered_map>
00018 #include <vector>
00019 #include <string>
00020 #include <tuple>
00021 #include <memory>
00022 #include <initializer_list>
00023
00024 #ifndef STANDALONE
00025 #include "CommonTools/Utils/interface/FormulaEvaluator.h"
00026 #else
00027 #include <TFormula.h>
00028 #endif
00029
00030 enum class Variation {
00031 NOMINAL = 0,
00032 DOWN = 1,
00033 UP = 2
00034 };
00035
00036 template <typename T>
00037 T clip(const T& n, const T& lower, const T& upper) {
00038 return std::max(lower, std::min(n, upper));
00039 }
00040
00041 namespace JME {
00042 template <typename T, typename U>
00043 struct bimap {
00044 typedef std::unordered_map<T, U> left_type;
00045 typedef std::unordered_map<U, T> right_type;
00046
00047 left_type left;
00048 right_type right;
00049
00050 bimap(std::initializer_list<typename left_type::value_type> l) {
00051 for (auto& v: l) {
00052 left.insert(v);
00053 right.insert(typename right_type::value_type(v.second, v.first));
00054 }
00055 }
00056
00057 bimap() {
00058
00059 }
00060
00061 bimap(bimap&& rhs) {
00062 left = std::move(rhs.left);
00063 right = std::move(rhs.right);
00064 }
00065 };
00066
00067 enum class Binning {
00068 JetPt = 0,
00069 JetEta,
00070 JetAbsEta,
00071 JetE,
00072 JetArea,
00073 Mu,
00074 Rho,
00075 NPV,
00076 };
00077
00078 }
00079
00080
00081 namespace std {
00082 template<>
00083 struct hash<JME::Binning> {
00084 typedef JME::Binning argument_type;
00085 typedef std::size_t result_type;
00086
00087 hash<uint8_t> int_hash;
00088
00089 result_type operator()(argument_type const& s) const {
00090 return int_hash(static_cast<uint8_t>(s));
00091 }
00092 };
00093 }
00094
00095 namespace JME {
00096
00097 class JetParameters {
00098 public:
00099 typedef std::unordered_map<Binning, float> value_type;
00100
00101 JetParameters() = default;
00102 JetParameters(JetParameters&& rhs);
00103 JetParameters(std::initializer_list<typename value_type::value_type> init);
00104
00105
00106 JetParameters& setJetPt(float pt);
00107 JetParameters& setJetEta(float eta);
00108 JetParameters& setJetE(float e);
00109 JetParameters& setJetArea(float area);
00110 JetParameters& setMu(float mu);
00111 JetParameters& setRho(float rho);
00112 JetParameters& setNPV(float npv);
00113 JetParameters& set(const Binning& bin, float value);
00114 JetParameters& set(const typename value_type::value_type& value);
00115
00116 static const bimap<Binning, std::string> binning_to_string;
00117
00118 std::vector<float> createVector(const std::vector<Binning>& binning) const;
00119
00120 private:
00121 value_type m_values;
00122 };
00123
00124
00125 class JetResolutionObject {
00126
00127 public:
00128
00129 struct Range {
00130 float min;
00131 float max;
00132
00133 Range() {
00134
00135 }
00136
00137 Range(float min, float max) {
00138 this->min = min;
00139 this->max = max;
00140 }
00141
00142 bool is_inside(float value) const {
00143 return (value >= min) && (value <= max);
00144 }
00145
00146 COND_SERIALIZABLE;
00147 };
00148
00149 class Definition {
00150
00151 public:
00152 Definition() {
00153
00154 }
00155
00156 Definition(const std::string& definition);
00157
00158 const std::vector<std::string>& getBinsName() const {
00159 return m_bins_name;
00160 }
00161
00162 const std::vector<Binning>& getBins() const {
00163 return m_bins;
00164 }
00165
00166 std::string getBinName(size_t bin) const {
00167 return m_bins_name[bin];
00168 }
00169
00170 size_t nBins() const {
00171 return m_bins_name.size();
00172 }
00173
00174 const std::vector<std::string>& getVariablesName() const {
00175 return m_variables_name;
00176 }
00177
00178 const std::vector<Binning>& getVariables() const {
00179 return m_variables;
00180 }
00181
00182 std::string getVariableName(size_t variable) const {
00183 return m_variables_name[variable];
00184 }
00185
00186 size_t nVariables() const {
00187 return m_variables.size();
00188 }
00189
00190 const std::vector<std::string>& getParametersName() const { return m_parameters_name; }
00191
00192 size_t nParameters() const { return m_parameters_name.size(); }
00193
00194 std::string getFormulaString() const {
00195 return m_formula_str;
00196 }
00197
00198 #ifndef STANDALONE
00199 const reco::FormulaEvaluator* getFormula() const {
00200 return m_formula.get();
00201 }
00202 #else
00203 TFormula const * getFormula() const {
00204 return m_formula.get();
00205 }
00206 #endif
00207 void init();
00208
00209 private:
00210 std::vector<std::string> m_bins_name;
00211 std::vector<std::string> m_variables_name;
00212 std::string m_formula_str;
00213
00214 #ifndef STANDALONE
00215 std::shared_ptr<reco::FormulaEvaluator> m_formula COND_TRANSIENT;
00216 #else
00217 std::shared_ptr<TFormula> m_formula COND_TRANSIENT;
00218 #endif
00219 std::vector<Binning> m_bins COND_TRANSIENT;
00220 std::vector<Binning> m_variables COND_TRANSIENT;
00221 std::vector<std::string> m_parameters_name COND_TRANSIENT;
00222
00223 COND_SERIALIZABLE
00224 };
00225
00226 class Record {
00227 public:
00228 Record() {
00229
00230 }
00231
00232 Record(const std::string& record, const Definition& def);
00233
00234 const std::vector<Range>& getBinsRange() const {
00235 return m_bins_range;
00236 }
00237
00238 const std::vector<Range>& getVariablesRange() const {
00239 return m_variables_range;
00240 }
00241
00242 const std::vector<float>& getParametersValues() const {
00243 return m_parameters_values;
00244 }
00245
00246 size_t nVariables() const {
00247 return m_variables_range.size();
00248 }
00249
00250 size_t nParameters() const {
00251 return m_parameters_values.size();
00252 }
00253
00254 private:
00255 std::vector<Range> m_bins_range;
00256 std::vector<Range> m_variables_range;
00257 std::vector<float> m_parameters_values;
00258
00259 COND_SERIALIZABLE
00260 };
00261
00262 public:
00263 JetResolutionObject(const std::string& filename);
00264 JetResolutionObject(const JetResolutionObject& filename);
00265 JetResolutionObject();
00266
00267 void dump() const;
00268 void saveToFile(const std::string& file) const;
00269
00270 const Record* getRecord(const JetParameters& bins) const;
00271 float evaluateFormula(const Record& record, const JetParameters& variables) const;
00272
00273 const std::vector<Record>& getRecords() const {
00274 return m_records;
00275 }
00276
00277 const Definition& getDefinition() const {
00278 return m_definition;
00279 }
00280
00281 private:
00282 Definition m_definition;
00283 std::vector<Record> m_records;
00284
00285 bool m_valid = false;
00286
00287 COND_SERIALIZABLE
00288 };
00289 }
00290
00291 #endif