forked from govoni/NuiScan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadDC_and_submit.py
213 lines (147 loc) · 5.77 KB
/
readDC_and_submit.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
#!/usr/bin/python
import os
import sys
import subprocess
import operator
from ROOT import TFile, TH1F, TCanvas, gStyle, TLine
from commands import getstatusoutput
from operator import itemgetter
# ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
def runLimitCalc (filename) :
filenameBash = filename+".submit.sh"
filenameResult = filename+".limit.txt"
argslist = [
'combine',
'-M' ,
'Asymptotic',
filename,
' &> ',
filenameResult
]
f = open (filenameBash, 'w')
f.write ("#/bash/sh \n")
os.getcwd()
f.write ("cd "+os.getcwd()+"\n")
f.write ("cd ../ \n")
f.write ("eval `scramv1 runtime -sh` \n")
f.write ("cd - \n")
f.write ("cd /tmp/ \n")
f.write (' '.join(argslist))
f.write("\n")
os.getcwd()
#copyremote = "cp "+filenameResult+" "+os.getcwd()
#f.write (copyremote)
#f.write("\n")
f.close ()
os.system("chmod +x "+filenameBash)
print "bsub -q 8nm "+filenameBash
os.system("bsub -q 8nm "+filenameBash)
# ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
def getLimitFromFile (filename) :
filenameResult = filename+".limit.txt"
f = open (filenameResult, 'r')
calcLimit = f.readlines()
thisLimit = [line for line in calcLimit.split ('\n') if line.find ('Expected 50.0') != -1][0].split ()[4]
return thisLimit
# ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
def clean (basename) :
getstatusoutput('rm ./' + basename + '*')
# ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
def lookAtSystematics (datacardname) :
# open the datacard file
# ---- ---- ---- ---- ---- ---- ---- ----
print 'Opening original input datacard: ', datacardname
lines = open (datacardname, 'r').read().split ('\n')
nametag = datacardname.split ('/')[-1].replace ('.txt', '')
thepath = datacardname.replace (nametag + '.txt', '')
print "nametag="+nametag
print "thepath="+thepath
# separate header and systematics
# ---- ---- ---- ---- ---- ---- ---- ----
gStyle.SetGridStyle (1)
gStyle.SetGridColor (15)
# gStyle.SetGridWidth (float (0.5))
systime = 0
header = []
systematics = []
systematics_fixed = []
for linea in lines:
if '---' in linea : continue
if systime == 0 :
header.append (linea)
if linea.split (' ')[0] == 'rate' :
systime = 1
elif 'Deco' in linea:
systematics_fixed.append(linea)
elif 'CMS_sig' in linea:
systematics_fixed.append(linea)
else:
systematics.append (linea)
systematics = [elem for elem in systematics if len (elem.split ()) > 0]
# run the actual result with the original datacard
# ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
runLimitCalc (datacardname)
# remove, one at a time, one systematic, and run the limit
# ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
syslist = []
syslist.append ('NOMINAL')
for it in range (len (systematics)) :
elements = systematics[it].split ()
syslist.append (systematics[it].split ()[0])
if len (elements) == 0 : continue
filename = thepath + 'tempo.remove.' + str (it) + '.' + systematics[it].split ()[0] + '.' + nametag
f = open(filename, 'w')
for linea in header: f.write (linea + '\n')
for it1 in range (len (systematics)) :
if (it1 == it) : continue
if len (systematics[it1].split ()) == 0 : continue
f.write (systematics[it1] + '\n')
for linea in systematics_fixed : f.write (linea + '\n')
f.close ()
runLimitCalc (filename)
# run the limit with no systematics
# ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
filename = thepath + 'tempo.stats.' + str (0.) + nametag +'.txt'
f = open(filename, 'w')
for linea in header: f.write (linea + '\n')
f.close ()
runLimitCalc (filename)
# run the limit with only shape systematics
# ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
filename = thepath + 'tempo.shape.' + str (0.) + nametag +'.txt'
f = open(filename, 'w')
for linea in header: f.write (linea + '\n')
for linea in systematics_fixed : f.write (linea + '\n')
f.close ()
runLimitCalc (filename)
# add, one at a time, only one systematic source, and run the limit
# ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
for it in range (len (systematics)) :
elements = systematics[it].split ()
if len (elements) == 0 : continue
filename = thepath + 'tempo.add.' + str (it) + '.' + systematics[it].split ()[0] + "." + nametag
f = open(filename, 'w')
for linea in header: f.write (linea + '\n')
f.write (systematics[it] + '\n')
for linea in systematics_fixed : f.write (linea + '\n')
f.close ()
runLimitCalc (filename)
# clean
# ----
#clean (thepath + 'tempo')
#clean ('roostats')
# ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
if __name__ == '__main__':
if len (sys.argv) < 2 :
print 'input datacard folder missing\n'
exit (1)
folderName = sys.argv[1].split ('/')[-1] + '_copy_01jet'
result = getstatusoutput ('rm -rf ' + folderName)
if result[0] == 0 : print 'NB folder ' + folderName + ' cleaned, being replaced'
currentFolder = getstatusoutput ('pwd')[1]
getstatusoutput ('cp -r ' + sys.argv[1] + ' ./' + folderName)
listOfDatacards = []
for elem in getstatusoutput ('ls ' + str (folderName) + ' | grep txt')[1].split ('\n'):
listOfDatacards.append (currentFolder + '/' + folderName + '/' + str (elem))
for datacard in listOfDatacards :
lookAtSystematics (datacard)