-
Notifications
You must be signed in to change notification settings - Fork 4
/
LimitsUtil.py
executable file
·421 lines (338 loc) · 15.3 KB
/
LimitsUtil.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
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
from ROOT import *
import os,sys,json,time,re
import logging
from shutil import copy,copy2
from pprint import pformat
from multiprocessing import Pool, TimeoutError, current_process
from HiggsAnalysis.bbggLimits.DataCardUtils import *
from HiggsAnalysis.bbggLimits.IOUtils import *
from HiggsAnalysis.bbggLimits.CombineUtils import *
from HiggsAnalysis.bbggLimits.AnalyticalReweighting import *
from os import listdir
from os.path import isfile, join
import getpass
__username__ = getpass.getuser()
__BAD__ = 666
def printTime(t1, t2, log):
tNew = time.time()
log.debug('Time since start of worker: %.2f sec; since previous point: %.2f sec' %(tNew-t2,tNew-t1))
return tNew
def runFullChain(opt, Params, point=None, NRgridPoint=-1, extraLabel=''):
#print 'Running: ', sys._getframe().f_code.co_name, " Node=",point
# print sys._getframe().f_code
PID = os.getpid()
if opt.verb==0:
logLvl = logging.ERROR
elif opt.verb==1:
logLvl = logging.INFO
else:
logLvl = logging.DEBUG
LTDir_type = os.getenv("CMSSW_BASE")+Params['LTDIR']
if '/tmp' in Params['LTDIR'] or '/store' in Params['LTDIR'] or '/afs' in Params['LTDIR']:
LTDir_type = Params['LTDIR']
if '/store' in Params['LTDIR']:
LTDir_type = '/eos/cms'+Params['LTDIR']
signalModelCard = os.getenv("CMSSW_BASE")+Params['signal']['signalModelCard']
lumi = 35.87 # Only used for plot produced by bbgg2Dfitter
energy = str(Params['other']["energy"])
mass = Params['other']["higgsMass"]
addHiggs = Params['other']["addHiggs"]
scaleSingleHiggs = Params['other']["scaleSingleHiggs"]
doBlinding = Params['other']["doBlinding"]
doBands = Params['other']["doBands"]
NCAT = Params['other']["ncat"]
doBrazilianFlag = Params['other']["doBrazilianFlag"]
Combinelxbatch = Params['other']['Combinelxbatch']
doSingleLimit = Params['other']['doSingleLimit']
drawSignalFit = Params['other']['drawSignalFit']
doCombine = Params['other']["runCombine"]
useSigTheoryUnc = Params['other']["useSigTheoryUnc"]
HH = Params['other']["HH"]
base = Params['other']["base"]
low = Params['other']["low"]
obs = Params['other']["obs"]
twotag=Params['other']["twotag"]
dataName = Params['data']['name']
combineOpt = Params['other']['combineOption']
doBias = Params['other']['doBias']
biasConfig = Params['other']['biasConfig']
doDoubleSidedCB = Params['other']['doDoubleSidedCB']
massCuts = [Params['other']["minMggMassFit"], Params['other']["maxMggMassFit"],
Params['other']["minMjjMassFit"], Params['other']["maxMjjMassFit"],
Params['other']["minSigFitMgg"], Params['other']["maxSigFitMgg"],
Params['other']["minSigFitMjj"], Params['other']["maxSigFitMjj"],
Params['other']["minHigMggFit"], Params['other']["maxHigMggFit"],
Params['other']["minHigMjjFit"], Params['other']["maxHigMjjFit"]]
if NCAT > 3:
procLog.error("Error NCAT>3!")
return __BAD__
signalTypes = Params['signal']['types']
if point!=None and NRgridPoint!=-1:
print 'WARning: cannot have both the Node and grid Point. Chose one and try again'
return __BAD__
elif opt.analyticalRW==True:
Label = "_ARW_"
elif point!=None:
Label = "_Node_"+str(point)
elif opt.Mjj_VBFCut!=None and opt.dEta_VBFCut!=None:
Label = "VBFHH"
elif NRgridPoint!=-1:
Label = "_gridPoint_"+str(NRgridPoint)
else:
print 'WARning: using list of nodes from the json input file'
return __BAD__
sigCat = 0
isRes = 0
if point==None:
sigCat = -1
elif point == 'SM':
sigCat = 0
elif point == 'box':
sigCat = 1
elif int(point) > 15:
sigCat = int(point)
isRes = 1
Label.replace("Node", "Mass")
else:
sigCat = int(point)
Label += extraLabel
print "Label=",Label
if opt.outDir:
baseFolder="./"+opt.outDir
else:
baseFolder="./bbggToolsResults"
# Create PID file to track the job:
pidfile = "/tmp/"+__username__+"/PIDs/PoolWorker"+Label+".pid"
file(pidfile, 'w').write(str(PID))
procName = current_process().name
try:
logging.basicConfig(level=logLvl,
format='%(asctime)s PID:%(process)d %(name)-12s %(levelname)-8s %(message)s',
datefmt='%m-%d %H:%M',
filename=baseFolder+'/logs/processLog_'+str(procName)+Label+'.log',
filemode='w')
except:
print 'I got excepted!'
return __BAD__
procLog = logging.getLogger('Process.Log')
procLog.info('\n\n New process Log started. PID = %d, job label: %r\n', PID, Label)
procLog.info("This log filename = "+logging.getLoggerClass().root.handlers[0].baseFilename)
procLog.info('Node or Mass=%r gridPoint=%r PID=%r \n Options: %s',point, NRgridPoint, PID, pformat(opt))
start = time.time()
# For now the mass cuts are all the same, but can be changed in future.
# ParamsForFits = {'SM': massCuts, 'box': massCuts}
SignalFile = "/LT_output_GluGluToHHTo2B2G_node_"+str(point)+"_13TeV-madgraph.root"
if "LT_StrikeBack" in LTDir_type or "MadMax" in LTDir_type or "ttH" in LTDir_type:
SignalFile = "/LT_output_GluGluToHHTo2B2G_node_"+str(point)+"_13TeV-madgraph_0.root"
if isRes:
SignalFile = "/LT_output_GluGluToTYPEToHHTo2B2G_M-"+str(point)+"_narrow_13TeV-madgraph.root"
if "RES_Mar21" in LTDir_type:
SignalFile = "/LT_output_GluGluToTYPEToHHTo2B2G_M-"+str(point)+"_narrow_13TeV-madgraph_0.root"
if NRgridPoint >= 0:
SignalFile = "/LT_NR_Nodes_2to13_merged.root"
if opt.analyticalRW == True:
pointStr = "_".join(['kl',str(opt.ARW_kl),'kt',str(opt.ARW_kt),'cg',str(opt.ARW_cg),'c2',str(opt.ARW_c2),'c2g',str(opt.ARW_c2g)]).replace('.', 'p').replace('-', 'm')
SignalFile="/LT_NR_Nodes_All_merged_"+pointStr+".root"
if opt.Mjj_VBFCut!=None and opt.dEta_VBFCut!=None:
SignalFile="/LT_output_VBFHHTo2B2G_CV_1_C2V_1_C3_1_13TeV-madgraph_0.root"
procLog.debug('%s, %s', SignalFile, pformat(signalTypes))
for t in signalTypes:
newFolder = baseFolder+ str('/'+t+Label)
thisSignalFile = SignalFile.replace("TYPE", t)
procLog.info('Type = %s, %s', t, newFolder)
createDir(newFolder,procLog)
HLFactoryname= str(t+Label)
hlf = RooStats.HLFactory(HLFactoryname, signalModelCard, False)
w = hlf.GetWs()
theFitter = bbgg2DFitter()
theStyle = theFitter.style()
gROOT.SetStyle('hggPaperStyle')
theFitter.Initialize( w, sigCat, lumi, newFolder, energy, doBlinding, NCAT, addHiggs,
massCuts[0],massCuts[1],massCuts[2],
massCuts[3],massCuts[4],massCuts[5],
massCuts[6],massCuts[7],massCuts[8],
massCuts[9],massCuts[10],massCuts[11], NRgridPoint,
logging.getLoggerClass().root.handlers[0].baseFilename+'.bbgg2D', opt.analyticalRW)
theFitter.SetVerbosityLevel(opt.verb)
if opt.ttHTaggerCut!=None:
theFitter.SetCut("ttHTagger > "+str(opt.ttHTaggerCut))
if opt.verb>0:
procLog.info('Apply the cut on ttHTagger: ' + str(opt.ttHTaggerCut))
if opt.Mjj_VBFCut!=None and opt.dEta_VBFCut!=None:
if not opt.removeHHVBF:
print "mjj_vbf > "+str(opt.Mjj_VBFCut)+" && deta_vbf > "+str(opt.dEta_VBFCut)
theFitter.SetCut("mjj_vbf > "+str(opt.Mjj_VBFCut)+" && deta_vbf > "+str(opt.dEta_VBFCut))
procLog.info("mjj_vbf > "+str(opt.Mjj_VBFCut)+" && deta_vbf > "+str(opt.dEta_VBFCut))
else:
theFitter.SetCut("mjj_vbf < "+str(opt.Mjj_VBFCut)+" || deta_vbf < "+str(opt.dEta_VBFCut))
procLog.info("mjj_vbf < "+str(opt.Mjj_VBFCut)+" || deta_vbf < "+str(opt.dEta_VBFCut))
if 'HighMass' in t:
theFitter.SetNCat0(2)
else:
theFitter.SetNCat0(0)
if opt.verb>0:
procLog.info('Using Double Sided Crystal Ball as Signal Model: %r', doDoubleSidedCB)
if doDoubleSidedCB: theFitter.UseDoubleSidedCB()
LTDir = LTDir_type.replace('TYPE', t)
mass = 125.0
if opt.verb>0:
procLog.info('Signal File:\n'+LTDir+thisSignalFile)
if not os.path.isfile(LTDir+thisSignalFile):
print 'File does not exist: ', LTDir+thisSignalFile
return __BAD__
openStatus = theFitter.AddSigData( mass, str(LTDir+thisSignalFile))
if openStatus==-1:
procLog.error('There is a problem with openStatus')
return __BAD__
procLog.info("\t SIGNAL ADDED. Node=%r, GridPoint=%r, type=%r", point,NRgridPoint,t)
if opt.verb>0: p1 = printTime(start, start, procLog)
createDir(newFolder+'/workspaces',procLog)
createDir(newFolder+'/datacards',procLog)
theFitter.SigModelFit( mass)
procLog.info("\t SIGNAL FITTED. Node=%r, GridPoint=%r, type=%r", point,NRgridPoint,t)
if opt.verb>0: p2 = printTime(p1,start, procLog)
fileBaseName = "hhbbgg.mH"+str(mass)[0:3]+"_13TeV"
theFitter.MakeSigWS( fileBaseName)
procLog.info("\t SIGNAL'S WORKSPACE DONE. Node=%r, GridPoint=%r, type=%r", point,NRgridPoint,t)
if opt.verb>0: p3 = printTime(p2,start,procLog)
if drawSignalFit:
theFitter.MakePlots( mass)
procLog.info("\t SIGNAL'S PLOT DONE. Node=%r, GridPoint=%r, type=%r", point,NRgridPoint,t)
if opt.verb>0: p4 = printTime(p3,start,procLog)
if addHiggs:
higTypes = Params['higgs']['type']
if opt.verb>1:
procLog.debug('Here will add SM Higgs contributions \n Higgs types: '+ pformat(higTypes))
higgsExp = {}
for iht,HT in enumerate(higTypes):
higgsExp[HT] = [0,0]
ht = higTypes[HT]
if opt.verb>1:
procLog.debug('iht = %r, ht = %r, HT = %r' % (iht,ht,HT))
higFileName = str(LTDir)+"/LT_output_"+str(ht)+".root"
exphig = theFitter.AddHigData( mass,higFileName,iht, str(HT))
theFitter.HigModelFit(mass,iht, str(HT) )
theFitter.MakeHigWS(str('hhbbgg.')+str(HT), iht, str(HT))
higgsExp[HT] = [exphig[0], exphig[1]]
if opt.verb>1:
procLog.debug("Done SM Higgs bzz")
ddata = str(LTDir + '/LT_'+dataName+'.root')
ddata = ddata.replace("%MASS%", str(point))
theFitter.AddBkgData(ddata)
procLog.info("\t BKG ADDED. Node=%r, GridPoint=%r, type=%r, data file=%s", point,NRgridPoint,t,ddata)
if opt.verb>0: p4 = printTime(p3,start, procLog)
if opt.verb>1:
theFitter.PrintWorkspace();
fitresults = theFitter.BkgModelFit( doBands, addHiggs)
procLog.info("\t BKG FITTED. Node=%r, GridPoint=%r, type=%r", point,NRgridPoint,t)
if opt.verb>0: p5 = printTime(p4,start,procLog)
if fitresults==None:
procLog.error("PROBLEM with fitresults !!")
return __BAD__
if opt.verb>1:
procLog.debug("\n Fit Results: \n\n"+pformat(fitresults.Print()))
wsFileBkgName = "hhbbgg.inputbkg_13TeV"
theFitter.MakeBkgWS( wsFileBkgName);
procLog.info("\t BKG'S WORKSPACE DONE. Node=%r, GridPoint=%r, type=%r", point,NRgridPoint,t)
if opt.verb>0: p6 = printTime(p5,start,procLog)
##do fits for bias study, if needed
procLog.info("\t Making Fits and WS for Bias Study? * %r * Node=%r, GridPoint=%r, type=%r", doBias, point,NRgridPoint,t)
if doBias:
createDir(newFolder+'/bias',procLog)
theFitter.MakeFitsForBias(str(os.getenv("CMSSW_BASE")+'/src/HiggsAnalysis/bbggLimits/'+biasConfig), str(newFolder+'/bias/biasWorkspace.root'))
procLog.info("\t BIAS FITS DONE. Node=%r, GridPoint=%r, type=%r", point,NRgridPoint,t)
if opt.verb>0: p7 = printTime(p6,start,procLog)
# print PID, "IM HERE"
sigExp = []
bkgObs = []
for cc in xrange(NCAT):
sigExp.append(-1)
bkgObs.append(-1)
sigExpStr = ''
bkgObsStr = ''
for cc in xrange(NCAT):
sigExp[cc] = theFitter.GetSigExpectedCats(cc);
if not doBlinding:
bkgObs[cc] = theFitter.GetObservedCats(cc);
sigExpStr += "%f" % sigExp[cc]
bkgObsStr += "%f" % bkgObs[cc]
if cc < NCAT-1:
sigExpStr += ","
bkgObsStr += ","
# print PID, "IM HERE2"
# Make datacards:
myLoc = os.getenv("CMSSW_BASE") + '/src/HiggsAnalysis/bbggLimits/'+newFolder
if isRes==1:
DataCardMaker(str(myLoc), NCAT, sigExpStr, bkgObsStr, isRes)
elif addHiggs == 0:
DataCardMaker(str(myLoc), NCAT, sigExpStr, bkgObsStr, isRes, t)
else:
DataCardMaker_wHiggs(str(myLoc), NCAT, sigExpStr, bkgObsStr, higgsExp, t)
procLog.info("\t DATACARD DONE. Node/Mass=%r, GridPoint=%r, type=%r", point,NRgridPoint,t)
if opt.verb>0: p8 = printTime(p7,start,procLog)
# print PID, "IM HERE3"
# Limits by type:
if doSingleLimit or isRes:
# print PID, "IM HERE4"
if doCombine:
# print PID, "IM HERE5"
if Combinelxbatch:
# print PID, "IM HERE6"
runCombineOnLXBatch(myLoc+"/datacards/", doBlinding, procLog, combineOpt, t+Label)
else:
# print PID, "IM HERE7"
runCombine(newFolder+"/datacards/", doBlinding, procLog, combineOpt, Combinelxbatch, t+Label)
# End of loop over Types
## <-- indent
#Nonresonant data card massaging...
if not isRes:
# Here we merge datacars of all categories (in this case two)
cardsToMerge = ''
for t in signalTypes:
cardsToMerge += baseFolder+'/'+t+Label+'/datacards/hhbbgg_13TeV_DataCard.txt '
newDir = baseFolder+'/CombinedCard'+Label
createDir(newDir,procLog)
combCard = newDir+'/hhbbgg_13TeV_DataCard.txt'
os.system("combineCards.py "+ cardsToMerge + " > " + combCard)
# Now we actually need to fix the combined card
for t in signalTypes:
strReplace = baseFolder+'/'+t+Label+'/datacards/'+os.getenv("CMSSW_BASE")+'/src/HiggsAnalysis/bbggLimits/'
os.system("sed -i 's|"+strReplace+"||g' "+combCard)
procLog.info("String to replace: "+ strReplace)
if opt.analyticalRW:
# Make another datacard, with entries for kt-reweihgting of single Higgs background
ktScaled = newDir+'/kt_scaled_hhbbgg_13TeV_DataCard.txt'
copy2(combCard, ktScaled)
with open(ktScaled, "a") as myfile:
HigScale = opt.ARW_kt*opt.ARW_kt
appendString = '\n \n'
appendString+= 'h_norm_ggh rateParam * ggh %.4f \n' % HigScale
appendString+= 'nuisance edit freeze h_norm_ggh \n'
appendString+= 'h_norm_tth rateParam * tth %.4f \n' % HigScale
appendString+= 'nuisance edit freeze h_norm_tth \n'
myfile.write(appendString)
if doCombine:
if Combinelxbatch:
# print PID, "IM HERE6"
myLoc = os.getenv("CMSSW_BASE") + '/src/HiggsAnalysis/bbggLimits/' + newDir
runCombineOnLXBatch(myLoc+"/", doBlinding, procLog, combineOpt, "CombinedCard"+Label)
else:
for method in [1,2,3]:
# If options 1,2,3 are provided - run the corresponding limits:
# 1 - asymptotic, 2 - asymptotoc with adaptive azimov option; 3 - hybridnew
# If combineOpt==4: run all of them at once
if combineOpt!=4 and method!=combineOpt: continue
try:
combStatus = runCombine(newDir, doBlinding, procLog, method, Combinelxbatch, Label, scaleSingleHiggs)
except:
return __BAD__
procLog.info("\t COMBINE with Option=%r is DONE. Node=%r, GridPoint=%r, type=%r \n \t Status = %r",
method, point,NRgridPoint,t, combStatus)
if combStatus!=0:
procLog.error('Combine failed...')
# return __BAD__
if opt.verb>0: p9 = printTime(p8,start,procLog)
os.remove(pidfile)
# procLog.handlers = []
procLog.info('This process has ended. Label=%r', Label)
return 42