-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfit.py
More file actions
347 lines (251 loc) · 12.4 KB
/
fit.py
File metadata and controls
347 lines (251 loc) · 12.4 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
# -*- coding: utf-8 -*-
"""
Created on Thu Mar 21 09:45:40 2017
@author: christina reissel
"""
import ROOT as r
import matplotlib.pyplot as plt
import rootpy.plotting.root2matplotlib as rplt
from rootpy.plotting import Hist, HistStack, Legend, Canvas
from rootpy.io import root_open
import rootpy
import numpy as np
import scipy.optimize as optimize
import scipy.stats as stats
from array import *
import matplotlib.pylab as pylab
# Generation of toy data from generator information
def generate_data(input_file, output_file, alpha, beta):
file = root_open(input_file,'READ')
##### Signal
signal = file.Get('ttH_hbb__sl_jge6_tge4__btag_LR_4b_2b_btagCSV_logit')
##### Background
diff_background = ['ttbarPlusB', 'ttbarPlus2B', 'ttbarOther', 'ttbarPlusBBbar', 'ttbarPlusCCbar']
background = { i : file.Get('{0}__sl_jge6_tge4__btag_LR_4b_2b_btagCSV_logit'.format(i)) for i in diff_background}
##### Variation
variation_up = { i : file.Get('{0}__sl_jge6_tge4__btag_LR_4b_2b_btagCSV_logit__CMS_scaleFragmentation_jUp'.format(i)) for i in diff_background}
variation_down = { i : file.Get('{0}__sl_jge6_tge4__btag_LR_4b_2b_btagCSV_logit__CMS_scaleFragmentation_jDown'.format(i)) for i in diff_background}
# Rebinning histograms to avoid empty bins
xbins = array('d',[4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0])
signal = signal.Rebin(4, 'signal_rebinned', xbins)
for i in diff_background:
background[i] = background[i].Rebin(4, 'background_{0}_rebinned'.format(i), xbins)
variation_up[i] = variation_up[i].Rebin(4, 'variationUp_{0}_rebinned'.format(i), xbins)
variation_down[i] = variation_down[i].Rebin(4, 'variationDown_{0}_rebinned'.format(i), xbins)
# Saving data file
output_file = root_open(output_file, 'RECREATE')
signal.Write('signal__sl_jge6_tge4__btag_LR_4b_2b_btagCSV_logit')
print 'signal, nbins=', signal.GetSize()
print 'signal, rate=', signal.Integral()
# Generate data set:
data = signal.Clone('data_obs__sl_jge6_tge4__btag_LR_4b_2b_btagCSV_logit')
signal_coeff = alpha
background_coeff = {i : beta for i in diff_background}
data.Scale(signal_coeff)
for i in diff_background:
data.Add(background[i], background_coeff[i])
data.Write()
print 'data, nbins=', data.GetSize()
for i in diff_background:
if i != 'ttbarPlusB':
background['ttbarPlusB'].Add(background[i])
variation_up['ttbarPlusB'].Add(variation_up[i])
variation_down['ttbarPlusB'].Add(variation_down[i])
variation_down['ttbarPlusB'].Write('background__sl_jge6_tge4__btag_LR_4b_2b_btagCSV_logit__CMS_scaleFragmentation_jDown')
variation_up['ttbarPlusB'].Write('background__sl_jge6_tge4__btag_LR_4b_2b_btagCSV_logit__CMS_scaleFragmentation_jUp')
background['ttbarPlusB'].Write('background__sl_jge6_tge4__btag_LR_4b_2b_btagCSV_logit')
print 'var_up, nbins=', variation_up['ttbarPlusB'].GetSize()
print 'var_down, nbins=', variation_down['ttbarPlusB'].GetSize()
print 'background, nbins=', background['ttbarPlusB'].GetSize()
print 'background, rate=', background['ttbarPlusB'].Integral()
print 'Signal:', signal_coeff
for i in diff_background:
print 'Background '+str(i)+':', background_coeff[i]
output_file.close()
print 'Generation of data finished'
def fit_python(file_mc, file_data):
file_mc = root_open(file_mc,'READ')
file_data = root_open(file_data, 'UPDATE')
# Load histograms from file
##### Data
data = file_data.Get('data_obs__sl_jge6_tge4__btag_LR_4b_2b_btagCSV_logit')
##### Signal
signal = file_mc.Get('ttH_hbb__sl_jge6_tge4__btag_LR_4b_2b_btagCSV_logit')
##### Background
diff_background = ['ttbarPlusB', 'ttbarPlus2B', 'ttbarOther', 'ttbarPlusBBbar', 'ttbarPlusCCbar']
background = { i : file_mc.Get('{0}__sl_jge6_tge4__btag_LR_4b_2b_btagCSV_logit'.format(i)) for i in diff_background}
variation_up = { i : file_mc.Get('{0}__sl_jge6_tge4__btag_LR_4b_2b_btagCSV_logit__CMS_scaleFragmentation_jUp'.format(i)) for i in diff_background}
variation_down = { i : file_mc.Get('{0}__sl_jge6_tge4__btag_LR_4b_2b_btagCSV_logit__CMS_scaleFragmentation_jDown'.format(i)) for i in diff_background}
# Rebinning histograms to avoid empty bins
xbins = array('d',[4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0])
#data = data.Rebin(4, 'data_rebinned', xbins)
signal = signal.Rebin(4, 'signal_rebinned', xbins)
for i in diff_background:
background[i] = background[i].Rebin(4, 'background_{0}_rebinned'.format(i), xbins)
variation_up[i] = variation_up[i].Rebin(4, 'variationUp_{0}_rebinned'.format(i), xbins)
variation_down[i] = variation_down[i].Rebin(4, 'variationDown_{0}_rebinned'.format(i), xbins)
# Initializing arrays from histograms
Nbin = data.GetSize()
arr_data = np.zeros(Nbin)
arr_signal = np.zeros(Nbin)
arr_background = { i : np.zeros(Nbin) for i in diff_background}
arr_variationUp = { i : np.zeros(Nbin) for i in diff_background}
arr_variationDown = { i : np.zeros(Nbin) for i in diff_background}
for i in range(Nbin):
arr_data[i] = data.GetBinContent(i)
arr_signal[i] = signal.GetBinContent(i)
for j in diff_background:
(arr_background[j])[i] = background[j].GetBinContent(i)
(arr_variationUp[j])[i] = variation_up[j].GetBinContent(i)
(arr_variationDown[j])[i] = variation_down[j].GetBinContent(i)
print 'Array Data:', arr_data
print 'Array Signal:', arr_signal
#for j in diff_background:
# print j, arr_background[j]
arr_combined_background = np.zeros(Nbin)
arr_combined_variationUp = np.zeros(Nbin)
arr_combined_variationDown = np.zeros(Nbin)
for j in diff_background:
arr_combined_background += arr_background[j]
arr_combined_variationUp += arr_variationUp[j]
arr_combined_variationDown += arr_variationDown[j]
print 'Array Background:', arr_combined_background
print 'Array Variation up:', arr_combined_variationUp
print 'Array Variation down:', arr_combined_variationDown
# Calculation error
sigma_up = (arr_combined_variationUp - arr_combined_background)/(arr_combined_background)
sigma_down = (arr_combined_background - arr_combined_variationDown)/(arr_combined_background)
sigma = 1.0*(np.abs(sigma_up) + np.abs(sigma_down))/2.0
print 'Sigma:', sigma
"""# Fill variation histograms with symmetric sigma
hist_variationUp = rootpy.plotting.Hist(4,4.0,8.0,name='background__sl_jge6_tge4__btag_LR_4b_2b_btagCSV_logit__CMS_scaleFragmentation_jUp')
hist_variationDown = rootpy.plotting.Hist(4,4.0,8.0,name='background__sl_jge6_tge4__btag_LR_4b_2b_btagCSV_logit__CMS_scaleFragmentation_jDown')
variationUp = arr_combined_background + sigma
variationDown = arr_combined_background - sigma
for i in range(4):
hist_variationUp.SetBinContent(i+1, variationUp[i+1])
hist_variationDown.SetBinContent(i+1, variationDown[i+1])
hist_variationUp.Rebin(4,'hist_variationUp_rebinned', xbins)
hist_variationDown.Rebin(4, 'hist_variationDwon_rebinned', xbins)
hist_variationUp.Write()
hist_variationDown.Write()"""
#print 'Sigma_up:', sigma_up
#print 'Sigma_down:', sigma_down
# Building function for calculating likelihood for each bin
def nll(p, arr_data, arr_signal, arr_combined_background, sigma, i):
alpha = p[0]
theta = p[1]
#sigma = 0.1
mu = alpha*arr_signal[i] + theta*arr_combined_background[i]
llog = stats.poisson.logpmf(int(arr_data[i]), mu) + np.log(1/np.sqrt(2*3.14*sigma[i])*np.exp(-(theta-1)**2/(2*sigma[i])))
#print 'exp:', stats.poisson.logpmf(int(arr_data[i]), mu)
#print 'norm:', np.log(1/np.sqrt(2*3.14*sigma)*np.exp(-(theta-1)**2/(2*sigma)))
#print -(llog)
return -(llog)
#p0 = np.array([10.0, 2.0])
#for j in range(Nbin):
# print nll(p0, arr_data, arr_signal, arr_combined_background, j)
# Minimizing nll for each bin
dim = len(diff_background) + 1
"""p0 = np.array([1.0, 2.0])
for j in range(Nbin):
result = optimize.minimize(nll, p0, (arr_data, arr_signal, arr_combined_background, j), bounds = [(None,None), (None, None)])
print 'alpha:', result.x[0], 'theta:', result.x[1]
#for i in range(dim):
# if i == 0:
# print 'Signal'
# else:
# print diff_background[i-1]
# print result.x[i]
# Minimizing nll for all bins simultaneously
# Definition of summed nll
def sum_nll(p, arr_data, arr_signal, arr_combined_background, Nbin):
list_nll = []
for j in range(Nbin):
list_nll.append(nll(p, arr_data, arr_signal, arr_combined_background, j))
sum_nll = 0.0
for l in range(len(list_nll)):
sum_nll = sum_nll + list_nll[l]
return sum_nll
p0 = np.array([1.0, 2.0])
result = optimize.minimize(sum_nll, p0, (arr_data, arr_signal, arr_combined_background, Nbin), bounds = [(None,None), (None,None)])
print result
#print 'Sum_nll minimizing result:'
#for i in range(dim):
# print result.x[i]"""
# Profile likelihood for each bin
def profile_likelihood(alpha, arr_data, arr_signal, arr_combined_background, sigma, i):
# nominator
def nll_nom(theta, alpha, arr_data, arr_signal, arr_combined_background, sigma, i):
p = np.array([alpha, theta])
return nll(p, arr_data, arr_signal, arr_combined_background, sigma, i)
theta = 2.0
result_nom = optimize.minimize(nll_nom, theta, (alpha, arr_data, arr_signal, arr_combined_background, sigma, i))
#print result_nom
# denominator
p0 = np.array([1.0, 2.0])
result_denom = optimize.minimize(nll, p0, (arr_data, arr_signal, arr_combined_background, sigma, i))
#print result_denom
# Combine
return nll_nom(result_nom.x[0], alpha, arr_data, arr_signal, arr_combined_background, sigma, i) - nll(result_denom.x, arr_data, arr_signal, arr_combined_background, sigma, i)
#print profile_likelihood(10.0, arr_data, arr_signal, arr_combined_background, 4)
#alpha = 9.0
#result = optimize.minimize(profile_likelihood, alpha, (arr_data, arr_signal, arr_combined_background, 4), method = 'Powell')
#print result
#for i in range(12):
# plt.plot(i, profile_likelihood(i, arr_data, arr_signal, arr_combined_background, 4),'b*')
#plt.show()
# Definition of summed profile likelihood
def sum_profile(alpha, arr_data, arr_signal, arr_combined_background, sigma, Nbin):
list_profile = []
for j in range(Nbin):
list_profile.append(profile_likelihood(alpha, arr_data, arr_signal, arr_combined_background, sigma, j))
sum_profile = 0.0
for l in range(len(list_profile)):
sum_profile = sum_profile + list_profile[l]
return sum_profile
# Function to plot likelihood
x = np.linspace(-2.0,2.0,100)
x.tolist()
y = []
for i in x:
y.append(sum_profile(i, arr_data, arr_signal, arr_combined_background, sigma, Nbin))
plt.rc('text', usetex=True)
plt.rc('font', family='serif')
params = {'legend.fontsize': 'x-large', 'axes.labelsize': 'x-large', 'axes.titlesize':'x-large', 'xtick.labelsize':'x-large', 'ytick.labelsize':'x-large'}
pylab.rcParams.update(params)
fig = plt.figure()
plt.plot(x,y,'b-')
xh = np.linspace(-2.0,2.0,200)
yh = np.array([0.5 for i in xrange(len(xh))])
plt.plot(xh, yh,'r-')
plt.xlabel(r'$\mu$')
plt.ylabel(r'$-\ln(\lambda(\mu))$')
fig.savefig('profile_likelihood.pdf')
#plt.show()
alpha = 0.0
result = optimize.minimize(sum_profile, alpha, (arr_data, arr_signal, arr_combined_background, sigma, Nbin), method = 'Powell')
print result.x
# Error of estimator
err = lambda x: sum_profile(x, arr_data, arr_signal, arr_combined_background, sigma, Nbin)-(sum_profile(result.x, arr_data, arr_signal, arr_combined_background, sigma, Nbin)+0.5)
down = result.x - optimize.fsolve(err,(result.x - 3.0))[0]
up = optimize.fsolve(err,(result.x + 0.01))[0] - result.x
print down
print up
print 'Function fit_python() works'
##### Main
if __name__ == "__main__":
##### Settings
input_file = '/mnt/t3nfs01/data01/shome/creissel/data/simulation/sl_jge6_tge4__btag_LR_4b_2b_btagCSV_logit.root'
#output_file = 'data.root'
#beta = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]
#output = {0.0 : 'file00.root', 0.1 : 'file01.root', 0.2 : 'file02.root', 0.3 : 'file03.root', 0.4 : 'file04.root', 0.5 : 'file05.root', 0.6 : 'file07.root', 0.7 : 'file07.root', 0.8 : 'file08.root', 0.9 : 'file09.root', 1.0 : 'file1.root', 2.0 : 'file2.root', 3.0 : 'file3.root', 4.0 : 'file4.root', 5.0 : 'file5.root', 6.0 : 'file6.root', 7.0 : 'file7.root', 8.0 : 'file8.root', 9.0 : 'file9.root', 10.0 : 'file10.root'}
beta = [1.1, 1.2, 1.3, 1.4, 1.5]
output = {1.1 : 'file11.root', 1.2 : 'file12.root', 1.3 : 'file13.root', 1.4 : 'file14.root', 1.5 : 'file15.root'}
print 'Settings complete'
for i in beta:
generate_data(input_file, output[i], 1.0, i)
#fit_python(input_file, output[i])"""
#generate_data(input_file, 'control2.root', 1.010, 2.000)
#generate_data(input_file, output_file, 1.0, beta)
#fit_python(input_file, 'file05.root')