forked from kit-cn-cms/BoostedAnalyzer_runscripts_NAF
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmakeTreesReady.py
More file actions
482 lines (441 loc) · 16.1 KB
/
makeTreesReady.py
File metadata and controls
482 lines (441 loc) · 16.1 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
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
import sys
from subprocess import call
from datetime import date
import os
import ROOT
import glob
# changed to handle new analyzer where JES trees are doen with the same scripts
if len(sys.argv)<=2:
print "script to manage the BoostedAnalyzer output trees"
print "open every file in the given JobList.txt and reads its contents"
print "it will check if they have a minimal filesize and will hadd them together or rewrite the given JobList.txt"
print "sample combination depends on the direcotries the trees are in"
print "will also merge the cutflows, copy the folder with the JobScripts and create the Yield Tables"
print "usage:"
print "python makeTreesReady.py [-j] UTPUT_DIRECTORY JOBLIST.TXT "
print "-j also handle JES/JER trees if they dont have their own JobScripts"
exit(0)
DONEWJESVARIATION=False
if sys.argv[1]=="-j":
DONEWJESVARIATION=True
outPath = sys.argv[2]
sampleList=sys.argv[3]
else :
outPath = sys.argv[1]
sampleList=sys.argv[2]
checklogs=False
print "Path to skimmed Samples ", outPath
sampleListFile=open(sampleList,"r")
jobFiles=list(sampleListFile)
#print jobFiles
call(["mkdir",outPath+"/addedTrees"])
call(["mkdir",outPath+"/addedTrees/AnalysisConfigs"])
log=open(outPath+"/addedTrees/AnalysisConfigs/AnalysisLog.txt","w")
samples=[]
sampleFiles=[]
for job in jobFiles:
#print job
filename=job.rsplit("\n",1)[0]
#print repr(filename)
filef=open(filename,"r")
lines=list(filef)
#print lines
for line in lines:
if "FILE_NAMES" in line:
bufferf=line.split("\"",1)[1]
bufferf=bufferf.rsplit("\"",1)[0]
bufferf=bufferf.split(" ")[0]
rawSamplePath=bufferf.rsplit("/",1)[0]
#print rawSamplePath
if "OUTFILE_NAME" in line:
bufferf=line.split("\"",1)[1]
bufferf=bufferf.rsplit("\"",1)[0]
outSamplePath=bufferf.rsplit("/",1)[0]
outSampleFile=bufferf
#print outSamplePath
sampleAlreadyExists=False
for s in samples:
if outSamplePath==s[0]:
sampleAlreadyExists=True
if rawSamplePath not in s:
s.append(rawSamplePath)
if sampleAlreadyExists==False:
samples.append([outSamplePath,rawSamplePath])
sampleAlreadyExists=False
for s in sampleFiles:
if outSamplePath==s[0]:
sampleAlreadyExists=True
if outSampleFile not in s:
s.append(outSampleFile)
if DONEWJESVARIATION:
s.append(outSampleFile.replace("nominal","JESUP"))
s.append(outSampleFile.replace("nominal","JESDOWN"))
s.append(outSampleFile.replace("nominal","JERUP"))
s.append(outSampleFile.replace("nominal","JERDOWN"))
if sampleAlreadyExists==False:
if DONEWJESVARIATION:
sampleFiles.append([outSamplePath,outSampleFile,outSampleFile.replace("nominal","JESUP"),outSampleFile.replace("nominal","JESDOWN"),outSampleFile.replace("nominal","JERUP"),outSampleFile.replace("nominal","JERDOWN")])
else:
sampleFiles.append([outSamplePath,outSampleFile])
filef.close()
#print samples
#print "individual files:"
print sampleFiles
#exit(0)
sampleListFile.close()
log.write("Path to Samples "+outPath+"\n")
log.write("produced on "+str(date.today())+"\n\n")
log.write("Samples and theirs SubSamples\n\n")
for s in samples:
log.write(s[0]+"\n")
for ss in s[1:]:
log.write(ss+"\n")
log.write("\n")
#copy the scripts to make the the trees and the sampleList.txt to the output folder
call(["cp","-r","output",outPath+"/addedTrees/JObScripts"])
print "copied the job scripts"
call(["cp",sampleList,sampleList+"_original"])
# check each file for size and existence
everythingAllright=True
newSampleListFile=open("output/sampleListe_Problems.txt","w")
ROOT.gErrorIgnoreLevel=ROOT.kError
JobsToRedo=[]
for s in sampleFiles:
#print s[0]
for f in s[1:]:
thisJobisOK=False
thisJobFile="NOTFOUND"
InitialEventsFromCutflow=-1
FinalEventsFromCutflow=-1
nEventsFromInputFiles=0
nEventsFromOutputTree=0
size=0
sizecutflow=0
ffexists=False
cffexists=False
treeIsGood=False
logFilehasTreeWritten=False
logFileIsGood=True
thisJobInputFiles=[]
tmpfile=open("tmp.txt","w")
ff = f+"_Tree.root"
print "checking ", ff
cff=ff.replace("Tree.root","Cutflow.txt")
call(["du",ff],stdout=tmpfile,stderr=tmpfile)
tmpfile.close()
tmpfile=open("tmp.txt","r")
bufferf=list(tmpfile)
firstpart=bufferf[0].split("\t")[0]
tmpfile.close()
if firstpart.find("cannot access")==-1:
size=int(firstpart)
tmpfile=open("tmp.txt","w")
call(["du",cff],stdout=tmpfile,stderr=tmpfile)
tmpfile.close()
tmpfile=open("tmp.txt","r")
bufferf=list(tmpfile)
firstpart=bufferf[0].split("\t")[0]
tmpfile.close()
if firstpart.find("cannot access")==-1:
sizecutflow=int(firstpart)
# check existence and get number of events from cutflow and output tree
ffexists=os.path.exists(ff)
cffexists=os.path.exists(cff)
if cffexists and sizecutflow>1:
thisCutflowFile=open(cff,"r")
cfflines=list(thisCutflowFile)
for cffline in cfflines:
if "all" in cffline:
InitialEventsFromCutflow=int(cffline.replace(" ","").split(":")[2])
FinalEventsFromCutflow=int(cfflines[len(cfflines)-1].replace(" ","").split(":")[2])
#print InitialEventsFromCutflow, FinalEventsFromCutflow
ftf=ROOT.TFile(ff,"READ")
ftree=ftf.Get("MVATree")
if ftree!=None:
nEventsFromOutputTree=ftree.GetEntries()
treeIsGood=True
ftf.Close()
#print nEventsFromOutputTree
#find jobfile corresponding to output tree
for job in jobFiles:
foundjobfile=False
filename=job.rsplit("\n",1)[0]
filef=open(filename,"r")
lines=list(filef)
for l in lines:
if l.find(f)>=0 or l.find(f.replace("JESUP","nominal"))>=0 or l.find(f.replace("JESDOWN","nominal"))>=0 or l.find(f.replace("JERUP","nominal"))>=0 or l.find(f.replace("JERDOWN","nominal"))>=0:
print "corresponing job file : "+filename
thisJobFile=filename
for ll in lines:
if "FILE_NAMES" in ll:
bufferf=ll.split("\"",1)[1]
bufferf=bufferf.rsplit("\"",1)[0]
thisJobInputFiles=bufferf.split(" ")
#print thisJobInputFiles
foundjobfile=True
break
filef.close()
if foundjobfile:
break
#get total number of input events from input trees
for thisinfile in thisJobInputFiles:
#print thisinfile
if thisinfile=="" or thisinfile==" ":
continue
tf=ROOT.TFile(thisinfile,"READ")
tree=tf.Get("Events")
nEventsFromInputFiles+=tree.GetEntries()
tf.Close()
#print nEventsFromInputFiles
# check the logfiles
# might want to not do this in case people dont delete the old logfiles first
if checklogs:
logFiles=glob.glob(thisJobFile.replace("output","logs").replace(".sh","")+".*")
#print logFiles
for logf in logFiles:
thisLogFile=open(logf,"r")
thisLogFileLines=list(thisLogFile)
for thisLogFileline in thisLogFileLines:
if "Tree Written" in thisLogFileline:
logFilehasTreeWritten=True
logFileIsGood=logFilehasTreeWritten
thisJobisOK = size>100 and sizecutflow>1 and ffexists and cffexists and treeIsGood and InitialEventsFromCutflow==nEventsFromInputFiles and FinalEventsFromCutflow==nEventsFromOutputTree and logFileIsGood
print size>100 , sizecutflow>1 , ffexists , cffexists , treeIsGood , InitialEventsFromCutflow==nEventsFromInputFiles , FinalEventsFromCutflow==nEventsFromOutputTree , logFileIsGood
print InitialEventsFromCutflow, nEventsFromInputFiles , FinalEventsFromCutflow, nEventsFromOutputTree
if thisJobisOK==False:
print "something wrong with "+ff
everythingAllright=False
print "corresponing job file :"+thisJobFile
if thisJobFile not in JobsToRedo:
JobsToRedo.append(thisJobFile)
print JobsToRedo
for j in JobsToRedo:
newSampleListFile.write(j+"\n")
newSampleListFile.close()
if everythingAllright==False:
print "rewrote sampleList.txt -> Ready to do runProblems.sh"
exit(0)
if everythingAllright==True:
print "all files seem to be ok -> adding trees"
log.write("hadded Trees and merged Cutflows\n\n")
AllCutflows_nominal=[]
AllCutflows_JESUP=[]
AllCutflows_JESDOWN=[]
MergedCutflows_JESUP=[]
MergedCutflows_JESDOWN=[]
AllCutflows_JERUP=[]
AllCutflows_JERDOWN=[]
MergedCutflows_JERUP=[]
MergedCutflows_JERDOWN=[]
MergedCutflows_nominal=[]
print sampleFiles
for s in sampleFiles:
name=s[0].rsplit("/",1)[1]
addedPath=s[0].rsplit("/",1)[0]
trees_nominal=[]
trees_JESUP=[]
trees_JESDOWN=[]
trees_JERUP=[]
trees_JERDOWN=[]
cutflows_nominal=[]
cutflows_JESUP=[]
cutflows_JESDOWN=[]
cutflows_JERUP=[]
cutflows_JERDOWN=[]
subSamplecutflows_nominal=[]
subSamplecutflows_JESUP=[]
subSamplecutflows_JESDOWN=[]
subSamplecutflows_JERUP=[]
subSamplecutflows_JERDOWN=[]
subSamples=[]
for f in s[1:]:
ff = f+"_Tree.root"
if ff.find("nominal")>=0:
trees_nominal.append(ff)
if ff.find("JESUP")>=0:
trees_JESUP.append(ff)
if ff.find("JESDOWN")>=0:
trees_JESDOWN.append(ff)
if ff.find("JERUP")>=0:
trees_JERUP.append(ff)
if ff.find("JERDOWN")>=0:
trees_JERDOWN.append(ff)
cff=f+"_Cutflow.txt"
sf=f.rsplit("_",1)[0]
#print sf
if sf not in subSamples:
subSamples.append(sf)
if cff.find("nominal")>=0:
subSamplecutflows_nominal.append([sf])
if cff.find("JESUP")>=0:
subSamplecutflows_JESUP.append([sf])
if cff.find("JESDOWN")>=0:
subSamplecutflows_JESDOWN.append([sf])
if cff.find("JERUP")>=0:
subSamplecutflows_JERUP.append([sf])
if cff.find("JERDOWN")>=0:
subSamplecutflows_JERDOWN.append([sf])
#print "subsamples", subSamples, "\n"
#print "subsample cutflows", subSamplecutflows_nominal, "\n"
if cff.find("nominal")>=0:
cutflows_nominal.append(cff)
for subsample in subSamplecutflows_nominal:
if sf==subsample[0]:
subsample.append(cff)
if cff.find("JESUP")>=0:
cutflows_JESUP.append(cff)
for subsample in subSamplecutflows_JESUP:
if sf==subsample[0]:
subsample.append(cff)
if cff.find("JESDOWN")>=0:
cutflows_JESDOWN.append(cff)
for subsample in subSamplecutflows_JESDOWN:
if sf==subsample[0]:
subsample.append(cff)
if cff.find("JERUP")>=0:
cutflows_JERUP.append(cff)
for subsample in subSamplecutflows_JERUP:
if sf==subsample[0]:
subsample.append(cff)
if cff.find("JERDOWN")>=0:
cutflows_JERDOWN.append(cff)
for subsample in subSamplecutflows_JERDOWN:
if sf==subsample[0]:
subsample.append(cff)
if len(trees_nominal)>0:
an=addedPath+"/addedTrees/"+name+"_nominal.root"
mn=addedPath+"/addedTrees/"+name+"_nominal_Cutflow.txt"
call(["rm","-f",an])
call(["hadd",an]+trees_nominal)
call(["python","merge_cutflow.py",mn]+cutflows_nominal)
log.write(an+"\n")
if len(subSamplecutflows_nominal)>1:
for subsample in subSamplecutflows_nominal:
ssn=subsample[0].rsplit("/",1)[1]
#print ssn
smn=addedPath+"/addedTrees/"+ssn+"_Cutflow.txt"
sscf=subsample[1:]
call(["python","merge_cutflow.py",smn]+sscf)
log.write(smn+"\n")
print "merged cutflows for ", smn
AllCutflows_nominal.append(smn)
else:
AllCutflows_nominal.append(mn)
MergedCutflows_nominal.append(mn)
log.write(mn+"\n")
if len(trees_JESUP)>0:
an=addedPath+"/addedTrees/"+name+"_JESUP.root"
mn=addedPath+"/addedTrees/"+name+"_JESUP_Cutflow.txt"
call(["rm","-f",an])
call(["hadd",an]+trees_JESUP)
call(["python","merge_cutflow.py",mn]+cutflows_JESUP)
log.write(an+"\n")
if len(subSamplecutflows_JESUP)>1:
for subsample in subSamplecutflows_JESUP:
ssn=subsample[0].rsplit("/",1)[1]
#print ssn
smn=addedPath+"/addedTrees/"+ssn+"_Cutflow.txt"
call(["python","merge_cutflow.py",smn]+subsample[1:])
log.write(smn+"\n")
AllCutflows_JESUP.append(smn)
else:
AllCutflows_JESUP.append(mn)
MergedCutflows_JESUP.append(mn)
log.write(mn+"\n")
if len(trees_JESDOWN)>0:
an=addedPath+"/addedTrees/"+name+"_JESDOWN.root"
mn=addedPath+"/addedTrees/"+name+"_JESDOWN_Cutflow.txt"
call(["rm","-f",an])
call(["hadd",an]+trees_JESDOWN)
call(["python","merge_cutflow.py",mn]+cutflows_JESDOWN)
log.write(an+"\n")
if len(subSamplecutflows_JESDOWN)>1:
for subsample in subSamplecutflows_JESDOWN:
ssn=subsample[0].rsplit("/",1)[1]
#print ssn
smn=addedPath+"/addedTrees/"+ssn+"_Cutflow.txt"
call(["python","merge_cutflow.py",smn]+subsample[1:])
log.write(smn+"\n")
AllCutflows_JESDOWN.append(smn)
else:
AllCutflows_JESDOWN.append(mn)
MergedCutflows_JESDOWN.append(mn)
log.write(mn+"\n")
if len(trees_JERUP)>0:
an=addedPath+"/addedTrees/"+name+"_JERUP.root"
mn=addedPath+"/addedTrees/"+name+"_JERUP_Cutflow.txt"
call(["rm","-f",an])
call(["hadd",an]+trees_JERUP)
call(["python","merge_cutflow.py",mn]+cutflows_JERUP)
log.write(an+"\n")
if len(subSamplecutflows_JERUP)>1:
for subsample in subSamplecutflows_JERUP:
ssn=subsample[0].rsplit("/",1)[1]
#print ssn
smn=addedPath+"/addedTrees/"+ssn+"_Cutflow.txt"
call(["python","merge_cutflow.py",smn]+subsample[1:])
log.write(smn+"\n")
AllCutflows_JERUP.append(smn)
else:
AllCutflows_JERUP.append(mn)
MergedCutflows_JERUP.append(mn)
log.write(mn+"\n")
if len(trees_JERDOWN)>0:
an=addedPath+"/addedTrees/"+name+"_JERDOWN.root"
mn=addedPath+"/addedTrees/"+name+"_JERDOWN_Cutflow.txt"
call(["rm","-f",an])
call(["hadd",an]+trees_JERDOWN)
call(["python","merge_cutflow.py",mn]+cutflows_JERDOWN)
log.write(an+"\n")
if len(subSamplecutflows_JERDOWN)>1:
for subsample in subSamplecutflows_JERDOWN:
ssn=subsample[0].rsplit("/",1)[1]
#print ssn
smn=addedPath+"/addedTrees/"+ssn+"_Cutflow.txt"
call(["python","merge_cutflow.py",smn]+subsample[1:])
log.write(smn+"\n")
AllCutflows_JERDOWN.append(smn)
else:
AllCutflows_JERDOWN.append(mn)
MergedCutflows_JERDOWN.append(mn)
log.write(mn+"\n")
log.write("\n")
print "added trees and merged the cutflows"
yieldpaths=outPath+"/addedTrees/CutflowTables/"
call(["mkdir",yieldpaths])
# doint the yield tables
if len(AllCutflows_nominal)>0:
on=yieldpaths+"SubSamples_nominal"
call(["python","makeCutflowTables.py",on]+AllCutflows_nominal)
if len(AllCutflows_JESUP)>0:
on=yieldpaths+"SubSamples_JESUP"
call(["python","makeCutflowTables.py",on]+AllCutflows_JESUP)
if len(AllCutflows_JESDOWN)>0:
on=yieldpaths+"SubSamples_JESDOWN"
call(["python","makeCutflowTables.py",on]+AllCutflows_JESDOWN)
if len(AllCutflows_JERUP)>0:
on=yieldpaths+"SubSamples_JERUP"
call(["python","makeCutflowTables.py",on]+AllCutflows_JERUP)
if len(AllCutflows_JERDOWN)>0:
on=yieldpaths+"SubSamples_JERDOWN"
call(["python","makeCutflowTables.py",on]+AllCutflows_JERDOWN)
if len(MergedCutflows_nominal)>0:
on=yieldpaths+"Merged_nominal"
call(["python","makeCutflowTables.py",on]+MergedCutflows_nominal)
if len(MergedCutflows_JESUP)>0:
on=yieldpaths+"Merged_JESUP"
call(["python","makeCutflowTables.py",on]+MergedCutflows_JESUP)
if len(MergedCutflows_JESDOWN)>0:
on=yieldpaths+"Merged_JESDOWN"
call(["python","makeCutflowTables.py",on]+MergedCutflows_JESDOWN)
if len(MergedCutflows_JERUP)>0:
on=yieldpaths+"Merged_JERUP"
call(["python","makeCutflowTables.py",on]+MergedCutflows_JERUP)
if len(MergedCutflows_JERDOWN)>0:
on=yieldpaths+"Merged_JERDOWN"
call(["python","makeCutflowTables.py",on]+MergedCutflows_JERDOWN)
log.close()
#finally collect BoostedTTH and MiniAODHelper Software and put them in a tarball
call(["tar","-a","-cf",outPath+"/addedTrees/AnalysisConfigs/CMSSW.tar.gz","/afs/desy.de/user/k/kelmorab/CMSSW_7_2_3/src/BoostedTTH","/afs/desy.de/user/k/kelmorab/CMSSW_7_2_3/src/MiniAOD"])
call(["tar","-a","-cf",outPath+"/addedTrees/AnalysisConfigs/runscripts.tar.gz","/afs/desy.de/user/k/kelmorab/runNew"])
print "created tarball with used software"