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