CMS 3D CMS Logo

EnergyScaleCorrection.h
Go to the documentation of this file.
1 #ifndef RecoEgamma_EgammaTools_EnergyScaleCorrection_h
2 #define RecoEgamma_EgammaTools_EnergyScaleCorrection_h
3 
4 //author: Alan Smithee
5 //description:
6 // A port of Shervin Nourbakhsh's EnergyScaleCorrection_class in EgammaAnalysis/ElectronTools
7 // this reads the scale & smearing corrections in from a text file for given categories
8 // it then allows these values to be accessed
9 
10 #include <iostream>
11 #include <fstream>
12 #include <vector>
13 #include <map>
14 #include <cmath>
15 #include <string>
16 #include <bitset>
17 
19 public:
21 
23 
29  kErrNone = 0,
30  kErrStat = 1,
31  kErrSyst = 2,
32  kErrGain = 4,
37  };
38 
40  public:
42  ScaleCorrection(float iScale, float iScaleErrStat, float iScaleErrSyst, float iScaleErrGain)
43  : scale_(iScale), scaleErrStat_(iScaleErrStat), scaleErrSyst_(iScaleErrSyst), scaleErrGain_(iScaleErrGain) {}
44 
45  float scale() const { return scale_; }
46  float scaleErr(const std::bitset<kErrNrBits>& uncBitMask) const;
47  float scaleErrStat() const { return scaleErrStat_; }
48  float scaleErrSyst() const { return scaleErrSyst_; }
49  float scaleErrGain() const { return scaleErrGain_; }
50 
51  friend std::ostream& operator<<(std::ostream& os, const ScaleCorrection& a) { return a.print(os); }
52  std::ostream& print(std::ostream& os) const;
53 
54  private:
56  };
57 
58  struct SmearCorrection {
59  public:
60  SmearCorrection() : rho_(0.), rhoErr_(0.), phi_(0.), phiErr_(0.), eMean_(0.), eMeanErr_(0.) {}
61  SmearCorrection(float iRho, float iRhoErr, float iPhi, float iPhiErr, float iEMean, float iEMeanErr)
62  : rho_(iRho), rhoErr_(iRhoErr), phi_(iPhi), phiErr_(iPhiErr), eMean_(iEMean), eMeanErr_(iEMeanErr) {}
63 
64  friend std::ostream& operator<<(std::ostream& os, const SmearCorrection& a) { return a.print(os); }
65  std::ostream& print(std::ostream& os) const;
66 
67  float sigma(const float et, const float nrSigmaRho = 0., const float nrSigmaPhi = 0.) const {
68  const float rhoVal = rho_ + rhoErr_ * nrSigmaRho;
69  const float phiVal = phi_ + phiErr_ * nrSigmaPhi;
70  const float constTerm = rhoVal * std::sin(phiVal);
71  const float alpha = rhoVal * eMean_ * std::cos(phiVal);
72  return std::sqrt(constTerm * constTerm + alpha * alpha / et);
73  }
74 
75  private:
76  float rho_, rhoErr_;
77  float phi_, phiErr_;
78  float eMean_, eMeanErr_;
79  };
80 
82  public:
83  CorrectionCategory(const std::string& category, int runnrMin = 0, int runnrMax = 999999);
85  const unsigned int runnr, const float et, const float eta, const float r9, const unsigned int gainSeed)
86  : runMin_(runnr),
87  runMax_(runnr),
88  etaMin_(std::abs(eta)),
89  etaMax_(std::abs(eta)),
90  r9Min_(r9),
91  r9Max_(r9),
92  etMin_(et),
93  etMax_(et),
94  gain_(gainSeed) {}
95 
96  CorrectionCategory(unsigned int runMin,
97  unsigned int runMax,
98  float etaMin,
99  float etaMax,
100  float r9Min,
101  float r9Max,
102  float etMin,
103  float etMax,
104  unsigned int gainSeed);
105 
106  bool operator<(const CorrectionCategory& b) const;
107  bool inCategory(
108  const unsigned int runnr, const float et, const float eta, const float r9, const unsigned int gainSeed) const;
109 
110  friend std::ostream& operator<<(std::ostream& os, const CorrectionCategory& a) { return a.print(os); }
111  std::ostream& print(std::ostream& os) const;
112 
113  private:
114  //all boundaries are inclusive (X<=Y<=Z)
115  unsigned int runMin_;
116  unsigned int runMax_;
117  float etaMin_;
118  float etaMax_;
119  float r9Min_;
120  float r9Max_;
121  float etMin_;
122  float etMax_;
123  unsigned int gain_;
124  };
125 
126 public:
127  EnergyScaleCorrection(const std::string& correctionFileName, unsigned int genSeed = 0);
130 
131  float scaleCorr(unsigned int runnr,
132  double et,
133  double eta,
134  double r9,
135  unsigned int gainSeed = 12,
136  std::bitset<kErrNrBits> uncBitMask = kErrNone) const;
137 
138  float scaleCorrUncert(unsigned int runnr,
139  double et,
140  double eta,
141  double r9,
142  unsigned int gainSeed,
143  std::bitset<kErrNrBits> uncBitMask = kErrNone) const;
144 
145  float smearingSigma(
146  int runnr, double et, double eta, double r9, unsigned int gainSeed, ParamSmear par, float nSigma = 0.) const;
147  float smearingSigma(
148  int runnr, double et, double eta, double r9, unsigned int gainSeed, float nSigmaRho, float nSigmaPhi) const;
149 
151 
152  const ScaleCorrection* getScaleCorr(unsigned int runnr, double et, double eta, double r9, unsigned int gainSeed) const;
153  const SmearCorrection* getSmearCorr(unsigned int runnr, double et, double eta, double r9, unsigned int gainSeed) const;
154 
155 private:
156  void addScale(const std::string& category,
157  int runMin,
158  int runMax,
159  double deltaP,
160  double errDeltaP,
161  double errSystDeltaP,
162  double errDeltaPGain);
163 
164  void addScale(int runMin,
165  int runMax,
166  double etaMin,
167  double etaMax,
168  double r9Min,
169  double r9Max,
170  double etMin,
171  double etMax,
172  unsigned int gain,
173  double energyScale,
174  double energyScaleErrStat,
175  double energyScaleErrSyst,
176  double energyScaleErrGain);
177 
178  void addSmearing(const std::string& category,
179  int runMin,
180  int runMax,
181  double rho,
182  double errRho,
183  double phi,
184  double errPhi,
185  double eMean,
186  double errEMean);
187 
189  void readSmearingsFromFile(const std::string& filename);
190 
191  //static data members
192  static constexpr float kDefaultScaleVal_ = 1.0;
193  static constexpr float kDefaultSmearVal_ = 0.0;
194 
195  //data members
197  std::map<CorrectionCategory, ScaleCorrection> scales_;
198  std::map<CorrectionCategory, SmearCorrection> smearings_;
199 
200  template <typename T1, typename T2>
201  class Sorter {
202  public:
203  bool operator()(const std::pair<T1, T2>& lhs, const T1& rhs) const { return lhs.first < rhs; }
204  bool operator()(const std::pair<T1, T2>& lhs, const std::pair<T1, T2>& rhs) const { return lhs.first < rhs.first; }
205  bool operator()(const T1& lhs, const std::pair<T1, T2>& rhs) const { return lhs < rhs.first; }
206  bool operator()(const T1& lhs, const T1& rhs) const { return lhs < rhs; }
207  };
208 };
209 
210 #endif
float alpha
Definition: AMPTWrapper.h:95
bool operator()(const std::pair< T1, T2 > &lhs, const T1 &rhs) const
void readSmearingsFromFile(const std::string &filename)
static constexpr float kDefaultSmearVal_
ScaleCorrection(float iScale, float iScaleErrStat, float iScaleErrSyst, float iScaleErrGain)
void addScale(const std::string &category, int runMin, int runMax, double deltaP, double errDeltaP, double errSystDeltaP, double errDeltaPGain)
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
std::ostream & print(std::ostream &os) const
void readScalesFromFile(const std::string &filename)
bool operator()(const T1 &lhs, const std::pair< T1, T2 > &rhs) const
bool operator()(const std::pair< T1, T2 > &lhs, const std::pair< T1, T2 > &rhs) const
const ScaleCorrection * getScaleCorr(unsigned int runnr, double et, double eta, double r9, unsigned int gainSeed) const
T sqrt(T t)
Definition: SSEVec.h:18
void setSmearingType(FileFormat value)
float smearingSigma(int runnr, double et, double eta, double r9, unsigned int gainSeed, ParamSmear par, float nSigma=0.) const
static constexpr float kDefaultScaleVal_
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
const SmearCorrection * getSmearCorr(unsigned int runnr, double et, double eta, double r9, unsigned int gainSeed) const
unsigned int gain_
12, 6, 1, 61 (double gain switch)
std::map< CorrectionCategory, SmearCorrection > smearings_
Definition: value.py:1
friend std::ostream & operator<<(std::ostream &os, const CorrectionCategory &a)
CorrectionCategory(const unsigned int runnr, const float et, const float eta, const float r9, const unsigned int gainSeed)
float scaleErr(const std::bitset< kErrNrBits > &uncBitMask) const
float sigma(const float et, const float nrSigmaRho=0., const float nrSigmaPhi=0.) const
friend std::ostream & operator<<(std::ostream &os, const SmearCorrection &a)
friend std::ostream & operator<<(std::ostream &os, const ScaleCorrection &a)
void addSmearing(const std::string &category, int runMin, int runMax, double rho, double errRho, double phi, double errPhi, double eMean, double errEMean)
double b
Definition: hdecay.h:120
et
define resolution functions of each parameter
bool operator<(DTCELinkId const &lhs, DTCELinkId const &rhs)
Definition: DTCELinkId.h:73
bool operator()(const T1 &lhs, const T1 &rhs) const
std::ostream & print(std::ostream &os) const
double a
Definition: hdecay.h:121
std::map< CorrectionCategory, ScaleCorrection > scales_
static const char gain_[]
SmearCorrection(float iRho, float iRhoErr, float iPhi, float iPhiErr, float iEMean, float iEMeanErr)
std::ostream & print(std::ostream &os) const
#define constexpr
float scaleCorrUncert(unsigned int runnr, double et, double eta, double r9, unsigned int gainSeed, std::bitset< kErrNrBits > uncBitMask=kErrNone) const
float scaleCorr(unsigned int runnr, double et, double eta, double r9, unsigned int gainSeed=12, std::bitset< kErrNrBits > uncBitMask=kErrNone) const