Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 //
0002 // Original Author:  Fedor Ratnikov Nov 9, 2007
0003 // $Id: JetCorrectorParameters.h,v 1.15 2012/03/01 18:24:52 srappocc Exp $
0004 //
0005 // Generic parameters for Jet corrections
0006 //
0007 #ifndef JetCorrectorParameters_h
0008 #define JetCorrectorParameters_h
0009 
0010 #include "CondFormats/Serialization/interface/Serializable.h"
0011 #include "CondFormats/JetMETObjects/interface/Utilities.h"
0012 
0013 #include <string>
0014 #include <vector>
0015 #include <tuple>
0016 #include <algorithm>
0017 #include <functional>
0018 #include <iostream>
0019 #include <memory>
0020 #include "FWCore/Utilities/interface/Exception.h"
0021 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0022 
0023 class JetCorrectorParametersHelper;
0024 
0025 class JetCorrectorParameters {
0026   //---------------- JetCorrectorParameters class ----------------
0027   //-- Encapsulates all the information of the parametrization ---
0028 public:
0029   //---------------- Definitions class ---------------------------
0030   //-- Global iformation about the parametrization is kept here --
0031   class Definitions {
0032   public:
0033     //-------- Constructors --------------
0034     Definitions() : mIsResponse(false) {}
0035     Definitions(const std::vector<std::string>& fBinVar,
0036                 const std::vector<std::string>& fParVar,
0037                 const std::string& fFormula,
0038                 bool fIsResponse);
0039     Definitions(const std::string& fLine);
0040     //-------- Member functions ----------
0041     unsigned nBinVar() const { return mBinVar.size(); }
0042     unsigned nParVar() const { return mParVar.size(); }
0043     std::vector<std::string> parVar() const { return mParVar; }
0044     std::vector<std::string> binVar() const { return mBinVar; }
0045     std::string parVar(unsigned fIndex) const { return mParVar[fIndex]; }
0046     std::string binVar(unsigned fIndex) const { return mBinVar[fIndex]; }
0047     std::string formula() const { return mFormula; }
0048     std::string level() const { return mLevel; }
0049     bool isResponse() const { return mIsResponse; }
0050 
0051   private:
0052     //-------- Member variables ----------
0053     bool mIsResponse;
0054     std::string mLevel;
0055     std::string mFormula;
0056     std::vector<std::string> mParVar;
0057     std::vector<std::string> mBinVar;
0058 
0059     COND_SERIALIZABLE;
0060   };
0061   //---------------- Record class --------------------------------
0062   //-- Each Record holds the properties of a bin -----------------
0063   class Record {
0064   public:
0065     //-------- Constructors --------------
0066     Record() : mNvar(0), mMin(0), mMax(0) {}
0067     Record(unsigned fNvar,
0068            const std::vector<float>& fXMin,
0069            const std::vector<float>& fXMax,
0070            const std::vector<float>& fParameters)
0071         : mNvar(fNvar), mMin(fXMin), mMax(fXMax), mParameters(fParameters) {}
0072     Record(const std::string& fLine, unsigned fNvar);
0073     //-------- Member functions ----------
0074     unsigned nVar() const { return mNvar; }
0075     float xMin(unsigned fVar) const { return mMin[fVar]; }
0076     float xMax(unsigned fVar) const { return mMax[fVar]; }
0077     float xMiddle(unsigned fVar) const { return 0.5 * (xMin(fVar) + xMax(fVar)); }
0078     float parameter(unsigned fIndex) const { return mParameters[fIndex]; }
0079     std::vector<float> parameters() const { return mParameters; }
0080     unsigned nParameters() const { return mParameters.size(); }
0081     bool operator<(const Record& other) const {
0082       if (xMin(0) < other.xMin(0))
0083         return true;
0084       if (xMin(0) > other.xMin(0))
0085         return false;
0086       auto const sz = mMin.size();
0087       auto const otherSz = other.mMin.size();
0088       if (sz > 1 and otherSz > 1) {
0089         if (xMin(1) < other.xMin(1))
0090           return true;
0091         if (xMin(1) > other.xMin(1))
0092           return false;
0093         if (sz > 2 and otherSz > 2) {
0094           return (xMin(2) < other.xMin(2));
0095         }
0096       }
0097       //xMins were equal up until one of them
0098       // ran out of values
0099       return sz < otherSz;
0100     }
0101 
0102   private:
0103     //-------- Member variables ----------
0104     unsigned mNvar;
0105     std::vector<float> mMin;
0106     std::vector<float> mMax;
0107     std::vector<float> mParameters;
0108 
0109     COND_SERIALIZABLE;
0110   };
0111 
0112   //-------- Constructors --------------
0113   JetCorrectorParameters() { valid_ = false; }
0114   JetCorrectorParameters(const std::string& fFile, const std::string& fSection = "");
0115   JetCorrectorParameters(const JetCorrectorParameters::Definitions& fDefinitions,
0116                          const std::vector<JetCorrectorParameters::Record>& fRecords)
0117       : mDefinitions(fDefinitions), mRecords(fRecords) {
0118     valid_ = true;
0119   }
0120   //-------- Member functions ----------
0121   const Record& record(unsigned fBin) const { return mRecords[fBin]; }
0122   const Definitions& definitions() const { return mDefinitions; }
0123   unsigned size() const { return mRecords.size(); }
0124   unsigned size(unsigned fVar) const;
0125   int binIndex(const std::vector<float>& fX) const;
0126   int binIndexN(const std::vector<float>& fX) const;
0127   int neighbourBin(unsigned fIndex, unsigned fVar, bool fNext) const;
0128   std::vector<float> binCenters(unsigned fVar) const;
0129   void printScreen() const;
0130   void printFile(const std::string& fFileName) const;
0131   bool isValid() const { return valid_; }
0132   void init();
0133 
0134   static const int MAX_SIZE_DIMENSIONALITY = 3 COND_TRANSIENT;
0135 
0136 private:
0137   //-------- Member variables ----------
0138   JetCorrectorParameters::Definitions mDefinitions;
0139   std::vector<JetCorrectorParameters::Record> mRecords;
0140   bool valid_;  /// is this a valid set?
0141 
0142   std::shared_ptr<JetCorrectorParametersHelper> helper COND_TRANSIENT;
0143 
0144   COND_SERIALIZABLE;
0145 };
0146 std::ostream& operator<<(std::ostream& out, const JetCorrectorParameters::Record& fBin);
0147 
0148 class JetCorrectorParametersCollection {
0149   //---------------- JetCorrectorParametersCollection class ----------------
0150   //-- Adds several JetCorrectorParameters together by algorithm type ---
0151   //--     to reduce the number of payloads in the Database ---
0152   //-- NB: The enum is listed in "human-logical" order, but the actual
0153   //       enum value is in order of appearance when people thought of them.
0154 public:
0155   enum Level_t {
0156     L1Offset = 0,
0157     L1JPTOffset = 7,
0158     L1FastJet = 10,
0159     L2Relative = 1,
0160     L3Absolute = 2,
0161     L2L3Residual = 8,
0162     L4EMF = 3,
0163     L5Flavor = 4,
0164     L6UE = 5,
0165     L7Parton = 6,
0166     Uncertainty = 9,
0167     UncertaintyAbsolute = 11,
0168     UncertaintyHighPtExtra = 12,
0169     UncertaintySinglePionECAL = 13,
0170     UncertaintySinglePionHCAL = 27,
0171     UncertaintyFlavor = 14,
0172     UncertaintyTime = 15,
0173     UncertaintyRelativeJEREC1 = 16,
0174     UncertaintyRelativeJEREC2 = 17,
0175     UncertaintyRelativeJERHF = 18,
0176     UncertaintyRelativePtEC1 = 28,
0177     UncertaintyRelativePtEC2 = 29,
0178     UncertaintyRelativePtHF = 30,
0179     UncertaintyRelativeStatEC2 = 19,
0180     UncertaintyRelativeStatHF = 20,
0181     UncertaintyRelativeFSR = 21,
0182     UncertaintyRelativeSample = 31,
0183     UncertaintyPileUpDataMC = 22,
0184     UncertaintyPileUpOOT = 23,
0185     UncertaintyPileUpPtBB = 24,
0186     UncertaintyPileUpPtEC = 32,
0187     UncertaintyPileUpPtHF = 33,
0188     UncertaintyPileUpBias = 25,
0189     UncertaintyPileUpJetRate = 26,
0190     L1RC = 34,
0191     L1Residual = 35,
0192     UncertaintyAux3 = 36,
0193     UncertaintyAux4 = 37,
0194     N_LEVELS = 38
0195   };
0196 
0197   enum L5_Species_t { L5_bJ = 0, L5_cJ, L5_qJ, L5_gJ, L5_bT, L5_cT, L5_qT, L5_gT, N_L5_SPECIES };
0198   enum L7_Species_t { L7_gJ = 0, L7_qJ, L7_cJ, L7_bJ, L7_jJ, L7_qT, L7_cT, L7_bT, L7_jT, N_L7_SPECIES };
0199   typedef int key_type;
0200   typedef std::string label_type;
0201   typedef JetCorrectorParameters value_type;
0202   typedef std::pair<key_type, value_type> pair_type;
0203   typedef std::vector<pair_type> collection_type;
0204 
0205   // Constructor... initialize all three vectors to zero
0206   JetCorrectorParametersCollection() {
0207     corrections_.clear();
0208     correctionsL5_.clear();
0209     correctionsL7_.clear();
0210   }
0211 
0212   // Add a JetCorrectorParameter object, possibly with flavor.
0213   void push_back(key_type i, value_type const& j, label_type const& flav = "");
0214 
0215   // Access the JetCorrectorParameter via the key k.
0216   // key_type is hashed to deal with the three collections
0217   JetCorrectorParameters const& operator[](key_type k) const;
0218 
0219   // Access the JetCorrectorParameter via a string.
0220   // Will find the hashed value for the label, and call via that
0221   // operator.
0222   JetCorrectorParameters const& operator[](std::string const& label) const { return operator[](findKey(label)); }
0223 
0224   // Get a list of valid keys. These will contain hashed keys
0225   // that are aware of all three collections.
0226   void validKeys(std::vector<key_type>& keys) const;
0227 
0228   // Helper method to find all of the sections in a given
0229   // parameters file
0230   static void getSections(std::string inputFile, std::vector<std::string>& outputs);
0231 
0232   // Find the L5 bin for hashing
0233   static key_type getL5Bin(std::string const& flav);
0234   // Find the L7 bin for hashing
0235   static key_type getL7Bin(std::string const& flav);
0236   // Check if this is an L5 hashed value
0237   static bool isL5(key_type k);
0238   // Check if this is an L7 hashed value
0239   static bool isL7(key_type k);
0240 
0241   static std::string findLabel(key_type k);
0242   static std::string findL5Flavor(key_type k);
0243   static std::string findL7Parton(key_type k);
0244 
0245 protected:
0246   // Find the key corresponding to each label
0247   key_type findKey(std::string const& label) const;
0248 
0249   collection_type corrections_;
0250   collection_type correctionsL5_;
0251   collection_type correctionsL7_;
0252 
0253   collection_type& getCorrections() { return corrections_; }
0254   collection_type& getCorrectionsL5() { return correctionsL5_; }
0255   collection_type& getCorrectionsL7() { return correctionsL7_; }
0256 
0257   friend struct JetCorrectorParametersInitializeTransients;
0258 
0259   COND_SERIALIZABLE;
0260 };
0261 
0262 struct JetCorrectorParametersInitializeTransients {
0263   void operator()(JetCorrectorParametersCollection& jcpc) {
0264     for (auto& ptype : jcpc.getCorrections()) {
0265       ptype.second.init();
0266     }
0267     for (auto& ptype : jcpc.getCorrectionsL5()) {
0268       ptype.second.init();
0269     }
0270     for (auto& ptype : jcpc.getCorrectionsL7()) {
0271       ptype.second.init();
0272     }
0273   }
0274 };
0275 
0276 #endif