forked from CMSCompOps/WmAgentScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbsTest.py~
484 lines (444 loc) · 18.3 KB
/
dbsTest.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
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
479
480
481
482
483
#!/usr/bin/env python
import urllib2,urllib, httplib, sys, re, os, json, phedexSubscription
from xml.dom.minidom import getDOMImplementation
from das_client import get_data
#das_host='https://das.cern.ch'
#das_host='https://cmsweb.cern.ch'
das_host='https://cmsweb-testbed.cern.ch'
#das_host='https://das-dbs3.cern.ch'
def getWorkflowType(url, workflow):
conn = httplib.HTTPSConnection(url, cert_file = os.getenv('X509_USER_PROXY'), key_file = os.getenv('X509_USER_PROXY'))
r1=conn.request("GET",'/reqmgr/reqMgr/request?requestName='+workflow)
r2=conn.getresponse()
request = json.loads(r2.read())
requestType=request['RequestType']
return requestType
def getRunWhitelist(url, workflow):
conn = httplib.HTTPSConnection(url, cert_file = os.getenv('X509_USER_PROXY'), key_file = os.getenv('X509_USER_PROXY'))
r1=conn.request("GET",'/reqmgr/reqMgr/request?requestName='+workflow)
r2=conn.getresponse()
request = json.loads(r2.read())
runWhitelist=request['RunWhitelist']
return runWhitelist
def getBlockWhitelist(url, workflow):
conn = httplib.HTTPSConnection(url, cert_file = os.getenv('X509_USER_PROXY'), key_file = os.getenv('X509_USER_PROXY'))
r1=conn.request("GET",'/reqmgr/reqMgr/request?requestName='+workflow)
r2=conn.getresponse()
request = json.loads(r2.read())
BlockWhitelist=request['BlockWhitelist']
return BlockWhitelist
def getInputDataSet(url, workflow):
conn = httplib.HTTPSConnection(url, cert_file = os.getenv('X509_USER_PROXY'), key_file = os.getenv('X509_USER_PROXY'))
r1=conn.request("GET",'/reqmgr/reqMgr/request?requestName='+workflow)
r2=conn.getresponse()
request = json.loads(r2.read())
while 'exception' in request:
conn = httplib.HTTPSConnection(url, cert_file = os.getenv('X509_USER_PROXY'), key_file = os.getenv('X509_USER_PROXY'))
r1=conn.request("GET",'/reqmgr/reqMgr/request?requestName='+workflow)
r2=conn.getresponse()
request = json.loads(r2.read())
inputDataSets=request['InputDataset']
if len(inputDataSets)<1:
print "No InputDataSet for workflow " +workflow
else:
return inputDataSets
def duplicateRunLumi(dataset):
RunlumisChecked={}
query="file run lumi dataset="+dataset
das_data = get_data(das_host,query,0,0,0)
if isinstance(das_data, basestring):
result = json.loads(das_data)
else:
result = das_data
if result['status'] == 'fail' :
print 'DAS query failed with reason:',result['reason']
else:
preresult=result['data']
for filename in preresult:
run=filename['run'][0]['run_number']
if run not in RunlumisChecked:
RunlumisChecked[run]=[]
newLumis=filename['lumi'][0]['number']
for lumiRange in newLumis:
newlumiRange=range(lumiRange[0], lumiRange[1]+1)
for lumi in newlumiRange:
if lumi in RunlumisChecked[run]:
return True
else:
RunlumisChecked[run].append(lumi)
return False
def duplicateLumi(dataset):
lumisChecked=[]
query="file lumi dataset="+dataset
das_data = get_data(das_host,query,0,0,0)
if isinstance(das_data, basestring):
result = json.loads(das_data)
else:
result = das_data
if result['status'] == 'fail' :
print 'DAS query failed with reason:',result['reason']
else:
preresult=result['data']
for filename in preresult:
newLumis=filename['lumi'][0]['number']
for lumiRange in newLumis:
newlumiRange=[lumiRange[0]]
if lumiRange[0]<lumiRange[1]:
newlumiRange=range(lumiRange[0], lumiRange[1])
for lumi in newlumiRange:
if lumi in lumisChecked:
return True
else:
lumisChecked.append(lumi)
return False
def getRunsInDataset(das_url, dataset):
query="run dataset="+dataset
das_data = get_data(das_url,query,0,0,0)
if isinstance(das_data, basestring):
result = json.loads(das_data)
else:
result = das_data
if result['status'] == 'fail' :
print 'DAS query failed with reason:',result['reason']
else:
runs=[]
preresult=result['data']
for run in preresult:
runs.append(run['run'][0]['run_number'])
return runs
def getNumberofFilesPerRun(das_url, dataset, run):
query="file dataset="+dataset+" run="+str(run)+" | count(file.name)"
das_data = get_data(das_url,query,0,0,0)
if isinstance(das_data, basestring):
result = json.loads(das_data)
else:
result = das_data
if result['status'] == 'fail' :
print 'DAS query failed with reason:',result['reason']
else:
return result['data'][0]['result']['value']
#Return true if there are duplicate evnets , false otherwise
def duplicateEventsMonteCarlo(dataset):
das_url=das_host
runs=getRunsInDataset(das_url, dataset)
for run in runs:
NumFilesRun=getNumberofFilesPerRun(das_url, dataset, run)
NumLumis=getRunLumiCountDatasetRun(das_url, dataset, run)
if NumLumis>NumFilesRun:#It means at least one lumi is split into more than one file
return True
return False
#Return the number of events for a given dataset given a runlist
def EventsRunList(das_url, dataset, runlist):
events=0
for run in runList:
events=events+getEventsRun(das_url, dataset, run)
return events
# Return the number of events in a dataset and in a run
def getEventsRun(das_url, dataset, run):
query="summary dataset dataset="+dataset+" run="+str(run)+" | grep grep summary.nevents"
das_data = get_data(das_url,query,0,0,0)
if isinstance(das_data, basestring):
result = json.loads(das_data)
else:
result = das_data
if result['status'] == 'fail' :
print 'DAS query failed with reason:',result['reason']
else:
if len(result['data'])==0:#dataset not yet registered in DBS
return 0
preresult=result['data'][0]['summary']
for key in preresult:
if 'nevents' in key:
return key['nevents']
return -1
#Returns the number of events in a dataset using DAS
def getEventCountDataSet(das_url, dataset):
query="dataset dataset="+dataset+" status=* | grep dataset.nevents"
das_data = get_data(das_url,query,0,0,0)
if isinstance(das_data, basestring):
result = json.loads(das_data)
else:
result = das_data
if result['status'] == 'fail' :
print 'DAS query' + query+' failed with reason:',result['reason']
else:
if len(result['data'])==0:#dataset not yet registered in DBS
return 0
preresult=result['data'][0]['dataset']
for key in preresult:
if 'nevents' in key:
return key['nevents']
return -1
#Returns a list of runs of a dataset
def getRunsDataset(das_url, dataset):
runList=[]
query="run dataset="+dataset+"| grep run.run_number"
das_data = get_data(das_url,query,0,0,0)
if isinstance(das_data, basestring):
result = json.loads(das_data)
else:
result = das_data
if result['status'] == 'fail' :
print 'DAS query failed with reason:',result['reason']
else:
if len(result['data'])==0:#dataset not yet registered in DBS
return runList
preresult=result['data']
for key in preresult:
if 'run' in key:
runList.append(key['run'][0]['run_number'])
return runList
#Returns the number of lumis in a given run for a dataset using DAS.
def getRunLumiCountDatasetRun(das_url, dataset, run):
lumis=0
query="summary dataset="+dataset+" run="+str(run)+" | sum(summary.nlumis)"
das_data = get_data(das_url,query,0,0,0)
if isinstance(das_data, basestring):
result = json.loads(das_data)
else:
result = das_data
if result['status'] == 'fail' :
print 'DAS query failed with reason:',result['reason']
else:
if len(result['data'])==0:#dataset not yet registered in DBS
return 0
preresult=result['data']
for key in preresult:
if 'result' in key:
return key['result']['value']
return -1
# Get the number of unique lumis in a dataset
def getRunLumiCountDataset(das_url, dataset):
lumis=0
query="summary dataset="+dataset+" | grep summary.nlumis"
das_data = get_data(das_url,query,0,0,0)
if isinstance(das_data, basestring):
result = json.loads(das_data)
else:
result = das_data
if result['status'] == 'fail' :
print 'DAS query failed with reason:',result['reason']
else:
if len(result['data'])==0:#dataset not yet registered in DBS
return 0
preresult=result['data'][0]
for key in preresult:
if 'summary' in key:
return preresult['summary'][0]['nlumis']
return -1
#Returns the number of lumis in a dataset and in a given runlist
def getRunLumiCountDatasetListDAS(das_url,dataset, runlist):
lumis=0
runChunks=chunks(runlist,30)
for runList in runChunks:
lumis=lumis+getRunLumiCountDatasetListLimited(das_url,dataset, runList)
return lumis
#Returns the number of lumis in a dataset and in a given runlist but with a limit of 30 runs
def getRunLumiCountDatasetListLimited(das_url,dataset, runlist):
lumis=0
query="summary dataset="+dataset+" run in "+str(runlist)+ " | grep summary.nlumis"
das_data = get_data(das_url,query,0,0,0)
if isinstance(das_data, basestring):
result = json.loads(das_data)
else:
result = das_data
if result['status'] == 'fail' :
print 'DAS query failed with reason:',result['reason']
else:
if len(result['data'])==0:#dataset not yet registered in DBS
return 0
preresult=result['data'][0]
for key in preresult:
if 'summary' in key:
return preresult['summary'][0]['nlumis']
return -1
#GEN check generator true if everything is fine, false otherwise
def checkCorrectLumisEventGEN(dataset):
das_url=das_host
numlumis=getRunLumiCountDataset(das_url, dataset)
numEvents=getEventCountDataSet(das_url, dataset)
if numlumis>=numEvents/300.0:
return True
else:
return False
# SPlits a list of chunks of size(n)
def chunks(lis, n):
return [lis[i:i+n] for i in range(0, len(lis), n)]
# Return the number of events in a block using DAS
def getEventsBlock(das_url, block_name):
query="block="+block_name+" | grep block.nevents"
das_data = get_data(das_url,query,0,0,0)
if isinstance(das_data, basestring):
result = json.loads(das_data)
else:
result = das_data
if result['status'] == 'fail' :
print 'DAS quert ', query
print 'failed with reason:',result['reason']
else:
if len(result['data'])==0:#dataset not yet registered in DBS
return 0
preresult=result['data'][0]['block']
for key in preresult:
if 'nevents' in key:
return key['nevents']
return -1
return 0
#Return the number of events for a given dataset given a blocklist
def EventsBlockList(das_url, dataset, blocklist):
events=0
if len(blocklist)==0:
return getEventCountDataSet(das_url, dataset)
for block in blocklist:
events=events+getEventsBlock(das_url, block)
return events
def getRunLumiCountDatasetBlockList(dataset,blockList):
lumis=0
querry="./dbssql --limit=1000000 --input='find block, run, lumi where dataset="+dataset+"' |egrep '("
for block in blockList:
querry=querry+block+ "|"
querry=querry+blockList[0]+")'"
querry=querry+"| awk '{print $2, $3}' | sort | uniq | wc -l"
output=os.popen(querry).read()
if not output:
lumis=lumis
try:
lumis=lumis+int(output)
except ValueError:
lumis=lumis
return lumis
def handleTaskChain(request):
# Check if it's MC from scratch
if 'RequestNumEvents' in request['Task1']:
if request['Task1']['RequestNumEvents'] is not None:
return request['Task1']['RequestNumEvents']
blockWhitelist = blockBlacklist = runWhitelist = runBlacklist = []
if 'InputDataset' in request['Task1']:
inputDataSet=request['Task1']['InputDataset']
if 'BlockWhitelist' in request['Task1']:
blockWhitelist=request['Task1']['BlockWhitelist']
if 'BlockBlacklist' in request['Task1']:
blockBlacklist=request['Task1']['BlockBlacklist']
if 'RunWhitelist' in request['Task1']:
runWhitelist=request['Task1']['RunWhitelist']
if 'RunBlacklist' in request['Task1']:
runBlacklist=request['Task1']['RunBlacklist']
if len(blockWhitelist)>0:
return getRunLumiCountDatasetBlockList(das_host, inputDataSet,blockWhitelist)
if len(blockBlacklist)>0:
return getRunLumiCountDataset(das_host,inputDataSet)-getRunLumiCountDatasetBlockList(das_host, inputDataSet,blockBlacklist)
if len(runWhitelist)>0:
return getRunLumiCountDatasetListDAS(das_host, inputDataSet, runWhitelist)
else:
return getRunLumiCountDataset(das_host,inputDataSet)
### TODO: implement multi white/black list
# if len(blockWhitelist)>0 and len(runWhitelist)>0:
# print "Hey, you have block and run white list :-D"
# return getRunLumiCountDatasetBlockList(inputDataSet,BlockWhitelist)
# elif len(blockWhitelist)>0 and len(runWhitelist)==0:
# print "Hey, you have block white list but NOT run white list :-D"
# elif len(blockWhitelist)==0 and len(runWhitelist)>0:
# print "Hey, you have NO block white list but you do have run white list :-D"
# return getRunLumiCountDatasetList(inputDataSet, runWhitelist)
# elif len(blockWhitelist)==0 and len(runWhitelist)==0:
# print "Hey, you have NO block and run white list :-D"
#
# if len(BlockBlacklist)>0 and len(runBlacklist)>0:
# print "Hey, you have block and run black list :-("
# return getRunLumiCountDataset(inputDataSet)-getRunLumiCountDatasetBlockList(inputDataSet,BlockBlacklist)
# elif len(BlockBlacklist)>0 and len(runBlacklist)==0:
# print "Hey, you have block black list but NOT run black list :-("
# elif len(BlockBlacklist)==0 and len(runBlacklist)>0:
# print "Hey, you have NO block black list but you do have run black list :-("
# elif len(BlockBlacklist)==0 and len(runBlacklist)==0:
# print "Hey, you have NO block and run black list :-("
def getInputEvents(url, workflow):
conn = httplib.HTTPSConnection(url, cert_file = os.getenv('X509_USER_PROXY'), key_file = os.getenv('X509_USER_PROXY'))
r1=conn.request("GET",'/reqmgr/reqMgr/request?requestName='+workflow)
r2=conn.getresponse()
request = json.loads(r2.read())
while 'exception' in request:
conn = httplib.HTTPSConnection(url, cert_file = os.getenv('X509_USER_PROXY'), key_file = os.getenv('X509_USER_PROXY'))
r1=conn.request("GET",'/reqmgr/reqMgr/request?requestName='+workflow)
r2=conn.getresponse()
request = json.loads(r2.read())
requestType=request['RequestType']
if requestType=='MonteCarlo' or requestType=='LHEStepZero':
if 'RequestNumEvents' in request:
if request['RequestNumEvents']>0:
return request['RequestNumEvents']
if 'RequestSizeEvents' in request:
return request['RequestSizeEvents']
else:
return 0
if requestType == 'TaskChain':
return handleTaskChain(request)
#In case some parameters miss in the request like blockwhitelist, blockblack list and so on or it was injected as a string.
for listitem in ["RunWhitelist", "RunBlacklist", "BlockWhitelist",
"BlockBlacklist"]:
if listitem in request:
if request[listitem]=='[]':
request[listitem]=[]
if type(request[listitem]) is not list:#if there is not a list but just one element we convert it to a list with one element.
request[listitem]=re.split(r",",request[listitem])
else:
request[listitem]=[]
inputDataSet=request['InputDataset']
if requestType=='ReReco':
if len(request['BlockWhitelist'])>0:
return getRunLumiCountDatasetBlockList(das_host, request['InputDataset'],request['BlockWhitelist'])
if len(request['BlockBlacklist'])>0:
return getRunLumiCountDataset(request['InputDataset'])-getRunLumiCountDatasetBlockList(request['InputDataset'],request['BlockBlacklist'])
if len(request['RunWhitelist'])>0:
return getRunLumiCountDatasetListDAS(das_host, request['InputDataset'], request['RunWhitelist'])
else:
return getRunLumiCountDataset(das_host, request['InputDataset'])
events=getEventCountDataSet(das_host, request['InputDataset'])
if len(request['BlockBlacklist'])>0:
events=events-EventsBlockList(request['InputDataset'], request['BlockBlacklist'])
if len(request['RunWhitelist'])>0:
events=EventsRunList(das_host, request['InputDataset'], request['RunWhitelist'])
if len(request['BlockWhitelist'])>0:
events=EventsBlockList(das_host, request['InputDataset'], request['BlockWhitelist'])
if 'FilterEfficiency' in request.keys():
return float(request['FilterEfficiency'])*events
else:
return events
def getOutputEvents(url, workflow, dataset):
conn = httplib.HTTPSConnection(url, cert_file = os.getenv('X509_USER_PROXY'), key_file = os.getenv('X509_USER_PROXY'))
r1=conn.request("GET",'/reqmgr/reqMgr/request?requestName='+workflow)
r2=conn.getresponse()
request = json.loads(r2.read())
while 'exception' in request:
conn = httplib.HTTPSConnection(url, cert_file = os.getenv('X509_USER_PROXY'), key_file = os.getenv('X509_USER_PROXY'))
r1=conn.request("GET",'/reqmgr/reqMgr/request?requestName='+workflow)
r2=conn.getresponse()
request = json.loads(r2.read())
requestType=request['RequestType']
if requestType=='ReReco':
return getRunLumiCountDataset(das_host, dataset)
elif requestType=='TaskChain':
if 'InputDataset' in request['Task1']:
return getRunLumiCountDataset(das_host, dataset)
else:
return getEventCountDataSet(das_host, dataset)
else:
return getEventCountDataSet(das_host, dataset)
def main():
args=sys.argv[1:]
if not len(args)==1:
print "usage:dbsTest workflowname"
sys.exit(0)
workflow=args[0]
url='cmsweb.cern.ch'
outputDataSets=phedexSubscription.outputdatasetsWorkflow(url, workflow)
inputEvents=getInputEvents(url, workflow)
for dataset in outputDataSets:
outputEvents=getOutputEvents(url, workflow, dataset)
if inputEvents!=0:
print dataset+" match: "+str(outputEvents/float(inputEvents)*100) +"%"
else:
print "Input Events 0"
sys.exit(0);
if __name__ == "__main__":
main()