-
Notifications
You must be signed in to change notification settings - Fork 0
/
AnalysisManager.py
159 lines (144 loc) · 5.57 KB
/
AnalysisManager.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
import ROOT
from array import array
from subprocess import call
from QueHelper import QueHelper
import time as timer
import sys
import json
import Definitions
import os
import Sample
from subprocess import check_output
import stat
import glob
class AnalysisManager:
def __init__(self,currentPath):
self.Path=currentPath
self.Verbose=True
self.Categories=Definitions.Categories
self.OutputDirectoryForPreparedTrees=Definitions.OutputDirectoryForPreparedTrees
self.MCSamplesDefinitions=Definitions.MCSamples
#self.DATASamplesDefinitions=Definitions.DataSamples
self.MCSamples=[]
self.DataSamples=[]
self.MCinputDirectory=Definitions.MCinputDirectory
#self.DATAinputDirectory=Definitions.DATAinputDirectory
self.MaxEventsPerTree=Definitions.MaxEventsPerTree
self.JobIDList=[]
self.JobScriptList=[]
self.QueHelper=""
self.SystematicTreeNames=Definitions.SystematicTreeNames
def CreateOutTreePaths(self):
if not os.path.exists(self.OutputDirectoryForPreparedTrees):
os.makedirs(self.OutputDirectoryForPreparedTrees)
for cat in self.Categories:
catname=cat[0]
if not os.path.exists(self.OutputDirectoryForPreparedTrees+'/Category_'+catname):
os.makedirs(self.OutputDirectoryForPreparedTrees+'/Category_'+catname)
#for split in ["Even","Odd"]:
#if not os.path.exists(self.OutputDirectoryForPreparedTrees+'/Category_'+catname+'/'+split):
#os.makedirs(self.OutputDirectoryForPreparedTrees+'/Category_'+catname+'/'+split)
print "created directories for prepared Trees"
def SetUpSamples(self):
for mc in self.MCSamplesDefinitions:
newsample=Sample.Sample()
newsample.ProcessName=mc[0]
newsample.SplitMode=mc[2]
newsample.UseFlavorSplitting=mc[3]
newsample.RateSystematics=mc[4]
newsample.ShapeSystematics=mc[5]
for ss in mc[1]:
inpath=self.MCinputDirectory+"/"+ss
#print ss, inpath
#indir=check_output(["dir","-1",inpath]).splitlines()
#for rf in indir:
#if ".root" in rf and "nominal" in rf:
#newsample.ListOfRawInputTrees.append(inpath+"/"+rf)
newsample.ListOfRawInputTrees+=glob.glob(inpath)
print newsample.ListOfRawInputTrees
#raw_input()
self.MCSamples.append(newsample)
print "sat up intrees"
def SetQueHelper(self,quesystem):
self.QueHelper=QueHelper(quesystem)
def PrepareTrees(self):
self.CompileAndSetupClientExecutable()
mainrunline=self.QueHelper.GetRunLines()
runlines=[]
if not os.path.exists("PreparationScripts"):
os.makedirs("PreparationScripts")
testcount=0
for sample in self.MCSamples:
#print sample.ProcessName
#print sample.ListOfRawInputTrees
for i,f in enumerate(sample.ListOfRawInputTrees):
for syss in self.SystematicTreeNames:
if syss in f:
thisSys=syss
#testcount+=1
#if testcount>3:
# break
outname=sample.ProcessName
joblines=[]
#print joblines
jj=self.QueHelper.GetExecLines()
for jjj in jj:
joblines.append(jjj)
#print joblines
joblines.append("cd "+self.Path+"/PreparationScripts\n")
joblines.append("export myCONFIGFILE=PrepConfig_"+sample.ProcessName+"_"+str(i)+".txt\n")
joblines.append("./Splitter")
#print joblines
outfile=open("PreparationScripts/"+sample.ProcessName+"_"+str(i)+".sh","w")
for line in joblines:
outfile.write(line)
outfile.close()
st = os.stat("PreparationScripts/"+sample.ProcessName+"_"+str(i)+".sh")
os.chmod("PreparationScripts/"+sample.ProcessName+"_"+str(i)+".sh", st.st_mode | stat.S_IEXEC)
configlines=[]
configlines.append("INNAME "+f+"\n")
configlines.append("OUTNAME "+sample.ProcessName+"_"+str(i)+"\n")
configlines.append("OUTDIR "+self.OutputDirectoryForPreparedTrees+"\n")
for cat in self.Categories:
configlines.append("CATEGORY "+cat[0]+" "+cat[1]+"\n")
configlines.append("SPLITMODE "+sample.SplitMode+"\n")
configlines.append("FLAVORSPLITTING "+sample.UseFlavorSplitting+"\n")
configlines.append("SYSTEMATIC "+thisSys+"\n")
configlines.append("MAXEVENTSPERTREE "+str(self.MaxEventsPerTree)+"\n")
conf=open(self.Path+"/PreparationScripts/PrepConfig_"+sample.ProcessName+"_"+str(i)+".txt","w")
for ll in configlines:
conf.write(ll)
conf.close()
runlines=[]
thisrl=mainrunline[0]
runlines.append(thisrl)
#print runlines[-1]
runlines[-1]=runlines[-1].replace("INSERTPATHHERE",self.Path)
runlines[-1]=runlines[-1].replace("INSERTEXECSCRIPTHERE","PreparationScripts/"+sample.ProcessName+"_"+str(i)+".sh")
runfile=open("runPrep.sh","w")
for rl in runlines:
runfile.write(rl)
runfile.close()
st = os.stat("runPrep.sh")
os.chmod("runPrep.sh", st.st_mode | stat.S_IEXEC)
thisID=self.QueHelper.StartJob("./runPrep.sh")
self.JobIDList.append(thisID)
print "submitted ", "PreparationScripts/"+sample.ProcessName+"_"+str(i)+".sh"
#now check the que until its finished
JobsStillRunning=True
nJobsStillRunning=len(self.JobIDList)
while(JobsStillRunning):
timer.sleep(30)
nJobsStillRunning=0
for job in self.JobIDList:
if self.QueHelper.GetIsJobRunning(job):
nJobsStillRunning+=1
if nJobsStillRunning>0:
JobsStillRunning=True
else:
JobsStillRunning=False
print nJobsStillRunning, " jobs still running"
def CompileAndSetupClientExecutable(self):
print "Freshly Compiling Splitter.C"
call(["g++ -o PreparationScripts/Splitter Splitter.C `root-config --cflags --glibs` -lTMVA -lTreePlayer"],shell=True)
print "done"