Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 //
0002 #ifndef METCorrectorParameters_h
0003 #define METCorrectorParameters_h
0004 
0005 #include "CondFormats/Serialization/interface/Serializable.h"
0006 
0007 #include <string>
0008 #include <vector>
0009 #include <algorithm>
0010 #include <iostream>
0011 #include "FWCore/Utilities/interface/Exception.h"
0012 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0013 
0014 class METCorrectorParameters {
0015   //---------------- METCorrectorParameters class ----------------
0016   //-- Encapsulates all the information of the parametrization ---
0017 public:
0018   //---------------- Definitions class ---------------------------
0019   //-- Global iformation about the parametrization is kept here --
0020   class Definitions {
0021   public:
0022     //-------- Constructors --------------
0023     Definitions() {}
0024     Definitions(const std::vector<std::string>& fVar,
0025                 const std::vector<std::string>& fParVar,
0026                 const std::string& fFormula);
0027     Definitions(const std::string& fLine);
0028     //-------- Member functions ----------
0029     unsigned nBinVar() const { return mBinVar.size(); }
0030     unsigned nParVar() const { return mParVar.size(); }
0031     std::vector<std::string> parVar() const { return mParVar; }
0032     std::vector<std::string> binVar() const { return mBinVar; }
0033     std::string parVar(unsigned fIndex) const { return mParVar[fIndex]; }
0034     std::string binVar(unsigned fIndex) const { return mBinVar[fIndex]; }
0035     std::string formula() const { return mFormula; }
0036 
0037   private:
0038     //-------- Member variables ----------
0039     int ptclType;
0040     std::string mFormula;
0041     std::vector<std::string> mParVar;
0042     std::vector<std::string> mBinVar;
0043 
0044     COND_SERIALIZABLE;
0045   };
0046   //---------------- Record class --------------------------------
0047   //-- Each Record holds the properties of a bin -----------------
0048   class Record {
0049   public:
0050     //-------- Constructors --------------
0051     Record() : mNvar(0), mMin(0), mMax(0), mParameters(0) {}
0052     Record(unsigned fNvar,
0053            const std::vector<float>& fXMin,
0054            const std::vector<float>& fXMax,
0055            const std::vector<float>& fParameters)
0056         : mNvar(fNvar), mMin(fXMin), mMax(fXMax), mParameters(fParameters) {}
0057     Record(const std::string& fLine, unsigned fNvar);
0058     //-------- Member functions ----------
0059     float xMin(unsigned fVar) const { return mMin[fVar]; }
0060     float xMax(unsigned fVar) const { return mMax[fVar]; }
0061     float xMiddle(unsigned fVar) const { return 0.5 * (xMin(fVar) + xMax(fVar)); }
0062     float parameter(unsigned fIndex) const { return mParameters[fIndex]; }
0063     std::vector<float> parameters() const { return mParameters; }
0064     unsigned nParameters() const { return mParameters.size(); }
0065     int operator<(const Record& other) const { return xMin(0) < other.xMin(0); }
0066 
0067   private:
0068     //-------- Member variables ----------
0069     unsigned mNvar;
0070     std::vector<float> mMin;
0071     std::vector<float> mMax;
0072     std::vector<float> mParameters;
0073 
0074     COND_SERIALIZABLE;
0075   };
0076 
0077   //-------- Constructors --------------
0078   METCorrectorParameters() { valid_ = false; }
0079   METCorrectorParameters(const std::string& fFile, const std::string& fSection = "");
0080   METCorrectorParameters(const METCorrectorParameters::Definitions& fDefinitions,
0081                          const std::vector<METCorrectorParameters::Record>& fRecords)
0082       : mDefinitions(fDefinitions), mRecords(fRecords) {
0083     valid_ = true;
0084   }
0085   //-------- Member functions ----------
0086   const Record& record(unsigned fBin) const { return mRecords[fBin]; }
0087   const Definitions& definitions() const { return mDefinitions; }
0088   unsigned size() const { return mRecords.size(); }
0089   unsigned size(unsigned fVar) const;
0090   int binIndex(const std::vector<float>& fX) const;
0091   int neighbourBin(unsigned fIndex, unsigned fVar, bool fNext) const;
0092   std::vector<float> binCenters(unsigned fVar) const;
0093   void printScreen() const;
0094   void printFile(const std::string& fFileName) const;
0095   bool isValid() const { return valid_; }
0096 
0097 private:
0098   //-------- Member variables ----------
0099   METCorrectorParameters::Definitions mDefinitions;
0100   std::vector<METCorrectorParameters::Record> mRecords;
0101   bool valid_;  /// is this a valid set?
0102 
0103   COND_SERIALIZABLE;
0104 };
0105 
0106 class METCorrectorParametersCollection {
0107 public:
0108   enum Level_t { MiniAod = 0, N_LEVELS = 1 };
0109 
0110   typedef int key_type;
0111   typedef std::string label_type;
0112   typedef METCorrectorParameters value_type;
0113   typedef std::pair<key_type, value_type> pair_type;
0114   typedef std::vector<pair_type> collection_type;
0115 
0116   // Constructor... initialize all three vectors to zero
0117   METCorrectorParametersCollection() { correctionsMiniAod_.clear(); }
0118 
0119   // Add a METCorrectorParameter object, for each source
0120   void push_back(key_type i, value_type const& j, label_type const& source = "");
0121 
0122   // Access the METCorrectorParameter via the key k.
0123   // key_type is hashed to deal with the three collections
0124   METCorrectorParameters const& operator[](key_type k) const;
0125 
0126   // Access the METCorrectorParameter via a string.
0127   // Will find the hashed value for the label, and call via that
0128   // operator.
0129   METCorrectorParameters const& operator[](std::string const& label) const { return operator[](findKey(label)); }
0130 
0131   // Get a list of valid keys. These will contain hashed keys
0132   // that are aware of all three collections.
0133   void validKeys(std::vector<key_type>& keys) const;
0134 
0135   // Helper method to find all of the sections in a given
0136   // parameters file
0137   static void getSections(std::string inputFile, std::vector<std::string>& outputs);
0138   // Find the MiniAod bin for hashing
0139   static key_type getMiniAodBin(std::string const& source);
0140 
0141   static bool isMiniAod(key_type k);
0142 
0143   static std::string findLabel(key_type k);
0144   static std::string findMiniAodSource(key_type k);
0145 
0146 protected:
0147   // Find the key corresponding to each label
0148   key_type findKey(std::string const& label) const;
0149 
0150   collection_type correctionsMiniAod_;
0151 
0152   COND_SERIALIZABLE;
0153 };
0154 
0155 #endif