-
Notifications
You must be signed in to change notification settings - Fork 1
/
calcResErr.cxx
346 lines (245 loc) · 7.24 KB
/
calcResErr.cxx
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
///////////////////////////////////////////////////////////////////////////
// Functions to calculate "residual" uncertainty between two mc histograms
#ifndef CALCRES_C
#define CALCRES_C
#include "calcResErr.h"
using namespace std;
void cumulatebackward(TH1D* hh){
const int N = hh->GetNbinsX();
double cc[N];
int lastbin = hh->GetNbinsX();
for (int ibin=lastbin; ibin<=1; ibin--){
cc[ibin] = hh->Integral(1,lastbin);
}
return;
}
double integrateError(TH1D* hh, int binlow, int binhigh){
double sumerr = 0.;
for (int i=binlow; i<=binhigh; i++){
double binerr = hh->GetBinError(i);
sumerr+=(binerr*binerr);
}
return TMath::Sqrt(sumerr);
}
void cumulateforward(TH1D* hh){
const int N = hh->GetNbinsX()+1;
double cc[N];
double ccerr[N];
int lastbin = hh->GetNbinsX();
for (int ibin=0; ibin<=lastbin; ibin++){
cc[ibin] = hh->Integral(ibin,lastbin);
ccerr[ibin] = integrateError(hh,ibin,lastbin);
}
for (int ibin=0; ibin<=lastbin; ibin++){
hh->SetBinContent(ibin,cc[ibin]);
hh->SetBinError(ibin,ccerr[ibin]);
}
return;
}
int getMostSigBin(TH1D* hh, double baseline){
int nn = hh->GetNbinsX();
int isigbin = -1;
double sigval = -1.;
double sigbest = -1;
for (int ibin=1; ibin<nn; ibin++){
if (hh->GetBinError(ibin)<=0.) continue;
sigval = (hh->GetBinContent(ibin) - baseline)/(hh->GetBinError(ibin));
if (TMath::Abs(sigval)>sigbest){
sigbest = TMath::Abs(sigval);
isigbin = ibin;
}
}
return isigbin;
}
void calcCumDiff(TH1D* hmc, TH1D* hdata){
cumulateforward(hmc);
cumulateforward(hdata);
hmc->Add(hdata,-1.);
for (int ibin=1; ibin<=hmc->GetNbinsX(); ibin++){
double binc = hmc->GetBinContent(ibin);
if (binc<0.){
hmc->SetBinContent(ibin,TMath::Abs(binc));
}
}
return;
}
// Returns the fractional difference between the cumulative
// histograms. If "flgfwd" (default) then cumulate forward,
// otherwise, cumulate backward along x-axis.
void calcCumFracDiff(TH1D* hmc, TH1D* hdata, int flgfwd){
if (flgfwd){
cumulateforward(hmc);
cumulateforward(hdata);
}
else{
cumulatebackward(hmc);
cumulatebackward(hdata);
}
// make tmp histogram
TH1D* hclone = (TH1D*)hmc->Clone("hclonemc");
// difference
hmc->Add(hdata,-1.);
// fractional difference
hmc->Divide(hclone);
// absolute fractional differnce
for (int ibin=1; ibin<=hmc->GetNbinsX(); ibin++){
double binc = hmc->GetBinContent(ibin);
if (binc<0.){
hmc->SetBinContent(ibin,TMath::Abs(binc));
}
}
//
return;
}
// returns the fractional difference at the cut value
double calcResErrCumFracDiff(TH1D* hmc, TH1D* hdata, double cut, int flgfwd){
// make histogram tmp copies
TString clonename = hmc->GetName();
clonename.Append("_clonemc");
TH1D* hclonem = (TH1D*)hmc->Clone(clonename.Data());
clonename = hdata->GetName();
clonename.Append("_clonemc");
TH1D* hcloned = (TH1D*)hdata->Clone(clonename.Data());
// turn mc clone into fractional cumulative difference
calcCumFracDiff(hclonem,hcloned,flgfwd);
// now evaluate at cut value
TGraph* gtmp = new TGraph(hclonem);
// return this fractional diff
return gtmp->Eval(cut);
}
// returns average fractional difference
double calcFracDiff(TH1D* hmc, TH1D* hdata){
// make histogram tmp copies
TH1D* hclonem = (TH1D*)hmc->Clone("hclonem");
TH1D* hcloned = (TH1D*)hdata->Clone("hcloned");
// find difference
hclonem->Add(hcloned,-1.);
// fractional difference
hclonem->Divide(hmc);
double wsum = 0.;
double norm = 0.;
for (int ibin=1; ibin<=hmc->GetNbinsX(); ibin++){
wsum+=(hmc->GetBinContent(ibin))*TMath::Abs(hclonem->GetBinContent(ibin));
norm+= hmc->GetBinContent(ibin);
hmc->SetBinContent(ibin,hmc->GetBinContent(ibin)*TMath::Abs(hclonem->GetBinContent(ibin)));
}
wsum/=norm;
return wsum;
}
// integrate method
double calcCumFracDiffI(TH1D* hmc, TH1D* hdata){
TH1D* hclone0 = (TH1D*)hmc->Clone("hclonemc0");
TH1D* hclonedata0 = (TH1D*)hdata->Clone("hclonedata0");
cumulateforward(hclone0);
cumulateforward(hclonedata0);
TH1D* hclone1 = (TH1D*)hclone0->Clone("hclone1");
hclone0->Add(hclonedata0,-1.);
hclone0->Divide(hclone1);
double binsum = 0.;
double norm = 1./hmc->Integral();
for (int ibin=1; ibin<=hmc->GetNbinsX(); ibin++){
double binc = hclone0->GetBinContent(ibin);
binc *= hmc->GetBinContent(ibin)*norm;
if (binc<0.){
binc = TMath::Abs(binc);
}
binsum += binc;
// hclone0->SetBinContent(ibin,binc);
// hmc->SetBinError(ibin,0.);
}
hclone0->Delete();
hclone1->Delete();
hclonedata0->Delete();
return binsum;
}
double calcResErrorI(TH1D* hmc, TH1D* hdata){
// clone
TH1D* hclone = (TH1D*)hmc->Clone("hclone");
TH1D* hcloned = (TH1D*)hdata->Clone("hcloned");
// get abosolute cumulative difference
calcCumFracDiffI(hclone,hcloned);
// return integral
double avg = hclone->Integral();
hclone->Delete();
hcloned->Delete();
return avg;
}
double calcResErrorFrac(TH1D* hmc, TH1D* hdata){
// clone
TH1D* hclone = (TH1D*)hmc->Clone("hclone");
TH1D* hcloned = (TH1D*)hdata->Clone("hcloned");
// get abosolute cumulative difference
calcCumDiff(hclone,hcloned);
// return max value
double max = hclone->GetMaximum();
hclone->Delete();
hcloned->Delete();
return max;
}
void absVal(TH1D* hh){
int nn = hh->GetNbinsX();
for (int i=1; i<=nn; i++){
double binc = hh->GetBinContent(i);
if (hh->GetBinContent(i)<0.){
hh->SetBinContent(i,TMath::Abs(binc));
}
}
return;
}
double calcResErrorFrac2(TH1D* hmc, TH1D* hdata){
// clone
TH1D* hclone = (TH1D*)hmc->Clone("hclone");
TH1D* hcloned = (TH1D*)hdata->Clone("hcloned");
// hclone->Add(hcloned,-1.);
// hclone->Divide(hmc);
// absVal(hclone);
hmc->Add(hdata,-1.);
hmc->Divide(hclone);
absVal(hmc);
hclone->Delete();
hcloned->Delete();
return 1.0;
}
double calcResErrorFrac3(TH1D* hmc, TH1D* hdata){
// clone
TH1D* hclone = (TH1D*)hmc->Clone("hclone");
TH1D* hcloned = (TH1D*)hdata->Clone("hcloned");
hmc->Add(hdata,-1.);
// hmc->Scale(1./hclone->Integral());
// hmc->Divide(hclone);
//j: absVal(hmc);
hclone->Delete();
hcloned->Delete();
return 1.0;
}
void getSignif(TH1D* hmc, TH1D* hdata){
hmc->Sumw2();
hdata->Sumw2();
TH1D* hclone = (TH1D*)hmc->Clone("hclone");
TH1D* hcloned = (TH1D*)hdata->Clone("hcloned");
hclone->Divide(hdata);
for (int ibin=1; ibin<=hmc->GetNbinsX(); ibin++){
double diff = hclone->GetBinContent(ibin) -1.;
double unc = hclone->GetBinError(ibin);
cout<<diff<<endl;
cout<<unc<<endl;
cout<<"--------------"<<endl;
if (unc>0.) hmc->SetBinContent(ibin,TMath::Abs(diff)/unc);
}
return ;
}
double calcResError(TH1D* hmc, TH1D* hdata){
return calcFracDiff(hmc,hdata);
}
void applyResError(TH1D* hmc, TH1D* hdata){
double fracerr = calcResError(hmc,hdata)/hmc->Integral();
for (int ibin=1; ibin<hmc->GetNbinsX(); ibin++){
double binerr = hmc->GetBinError(ibin);
double binc = hmc->GetBinContent(ibin);
double reserr = binc*fracerr;
double toterr = TMath::Sqrt(binerr*binerr + reserr*reserr);
hmc->SetBinError(ibin,toterr);
}
return;
}
#endif