-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhisto_manager.py
263 lines (241 loc) · 11.3 KB
/
histo_manager.py
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
import numpy as np
import math
import ROOT
from ROOT import TH1D, TH2D, TH3D, TMath, TH1F
def rebin_histogram_2d(h2org, nx,xmin,xmax,ny,ymin,ymax):
#this supports only for constant bin width
h2 = TH2D("{0}_rebin".format(h2org.GetName()),"{0}_rebin".format(h2org.GetName()), int(nx),xmin,xmax,int(ny),ymin,ymax);
h2.SetXTitle(h2org.GetXaxis().GetTitle());
h2.SetYTitle(h2org.GetYaxis().GetTitle());
bw_x_org = h2org.GetXaxis().GetBinWidth(1);
bw_y_org = h2org.GetYaxis().GetBinWidth(1);
bw_x = h2.GetXaxis().GetBinWidth(1);
bw_y = h2.GetYaxis().GetBinWidth(1);
if bw_x < bw_x_org :
print("check bin width on x axis");
return None;
if bw_y < bw_y_org :
print("check bin width on y axis");
return None;
if nx > h2org.GetNbinsX():
print("check nbins x");
return None;
if ny > h2org.GetNbinsY():
print("check nbins y");
return None;
if xmin < h2org.GetXaxis().GetXmin():
print("check min. value of x");
return None;
if xmax > h2org.GetXaxis().GetXmax():
print("check max. value of x");
return None;
if ymin < h2org.GetYaxis().GetXmin():
print("check min. value of y");
return None;
if ymax > h2org.GetYaxis().GetXmax():
print("check max. value of y");
return None;
#ng_x = int(bw_x / bw_x_org);
#ng_y = int(bw_y / bw_y_org);
#h2tmp = h2org.Clone("h2tmp");
#h2tmp.RebinX(ng_x);
#h2tmp.RebinY(ng_y);
#now, bin width of h2org is adjusted for h2 on both x and y. !this is wrong 11.Feb.2022
for ix in range(0,nx):
for iy in range(0,ny):
#h2.SetBinContent(ix+1, iy+1, h2tmp.GetBinContent(ix+1, iy+1));
#h2.SetBinError(ix+1, iy+1, h2tmp.GetBinError(ix+1, iy+1));
xlow = h2.GetXaxis().GetBinLowEdge(ix+1);
xhigh = h2.GetXaxis().GetBinLowEdge(ix+2);
ylow = h2.GetYaxis().GetBinLowEdge(iy+1);
yhigh = h2.GetYaxis().GetBinLowEdge(iy+2);
#print(xlow, xhigh, ylow, yhigh);
bin_xlow_org = h2org.GetXaxis().FindBin(xlow + 1e-6);
bin_xhigh_org = h2org.GetXaxis().FindBin(xhigh - 1e-6);
bin_ylow_org = h2org.GetYaxis().FindBin(ylow + 1e-6);
bin_yhigh_org = h2org.GetYaxis().FindBin(yhigh - 1e-6);
n = 0;
err = 0;
#print(bin_xlow_org, bin_xhigh_org, bin_ylow_org, bin_yhigh_org);
#print("=============");
for ix2 in range(bin_xlow_org, bin_xhigh_org+1):
for iy2 in range(bin_ylow_org, bin_yhigh_org+1):
#print(ix2, iy2);
n += h2org.GetBinContent(ix2,iy2);
err += h2org.GetBinError(ix2,iy2) * h2org.GetBinError(ix2,iy2) ;
#print(n,err);
h2.SetBinContent(ix+1, iy+1, n);
h2.SetBinError(ix+1, iy+1, TMath.Sqrt(err));
return h2;
#______________________________________________________________________
def rebin_histogram(h1, arrX, isdiff, is_syst=False):
h1tmp = h1.Clone("h1tmp");
h1rebin = h1tmp.Rebin(len(arrX)-1,"h1rebin",arrX);
h1rebin.SetName("{0}_rebin".format(h1.GetName()));
h1rebin.Sumw2();
if is_syst:
for i in range(0,h1rebin.GetNbinsX()):
x_min = h1rebin.GetBinLowEdge(i+1);
x_max = h1rebin.GetBinLowEdge(i+2);
bin0 = h1.FindBin(x_min + 1e-6);
bin1 = h1.FindBin(x_max - 1e-6);
y = 0;
err = 0;
for j in range(bin0, bin1+1):
y += h1.GetBinContent(j);
err += h1.GetBinError(j) / h1.GetBinContent(j) * h1.GetBinContent(j);
h1rebin.SetBinContent(i+1, y);
h1rebin.SetBinError(i+1, err);
#print("check 0 rel syst. = ", h1rebin.GetBinError(i+1) / h1rebin.GetBinContent(i+1));
if isdiff and (h1rebin.Class() == TH1D.Class() or h1rebin.Class() == TH1F.Class() ) : #do you want differential histogram? e.g. dN/dpT, dN/dm.
h1rebin.Scale(1.,"width");
return h1rebin;
#______________________________________________________________________
def slice_histogram(h2,x0,x1,axis,isdiff):
h1 = 0;
delta = 1e-6;
if "x" in axis.lower():
bin0 = h2.GetYaxis().FindBin(x0 + delta);
bin1 = h2.GetYaxis().FindBin(x1 - delta);
h1 = h2.ProjectionX("h1prjx_{0}".format(h2.GetName()),bin0,bin1,"");
elif "y" in axis.lower():
bin0 = h2.GetXaxis().FindBin(x0 + delta);
bin1 = h2.GetXaxis().FindBin(x1 - delta);
h1 = h2.ProjectionY("h1prjy_{0}".format(h2.GetName()),bin0,bin1,"");
if isdiff and h1.Class() == TH1D.Class(): #do you want differential histogram? e.g. dN/dpT, dN/dm.
h1.Scale(1.,"width");
return h1;
#______________________________________________________________________
def slice_profile(h2,x0,x1,axis,isdiff=False):
h1 = 0;
delta = 1e-6;
if "x" in axis.lower():
bin0 = h2.GetYaxis().FindBin(x0 + delta);
bin1 = h2.GetYaxis().FindBin(x1 - delta);
h1 = h2.ProfileX("h1prfx_{0}".format(h2.GetName()),bin0,bin1,"");
elif "y" in axis.lower():
bin0 = h2.GetXaxis().FindBin(x0 + delta);
bin1 = h2.GetXaxis().FindBin(x1 - delta);
h1 = h2.ProfileY("h1prfy_{0}".format(h2.GetName()),bin0,bin1,"");
if isdiff and h1.Class() == TProfile.Class(): #do you want differential profile?
h1.Scale(1.,"width");
return h1;
#______________________________________________________________________
def get_R_factor(h1m_ULSnp_mix, h1m_ULSpn_mix, h1m_LSpp_mix, h1m_LSnn_mix):
h1R = h1m_ULSnp_mix.Clone("h1R");
h1R.Reset();
#h1R.Sumw2();
h1m_ULS_mix = h1m_ULSnp_mix.Clone("h1m_ULS_mix");#sum of np+pn
if h1m_ULSpn_mix is not None:
h1m_ULS_mix.Add(h1m_ULSpn_mix,1.);
Nm = h1R.GetNbinsX();
#R = 1;
#R_err = 0;
for im in range(0,Nm):
uls = h1m_ULS_mix .GetBinContent(im+1);
uls_err = h1m_ULS_mix .GetBinError(im+1);
lspp = h1m_LSpp_mix.GetBinContent(im+1);
lspp_err = h1m_LSpp_mix.GetBinError(im+1);
lsnn = h1m_LSnn_mix.GetBinContent(im+1);
lsnn_err = h1m_LSnn_mix.GetBinError(im+1);
R = 1.;
R_err = 0.;
if uls > 1e-6:
if lspp * lsnn > 1e-6 :
R = uls / ( 2 * TMath.Sqrt( lspp*lsnn ) );
R_err = TMath.Sqrt( ( pow(lspp*lsnn_err*uls,2) + pow(lspp_err*lsnn*uls,2) + 4*pow(lspp*lsnn*uls_err,2) ) / ( 16 * pow(lspp*lsnn,3) ) );
elif lspp + lsnn > 1e-6 :
R = uls / ( 2 * 0.5 * (lspp + lsnn) );
R_err = TMath.Sqrt( (pow(uls*lspp_err,2) + pow(uls*lsnn_err,2) + pow(lspp+lsnn,2)*pow(uls_err,2) ) / pow(lspp+lsnn,4) );
else:
R = 1.;
R_err = 0.;
#print("mix im+1 = {0} , x = {1} , R = {2} , uls = {3} , lspp = {4} , lsnn = {5}".format(im+1,h1R.GetBinCenter(im+1),R,uls,lspp,lsnn));
h1R.SetBinContent(im+1, R);
h1R.SetBinError(im+1, R_err);
return h1R;
#______________________________________________________________________
def get_corrected_bkg(h1R, h1m_LSpp_same, h1m_LSnn_same):
h1bkg = h1m_LSpp_same.Clone("h1bkg");
h1bkg.Reset();
#h1bkg.Sumw2();
Nm = h1bkg.GetNbinsX();
for im in range(0,Nm):
lspp = h1m_LSpp_same.GetBinContent(im+1);
lspp_err = h1m_LSpp_same.GetBinError(im+1);
lsnn = h1m_LSnn_same.GetBinContent(im+1);
lsnn_err = h1m_LSnn_same.GetBinError(im+1);
R = h1R.GetBinContent(im+1);
R_err = h1R.GetBinError(im+1);
#print("im+1 = {0} , x = {1} , R = {2} , lspp = {3} , lsnn = {4}".format(im+1,h1R.GetBinCenter(im+1),R,lspp,lsnn));
bkg = 0;
bkg_err = 0;
if lspp * lsnn > 1e-6 : #geometric mean
bkg = 2.0 * R * TMath.Sqrt(lspp * lsnn);
bkg_err = TMath.Sqrt( pow(R_err/R,2) + 1./4*pow(lspp_err/lspp,2) + 1./4*pow(lsnn_err/lsnn,2) ) * bkg;
#print("im+1 = {0} , x = {1} , R = {2} , lspp = {3} , lsnn = {4}, bkg_err = {5}".format(im+1,h1R.GetBinCenter(im+1),R,lspp,lsnn,bkg_err));
#bkg_err = TMath.Sqrt( R*R * ( pow(lspp*lsnn_err,2) + pow(lsnn*lspp_err,2) ) / (lspp*lsnn) );
#print("old im+1 = {0} , x = {1} , R = {2} , lspp = {3} , lsnn = {4}, bkg_err = {5}".format(im+1,h1R.GetBinCenter(im+1),R,lspp,lsnn,bkg_err));
elif lspp + lsnn > 1e-6 : #arithmetic mean
bkg = 2.0 * R * 0.5 * (lspp + lsnn);
bkg_err = TMath.Sqrt( pow(R_err/R,2) + (pow(lspp_err,2)+pow(lsnn_err,2))/pow(lspp+lsnn,2) ) * bkg;
#print("im+1 = {0} , x = {1} , R = {2} , lspp = {3} , lsnn = {4}, bkg_err = {5}".format(im+1,h1R.GetBinCenter(im+1),R,lspp,lsnn,bkg_err));
#bkg_err = TMath.Sqrt( R*R * (lspp_err*lspp_err + lsnn_err*lsnn_err) );
#print("old im+1 = {0} , x = {1} , R = {2} , lspp = {3} , lsnn = {4}, bkg_err = {5}".format(im+1,h1R.GetBinCenter(im+1),R,lspp,lsnn,bkg_err));
h1bkg.SetBinContent(im+1,bkg);
h1bkg.SetBinError(im+1,bkg_err);
return h1bkg;
#______________________________________________________________________
def get_corrected_bkg_simple(R, R_err, h1m_LSpp_same, h1m_LSnn_same):
h1bkg = h1m_LSpp_same.Clone("h1bkg");
h1bkg.Reset();
#h1bkg.Sumw2();
Nm = h1bkg.GetNbinsX();
for im in range(0,Nm):
lspp = h1m_LSpp_same.GetBinContent(im+1);
lspp_err = h1m_LSpp_same.GetBinError(im+1);
lsnn = h1m_LSnn_same.GetBinContent(im+1);
lsnn_err = h1m_LSnn_same.GetBinError(im+1);
bkg = 0;
bkg_err = 0;
if lspp * lsnn > 1e-6 :
bkg = 2.0 * R * TMath.Sqrt(lspp * lsnn);
bkg_err = TMath.Sqrt( R*R * ( pow(lspp*lsnn_err,2) + pow(lsnn*lspp_err,2) ) / (lspp*lsnn) );
elif lspp + lsnn > 1e-6 : #arithmetic mean
bkg = 2.0 * R * 0.5 * (lspp + lsnn);
bkg_err = TMath.Sqrt( R*R * (lspp_err*lspp_err + lsnn_err*lsnn_err) );
h1bkg.SetBinContent(im+1,bkg);
h1bkg.SetBinError(im+1,bkg_err);
return h1bkg;
#______________________________________________________________________
def get_bkg_subtracted(h1m_ULS_same, h1bkg):
h1sig = h1m_ULS_same.Clone("h1sig");
#h1sig.Sumw2();
h1sig.Add(h1bkg,-1);
return h1sig;
#______________________________________________________________________
def get_SBratio(h1sig,h1bkg):
h1r = h1sig.Clone("h1r");
h1r.Reset();
h1r.Divide(h1sig,h1bkg,1.,1.,"B");
return h1r;
#______________________________________________________________________
#______________________________________________________________________
def get_significance(h1sig,h1bkg):
h1r = h1sig.Clone("h1r");
h1r.Reset();
n = h1r.GetNbinsX();
for i in range(0,n):
s = h1sig.GetBinContent(i+1);
b = h1bkg.GetBinContent(i+1);
s_err = h1sig.GetBinError(i+1);
b_err = h1bkg.GetBinError(i+1);
sig = s/sqrt(s + 2.*b);
sig_err = sqrt( pow(pow(s+2*b,-1/2.) - s/2.*pow(s+2.*b, -3/2) ,2) *pow(s_err,2) + pow( 2*s * pow(s+2*b,-3/2.) ,2) * pow(b_err,2) );
h1r.SetBinContent(i+1,sig);
h1r.SetBinError(i+1,sig_err);
return h1r;
#______________________________________________________________________
#______________________________________________________________________
#______________________________________________________________________
#______________________________________________________________________