Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:02:17

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     std::vector<float> createVector(const std::vector<std::string>& binname) const;
0112 
0113   private:
0114     value_type m_values;
0115   };
0116 
0117   class JetResolutionObject {
0118   public:
0119     struct Range {
0120       float min;
0121       float max;
0122 
0123       Range() {
0124         // Empty
0125       }
0126 
0127       Range(float min, float max) {
0128         this->min = min;
0129         this->max = max;
0130       }
0131 
0132       bool is_inside(float value) const { return (value >= min) && (value < max); }
0133 
0134       COND_SERIALIZABLE;
0135     };
0136 
0137     class Definition {
0138     public:
0139       Definition() {
0140         // Empty
0141       }
0142 
0143       Definition(const std::string& definition);
0144 
0145       const std::vector<std::string>& getBinsName() const { return m_bins_name; }
0146 
0147       const std::vector<Binning>& getBins() const { return m_bins; }
0148 
0149       std::string getBinName(size_t bin) const { return m_bins_name[bin]; }
0150 
0151       size_t nBins() const { return m_bins_name.size(); }
0152 
0153       const std::vector<std::string>& getVariablesName() const { return m_variables_name; }
0154 
0155       const std::vector<Binning>& getVariables() const { return m_variables; }
0156 
0157       std::string getVariableName(size_t variable) const { return m_variables_name[variable]; }
0158 
0159       size_t nVariables() const { return m_variables_name.size(); }
0160 
0161       const std::vector<std::string>& getParametersName() const { return m_parameters_name; }
0162 
0163       size_t nParameters() const { return m_parameters_name.size(); }
0164 
0165       std::string getFormulaString() const { return m_formula_str; }
0166 
0167 #ifndef STANDALONE
0168       const reco::FormulaEvaluator* getFormula() const { return m_formula.get(); }
0169 #else
0170       TFormula const* getFormula() const { return m_formula.get(); }
0171 #endif
0172       void init();
0173 
0174     private:
0175       std::vector<std::string> m_bins_name;
0176       std::vector<std::string> m_variables_name;
0177       std::string m_formula_str;
0178 
0179 #ifndef STANDALONE
0180       std::shared_ptr<reco::FormulaEvaluator> m_formula COND_TRANSIENT;
0181 #else
0182       std::shared_ptr<TFormula> m_formula COND_TRANSIENT;
0183 #endif
0184       std::vector<Binning> m_bins COND_TRANSIENT;
0185       std::vector<Binning> m_variables COND_TRANSIENT;
0186       std::vector<std::string> m_parameters_name COND_TRANSIENT;
0187 
0188       COND_SERIALIZABLE;
0189     };
0190 
0191     class Record {
0192     public:
0193       Record() {
0194         // Empty
0195       }
0196 
0197       Record(const std::string& record, const Definition& def);
0198 
0199       const std::vector<Range>& getBinsRange() const { return m_bins_range; }
0200 
0201       const std::vector<Range>& getVariablesRange() const { return m_variables_range; }
0202 
0203       const std::vector<float>& getParametersValues() const { return m_parameters_values; }
0204 
0205       size_t nVariables() const { return m_variables_range.size(); }
0206 
0207       size_t nParameters() const { return m_parameters_values.size(); }
0208 
0209     private:
0210       std::vector<Range> m_bins_range;
0211       std::vector<Range> m_variables_range;
0212       std::vector<float> m_parameters_values;
0213 
0214       COND_SERIALIZABLE;
0215     };
0216 
0217   public:
0218     JetResolutionObject(const std::string& filename);
0219     JetResolutionObject(const JetResolutionObject& filename);
0220     JetResolutionObject();
0221 
0222     void dump() const;
0223     void saveToFile(const std::string& file) const;
0224 
0225     const Record* getRecord(const JetParameters& bins) const;
0226     float evaluateFormula(const Record& record, const JetParameters& variables) const;
0227 
0228     const std::vector<Record>& getRecords() const { return m_records; }
0229 
0230     const Definition& getDefinition() const { return m_definition; }
0231 
0232   private:
0233     Definition m_definition;
0234     std::vector<Record> m_records;
0235 
0236     bool m_valid = false;
0237 
0238     COND_SERIALIZABLE;
0239   };
0240 };  // namespace JME
0241 
0242 #endif