forked from h2gglobe/h2gglobe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EnergySmearer.cc
328 lines (278 loc) · 12.8 KB
/
EnergySmearer.cc
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#include "EnergySmearer.h"
#include "PhotonReducedInfo.h"
#include <assert.h>
EnergySmearer::EnergySmearer(const energySmearingParameters& par, const std::vector<PhotonCategory> & presel) :
myParameters_(par), scaleOrSmear_(true), doCorrections_(false), doRegressionSmear_(false),
preselCategories_(presel)
{
syst_only_ = false;
rgen_ = new TRandom3(12345);
baseSeed_ = 0;
name_="EnergySmearer_"+ par.categoryType + "_" + par.parameterSetName;
//Checking consistency of input parameters
std::cerr << myParameters_.categoryType << " " << myParameters_.n_categories << std::endl;
assert( myParameters_.byRun || myParameters_.n_categories == myParameters_.smearing_sigma.size() );
assert( myParameters_.byRun || myParameters_.n_categories == myParameters_.smearing_sigma_error.size() );
assert( ( myParameters_.categoryType == "EBEE" && myParameters_.n_categories == 2 ) ||
( myParameters_.categoryType == "2CatR9_EBEE" && myParameters_.n_categories == 4 ) ||
( myParameters_.categoryType == "2CatR9_EBEE_ByRun" && myParameters_.n_categories == 4 ) ||
( myParameters_.categoryType == "2CatR9_EBEBm4EE" && myParameters_.n_categories == 6 ) ||
( myParameters_.categoryType == "2CatR9_EBEBm4EE_ByRun" && myParameters_.n_categories == 6 ) ||
( myParameters_.categoryType == "Automagic" &&
( myParameters_.byRun || myParameters_.n_categories == myParameters_.photon_categories.size() ) )
);
if( myParameters_.categoryType == "Automagic" ) {
myParameters_.n_categories = myParameters_.photon_categories.size();
}
if( myParameters_.byRun ) {
for(energySmearingParameters::eScaleVectorIt it=myParameters_.scale_offset_byrun.begin(); it!=myParameters_.scale_offset_byrun.end();
++it ) {
assert( myParameters_.n_categories == it->scale_offset.size() );
assert( myParameters_.n_categories == it->scale_offset_error.size() );
}
} else {
assert( myParameters_.n_categories == myParameters_.scale_offset.size() );
assert( myParameters_.n_categories == myParameters_.scale_offset_error.size() );
}
registerMe();
}
EnergySmearer::EnergySmearer(EnergySmearer * orig, const std::vector<PhotonCategory> & presel)
{
*this = *orig;
preselCategories_ = presel;
registerMe();
}
EnergySmearer::~EnergySmearer()
{
delete rgen_;
}
std::string EnergySmearer::photonCategory(PhotonReducedInfo & aPho) const
{
std::string myCategory="";
if (myParameters_.categoryType=="Automagic")
{
EnergySmearer::energySmearingParameters::phoCatVectorConstIt vit =
find(myParameters_.photon_categories.begin(),
myParameters_.photon_categories.end(),
std::make_pair( aPho.isSphericalPhoton(),
std::make_pair( fabs((float)aPho.caloPosition().PseudoRapidity()),(float)aPho.r9() ) )
);
if( vit == myParameters_.photon_categories.end() ) {
std::cerr << "Could not find energy scale correction for this photon " << aPho.isSphericalPhoton() << " " << (float)aPho.caloPosition().PseudoRapidity() << " " << (float)aPho.r9() << std::endl;
assert( 0 );
}
myCategory = vit->name;
/// /std::cout << myCategory << " " << aPho.isSphericalPhoton() << " " << (float)aPho.caloPosition().PseudoRapidity() << " " << (float)aPho.r9() << std::endl;
}
else if (myParameters_.categoryType=="2CatR9_EBEE")
{
if (aPho.iDet()==1)
myCategory+="EB";
else
myCategory+="EE";
if (aPho.r9()>=0.94)
myCategory+="HighR9";
else
myCategory+="LowR9";
}
else if (myParameters_.categoryType=="2CatR9_EBEBm4EE")
{
if (aPho.iDet()==1 && fabs(aPho.caloPosition().PseudoRapidity()) < 1.)
myCategory+="EB";
else if (aPho.iDet()==1 && fabs(aPho.caloPosition().PseudoRapidity()) > 1.)
myCategory+="EBm4";
else
myCategory+="EE";
if (aPho.r9()>=0.94)
myCategory+="HighR9";
else
myCategory+="LowR9";
}
else if (myParameters_.categoryType=="EBEE")
{
if (aPho.iDet()==1)
myCategory+="EB";
else
myCategory+="EE";
}
else
{
std::cout << "Unknown categorization. No category name is returned" << std::endl;
}
return myCategory;
}
float EnergySmearer::getScaleOffset(int run, const std::string & category) const
{
const std::map<std::string, float> * scale_offset = &(myParameters_.scale_offset);
if( myParameters_.byRun ) {
scale_offset = &(find(myParameters_.scale_offset_byrun.begin(),myParameters_.scale_offset_byrun.end(),run)->scale_offset) ;
}
energySmearingParameters::parameterMapConstIt it=scale_offset->find(category);
if ( it == scale_offset->end())
{
std::cout << "Category was not found in the configuration. Giving Up" << std::endl;
return false;
}
return 1. + it->second;
}
bool EnergySmearer::smearPhoton(PhotonReducedInfo & aPho, float & weight, int run, float syst_shift) const
{
if( syst_only_ && syst_shift == 0. ) { return true; }
if( ! doEfficiencies_ && ! doCorrections_ && ! doRegressionSmear_ && syst_shift == 0. ) {
int myId = smearerId();
if( aPho.hasCachedVal(myId) ) {
const std::pair<const BaseSmearer *, float> & cachedVal = aPho.cachedVal(myId);
assert( cachedVal.first == this );
aPho.setEnergy(aPho.energy() * cachedVal.second );
return true;
}
}
if( ! preselCategories_.empty() ) {
if( find(preselCategories_.begin(), preselCategories_.end(),
std::make_pair( aPho.isSphericalPhoton(),
std::make_pair( fabs((float)aPho.caloPosition().PseudoRapidity()),(float)aPho.r9() ) )
) == preselCategories_.end() ) {
return true;
}
}
std::string category=photonCategory(aPho);
if (category == "")
{
std::cout << "No category has been found associated with this photon. Giving Up" << std::endl;
return false;
}
/////////////////////// smearing or re-scaling photon energy ///////////////////////////////////////////
float newEnergy=aPho.energy();
/////////////////////// apply MC-based photon energy corrections ///////////////////////////////////////////
if ( doCorrections_ ) {
// corrEnergy is the corrected photon energy
newEnergy = aPho.corrEnergy() + syst_shift * myParameters_.corrRelErr * (aPho.corrEnergy() - aPho.energy());
} else if ( doRegressionSmear_){
// leave energy alone, bus change resolution (10% uncertainty on sigmaE/E scaling)
float newSigma;
if (fabs(aPho.caloPosition().Eta())<1.5){
newSigma = aPho.corrEnergyErr()*(1.+syst_shift*0.1);
} else {
newSigma = aPho.corrEnergyErr()*(1.+syst_shift*0.1);
}
aPho.setCorrEnergyErr(newSigma);
} else {
if( scaleOrSmear_ ) {
float scale_offset = getScaleOffset(run, category);
scale_offset += syst_shift * myParameters_.scale_offset_error.find(category)->second;
newEnergy *= scale_offset;
if( syst_shift == 0. ) {
aPho.cacheVal( smearerId(), this, scale_offset );
}
} else {
float smearing_sigma = myParameters_.smearing_sigma.find(category)->second;
float err_sigma= myParameters_.smearing_sigma_error.find(category)->second;
smearing_sigma += syst_shift * err_sigma;
// Careful here, if sigma < 0 now, it will be squared and so not correct, set to 0 in this case.
if (smearing_sigma < 0.) smearing_sigma=0.;
float smear = 1.;
if( smearing_sigma > 0. ) {
// deterministic smearing
int nsigmas = round(syst_shift);
if( nsigmas < 0 ) nsigmas = 1-nsigmas;
if( nsigmas < aPho.nSmearingSeeds() ) {
rgen_->SetSeed( baseSeed_+aPho.smearingSeed(nsigmas) );
}
smear = rgen_->Gaus(1.,smearing_sigma) ;
}
if( syst_shift == 0. ) {
aPho.cacheVal( smearerId(), this, smear );
}
newEnergy *= smear;
}
}
if( newEnergy == 0. ) {
std::cerr << "New energy is 0.: aborting " << this->name() << std::endl;
assert( newEnergy != 0. );
}
aPho.setEnergy(newEnergy);
/////////////////////// changing weigh of photon according to efficiencies ///////////////////////////////////////////
////////////////////// if you're doing corrections, don't touch the weights ////////////////////////////////////////
if(doEfficiencies_ && (!doCorrections_) ) {
if( !smearing_eff_graph_.empty() ){
weight = getWeight( ( aPho.energy() / cosh(aPho.caloPosition().PseudoRapidity()) ) ,category, syst_shift);
}
}
return true;
}
bool EnergySmearer::initEfficiency()
{
// if map is not empty, yuo're initilized and happy..
if( !smearing_eff_graph_.empty() ){
std:cout << "initialization of efficiencies already done; proceed with usage. " << std::endl;
return true;
}
//otherwise, get smearing functions from file and set up map
std::cout << "\n>>>initializing one efficiency for photon re-weighting; " << std::endl;
// do basic sanity checks first
if(doEnergy_){
std::cout << "*** Initializing reweighting for efficiencies; energy smearing active TOO, do you want them both? " << std::endl; return false; }
if(!doEfficiencies_){
std::cout << "*** Initializing reweighting for efficiencies - BUT doEfficiencies_ is set to false; doing nothing. " << std::endl; return false; }
if( effName_.empty()){
std::cout << "you're initialzinfg reweighting for efficiency but effName_ is empty ; doing nothing. " << std::endl; return false; }
if( myParameters_.efficiency_file.empty()){
std::cout << "you're initialzinfg reweighting for efficiency: " << effName_ << " but input file with TGraphErrors is not specified; doing nothing. " << std::endl; return false; }
theEfficiencyFile_ = new TFile(myParameters_.efficiency_file.c_str());
// initialize formulas for the four categories;
std::string effTmpName; std::string photonCat; TGraphAsymmErrors *graphTmp, *graphClone;
photonCat = std::string("EBHighR9");
effTmpName = effName_+std::string("_")+photonCat; graphTmp = (TGraphAsymmErrors*) theEfficiencyFile_->Get(effTmpName.c_str()); // smearing_eff_graph_[photonCat]=graphTmp;
graphClone=(TGraphAsymmErrors*)graphTmp->Clone(); smearing_eff_graph_[photonCat]=graphClone;
photonCat = std::string("EBLowR9");
effTmpName = effName_+std::string("_")+photonCat; graphTmp = (TGraphAsymmErrors*) theEfficiencyFile_->Get(effTmpName.c_str()); // smearing_eff_graph_[photonCat]=graphTmp;
graphClone=(TGraphAsymmErrors*)graphTmp->Clone(); smearing_eff_graph_[photonCat]=graphClone;
photonCat = std::string("EEHighR9");
effTmpName = effName_+std::string("_")+photonCat; graphTmp = (TGraphAsymmErrors*) theEfficiencyFile_->Get(effTmpName.c_str()); // smearing_eff_graph_[photonCat]=graphTmp;
graphClone=(TGraphAsymmErrors*)graphTmp->Clone(); smearing_eff_graph_[photonCat]=graphClone;
photonCat = std::string("EELowR9");
effTmpName = effName_+std::string("_")+photonCat; graphTmp = (TGraphAsymmErrors*) theEfficiencyFile_->Get(effTmpName.c_str()); // smearing_eff_graph_[photonCat]=graphTmp;
graphClone=(TGraphAsymmErrors*)graphTmp->Clone(); smearing_eff_graph_[photonCat]=graphClone;
delete theEfficiencyFile_;
return true;
}
double EnergySmearer::getWeight(double pt, std::string theCategory, float syst_shift) const
{
std::map<std::string,TGraphAsymmErrors*>::const_iterator theIter = smearing_eff_graph_.find(theCategory);
if( theIter != smearing_eff_graph_.end() ) {
// determine the pair of bins between which you interpolate
int numPoints = ( theIter->second )->GetN();
double x, y;
int myBin = -1;
for (int bin=0; bin<numPoints; bin++ ){
( theIter->second )->GetPoint(bin, x, y);
if(pt > x) {
myBin = bin; }
else break;
}
int binLow, binHigh;
if(myBin == -1) {binHigh = 0; binLow=0;}
else if (myBin == (numPoints-1)) {binHigh = numPoints-1; binLow=numPoints-1;}
else {binLow=myBin; binHigh=myBin+1;}
// get hold of efficiency ratio and error at either points
// low-high refer to the points ; up-down refers to the errors
double xLow, yLow; double xHigh, yHigh;
( theIter->second )->GetPoint(binLow, xLow, yLow);
( theIter->second )->GetPoint(binHigh, xHigh, yHigh);
double errLowYup = ( theIter->second )->GetErrorYhigh(binLow);
double errLowYdown = ( theIter->second )->GetErrorYlow(binLow);
double errHighYup = ( theIter->second )->GetErrorYhigh(binHigh);
double errHighYdown = ( theIter->second )->GetErrorYlow(binHigh);
double theErrorLow, theErrorHigh;
if(syst_shift>0) {theErrorLow = errLowYup; theErrorHigh = errHighYup;}
else {theErrorLow = errLowYdown; theErrorHigh = errHighYdown;}
double theWeight, theError;
theWeight = yLow + (yHigh-yLow) / (xHigh-xLow) * (pt-xLow);
theError = theErrorLow + (theErrorHigh-theErrorLow) / (xHigh-xLow) * (pt-xLow);
return ( theWeight + (theError*syst_shift));
}
else {
std::cout << "category asked: " << theCategory << " was not found - which is a problem. Returning weight 1. " << std::endl;
return 1.;
}
}