Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-01-24 01:11:18

0001 #ifndef JetResolutionObject_h
0002 #define JetResolutionObject_h
0003 
0004 // 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
0005 // -DSTANDALONE to the command line
0006 // In standalone mode, no reference to CMSSW exists, so the only way to retrieve resolutions and scale factors are from text files.
0007 
0008 #ifndef STANDALONE
0009 #include "CondFormats/Serialization/interface/Serializable.h"
0010 #else
0011 // Create no-op definitions of CMSSW macro
0012 #define COND_SERIALIZABLE
0013 #define COND_TRANSIENT
0014 #endif
0015 
0016 #include <unordered_map>
0017 #include <vector>
0018 #include <string>
0019 #include <tuple>
0020 #include <memory>
0021 #include <initializer_list>
0022 
0023 #ifndef STANDALONE
0024 #include "CommonTools/Utils/interface/FormulaEvaluator.h"
0025 #else
0026 #include <TFormula.h>
0027 #endif
0028 
0029 enum class Variation { NOMINAL = 0, DOWN = 1, UP = 2 };
0030 
0031 template <typename T>
0032 T clip(const T& n, const T& lower, const T& upper) {
0033   return std::max(lower, std::min(n, upper));
0034 }
0035 
0036 namespace JME {
0037   template <typename T, typename U>
0038   struct bimap {
0039     typedef std::unordered_map<T, U> left_type;
0040     typedef std::unordered_map<U, T> right_type;
0041 
0042     left_type left;
0043     right_type right;
0044 
0045     bimap(std::initializer_list<typename left_type::value_type> l) {
0046       for (auto& v : l) {
0047         left.insert(v);
0048         right.insert(typename right_type::value_type(v.second, v.first));
0049       }
0050     }
0051 
0052     bimap() {
0053       // Empty
0054     }
0055 
0056     bimap(bimap&& rhs) {
0057       left = std::move(rhs.left);
0058       right = std::move(rhs.right);
0059     }
0060   };
0061 
0062   enum class Binning {
0063     JetPt = 0,
0064     JetEta,
0065     JetAbsEta,
0066     JetE,
0067     JetArea,
0068     Mu,
0069     Rho,
0070     NPV,
0071   };
0072 
0073 };  // namespace JME
0074 
0075 // Hash function for Binning enum class
0076 namespace std {
0077   template <>
0078   struct hash<JME::Binning> {
0079     typedef JME::Binning argument_type;
0080     typedef std::size_t result_type;
0081 
0082     hash<uint8_t> int_hash;
0083 
0084     result_type operator()(argument_type const& s) const { return int_hash(static_cast<uint8_t>(s)); }
0085   };
0086 };  // namespace std
0087 
0088 namespace JME {
0089 
0090   class JetParameters {
0091   public:
0092     typedef std::unordered_map<Binning, float> value_type;
0093 
0094     JetParameters() = default;
0095     JetParameters(JetParameters&& rhs);
0096     JetParameters(std::initializer_list<typename value_type::value_type> init);
0097 
0098     JetParameters& setJetPt(float pt);
0099     JetParameters& setJetEta(float eta);
0100     JetParameters& setJetE(float e);
0101     JetParameters& setJetArea(float area);
0102     JetParameters& setMu(float mu);
0103     JetParameters& setRho(float rho);
0104     JetParameters& setNPV(float npv);
0105     JetParameters& set(const Binning& bin, float value);
0106     JetParameters& set(const typename value_type::value_type& value);
0107 
0108     static const bimap<Binning, std::string> binning_to_string;
0109 
0110     std::vector<float> createVector(const std::vector<Binning>& binning) const;
0111 
0112   private:
0113     value_type m_values;
0114   };
0115 
0116   class JetResolutionObject {
0117   public:
0118     struct Range {
0119       float min;
0120       float max;
0121 
0122       Range() {
0123         // Empty
0124       }
0125 
0126       Range(float min, float max) {
0127         this->min = min;
0128         this->max = max;
0129       }
0130 
0131       bool is_inside(float value) const { return (value >= min) && (value < max); }
0132 
0133       COND_SERIALIZABLE;
0134     };
0135 
0136     class Definition {
0137     public:
0138       Definition() {
0139         // Empty
0140       }
0141 
0142       Definition(const std::string& definition);
0143 
0144       const std::vector<std::string>& getBinsName() const { return m_bins_name; }
0145 
0146       const std::vector<Binning>& getBins() const { return m_bins; }
0147 
0148       std::string getBinName(size_t bin) const { return m_bins_name[bin]; }
0149 
0150       size_t nBins() const { return m_bins_name.size(); }
0151 
0152       const std::vector<std::string>& getVariablesName() const { return m_variables_name; }
0153 
0154       const std::vector<Binning>& getVariables() const { return m_variables; }
0155 
0156       std::string getVariableName(size_t variable) const { return m_variables_name[variable]; }
0157 
0158       size_t nVariables() const { return m_variables.size(); }
0159 
0160       const std::vector<std::string>& getParametersName() const { return m_parameters_name; }
0161 
0162       size_t nParameters() const { return m_parameters_name.size(); }
0163 
0164       std::string getFormulaString() const { return m_formula_str; }
0165 
0166 #ifndef STANDALONE
0167       const reco::FormulaEvaluator* getFormula() const { return m_formula.get(); }
0168 #else
0169       TFormula const* getFormula() const { return m_formula.get(); }
0170 #endif
0171       void init();
0172 
0173     private:
0174       std::vector<std::string> m_bins_name;
0175       std::vector<std::string> m_variables_name;
0176       std::string m_formula_str;
0177 
0178 #ifndef STANDALONE
0179       std::shared_ptr<reco::FormulaEvaluator> m_formula COND_TRANSIENT;
0180 #else
0181       std::shared_ptr<TFormula> m_formula COND_TRANSIENT;
0182 #endif
0183       std::vector<Binning> m_bins COND_TRANSIENT;
0184       std::vector<Binning> m_variables COND_TRANSIENT;
0185       std::vector<std::string> m_parameters_name COND_TRANSIENT;
0186 
0187       COND_SERIALIZABLE;
0188     };
0189 
0190     class Record {
0191     public:
0192       Record() {
0193         // Empty
0194       }
0195 
0196       Record(const std::string& record, const Definition& def);
0197 
0198       const std::vector<Range>& getBinsRange() const { return m_bins_range; }
0199 
0200       const std::vector<Range>& getVariablesRange() const { return m_variables_range; }
0201 
0202       const std::vector<float>& getParametersValues() const { return m_parameters_values; }
0203 
0204       size_t nVariables() const { return m_variables_range.size(); }
0205 
0206       size_t nParameters() const { return m_parameters_values.size(); }
0207 
0208     private:
0209       std::vector<Range> m_bins_range;
0210       std::vector<Range> m_variables_range;
0211       std::vector<float> m_parameters_values;
0212 
0213       COND_SERIALIZABLE;
0214     };
0215 
0216   public:
0217     JetResolutionObject(const std::string& filename);
0218     JetResolutionObject(const JetResolutionObject& filename);
0219     JetResolutionObject();
0220 
0221     void dump() const;
0222     void saveToFile(const std::string& file) const;
0223 
0224     const Record* getRecord(const JetParameters& bins) const;
0225     float evaluateFormula(const Record& record, const JetParameters& variables) const;
0226 
0227     const std::vector<Record>& getRecords() const { return m_records; }
0228 
0229     const Definition& getDefinition() const { return m_definition; }
0230 
0231   private:
0232     Definition m_definition;
0233     std::vector<Record> m_records;
0234 
0235     bool m_valid = false;
0236 
0237     COND_SERIALIZABLE;
0238   };
0239 };  // namespace JME
0240 
0241 #endif