Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:10:16

0001 #ifndef EgammaAnalysis_ElectronTools_EnergyScaleCorrection_class_h
0002 #define EgammaAnalysis_ElectronTools_EnergyScaleCorrection_class_h
0003 /// Read and get energy scale and smearings from .dat files
0004 /**\class EnergyScaleCorrection_class EnergyScaleCorrection_class.cc Calibration/ZFitter/src/EnergyScaleCorrection_class.cc
0005  *  \author Shervin Nourbakhsh
0006  *
0007  */
0008 
0009 /** Description
0010     This module is taken from the ECALELF package, used to derive the energy scales and smearings.
0011 
0012     There are two sub-classes: 
0013      - correctionValue_class that defines the corrections
0014      - correctionCategory_class that defines the categories
0015      There is one map that associates the correction to the category (values read from text file)
0016 
0017      There is one class that reads the text files with the corrections and returns the scale/smearings given the electron/photon properties
0018  */
0019 
0020 #include <TString.h>
0021 #include <iostream>
0022 #include <fstream>
0023 #include <map>
0024 #include <cmath>
0025 #include <TChain.h>
0026 #include <TRandom3.h>
0027 #include <string>
0028 
0029 //============================== First auxiliary class
0030 class correctionValue_class {
0031 public:
0032   // values
0033   float scale, scale_err, scale_err_syst;
0034   float rho, rho_err;
0035   float phi, phi_err;
0036   float Emean, Emean_err;
0037 
0038   correctionValue_class(void) {
0039     scale = 1;
0040     scale_err = 0;
0041     scale_err_syst = 0;
0042     rho = 0;
0043     rho_err = 0;
0044     phi = 0;
0045     phi_err = 0;
0046     Emean = 0;
0047     Emean_err = 0;
0048   };
0049 
0050   friend std::ostream& operator<<(std::ostream& os, const correctionValue_class a) {
0051     os << "( " << a.scale << " +/- " << a.scale_err << " +/- " << a.scale_err_syst << ")"
0052        << "\t" << a.rho << " +/- " << a.rho_err << "\t" << a.phi << " +/- " << a.phi_err << "\t" << a.Emean << " +/- "
0053        << a.Emean_err;
0054     return os;
0055   };
0056 };
0057 
0058 //============================== Second auxiliary class
0059 class correctionCategory_class {
0060   // for class definition and ordering
0061 public:
0062   unsigned int runmin;
0063   unsigned int runmax;
0064 
0065 private:
0066   // definition of the variables used for binning and the min-max ranges
0067   float r9min;   ///< min R9 vaule for the bin
0068   float r9max;   ///< max R9 value for the bin
0069   float etmin;   ///< min Et value for the bin
0070   float etmax;   ///< max Et value for the bin
0071   float etamin;  ///< min eta value for the bin
0072   float etamax;  ///< max eta value for the bin
0073 
0074 public:
0075   /** there are two constructors:
0076       - the first using the values taken from the e/gamma object
0077       - the second used to define the categories and the correction values
0078   */
0079 
0080   /** This constructor uses a string to define the category
0081       The string is used in the .dat file where the corrections are defined
0082       The syntax of the strings follows the definitions in the ECALELF ElectronCategory_class: http://ecalelfs.github.io/ECALELF/d5/d11/classElectronCategory__class.html
0083   */
0084   correctionCategory_class(
0085       TString category_);  ///< constructor with name of the category according to ElectronCategory_class
0086 
0087   /// this constructor is used to assign a category to the electron/photon given values in input
0088   inline correctionCategory_class(const unsigned int runNumber,
0089                                   const float etaEle,
0090                                   const float R9Ele,
0091                                   const float EtEle) {
0092     runmin = runNumber;
0093     runmax = runNumber;
0094     etamin = fabs(etaEle);
0095     etamax = fabs(etaEle);
0096     r9min = R9Ele;
0097     r9max = R9Ele;
0098     etmin = EtEle;
0099     etmax = EtEle;
0100   }
0101 
0102   /// for ordering of the categories
0103   bool operator<(const correctionCategory_class& b) const;
0104 
0105   /// for DEBUG
0106   friend std::ostream& operator<<(std::ostream& os, const correctionCategory_class a) {
0107     os << a.runmin << " " << a.runmax << "\t" << a.etamin << " " << a.etamax << "\t" << a.r9min << " " << a.r9max
0108        << "\t" << a.etmin << " " << a.etmax;
0109     return os;
0110   };
0111 };
0112 
0113 //==============================
0114 /// map associating the category and the correction
0115 typedef std::map<correctionCategory_class, correctionValue_class> correction_map_t;
0116 
0117 //============================== Main class
0118 class EnergyScaleCorrection_class {
0119 public:
0120   enum fileFormat_t { UNKNOWN = 0, GLOBE, ECALELF_TOY, ECALELF };
0121 
0122   enum paramSmear_t { kNone = 0, kRho, kPhi, kNParamSmear };
0123 
0124   bool doScale, doSmearings;
0125 
0126 public:
0127   EnergyScaleCorrection_class(std::string correctionFileName, unsigned int genSeed = 0);
0128   EnergyScaleCorrection_class(){};  ///< dummy constructor needed in ElectronEnergyCalibratorRun2
0129   ~EnergyScaleCorrection_class(void);
0130 
0131   //------------------------------ scales
0132   float ScaleCorrection(unsigned int runNumber,
0133                         bool isEBEle,
0134                         double R9Ele,
0135                         double etaSCEle,
0136                         double EtEle) const;  ///< method to get energy scale corrections
0137 
0138   float ScaleCorrectionUncertainty(unsigned int runNumber, bool isEBEle, double R9Ele, double etaSCEle, double EtEle)
0139       const;  ///< method to get scale correction uncertainties: it's stat+syst in eta x R9 categories
0140 
0141 private:
0142   correctionValue_class getScaleCorrection(unsigned int runNumber,
0143                                            bool isEBEle,
0144                                            double R9Ele,
0145                                            double etaSCEle,
0146                                            double EtEle) const;  ///< returns the correction value class
0147   float getScaleOffset(unsigned int runNumber,
0148                        bool isEBEle,
0149                        double R9Ele,
0150                        double etaSCEle,
0151                        double EtEle) const;  // returns the correction value
0152   float getScaleStatUncertainty(unsigned int runNumber,
0153                                 bool isEBEle,
0154                                 double R9Ele,
0155                                 double etaSCEle,
0156                                 double EtEle) const;  // returns the stat uncertainty
0157   float getScaleSystUncertainty(unsigned int runNumber,
0158                                 bool isEBEle,
0159                                 double R9Ele,
0160                                 double etaSCEle,
0161                                 double EtEle) const;  // technical implementation
0162 
0163   void ReadFromFile(
0164       TString
0165           filename);  ///<   category  "runNumber"   runMin  runMax   deltaP  err_deltaP_per_bin err_deltaP_stat err_deltaP_syst
0166 
0167   // this method adds the correction values read from the txt file to the map
0168   void AddScale(TString category_, int runMin_, int runMax_, double deltaP_, double err_deltaP_, double err_syst_deltaP);
0169 
0170   //============================== smearings
0171 public:
0172   float getSmearingSigma(
0173       int runNumber, bool isEBEle, float R9Ele, float etaSCEle, float EtEle, paramSmear_t par, float nSigma = 0.) const;
0174   float getSmearingSigma(
0175       int runNumber, bool isEBEle, float R9Ele, float etaSCEle, float EtEle, float nSigma_rho, float nSigma_phi) const;
0176 
0177 private:
0178   fileFormat_t smearingType_;
0179 
0180   correction_map_t scales, scales_not_defined;
0181   correction_map_t smearings, smearings_not_defined;
0182 
0183   void AddSmearing(TString category_,
0184                    int runMin_,
0185                    int runMax_,  //double smearing_, double err_smearing_);
0186                    double rho,
0187                    double err_rho,
0188                    double phi,
0189                    double err_phi,
0190                    double Emean,
0191                    double err_Emean);
0192   void ReadSmearingFromFile(TString filename);  ///< File structure: category constTerm alpha;
0193 public:
0194   inline void SetSmearingType(fileFormat_t value) {
0195     if (value <= 1) {
0196       smearingType_ = value;
0197     } else {
0198       smearingType_ = UNKNOWN;
0199     }
0200   };
0201 
0202   float getSmearingRho(
0203       int runNumber, bool isEBEle, float R9Ele, float etaSCEle, float EtEle) const;  ///< public for sigmaE estimate
0204 };
0205 
0206 #endif