-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinitialize.py
executable file
·340 lines (231 loc) · 9.9 KB
/
initialize.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
#!/usr/bin/env python
# initializes the setup for a new run and dataset
import sys, os
#----------------------------------------------------------------------
def getLocalJsonFname(tasks):
# finds the name of the json file created on the local file system
# from the previous tasks
# get the name of the local json file
for task in tasks:
if isinstance(task, JsonFileCopy):
return task.outputFname
# not found
return None
#----------------------------------------------------------------------
def ensureCmsEnv():
assert os.environ.has_key('CMSSW_BASE'), "CMSSW_BASE environment variable not set, did you run cmsenv ?"
#----------------------------------------------------------------------
def getLumiSectionsForRun(jsonFname, run):
# returns the (expanded) list of lumi sections for the given
# returns an empty list if this run is not found in this json file
import json
lumiSecs = json.load(open(jsonFname))
# get the good lumi sections for the run in question
ranges = lumiSecs.get(str(run),[])
retval = []
for theRange in ranges:
retval.extend(list(range(theRange[0], theRange[1]+1)))
return retval
#----------------------------------------------------------------------
class FileListMaker:
def __init__(self, options, prevTasks, runDir):
sys.path.append(runDir)
self.options = options
self.outputFname = os.path.join(runDir, "file_list_" + options.dstitle + "_" + str(options.run) + ".py")
self.name = 'make_file_list'
#----------------------------------------
def needsRunning(self):
return not os.path.exists(self.outputFname)
#----------------------------------------
def doRun(self):
import makeFileList
makeFileList.ensureDasClientExists()
makeFileList.ensureVomsProxy()
output_data = makeFileList.findFiles(options.rawDataSet, options.recoDataSet, options.run)
makeFileList.printData(output_data, self.outputFname)
print >> sys.stderr,"wrote",self.outputFname
#----------------------------------------------------------------------
class JsonFileCopy:
def __init__(self, options, prevTasks, runDir):
self.origJsonFile = options.jsonFile
self.skip = options.skipJsonCopy
self.name = 'copy_json'
parts = os.path.splitext(os.path.basename(self.origJsonFile))
self.outputFname = os.path.join(runDir, parts[0] + "-" + str(options.run) + parts[1])
#----------------------------------------
def needsRunning(self):
if self.skip:
return False
return not os.path.exists(self.outputFname)
#----------------------------------------
def doRun(self):
status = os.system("scp -p lxplus.cern.ch:" + self.origJsonFile + " " + self.outputFname)
assert(status == 0)
print >> sys.stderr,"wrote",self.outputFname
#----------------------------------------------------------------------
class LumiData:
"""logs into lxplus, runs brilcalc and copies the resulting csv file back to the
host we are running on"""
def __init__(self, options, prevTasks, runDir):
self.name = 'lumi_data'
self.run = options.run
self.output_dir = os.path.join("..","lumi-files")
self.outputFname = os.path.join(self.output_dir,
"lumi-by-bx-and-ls-%d.csv" % self.run)
#----------------------------------------
def needsRunning(self):
return not os.path.exists(self.outputFname)
#----------------------------------------
def doRun(self):
# create output directory if not existing
if not os.path.exists(self.output_dir):
os.makedirs(self.output_dir)
cmd_parts = [
"ssh",
"lxplus.cern.ch",
"\${HOME}/bin/brilcalc",
"lumi",
"-r " + str(self.run),
"--xing",
"-u hz/ub",
"--output-style csv",
]
cmd = " ".join(cmd_parts)
import commands
status, output = commands.getstatusoutput(cmd)
if status != 0:
raise Exception("failed to run command " + cmd)
fout = open(self.outputFname, "w")
fout.write(output)
fout.close()
print >> sys.stderr,"wrote",self.outputFname
#----------------------------------------------------------------------
class Step1ConfigFile:
# creates a config file for step1 from the template
def __init__(self, options, prevTasks, runDir):
self.name = 'step1_config_file'
self.jsonFile = getLocalJsonFname(prevTasks)
assert self.jsonFile is not None
self.outputFname = os.path.join(runDir,
"fedsizeanalyzer-" + str(options.run) + ".py")
self.templateFile = os.path.join(runDir,
"fedsizeanalyzer-template.py")
#----------------------------------------
def needsRunning(self):
return not os.path.exists(self.outputFname)
#----------------------------------------
def doRun(self):
text = open(self.templateFile).read()
output = text.format(text, JSONFILE=os.path.basename(self.jsonFile))
fout = open(self.outputFname, "w")
fout.write(output)
fout.close()
print >> sys.stderr,"wrote",self.outputFname
#----------------------------------------------------------------------
class MakeRunScript:
def __init__(self, options, prevTasks, runDir):
self.name = 'step1_config_file'
self.dstitle = options.dstitle
self.run = options.run
self.jsonFile = getLocalJsonFname(tasks)
assert self.jsonFile is not None
self.outputFname = os.path.join(runDir,
"run-" + str(options.run) + "-" + self.dstitle + ".sh")
self.templateFile = os.path.join(runDir,
"run-template.sh")
#----------------------------------------
def needsRunning(self):
return not os.path.exists(self.outputFname)
#----------------------------------------
def __findLumiSectionRange(self):
# we can only do this after the previous task
# copying the lumi section file is complete
# get the lumi section range for our run
# (for the moment we only care about the minimum and maximum)
lumiSections = getLumiSectionsForRun(self.jsonFile, options.run)
self.firstLs = min(lumiSections)
self.lastLs = max(lumiSections)
#----------------------------------------
def doRun(self):
# get lumi section range
self.__findLumiSectionRange()
text = open(self.templateFile).read()
output = text.format(text,
firstLs = self.firstLs,
lastLs = self.lastLs,
dstitle = self.dstitle,
run = self.run,
jsonFile = self.jsonFile,
)
fout = open(self.outputFname, "w")
fout.write(output)
fout.close()
os.chmod(self.outputFname, 0755)
print >> sys.stderr,"wrote",self.outputFname
#----------------------------------------------------------------------
# main
#----------------------------------------------------------------------
if __name__ == '__main__':
ensureCmsEnv()
from argparse import ArgumentParser, RawTextHelpFormatter
parser = ArgumentParser(
description =
"""
performs setup operations for a new run
""",
formatter_class=RawTextHelpFormatter,
)
parser.add_argument("--run",
type = int,
help = "run to analyze",
required = True,
)
parser.add_argument("--raw",
dest = "rawDataSet",
type = str,
help = "comma separated list of RAW datasets",
required = True,
)
parser.add_argument("--reco",
dest = "recoDataSet",
type = str,
help = "comma separated list of RECO/AOD/MINIAOD datasets",
required = True,
)
parser.add_argument("--dstitle",
type = str,
help = "short name (selected by user) for the dataset to distinguish e.g. multiple analyses on same run",
required = True,
)
parser.add_argument("--json",
type = str,
dest = "jsonFile",
help = "original json file on lxplus for lumi section validation",
required = True,
)
parser.add_argument("--skip-json-copy",
default = False,
action = 'store_true',
dest = 'skipJsonCopy',
help = "skip the json copy file step, assume the json file is already there (useful for hand written json files)",
)
options = parser.parse_args()
#----------------------------------------
options.rawDataSet = options.rawDataSet.split(',')
options.recoDataSet = options.recoDataSet.split(',')
runDir = os.path.join(os.environ["CMSSW_BASE"], "src/FedSizeAnalysis/FedSizeAnalyzer")
tasks = []
for clazz in [
FileListMaker,
JsonFileCopy,
LumiData,
Step1ConfigFile,
MakeRunScript,
]:
tasks.append(clazz(options, tasks, runDir))
for task in tasks:
if not task.needsRunning():
print >> sys.stderr, "skipping",task.name
continue
print >> sys.stderr, "running",task.name
task.doRun()