ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/VHbb/test/prepare_datacard.py
Revision: 1.2
Committed: Wed May 9 09:23:30 2012 UTC (13 years ago) by bortigno
Content type: text/x-python
Branch: MAIN
CVS Tags: AN-12-181-7TeV
Changes since 1.1: +11 -4 lines
Log Message:
bug fix

File Contents

# Content
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[2] = 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[3] = 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 print dyl_syst
77 print dyc_syst
78 print dyb_syst
79 print ttbar_syst
80
81 print "Final"
82
83 dyl_err = sum_errors( stat[0], dyl_syst )
84 dyc_err = sum_errors( stat[1], dyc_syst )
85 dyb_err = sum_errors( stat[2], dyb_syst )
86 ttbar_err = sum_errors( stat[3], ttbar_syst )
87
88 print dyl_err
89 print dyc_err
90 print dyb_err
91 print ttbar_err
92
93 correlation_file = open('CorrelationMatrix_Pt50To100.txt','r')
94 corr = get_correlation_matrix( correlation_file )
95 print corr
96
97 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'
98 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'
99 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'
100 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'
101
102 print dyl_string
103 print dyc_string
104 print dyb_string
105 print ttbar_string
106
107 outfile = open("Datacard.txt",'w')
108 outfile.writelines(dyl_string)
109 outfile.writelines(dyc_string)
110 outfile.writelines(dyb_string)
111 outfile.writelines(ttbar_string)
112 outfile.close()
113