-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprepinput_noshape.py
More file actions
297 lines (256 loc) · 11.8 KB
/
prepinput_noshape.py
File metadata and controls
297 lines (256 loc) · 11.8 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
#!/usr/bin/env python
import os,sys
from ROOT import *
from string import *
maketestcard = False
if len(sys.argv) > 1: date = sys.argv[1]
if len(sys.argv) > 2: signalname = sys.argv[2]
ApplyCorrelatedFakeSyst = True
ApplyCorrelatedPromptSyst = True
# Zero out certain bins
def zero_out_certain_bins(histo, ignoreBins):
for iBin in range(histo.GetNbinsX()+1):
if iBin in ignoreBins:
histo.SetBinContent(iBin, 0)
histo.SetBinError(iBin, 0)
return histo
# bins options for inclusion in limit setting
nobins = []
leptonbins = list(range(25, 48+1)) + list(range(50, 51+1))
hadronbins = list(range(1, 24+1)) + list(range(49, 49+1))
binstozero = nobins
#binstozero = leptonbins
# Directory suffix
dirext = ''
if ApplyCorrelatedFakeSyst is True or ApplyCorrelatedPromptSyst is True:
dirext = dirext + '_' + str(ApplyCorrelatedFakeSyst) + str(ApplyCorrelatedPromptSyst)
if len(binstozero) > 0:
dirext = dirext + '_nolep'
#date = '210520'
#date = '220818'
#date = '220914'
#date = '220924'
#date = '221011'
# Physics processes
# !!! Maybe already match here the process names and histograms
procs = [
'Signal',
'Shower',
'Muon',
'Fake'
]
#maindir = '/afs/desy.de/user/k/kutznerv/dust/public/disapptrk/interpretation/Histograms/Indium/v15shutterleps/'
#maindir = '/afs/desy.de/user/k/kutznerv/dust/public/disapptrk/interpretation/Histograms/Indium/v15/'
maindir = '/afs/desy.de/user/k/kutznerv/dust/public/disapptrk/interpretation/Histograms/Indium/v17/'
fb = TFile(maindir+'Background/predictionRun2.root')
fb.ls()
# Get all bg histograms
hsh = fb.Get('hShowerBaselineMASTER_BinNumberMethod2')
hsh.SetName('Shower')
hmn = fb.Get('hMuonBaselineMASTER_BinNumberMethod3')
hmn.SetName('Muon')
hfk = fb.Get('hFakeBaselineMASTER_BinNumberMethod1')
hfk.SetName('Fake')
#hobs = hsh.Clone()
#hobs.SetName('data_obs')
hobs = fb.Get('h_data')
#hobs.Add(hfk)
#hobs.Add(hmn)
print(hsh.Integral() + hmn.Integral() + hfk.Integral(), hobs.Integral())
# Zeroing out
zero_out_certain_bins(hsh, binstozero)
zero_out_certain_bins(hmn, binstozero)
#zero_out_certain_bins(hpi, binstozero)
zero_out_certain_bins(hfk, binstozero)
zero_out_certain_bins(hobs, binstozero)
#signalname = sys.argv[1]
#print 'Signal: '+signalname
signalname = 'T2btLL'
#signalname = 'T1qqqqLL'
#signalname = 'T1btbtLL'
#signalname = 'T2tbLL'
#signalname = 'PureHiggsino'
signaldir = maindir+'Signal/'+signalname+'/*.root'
signalfiles = os.popen('ls '+signaldir).readlines()
outrootdir = 'sigbgobsroot/'+date+'/'+signalname+dirext+'/'
outdcdir = 'datacards/'+date+'/'+signalname+dirext+'/'
if not os.path.exists(outrootdir):
os.system('mkdir -p '+outrootdir)
if not os.path.exists(outdcdir):
os.system('mkdir -p '+outdcdir)
# DATACARD CONTENT
# Write the datacard beginning lines
cnt = '''imax 49 number of channels
jmax %s number of backgrounds
kmax * number of nuisance parameters
#SHRINKFACTOR=%f
------------------------------------------------------------
observation %s
------------------------------------------------------------
'''
# Number of background processes
nbg = str(len(procs) - 1)
systs = ['BTag', 'Dedx', 'DtSfLong', 'DtSfShort', 'Isr', 'Jec', 'Scale', 'Trig']
if signalname == 'PureHiggsino':
systs.remove('Scale')
sighistsup = {}
sighistsdn = {}
for f in signalfiles:
f = f.strip()
signalhistofile = f
signalname = ((f.split('/'))[-1])[:-5]
print (signalname)
if "pm" in signalname:
print ("******* 1:", signalname)
signalname = signalname.replace("pm", "ne")
print ("******* 2:", signalname)
frn = signalname.replace("AnalysisHists", "limitinput") + '.root'
fdn = signalname.replace("AnalysisHists", "datacard") + '.txt'
fs = TFile(signalhistofile)
if signalname == 'T1qqqqLL' and '1075' in signalhistofile: continue
print ('*** Signal file:', signalhistofile)
# Get signal histograms
#hsig = fs.Get('hLongBaselineSystNom_BinNumberTruth')
#hsig.SetName('Signal')
hsig = fs.Get('hBaselineNom_BinNumberTruth')
hsig.SetName('Signal')
if hsig.Integral()>0: shrinkfactor = 10./hsig.GetMaximum()
else: shrinkfactor = 1.0
hsig.Scale(shrinkfactor)
zero_out_certain_bins(hsig, binstozero)
for sys in systs:
#if sys == 'Trig' or 'DtSf' in sys: continue
#if signalname == 'PureHiggsino' and sys == 'Scale': continue
#print sys
sighistsup[sys] = fs.Get('hBaselineSyst'+sys+'Up_BinNumberTruth')
sighistsup[sys].SetName('Signal_'+sys+'Up')
sighistsup[sys].Scale(shrinkfactor)
sighistsdn[sys] = fs.Get('hBaselineSyst'+sys+'Down_BinNumberTruth')
sighistsdn[sys].SetName('Signal_'+sys+'Down')
sighistsdn[sys].Scale(shrinkfactor)
zero_out_certain_bins(sighistsup[sys], binstozero)
zero_out_certain_bins(sighistsdn[sys], binstozero)
# Make the output file
fr = TFile(outrootdir+'/'+frn, 'RECREATE')
# Write histograms to the output file
hsig.Write()
for sys in systs:
#if sys == 'Trig' or 'DtSf' in sys: continue
sighistsup[sys].Write()
sighistsdn[sys].Write()
hmn.Write()
hsh.Write()
hfk.Write()
hobs.Write()
# Write BG syst histos
if maketestcard: fd = open('testypoo2.txt', 'w')
else: fd = open(outdcdir+'/'+fdn, 'w')
print('we hath created', outdcdir+'/'+fdn)
# Number of data events
ndatac = []
for ibin in range(1, hobs.GetXaxis().GetNbins()+1):
ndatac.append('%-14s' % int(hobs.GetBinContent(ibin)) + ('%-14s' % '')*3)
ndata = str(hobs.Integral())
# Write the datacard header
#print nbg, ndata, frn
fd.write(cnt % (nbg, shrinkfactor, ''.join(ndatac)))#ndata))
# Yields for processes
yields = []
for ibin in range(1,hobs.GetXaxis().GetNbins()+1):
for p in procs:
#yields.append(gDirectory.Get(p).Integral())
print 'doin', p
yields.append(gDirectory.Get(p).GetBinContent(ibin))
# Write processes and rates
row = '%-21s ' % 'bin'
for ibin in range(1,hobs.GetXaxis().GetNbins()+1):
for i in procs:
row = row+'%-14s' % ('SR'+str(ibin))
fd.write(row+'\n')
row = '%-21s ' % 'process'
for ibin in range(1,hobs.GetXaxis().GetNbins()+1):
for i in procs:
row = row+'%-14s' % i
fd.write(row+'\n')
row = '%-21s ' % 'process'
for ibin in range(1,hobs.GetXaxis().GetNbins()+1):
for i in range(len(procs)):
row = row+'%-14i' % i
fd.write(row+'\n')
row = '%-21s ' % 'rate'
for y in yields:
row = row+'%-14s' % str(round(y,3))
fd.write(row+'\n')
# Add random systematics
cnt2 = '''-----------------------------------------
'''
cnt2 += '''Lumi lnN '''+ '''1.025 - - - '''*hobs.GetXaxis().GetNbins()+'''
'''
cnt2 += '''jes lnN '''+ '''0.98/1.03 - - - '''*hobs.GetXaxis().GetNbins()+'''
'''
# Closure shape - 1.0 1.0 1.0
fd.write(cnt2)
for sys in systs:
#print 'sys', sys
rowc = []
rowc.append('%-14s ' % sys.strip() +'%-6s ' % 'lnN')
hup, hdown = fs.Get('hBaselineSyst'+sys+'Up_BinNumberTruth'), fs.Get('hBaselineSyst'+sys+'Down_BinNumberTruth')
hup.Scale(shrinkfactor)
hdown.Scale(shrinkfactor)
for ibin in range(1,hobs.GetXaxis().GetNbins()+1):
for p in procs:
relevant = p=='Signal'
if relevant:
if (hup.GetBinContent(ibin)>0 and hdown.GetBinContent(ibin)>0) and not abs(hup.GetBinContent(ibin)==hdown.GetBinContent(ibin)): rowc.append('%-14s' % (str(round(hup.GetBinContent(ibin)/hsig.GetBinContent(ibin),3))+'/'+str(round(hdown.GetBinContent(ibin)/hsig.GetBinContent(ibin),3))))
else: rowc.append('%-14s' % ('1.0'))
else: rowc.append('%-14s' % ('-'))
row = ''.join(rowc)
fd.write(row+'\n')
if True:
fd.write('''ShowerShortOne lnN '''+'''- - - - - - - - - 2.0 - - - 2.0 - - '''*int(hobs.GetXaxis().GetNbins()/4)+'''- '''*4+'\n')
if ApplyCorrelatedPromptSyst:
fd.write('''ShowerLongSyst lnN '''+'''- 1.2 - - - 1.2 - - - - - - - - - - '''*int(hobs.GetXaxis().GetNbins()/4)+'''- '''*4+'\n')
if ApplyCorrelatedFakeSyst:
fd.write('''FakeLongSyst lnN '''+'''- - - 1.3 - - - 1.3 - - - - - - - - '''*int(hobs.GetXaxis().GetNbins()/4)+'''- '''*4+'\n')
fd.write('''FakeShortSyst lnN '''+'''- - - - - - - - - - - 1.3 - - - 1.3 '''*int(hobs.GetXaxis().GetNbins()/4)+'''- '''*4+'\n')
for bgsys in [['FakeLong', '1', 'One'],['FakeShort', '1','One'], ['ShowerLong', '2', 'One'], ['MuonLong', '3', 'One']]:#, ['ShowerShort', '2', 'One']
#print 'bgsys', bgsys
hup, hdown = fb.Get('h'+bgsys[0]+'BaselineMASTER_BinNumberMethod'+bgsys[1]+bgsys[2]+'Up'), fb.Get('h'+bgsys[0]+'BaselineMASTER_BinNumberMethod'+bgsys[1]+bgsys[2]+'Down')
rowc = []
rowc.append('%-14s ' % (bgsys[0]+bgsys[2]).strip()+('%-6s ' % 'lnN'))
for ibin in range(1,hobs.GetXaxis().GetNbins()+1):
for p in procs:
relevant = bgsys[0].split('Long')[0].split('Short')[0]==p
if relevant:
if (hup.GetBinContent(ibin)>0 and hdown.GetBinContent(ibin)>0) and not abs(hup.GetBinContent(ibin)==hdown.GetBinContent(ibin)): rowc.append('%-14s' % (str(round(hup.GetBinContent(ibin)/gDirectory.Get(p).GetBinContent(ibin),3))+'/'+str(round(hdown.GetBinContent(ibin)/gDirectory.Get(p).GetBinContent(ibin),3))))
else: rowc.append('%-14s' % ('1.0'))
else: rowc.append('%-14s' % ('-'))
row = ''.join(rowc)
fd.write(row+'\n')
del rowc
for ibin in range(1,hobs.GetXaxis().GetNbins()+1):
for p in procs:
hp = gDirectory.Get(p)
rowc = []
#if hp.GetBinError(ibin)>0: linestem = str(float('%.3g' % (hp.GetBinContent(ibin)**2/hp.GetBinError(ibin)**2)))
if hp.GetBinError(ibin)>0: N = int(float('%.3g' % (hp.GetBinContent(ibin)**2/hp.GetBinError(ibin)**2)))
else: continue #else: continue#linestem = '0'
linestem = str(N)
if float(linestem.split('.')[-1])==0: linestem = str(int(float(linestem)))
nuisname = 'gma'+p.strip()+str(ibin)
rowc.append('%-21s ' % (nuisname+' gmN '+linestem))
for ibin_ in range(1,hobs.GetXaxis().GetNbins()+1):
for p_ in procs:
if not (ibin_==ibin and p_==p):
rowc.append('%-14s' % ('-'))
continue
if hp.GetBinContent(ibin)>0: rowc.append('%-14s' % str(round(hp.GetBinContent(ibin)/N,4)))
else: rowc.append('%-14s' % ('0'))
row = ''.join(rowc)
#os.system('echo '+row+' >> '+fd.name)
fd.write(row+'\n')
del rowc
#fd.write('''ShowerShortOne lnN '''+'''0.98/1.03 - - - '''*hobs.GetXaxis().GetNbins()+''')
fs.Close()
fd.close()
if maketestcard: exit(0)