-
Notifications
You must be signed in to change notification settings - Fork 0
/
crabInfo.py
executable file
·259 lines (230 loc) · 10.8 KB
/
crabInfo.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
#!/usr/bin/env python2
import re
import getpass
import os, subprocess as sp
from CRABClient.ClientUtilities import colors
import mergeTier2Files
def myMatch( regex, string ):
m = re.match( regex, string )
return [ m ] if m else []
def modifyDatasetName( dataset ):
# shorten dataset name
deletes = [
"_13TeV",
"-madgraph",
"_Tune4C",
"-tauola",
"_MSDecaysCKM_central",
"_TuneCUETP8M1",
"-amcatnloFXFX",
"-madspin",
"-pythia8",
"MLM",
"_pythia8",
"_Pythia8",
"-powheg",
"_TuneCUETP8M2T4"
]
if dataset.startswith("WGToLNuG_") or dataset.startswith("TTJets_Tune") or dataset.startswith("WJetsToLNu_") or dataset.startswith("DYJetsToLL_"):
# to be able to seperate these two datasets
deletes.remove( "-amcatnloFXFX" )
deletes.remove( "-madgraph" )
deletes.remove( "MLM" )
for d in deletes:
dataset = dataset.replace( d, "" )
return dataset
class CrabInfo:
def __init__( self, infoString ):
self.user=getpass.getuser()
if infoString.endswith("/crab.log"):
self.initFromLog( infoString )
elif "/pnfs/physik.rwth-aachen.de/cms/store" in infoString:
self.initFromSrm( infoString )
def initFromLog( self, logFileName ):
self.logFileDir = logFileName.replace("/crab.log","")
with open( logFileName ) as f:
lines = f.readlines()
for line in lines:
for m in myMatch("config.Data.outLFNDirBase = '(.*)'", line ):
self.outLFNDirBase = m.group(1)
for m in myMatch( ".*Task name:.*\s([\d_]+):.*_crab_(.*)", line ):
self.time = m.group(1)
for m in myMatch( "config.Data.outputDatasetTag = '(.*)'", line ):
self.outputDatasetTag = m.group(1)
for m in myMatch( "config.Data.inputDataset = '(.*)'", line ):
self.inputDataset = m.group(1)
self.datasetName, self.datasetMiddle, self.datasetType = self.inputDataset.split("/")[1:]
for m in myMatch( ".*Got information from status cache file: (.*)", line ):
self.details = eval( m.group(1) )
for m in myMatch( ".*Status on the CRAB server:(.*)", line ):
self.statusCRAB = m.group(1).strip()
for m in myMatch( ".*Status on the scheduler:(.*)", line ):
self.statusScheduler = m.group(1).strip()
if self.statusCRAB != "SUBMITFAILED":
self.nJobs = len(self.details)
self.jobStates = [x["State"] for x in self.details.values()]
else:
self.statusScheduler="Not available since submit failed"
def initFromSrm( self, srmPath ):
self.srmPath = srmPath
srmPathSplitted = srmPath.split("/")
self.time = srmPathSplitted[-2]
self.outputDatasetTag = srmPathSplitted[-3]
self.datasetName = srmPathSplitted[-4]
self.datasetMiddle="" # cannot easily find this, but crashes when undefined
def getOutFileName( self ):
#~ modifiedDatasetName = modifyDatasetName( self.datasetName )
modifiedDatasetName = self.datasetName
if "ext1" in self.datasetMiddle: modifiedDatasetName += "_ext"
elif "ext2" in self.datasetMiddle: modifiedDatasetName += "_ext2"
elif "backup" in self.datasetMiddle: modifiedDatasetName += "_backup"
if self.user == "kiesel":
if hasattr(self, "datasetType"):
if self.datasetType == "MINIAOD": # data
modifiedDatasetName += "_"+self.datasetMiddle
baseDir = "/user/kiesel/nTuples/"
baseDir = "/net/scratch_cms1b2/cms/user/kiesel/"
baseDir = os.path.join(baseDir, self.outputDatasetTag)
if not os.path.isdir(baseDir): os.mkdir(baseDir)
return os.path.join(baseDir, modifiedDatasetName+"_nTuple.root")
elif self.user == "jschulz":
if "Run2015" in self.datasetMiddle: # distinguish different data runs/recos
modifiedDatasetName+="_"+self.datasetMiddle
elif "Run2016" in self.datasetMiddle: # distinguish different data runs/recos
modifiedDatasetName+="_"+self.datasetMiddle
elif "T5gg" in self.datasetName: # extract mass
m=re.search(".*_mGluino-(.*)_mNeutralino-(.*)-.*",self.datasetMiddle)
if m and len(m.groups())==2:
modifiedDatasetName+="_g%s_n%s"%m.groups()
else:
modifiedDatasetName="UNKOWNPATTERN"
return "/user/jschulz/2016/data/run2/dl/{}.root".format(modifiedDatasetName)
elif self.user == "swuchterl":
if hasattr(self, "datasetType"):
if self.datasetType == "MINIAOD": # data
modifiedDatasetName += "_"+self.datasetMiddle
baseDir = "/user/swuchterl/nTuples/"
baseDir = "/net/scratch_cms1b2/cms/user/swuchterl/"
baseDir = os.path.join(baseDir, self.outputDatasetTag)
if not os.path.isdir(baseDir): os.mkdir(baseDir)
return os.path.join(baseDir, modifiedDatasetName+"_nTuple.root")
elif self.user == "dmeuser":
if "Run2015" in self.datasetMiddle: # distinguish different data runs/recos
modifiedDatasetName+="_"+self.datasetMiddle
elif "Run2016" in self.datasetMiddle: # distinguish different data runs/recos
modifiedDatasetName+="_"+self.datasetMiddle
elif "T5gg" in self.datasetName: # extract mass
m=re.search(".*_mGluino-(.*)_mNeutralino-(.*)-.*",self.datasetMiddle)
if m and len(m.groups())==2:
modifiedDatasetName+="_g%s_n%s"%m.groups()
else:
modifiedDatasetName="UNKOWNPATTERN"
if "RunIISummer16" in self.datasetMiddle:
return "/net/data_cms1b/user/dmeuser/top_analysis/2016/v05/{}.root".format(modifiedDatasetName)
elif "Run2017" in self.datasetMiddle:
return "/net/data_cms1b/user/dmeuser/top_analysis/2017/v02/{}.root".format(modifiedDatasetName)
elif "Run2018" in self.datasetMiddle:
return "/net/data_cms1b/user/dmeuser/top_analysis/2018/v02/{}.root".format(modifiedDatasetName)
return "outputFile.root"
def getSrmPath( self ):
if not hasattr(self, 'srmPath'):
self.srmPath = "/pnfs/physik.rwth-aachen.de/cms{}/{}/{}/{}/".format(self.outLFNDirBase,self.datasetName,self.outputDatasetTag,self.time)
return self.srmPath
def getSrmPathFull( self ):
return "srm://grid-srm.physik.rwth-aachen.de:8443/srm/managerv2?SFN="+self.getSrmPath()
def jobSummary( self ):
jobsPerStatus = dict([(x,self.jobStates.count(x)) for x in list(set(self.jobStates))])
for k,v in jobsPerStatus.iteritems():
if not v: continue
c = colors.NORMAL
if k == "failed": c = colors.RED
# if k == "running": c = colors.GREEN
if k == "transferring": c = colors.BLUE
if k == "finished": c = colors.GREEN
print "{}\t{}{} \t{:.1%} ({}{:3}{}/{:3})".format(c,k,colors.NORMAL,1.*v/self.nJobs,c,v,colors.NORMAL,self.nJobs)
"""# TODO: nice error output
for j, d in self.details["jobs"].iteritems():
if d["State"] == "failed":
for i in d["Error"]:
try:
if "code" in i: print i
except: pass
"""
def completed(self):
return self.statusScheduler == "COMPLETED"
def getMergeCommand( self ):
outFile = self.getOutFileName()
srmSrc = self.getSrmPathFull()
return "./mergeTier2Files.py {} {}".format(outFile,srmSrc)
def suggestMergeCommand(self):
doneDir = self.doneDir()
print "Suggested merge command:"
print " ",self.getMergeCommand()
print " mv {} {}".format( self.logFileDir, doneDir)
def doneDir(self):
""" Where the directory is moved when files have been downloaded
"""
doneDir=self.logFileDir.replace("/crab_","/.{}_crab_".format(self.time))
if self.user=="jschulz":
origDir = self.logFileDir
if origDir.endswith('/'): origDir=origDir[:-1]
doneDir=origDir.split('/')[-1]
doneDir = doneDir.replace("crab_","{}_crab_".format(self.outputDatasetTag))
doneDir+="_"+self.time
doneDir='/'.join(origDir.split('/')[:-1])+"/"+doneDir
return doneDir
def killedDir(self):
""" Where the directory is moved when files have been killed
"""
killedDir=self.logFileDir.replace("/crab_","/.killed/.{}_crab_".format(self.time))
return killedDir
def beautifyCrabStatus(self):
print self.logFileDir
if self.completed():
print "{}COMPLETED!{}".format(colors.GREEN+colors.BOLD,colors.NORMAL)
elif self.statusCRAB== "SUBMITFAILED":
print colors.BOLD+colors.RED,
print self.statusCRAB+colors.NORMAL
else:
if self.statusScheduler=="RESUBMITFAILED": print colors.BOLD+colors.RED,
print self.statusScheduler+colors.NORMAL
self.jobSummary()
def moveCompleted(self):
""" move completed directory
"""
doneDir=self.doneDir()
print "Moving crab directory to",doneDir
os.rename(self.logFileDir, doneDir)
def moveKilled(self):
""" move killed directory
"""
killedDir=self.killedDir()
print "Moving crab directory to",killedDir
os.rename(self.logFileDir, killedDir)
def download(self, downloadFirst=False):
"""
automatically download files belonging to the crab directory
and rename the crab directory
"""
print "Downloading to",self.getOutFileName()
# change library path to cmssw default
cmssw=os.environ['CMSSW_BASE']+"/src"
cmsenv="eval `scramv1 runtime -sh`"
crabLibPath=os.environ['LD_LIBRARY_PATH']
cmsswLibPath=sp.check_output("cd "+cmssw+";"+cmsenv+"; echo $LD_LIBRARY_PATH",shell=True)
os.environ['LD_LIBRARY_PATH']=cmsswLibPath
sucess = mergeTier2Files.mergeTier2Files( self.getOutFileName(), self.getSrmPathFull(), downloadFirst=downloadFirst )
# restore crabs library path
os.environ['LD_LIBRARY_PATH']=crabLibPath
if sucess: self.moveCompleted()
return sucess
if __name__ == '__main__':
import sys
if len(sys.argv)<2:
print "specify crab directory"
exit(0)
for dir in sys.argv[1:]:
info = CrabInfo( dir+"/crab.log" )
info.beautifyCrabStatus(False)
print "Tier2 output path:\n ",info.getSrmPathFull()
print "Download command:\n ",info.getMergeCommand()