-
Notifications
You must be signed in to change notification settings - Fork 5
/
dataCertFor2011.py
348 lines (312 loc) · 12.9 KB
/
dataCertFor2011.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
#! /usr/bin/env python
################################################################################
#
#
# $Author: borrell $
# $Date: 2012/04/23 15:04:29 $
# $Revision: 1.4 $
#
#
# Marco Rovere = [email protected]
#
################################################################################
import re, json, sys, ConfigParser, os, string, commands, time, socket
from rrapi import RRApi, RRApiError
class Certifier():
cfg='runreg.cfg'
OnlineRX = "%Online%ALL"
def __init__(self,argv,verbose=False):
self.verbose = verbose
if len(argv)==2:
self.cfg = argv[1]
else:
self.cfg = Certifier.cfg
self.qry = {}
self.qry.setdefault("GOOD", "isNull OR = true")
self.qry.setdefault("BAD", " = false")
self.readConfig()
def readConfig(self):
CONFIG = ConfigParser.ConfigParser()
if self.verbose:
print 'Reading configuration file from %s' % self.cfg
CONFIG.read(self.cfg)
cfglist = CONFIG.items('Common')
self.dataset = CONFIG.get('Common','DATASET')
self.group = CONFIG.get('Common','GROUP')
self.address = CONFIG.get('Common','RUNREG')
self.runmin = CONFIG.get('Common','RUNMIN')
self.runmax = CONFIG.get('Common','RUNMAX')
self.qflist = CONFIG.get('Common','QFLAGS').split(',')
self.bfield = CONFIG.get('Common','BFIELD_THR')
self.dcslist = CONFIG.get('Common','DCS').split(',')
self.jsonfile = CONFIG.get('Common','JSONFILE')
self.beamene = []
self.dbs_pds_all = ""
self.online_cfg = "FALSE"
self.usedbs = False
self.dsstate = ""
print "First run ", self.runmin
print "Last run ", self.runmax
print "Dataset name ", self.dataset
print "Group name ", self.group
print "Quality flags ", self.qflist
print "DCS flags ", self.dcslist
for item in cfglist:
if "BEAM_ENE" in item[0].upper():
self.beamene = item[1].split(',')
if "DBS_PDS" in item[0].upper():
self.dbs_pds_all = item[1]
self.usedbs = True
if "ONLINE" in item[0].upper():
self.online_cfg = item[1]
if "DSSTATE" in item[0].upper():
self.dsstate = item[1]
self.dbs_pds = self.dbs_pds_all.split(",")
self.online = False
if "TRUE" == self.online_cfg.upper() or \
"1" == self.online_cfg.upper() or \
"YES" == self.online_cfg.upper():
self.online = True
try:
self.bfield = float(self.bfield)
except:
print "BFIELD threshold value not understood:", self.bfield
sys.exit(1)
for e in range(0, len(self.beamene)):
try:
self.beamene[e] = float(self.beamene[e])
if self.verbose:
print "Beam Energy ", self.beamene
except:
print "BEAMENE value not understood: ", self.beamene
sys.exit(1)
def generateFilter(self):
self.filter = {}
self.filter.setdefault("dataset", {})\
.setdefault("rowClass", "org.cern.cms.dqm.runregistry.user.model.RunDatasetRowGlobal")
for qf in self.qflist:
(sys,value) = qf.split(':')
if self.verbose: print qf
if sys != "NONE":
if sys.lower() == 'tracker':
self.filter.setdefault('trackStatus', self.qry[value])
else:
self.filter.setdefault(sys.lower()+"Status", self.qry[value])
if sys.lower() != 'lumi':
self.filter.setdefault("dataset", {})\
.setdefault("filter", {})\
.setdefault(sys.lower(), {})\
.setdefault("status", " = %s" % value)
for dcs in self.dcslist:
self.filter.setdefault(dcs.lower()+"Ready", "isNull OR = true")
if self.verbose: print dcs
if self.online:
self.filter.setdefault("dataset", {})\
.setdefault("filter", {})\
.setdefault("datasetName", "like %s" % Certifier.OnlineRX)
self.filter.setdefault("dataset", {})\
.setdefault("filter", {})\
.setdefault("online", " = true")
else:
self.filter.setdefault("dataset", {})\
.setdefault("filter", {})\
.setdefault("datasetName", " like %s" % self.dataset.split(":")[0])
self.filter.setdefault("runNumber", ">= %d AND <= %d " %(int(self.runmin), int(self.runmax)))
self.filter.setdefault("dataset", {})\
.setdefault("filter", {})\
.setdefault("runClassName", self.group)
self.filter.setdefault("dataset", {})\
.setdefault("filter", {})\
.setdefault("run", {})\
.setdefault("rowClass", "org.cern.cms.dqm.runregistry.user.model.RunSummaryRowGlobal")
self.filter.setdefault("dataset", {})\
.setdefault("filter", {})\
.setdefault("run", {})\
.setdefault("filter",{})\
.setdefault("bfield", "> %.1f" % self.bfield)
self.filter.setdefault("cmsActive", "isNull OR = true")
if len(self.dsstate):
self.filter.setdefault("dataset", {})\
.setdefault("filter", {})\
.setdefault("datasetState", " = %s" % self.dsstate)
if len(self.beamene):
eneQuery = '{lhcEnergy} IS NULL OR {lhcEnergy} = 0 '
for e in self.beamene:
energyLow = e - 400
if energyLow < 0:
energyLow = 0
energyHigh = e + 400
eneQuery += 'OR ( {lhcEnergy} >= %.1d AND {lhcEnergy} <= %.1d) ' % (energyLow, energyHigh)
self.filter.setdefault("dataset", {})\
.setdefault("filter", {})\
.setdefault("run", {})\
.setdefault("query", eneQuery)
if self.verbose:
print json.dumps(self.filter)
def generateJson(self):
try:
self.api = RRApi(self.address, debug = self.verbose)
except RRApiError, e:
print e
sys.exit(1)
self.cert_json = self.api.data(workspace = 'GLOBAL'\
, table = 'datasetlumis'\
, template = 'json'\
, columns = ['runNumber', 'sectionFrom', 'sectionTo']\
, filter = self.filter)
if self.verbose:
print "Printing JSON file ", json.dumps(self.cert_json)
self.convertToOldJson()
if self.usedbs:
dbsjson=get_dbsjson(self.dbs_pds_all, self.runmin, self.runmax)
if self.verbose:
print "Printing dbsjson ", dbsjson
for element in self.cert_old_json:
combined=[]
dbsbad_int=invert_intervals(self.cert_old_json[element])
if self.verbose:
print " debug: Good Lumi ", self.cert_old_json[element]
print " debug: Bad Lumi ", dbsbad_int
for interval in dbsbad_int:
combined.append(interval)
if element in dbsjson.keys():
if self.verbose:
print " debug: Found in DBS, Run ", element, ", Lumi ", dbsjson[element]
dbsbad_int=invert_intervals(dbsjson[element])
if self.verbose:
print " debug DBS: Bad Lumi ", dbsbad_int
else:
dbsbad_int=[[1,9999]]
for interval in dbsbad_int:
combined.append(interval)
combined=merge_intervals(combined)
combined=invert_intervals(combined)
if len(combined)!=0:
self.cert_old_json[element]=combined
if self.verbose:
print json.dumps(self.cert_old_json)
def convertToOldJson(self):
old_json = {}
self.cert_old_json = {}
for block in self.cert_json:
if len(block) == 3:
runNum = block['runNumber']
lumiStart = block['sectionFrom']
lumiEnd = block['sectionTo']
if self.verbose:
print " debug: Run ", runNum, " Lumi ", lumiStart, ", ", lumiEnd
old_json.setdefault(str(runNum), []).append([lumiStart, lumiEnd])
if self.verbose:
print old_json[str(runNum)]
for block in old_json:
temp = []
temp = merge_intervals2(old_json[block])
self.cert_old_json.setdefault(block, temp)
if self.verbose:
print "Merging Done on Run ", block,
print " Interval ", temp
def writeJson(self):
js = open(self.jsonfile, 'w+')
json.dump(self.cert_old_json, js, sort_keys=True)
js.close()
if self.verbose:
print " "
print "-------------------------------------------"
print "Json file: %s written.\n" % self.jsonfile
def invert_intervals(intervals,min_val=1,max_val=9999):
if not intervals:
return []
intervals=merge_intervals(intervals)
intervals = sorted(intervals, key = lambda x: x[0])
result = []
if min_val==-1:
(a,b)=intervals[0]
min_val=a
if max_val==-1:
(a,b)=intervals[len(intervals)-1]
max_val=b
curr_min=min_val
for (x,y) in intervals:
if x>curr_min:
result.append((curr_min,x-1))
curr_min=y+1
if curr_min<max_val:
result.append((curr_min,max_val))
return result
def merge_intervals(intervals):
if not intervals:
return []
intervals = sorted(intervals, key = lambda x: x[0])
result = []
(a, b) = intervals[0]
for (x, y) in intervals[1:]:
if x <= b:
b = max(b, y)
else:
result.append((a, b))
(a, b) = (x, y)
result.append((a, b))
return result
def merge_intervals2(intervals):
if not intervals:
return []
intervals = sorted(intervals, key = lambda x: x[0])
result = []
(a, b) = intervals[0]
for (x, y) in intervals[1:]:
if x <= b+1:
b = max(b, y)
else:
result.append((a, b))
(a, b) = (x, y)
result.append((a, b))
return result
def get_dbsjson(datasets, runmin, runmax):
unsorted={}
for ds in datasets.split(","):
command='dbs search --query="find run,lumi where dataset=%s and run >=%s and run<=%s"' % (ds, runmin, runmax)
print command
(status, out) = commands.getstatusoutput(command)
if status:
sys.stderr.write(out)
print "\nERROR on dbs command: %s\nHave you done cmsenv?" % command
sys.exit(1)
for line in out.split('\n'):
fields=line.split()
if len(fields)!=2:
continue
if fields[0].isdigit() and fields[1].isdigit():
run=fields[0]
lumi=int(fields[1])
if run not in unsorted.keys():
unsorted[run]=[]
unsorted[run].append(lumi)
sorted={}
for run in unsorted.keys():
lumilist=unsorted[run]
lumilist.sort()
sorted[run]=lumilist
dbsjson={}
for run in sorted.keys():
lumilist=sorted[run]
lumiranges=[]
lumirange=[]
lumirange.append(lumilist[0])
lastlumi=lumilist[0]
for lumi in lumilist[1:]:
if lumi>lastlumi+1:
lumirange.append(lastlumi)
lumiranges.append(lumirange)
lumirange=[]
lumirange.append(lumi)
lastlumi=lumi
if len(lumirange)==1:
lumirange.append(lastlumi)
lumiranges.append(lumirange)
dbsjson[run]=lumiranges
return dbsjson
if __name__ == '__main__':
cert = Certifier(sys.argv, verbose=False)
cert.generateFilter()
cert.generateJson()
cert.writeJson()