-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.hpp
More file actions
395 lines (310 loc) · 9.36 KB
/
helpers.hpp
File metadata and controls
395 lines (310 loc) · 9.36 KB
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#ifndef HELPERS_H
#define HELPERS_H
#include <iostream>
#include <list>
#include <assert.h>
#include <TH1F.h>
#include <TH2F.h>
#include <TF1.h>
#include <THStack.h>
#include <TCanvas.h>
#include <TLegend.h>
#include <TMath.h>
#include <TFile.h>
#include <TLatex.h>
#include <TROOT.h>
#include <TSpline.h>
#include <TUnfold.h>
#include <TUnfoldSys.h>
#include <TGraphAsymmErrors.h>
#include <TGraphErrors.h>
#include <sstream>
#include <vector>
#include <utility>
#include <algorithm>
#include <TPave.h>
#include <TList.h>
#include <TPaveText.h>
#include <TVirtualPad.h>
#include <TClass.h>
#include "TApplication.h"
#include "TRandom3.h"
#include "TLorentzVector.h"
using namespace std;
// scales histogram to wanted value, works for 2d as well (!)
TH1* scale_to(TH1* h, double val)
{
h->Scale(val/h->Integral());
return h;
}
void fill_nooverflow_1d(TH1* h, double val, double weight)
{
if(val > h->GetXaxis()->GetXmax())
val = h->GetXaxis()->GetXmax()-0.00001;
if(val < h->GetXaxis()->GetXmin())
val = h->GetXaxis()->GetXmin()+0.00001;
h->Fill(val, weight);
}
// WARNING not optimized for performance; if used a lot you might want to do it yourself
// and cache the max/min axis values!
void fill_nooverflow_2d(TH2* h, double valx, double valy, double weight)
{
const double xmax = h->GetXaxis()->GetXmax();
const double xmin = h->GetXaxis()->GetXmin();
const double ymax = h->GetYaxis()->GetXmax();
const double ymin = h->GetYaxis()->GetXmin();
if(valx > xmax)
valx =xmax-0.00001;
if(valx < xmin)
valx = xmin+0.00001;
if(valy > ymax)
valy = ymax-0.00001;
if(valy < ymin)
valy = ymin+0.00001;
h->Fill(valx, valy, weight);
}
// finds bin by content and ensures a bin within actual range is returned
int findfixbin_nooverflow(TH1* h, double val)
{
int nbins = h->GetNbinsX();
int bin = h->FindBin(val);
if(bin < 1) bin = 1;
if(bin > nbins) bin = nbins;
return bin;
}
double asymmetry_error_naive(double pos, double neg)
{
return 2/pow(pos+neg,2)*sqrt(neg*pos*pos+pos*neg*neg);
}
double asymmetry_error_custom_errors(double pos, double poserr, double neg, double negerr)
{
return 2/pow(pos+neg,2)*sqrt(negerr*negerr*pos*pos + poserr*poserr*neg*neg);
}
double asymmetryerror_afterunfolding_1d(TH2* Corr, TH1* unf)
{
int int_maxbin = unf->GetNbinsX();
if(int_maxbin%2 != 0)
cout << "better choose different binning\n";
double Nplus = unf->Integral(int_maxbin/2+1,int_maxbin);
double Nminus = unf->Integral(1,int_maxbin/2);
double E_Asy_neg = -2*Nplus/pow (Nplus + Nminus, 2);
double E_Asy_pos = 2*Nminus/pow(Nplus + Nminus, 2);
TH1D der_vector("","",int_maxbin, 0, int_maxbin);
TH2D matrix_corr("","",int_maxbin, 0, int_maxbin, int_maxbin, 0, int_maxbin);
TH1D temp_vector("","", int_maxbin, 0, int_maxbin);
for(int i=1; i<=int_maxbin; i++)
{
if(i<=int_maxbin/2)
der_vector.SetBinContent(i-1, E_Asy_neg);
if(i>int_maxbin/2)
der_vector.SetBinContent(i-1, E_Asy_pos);
temp_vector.SetBinContent(i-1, 0);
for(int j=1; j<=int_maxbin; j++)
{
matrix_corr.SetBinContent(i-1, j-1, Corr->GetBinContent(i,j));
}
}
for(int i=0; i<int_maxbin; i++)
{
for(int j=0; j<int_maxbin; j++)
{
temp_vector.SetBinContent(i, temp_vector.GetBinContent(i) + matrix_corr.GetBinContent(i,j)*der_vector.GetBinContent(j));
}
}
double Error_Unf=0;
for(int i=0; i<int_maxbin; i++)
{
Error_Unf += der_vector.GetBinContent(i)*temp_vector.GetBinContent(i);
}
Error_Unf=sqrt(Error_Unf);
return Error_Unf;
}
double asymmetryerror_afterunfolding_2d(TH2* Corr, int int_maxbin, double Nplus, double Nminus, int nbinssensvar )
{
if(int_maxbin%2 != 0)
cerr << "Bin number not divisible by 2.\n";
double E_Asy_neg = -2*Nplus/pow (Nplus + Nminus, 2);
double E_Asy_pos = 2*Nminus/pow(Nplus + Nminus, 2);
TH1D der_vector("","",int_maxbin, 0, int_maxbin);
TH2D matrix_corr("","",int_maxbin, 0, int_maxbin, int_maxbin, 0, int_maxbin);
TH1D temp_vector("","", int_maxbin, 0, int_maxbin);
for(int i=1; i<=int_maxbin; i++)
{
// the unwrapped 1d histo has several cycles of the whole sensvar distribution, each with a different mass
// value (or pt_ttbar, or..). so we need to get the index within the current cycle to determine if we're
// looking at a bin with positive or negative sensvar.
const int xindex = int((i-1)/nbinssensvar);
const int mcleanedIndex = i - xindex * nbinssensvar;
if(mcleanedIndex<=nbinssensvar/2)
der_vector.SetBinContent(i-1, E_Asy_neg);
else
der_vector.SetBinContent(i-1, E_Asy_pos);
temp_vector.SetBinContent(i-1, 0);
for(int j=1; j<=int_maxbin; j++)
{
matrix_corr.SetBinContent(i-1, j-1, Corr->GetBinContent(i,j));
}
}
for(int i=0; i<int_maxbin; i++)
{
for(int j=0; j<int_maxbin; j++)
{
temp_vector.SetBinContent(i, temp_vector.GetBinContent(i) + matrix_corr.GetBinContent(i,j)*der_vector.GetBinContent(j));
}
}
double Error_Unf=0;
for(int i=0; i<int_maxbin; i++)
{
Error_Unf += der_vector.GetBinContent(i)*temp_vector.GetBinContent(i);
}
Error_Unf=sqrt(Error_Unf);
return Error_Unf;
}
// gives asymmetry error in one bin of the x axis. Nplus and Nminus already must contain the numbers for that sole bin.
double asymmetryerror_afterunfolding_2d_onexbin(TH2* Corr, int int_maxbin, double Nplus, double Nminus, int nbinssensvar, int xbin)
{
if(int_maxbin%2 != 0)
cerr << "Bin number not divisible by 2.\n";
double E_Asy_neg = -2*Nplus/pow (Nplus + Nminus, 2);
double E_Asy_pos = 2*Nminus/pow(Nplus + Nminus, 2);
TH1D der_vector("","",int_maxbin, 0, int_maxbin);
TH2D matrix_corr("","",int_maxbin, 0, int_maxbin, int_maxbin, 0, int_maxbin);
TH1D temp_vector("","", int_maxbin, 0, int_maxbin);
for(int i=1; i<=int_maxbin; i++)
{
// the unwrapped 1d histo has several cycles of the whole sensvar distribution, each with a different mass
// value (or pt_ttbar, or..). so we need to get the index within the current cycle to determine if we're
// looking at a bin with positive or negative sensvar.
const int xindex = int((i-1)/nbinssensvar);
if(xindex != xbin) // if it's in another x bin it doesn't go into asymmetry calculation, so derivative is zero
{
der_vector.SetBinContent(i-1, 0);
}
else
{
const int mcleanedIndex = i - xindex * nbinssensvar;
if(mcleanedIndex<=nbinssensvar/2)
der_vector.SetBinContent(i-1, E_Asy_neg);
else
der_vector.SetBinContent(i-1, E_Asy_pos);
}
temp_vector.SetBinContent(i-1, 0);
for(int j=1; j<=int_maxbin; j++)
{
matrix_corr.SetBinContent(i-1, j-1, Corr->GetBinContent(i,j));
}
}
for(int i=0; i<int_maxbin; i++)
{
for(int j=0; j<int_maxbin; j++)
{
temp_vector.SetBinContent(i, temp_vector.GetBinContent(i) + matrix_corr.GetBinContent(i,j)*der_vector.GetBinContent(j));
}
}
double Error_Unf=0;
for(int i=0; i<int_maxbin; i++)
{
Error_Unf += der_vector.GetBinContent(i)*temp_vector.GetBinContent(i);
}
Error_Unf=sqrt(Error_Unf);
return Error_Unf;
}
/*
// simulates the error on a sample with different number of events N
double asymmetry_error_customN(double pos, double neg, double N)
{
double ges = pos + neg;
pos = pos * N / ges;
neg = neg * N / ges;
return 2/pow(pos+neg,2)*sqrt(neg*pos*pos+pos*neg*neg);
}*/
// concatenates columns to make 1d histo out of 2d histo
void unwrap2dhisto(TH2* h2, TH1* h1)
{
int n=0;
for(int x=1; x<=h2->GetXaxis()->GetNbins(); x++)
{
for(int y=1; y<=h2->GetYaxis()->GetNbins(); y++)
{
n++;
h1->SetBinContent(n, h2->GetBinContent(x,y));
h1->SetBinError(n, h2->GetBinError(x,y));
}
}
h1->Sumw2();
}
string getEnv(const char* name)
{
const char* value;
value = getenv(name);
if(value)
{
return value;
}
else
{
cerr << "ENVVAR " << name << " MISSING, EXITING" << endl;
exit(1);
return string();
}
}
double Rapidity(const TLorentzVector& l){
double ee = l.E();
double ppz = l.Pz();
return .5f* log( (ee+ppz)/(ee-ppz) );
}
// normalize migmatrix column-wise
TH2* normalizeMigMat(TH2* h)
{
TH2* hclone = (TH2*) h->Clone();
const int xbins = hclone->GetNbinsX();
const int ybins = hclone->GetNbinsY();
for(int x=0; x<xbins; x++)
{
double integ = hclone->Integral(x+1, x+1, 1, ybins);
for(int y=0; y<ybins; y++)
{
hclone->SetBinContent(x+1,y+1, hclone->GetBinContent(x+1, y+1)/integ);
}
}
return hclone;
}
void saveAs(TCanvas* c, string path)
{
c->SaveAs((path+".pdf").c_str());
c->SaveAs((path+".png").c_str());
c->SaveAs((path+".eps").c_str());
}
void saveAs(TCanvas* c, TString path)
{
c->SaveAs(path+".pdf");
c->SaveAs(path+".png");
c->SaveAs(path+".eps");
}
void saveAs(TCanvas* c, const char* path)
{
TString p(path);
c->SaveAs(p+".pdf");
c->SaveAs(p+".png");
c->SaveAs(p+".eps");
}
void saveAs(TVirtualPad* c, string path)
{
c->SaveAs((path+".pdf").c_str());
c->SaveAs((path+".png").c_str());
c->SaveAs((path+".eps").c_str());
}
void saveAs(TVirtualPad* c, TString path)
{
c->SaveAs(path+".pdf");
c->SaveAs(path+".png");
c->SaveAs(path+".eps");
}
void saveAs(TVirtualPad* c, const char* path)
{
TString p(path);
c->SaveAs(p+".pdf");
c->SaveAs(p+".png");
c->SaveAs(p+".eps");
}
#endif // HELPERS_H