forked from CMSCompOps/WmAgentScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbs3Client.py
executable file
·250 lines (225 loc) · 7.67 KB
/
dbs3Client.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
#!/usr/bin/env python
"""
DBS 3 Client
Ports all the functionality from previously used
dbsTest.py to use DBS3 directly.
"""
import urllib2,urllib, httplib, sys, re, os, json
from xml.dom.minidom import getDOMImplementation
from dbs.apis.dbsClient import DbsApi
#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'
#das_host='https://dastest.cern.ch'
dbs3_url = r'https://cmsweb.cern.ch/dbs/prod/global/DBSReader'
def duplicateRunLumi(dataset, verbose=False):
"""
checks if output dataset has duplicate lumis
for every run.
returns true if at least one duplicate lumi was found
That is if there is the same lumi in the same run and
two different files
This can be used on datasets that have separate
runs.
"""
dbsapi = DbsApi(url=dbs3_url)
duplicated = False
#check each run
runs = getRunsDataset(dataset)
#if only one run in the list
if len(runs) == 1:
return duplicateLumi(dataset, verbose)
#else manually
for run in runs:
#create a set
lumisChecked={}
# retrieve files for that run
reply = dbsapi.listFiles(dataset=dataset)
for f in reply:
logical_file_name = f['logical_file_name']
reply2 = dbsapi.listFileLumis(logical_file_name=logical_file_name, run_num=run)
#retrieve lumis for each file
if reply2:
lumis = reply2[0]['lumi_section_num']
else:
continue
#check that each lumi is only in one file
for lumi in lumis:
if lumi in lumisChecked:
#if verbose print results, if not end quickly
if verbose:
print 'Lumi',lumi,'in run',run,'is in these files'
print logical_file_name
print lumisChecked[lumi]
duplicated = True
else:
return True
else:
lumisChecked[lumi] = logical_file_name
return duplicated
def duplicateLumi(dataset, verbose=False):
"""
checks if output dataset has duplicate lumis
returns true if at least one duplicate lumi was found
"""
# initialize API to DBS3
dbsapi = DbsApi(url=dbs3_url)
duplicated = False
lumisChecked={}
# retrieve files
reply = dbsapi.listFiles(dataset=dataset)
for f in reply:
logical_file_name = f['logical_file_name']
reply2 = dbsapi.listFileLumis(logical_file_name=logical_file_name)
#retrieve lumis for each file
lumis = reply2[0]['lumi_section_num']
#check that each lumi is only in one file
for lumi in lumis:
if lumi in lumisChecked:
#if verbose print results, if not end quickly
if verbose:
print 'Lumi',lumi,'is in these files'
print logical_file_name
print lumisChecked[lumi]
duplicated = True
else:
return True
else:
lumisChecked[lumi] = logical_file_name
return duplicated
def getMaxLumi(dataset):
"""
Gets the number of the last lumi in a given dataset
This is useful for appending new events to dataset
without collision
"""
dbsapi = DbsApi(url=dbs3_url)
reply = dbsapi.listFiles(dataset=dataset)
maxl = 0
for f in reply:
logical_file_name = f['logical_file_name']
reply2 = dbsapi.listFileLumis(logical_file_name=logical_file_name)
#retrieve lumis for each file
lumis = reply2[0]['lumi_section_num']
#check max of lumi
lumi = max(lumis)
if lumi > maxl:
maxl = lumi
return maxl
def getRunsDataset(dataset):
"""
returns a list with number of each run.
"""
dbsapi = DbsApi(url=dbs3_url)
# retrieve runs
reply = dbsapi.listRuns(dataset=dataset)
#a list with only the run numbersT3_US_Omaha
runs = []
#filter and clean
for run in reply:
if type(run['run_num']) is list:
runs.extend(run['run_num'])
else:
runs.append(run['run_num'])
return runs
def getNumberofFilesPerRun(das_url, dataset, run):
"""
Count number of files
"""
# initialize API to DBS3
dbsapi = DbsApi(url=dbs3_url)
# retrieve file list
reply = dbsapi.listFiles(dataset=dataset)
return len(reply)
def getEventCountDataSet(dataset):
"""
Returns the number of events in a dataset using DBS3
"""
# initialize API to DBS3
dbsapi = DbsApi(url=dbs3_url)
# retrieve dataset summary
reply = dbsapi.listBlockSummaries(dataset=dataset)
return reply[0]['num_event']
def getLumiCountDataSet(dataset):
"""
Get the number of unique lumis in a dataset
"""
# initialize API to DBS3
dbsapi = DbsApi(url=dbs3_url)
# retrieve dataset summary
reply = dbsapi.listFileSummaries(dataset=dataset)
return reply[0]['num_lumi']
def hasAllBlocksClosed(dataset):
"""
checks if a given dataset has all blocks closed
"""
# initialize API to DBS3
dbsapi = DbsApi(url=dbs3_url)
# retrieve dataset summary
reply = dbsapi.listBlocks(dataset=dataset, detail=True)
for block in reply:
#print block['block_name']
#print block['open_for_writing']
if block['open_for_writing']:
return False
return True
def getEventCountDataSetBlockList(dataset,blockList):
"""
Counts and adds all the events for a given lists
blocks inside a dataset
"""
# initialize API to DBS3
dbsapi = DbsApi(url=dbs3_url)
#transform from strin to list
if type(blockList) in (str, unicode):
blockList = eval(blockList)
total = 0
#get one by one block and add it so uri wont be too large
for block in blockList:
reply = dbsapi.listBlockSummaries(block_name=block)
total += reply[0]['num_event']
return total
def getEventCountDataSetRunList(dataset,runList):
"""
Counts and adds all the events for a given lists
of runs inside a dataset
"""
# initialize API to DBS3
dbsapi = DbsApi(url=dbs3_url)
# retrieve file aggregation only by the runs
#transform from strin to list
if type(runList) in (str, unicode):
runList = eval(runList)
total = 0
#get one by one run, so URI wont be too large
for run in runList:
reply = dbsapi.listFileSummaries(dataset=dataset,run_num=run)
if reply:
total += reply[0]['num_event']
return total
def main():
args=sys.argv[1:]
if not len(args)==1:
print "usage:dbs3Client workflow"
sys.exit(0)
workflow=args[0]
url='cmsweb.cern.ch'
outputDataSets=reqMgrClient.outputdatasetsWorkflow(url, workflow)
#runlist = [176801, 176807, 176702, 176796, 175896]
##inputEvents=getInputEvents(url, workflow)
##print " Runs", getEventCountDataSetRunList('/PhotonHad/Run2011B-v1/RAW',runlist)
#print " Events:", getEventCountDataSet('/Neutrino_Pt-2to20_gun/Fall13-POSTLS162_V1-v4/GEN-SIM')
for dataset in outputDataSets:
print dataset
print " Events:", getEventCountDataSet(dataset)
print " Lumis:", getLumiCountDataSet(dataset)
#print " Duplicated Lumis:", duplicateRunLumi(dataset)
#print " Duplicated Lumis:", duplicateLumi(dataset)
#print " Runs", getRunsDataset(dataset))
#print " Blocks", hasAllBlocksClosed(dataset)
#print " blockEvents", getEventCountDataSetBlockList(dataset,blocklist)
sys.exit(0);
if __name__ == "__main__":
main()
sys.exit(0);