1 |
#!/usr/bin/python2.6
|
2 |
|
3 |
import os
|
4 |
import math
|
5 |
|
6 |
def getfloat_from_line( line ):
|
7 |
start = line.find('.')
|
8 |
number = float(line[start-1:])
|
9 |
return number
|
10 |
|
11 |
def sum_errors( e1, e2 ):
|
12 |
return math.sqrt( e1 * e1 + e2 * e2 )
|
13 |
|
14 |
def get_errors( line, sf ):
|
15 |
if line.find('up') > 0:
|
16 |
sf.append(getfloat_from_line(line))
|
17 |
if line.find('down') > 0:
|
18 |
sf.append(getfloat_from_line(line))
|
19 |
return sf
|
20 |
|
21 |
def get_correlation_matrix( file ):
|
22 |
return [line.split() for line in file]
|
23 |
|
24 |
file = open('SFErrors_Pt50To100.txt','r')
|
25 |
stat = [0, 0, 0, 0]
|
26 |
dyl_btag = []
|
27 |
dyc_btag = []
|
28 |
dyb_btag = []
|
29 |
ttbar_btag = []
|
30 |
dyl_je = []
|
31 |
dyc_je = []
|
32 |
dyb_je = []
|
33 |
ttbar_je = []
|
34 |
|
35 |
for line in file:
|
36 |
# print line
|
37 |
|
38 |
if ( line.find('DYL') ) > 0:
|
39 |
if( line.find('stat') > 0 ):
|
40 |
stat[0] = getfloat_from_line(line)
|
41 |
if ( line.find('btag') > 0 ) or (line.find('mistag') > 0 ):
|
42 |
dyl_btag = get_errors( line, dyl_btag )
|
43 |
if ( line.find('jer') > 0 ) or ( line.find('jec') > 0):
|
44 |
dyl_je = get_errors( line, dyl_je )
|
45 |
|
46 |
if ( line.find('DYC') ) > 0:
|
47 |
if( line.find('stat') > 0 ):
|
48 |
stat[1] = getfloat_from_line(line)
|
49 |
if ( line.find('btag') > 0 ) or (line.find('mistag') > 0 ):
|
50 |
dyc_btag = get_errors( line, dyc_btag )
|
51 |
if ( line.find('jer') > 0 ) or ( line.find('jec') > 0):
|
52 |
dyc_je = get_errors( line, dyc_je )
|
53 |
|
54 |
if ( line.find('DYB') ) > 0:
|
55 |
if( line.find('stat') > 0 ):
|
56 |
stat[1] = getfloat_from_line(line)
|
57 |
if ( line.find('btag') > 0 ) or (line.find('mistag') > 0 ):
|
58 |
dyb_btag = get_errors( line, dyb_btag )
|
59 |
if ( line.find('jer') > 0 ) or ( line.find('jec') > 0):
|
60 |
dyb_je = get_errors( line, dyb_je )
|
61 |
|
62 |
if ( line.find('TTbar') ) > 0:
|
63 |
if( line.find('stat') > 0 ):
|
64 |
stat[2] = getfloat_from_line(line)
|
65 |
if ( line.find('btag') > 0 ) or (line.find('mistag') > 0 ):
|
66 |
ttbar_btag = get_errors( line, ttbar_btag )
|
67 |
if ( line.find('jer') > 0 ) or ( line.find('jec') > 0):
|
68 |
ttbar_je = get_errors( line, ttbar_je )
|
69 |
|
70 |
|
71 |
dyl_syst = sum_errors( max(dyl_btag), max(dyl_je) )
|
72 |
#dyc_syst = sum_errors( max(dyc_btag), max(dyc_je) )
|
73 |
dyb_syst = sum_errors( max(dyb_btag), max(dyb_je) )
|
74 |
ttbar_syst = sum_errors( max(dyl_btag), max(ttbar_je) )
|
75 |
|
76 |
|
77 |
print 'Systematics only'
|
78 |
print dyl_syst
|
79 |
#print dyc_syst
|
80 |
print dyb_syst
|
81 |
print ttbar_syst
|
82 |
|
83 |
print "Final"
|
84 |
|
85 |
dyl_err = sum_errors( stat[0], dyl_syst )
|
86 |
#dyc_err = sum_errors( stat[1], dyc_syst )
|
87 |
dyb_err = sum_errors( stat[1], dyb_syst )
|
88 |
ttbar_err = sum_errors( stat[2], ttbar_syst )
|
89 |
|
90 |
print dyl_err
|
91 |
#print dyc_err
|
92 |
print dyb_err
|
93 |
print ttbar_err
|
94 |
|
95 |
correlation_file = open('CorrelationMatrix_Pt50To100.txt','r')
|
96 |
corr = get_correlation_matrix( correlation_file )
|
97 |
print corr
|
98 |
|
99 |
# dyl_string = 'CMS_vhbb_ZjLF_SF lnN - - - ' + '%.3f' % (1.+float(corr[0][0])*dyl_err) + ' ' + '%.3f' %(1.+(float(corr[0][1])*dyl_err)) + ' ' + '%.3f' %(1.+float(corr[0][2])*dyl_err) + ' ' + '%.3f' %(1.+float(corr[0][3])*dyl_err) + ' - - - - \n'
|
100 |
# dyc_string = 'CMS_vhbb_ZjCF_SF lnN - - - ' + '%.3f' %(1.+float(corr[1][0])*dyc_err) + ' ' + '%.3f' %(1.+(float(corr[1][1])*dyc_err)) + ' ' + '%.3f' %(1.+float(corr[1][2])*dyc_err) + ' ' + '%.3f' %(1.+float(corr[1][3])*dyc_err) + ' - - - - \n'
|
101 |
# dyb_string = 'CMS_vhbb_ZjHF_SF lnN - - - ' + '%.3f' %(1.+float(corr[2][0])*dyb_err) + ' ' + '%.3f' %(1.+(float(corr[2][1])*dyb_err)) + ' ' + '%.3f' %(1.+float(corr[2][2])*dyb_err) + ' ' + '%.3f' %(1.+float(corr[2][3])*dyb_err) + ' - - - - \n'
|
102 |
# ttbar_string = 'CMS_vhbb_TT_SF lnN - - - ' + '%.3f' %(1.+float(corr[3][0])*ttbar_err) + ' ' + '%.3f' %(1.+(float(corr[3][1])*ttbar_err)) + ' ' + '%.3f' %(1.+float(corr[3][2])*ttbar_err) + ' ' + '%.3f' %(1.+float(corr[3][3])*ttbar_err) + ' - - - - \n'
|
103 |
|
104 |
dyl_string = 'CMS_vhbb_ZjLF_SF lnN - - - ' + '%.3f' % (1.+float(corr[0][0])*dyl_err) + ' ' + '%.3f' %(1.+(float(corr[0][1])*dyl_err)) + ' ' + '%.3f' %(1.+float(corr[0][2])*dyl_err) + ' - - - - \n'
|
105 |
dyb_string = 'CMS_vhbb_ZjHF_SF lnN - - - ' + '%.3f' %(1.+float(corr[1][0])*dyb_err) + ' ' + '%.3f' %(1.+(float(corr[1][1])*dyb_err)) + ' ' + '%.3f' %(1.+float(corr[1][2])*dyb_err) + ' - - - - \n'
|
106 |
ttbar_string = 'CMS_vhbb_TT_SF lnN - - - ' + '%.3f' %(1.+float(corr[2][0])*ttbar_err) + ' ' + '%.3f' %(1.+(float(corr[2][1])*ttbar_err)) + ' ' + '%.3f' %(1.+float(corr[2][2])*ttbar_err) + ' - - - - \n'
|
107 |
|
108 |
|
109 |
print dyl_string
|
110 |
#print dyc_string
|
111 |
print dyb_string
|
112 |
print ttbar_string
|
113 |
|
114 |
outfile = open("Datacard.txt",'w')
|
115 |
outfile.writelines(dyl_string)
|
116 |
#outfile.writelines(dyc_string)
|
117 |
outfile.writelines(dyb_string)
|
118 |
outfile.writelines(ttbar_string)
|
119 |
outfile.close()
|
120 |
|