4 from __future__
import print_function
5 from PyCool
import cool
6 from copy
import deepcopy
8 from CoolRunQuery.utils.AtlRunQueryUtils
import coolDbConn, MergeRanges, SmartRangeCalulator
9 from CoolRunQuery.utils.AtlRunQueryIOV
import IOVTime, IOVRange
13 from collections
import namedtuple, defaultdict
15 from itertools
import groupby
16 from operator
import itemgetter
19 OOO = namedtuple(
'OOO',
'channel payload iovrange isvf')
21 while coolobjs.goToNext():
22 obj=coolobjs.currentRef()
23 yield OOO(obj.channelId(),obj.payloadValue, IOVRange(obj),
False)
25 ChDesc = namedtuple(
'ChDesc',
'CoolPayloadKey,CoolChannel,ResultKey,Header,SelectShowRetrieve')
37 def __init__(self, x, keytype=UNDEFINED, selecttype = SELECT):
67 if isinstance(other,DataKey):
77 if isinstance(other,DataKey):
129 Selector._conddb =
'OFLP200'
135 return Selector._conddb
136 raise RuntimeError(
"CondDB not yet set")
138 Selector._conddb =
"CONDBR2" if run_number > 236100
else "COMP200"
139 print (
"Determinded %s, based on run number %i" % (Selector._conddb, run_number))
140 return Selector._conddb
144 return Selector.condDB(run_number)==
"CONDBR2"
167 def __init__(self, name, dbfolderkey, channelKeys):
168 super(Condition,self).
__init__(name)
175 self.schema, foldertag = dbfoldertag.split(
'::')
176 self.folder, self.
tagname = (foldertag.split(
'#')+[
''])[0:2]
185 x._select_show_retrieve = _ssr
199 f = coolDbConn.GetDBConn(schema = self.schema,db=Selector.condDB()).getFolder(self.folder)
200 if f.versioningMode()==0:
202 if self.
tagname not in [
"HEAD",
""]:
208 channelKey: (0,'SMK','MasterConfigurationKey')
209 ssr: 0-retrieve only, 1-select, 2-show, 3-select&show
218 self.
data_keys[-1]._select_show_retrieve = ssr
251 if listofchans
and cd.ResultKey
not in listofchans:
253 cd._select_show_retrieve |= 2
258 from CoolRunQuery.AtlRunQueryRun
import Run
260 if cd.SelectShowRetrieve<2:
262 Run.AddToShowOrder(cd)
266 def __init__(self, name, dbfolderkey, channelKeys=None):
267 super(RunLBBasedCondition,self).
__init__(name, dbfolderkey, channelKeys)
271 for ch1,ch2
in sortedRanges:
273 chansel = cool.ChannelSelection(ch1,ch2,cool.ChannelSelection.sinceBeforeChannel)
275 chansel.addRange(ch1,ch2)
277 return coolgen(f.browseObjects( iovmin, iovmax, chansel, self.
tagname))
281 iovpllist is a list [(IOV,payload),(IOV,payload),..,(IOV,payload)]
284 if IOVTime(runNr,1).
inRange(x[0]):
291 pld = defaultdict(list)
297 runnrlist = [r.runNr
for r
in runlist]
298 nlb = dict([(r.runNr,r.lastlb)
for r
in runlist])
301 firstrun = runnrlist[0]
302 lastrun = runnrlist[-1]
304 for iov, pl
in iovpllist:
305 first =
max(firstrun,iov.startTime.run)
306 last =
min(lastrun,(iov.endTime-1).run)
307 for r
in range(first,last+1):
309 pld[r].
append((deepcopy(iov),pl))
312 for runnr, iovplbyrun
in pld.items():
314 if iovplbyrun[0][0].startTime.lb==0:
315 iovplbyrun[0][0].startTime.lb=1
318 iovplbyrun[0][0].truncateToSingleRun(runnr)
323 if iovplbyrun[0][0].startTime == iovplbyrun[0][0].endTime:
327 lastiov = iovplbyrun[-1][0]
328 lastiov.truncateToSingleRun(runnr)
332 curTime = IOVTime(runnr,1)
333 while curTime<lastiov.startTime:
334 iov = iovplbyrun[idx][0]
335 if curTime < iov.startTime:
337 missingIOV = IOVRange(starttime=curTime, endtime=iov.startTime)
338 iovplbyrun.insert( idx, (missingIOV,
"n.a.") )
339 curTime = IOVTime(iov.endTime)
343 runEndTime = IOVTime(runnr,nlb[runnr]+1)
344 if lastiov.endTime<runEndTime:
345 missingIOV = IOVRange(starttime=lastiov.endTime, endtime=runEndTime)
346 iovplbyrun.append( (missingIOV,
"n.a.") )
352 condData = defaultdict(list)
354 keys = dict([(ch,
set(k))
for ch, k
in groupby(
sorted(self.
ChannelKeys(), key=itemgetter(0)), itemgetter(0))])
357 iovmin=(rr[0] << 32)+0
358 iovmax=((rr[1]+1) << 32)-1
361 objs = self.
_retrieve(iovmin, iovmax, f, sortedRanges)
365 for chtmp, internalKey, payloadKey
in keys[ch]:
366 if type(payloadKey)==tuple:
368 payload = obj.payload
370 payload = tuple(map(obj.payload, payloadKey))
372 payload = obj.payload(payloadKey)
374 condData[internalKey].
append( (IOVRange(obj.iovrange), payload) )
385 sortedRanges =
MergeRanges([(x,x)
for x
in sortedChannel])
391 condData = self.
readCondData(runranges, f, sortedRanges)
406 rejectSomething =
False
408 if run.runNr
not in condDataDict[k]:
410 rejectSomething =
True
411 run.addResult(k,
"n.a.")
414 datavec = condDataDict[k][run.runNr]
416 if not datavec
or len(datavec)==0:
417 run.addResult(k,
"n.a.")
420 if 'n.a.' in datavec:
421 run.showDataIncomplete =
True
423 for iov, data
in datavec:
427 rejectSomething =
True
431 run.selDataIncomplete =
True
433 if not (rejectSomething
and self.
rejectRun(run)):
434 newrunlist += [run.runNr]
436 runlist = [r
for r
in runlist
if r.runNr
in newrunlist]
438 duration = time() - start
441 print (
" ==> %i runs found (%.2f sec)" % (len(runlist),duration))
443 print (
" ==> Done (%g sec)" % duration)
449 return run.data.isRejected
453 def __init__(self, name, dbfolderkey, channelKeys=None):
454 super(TimeBasedCondition,self).
__init__(name, dbfolderkey, channelKeys)
461 if not iovpllist
or not runlist:
472 iovindex = startiovindex
475 iov = iovpllist[iovindex][0]
476 iovstart = iov.startTime.time
477 iovend = iov.endTime.time
480 startiovindex = iovindex
487 if iovend<run.sor
and iovindex==len(iovpllist)-1:
499 iovindex = startiovindex
501 iov = iovpllist[iovindex][0]
502 iovstart = iov.startTime.time
503 iovend = iov.endTime.time
505 if iovend>=run.eor
or iovindex==len(iovpllist)-1:
506 endiovindex = iovindex
513 for iov,pl
in iovpllist[startiovindex:endiovindex+1]:
514 iovstart = iov.startTime.time
515 iovend = iov.endTime.time
518 while lbindex<len(run.lbtimes)
and run.lbtimes[lbindex][0]<iovstart:
522 while lbindex<len(run.lbtimes)
and run.lbtimes[lbindex][0]<iovend:
527 if lbindex==len(run.lbtimes)-1:
535 lastIOV,lastpl=
None,
None
536 if len(pld[run.runNr])>0:
537 lastIOV,lastpl = pld[run.runNr][-1]
539 lastIOV.endTime.run = endrun
540 lastIOV.endTime.lb = endlb
542 pld[run.runNr] += [(IOVRange(runStart=run.runNr, lbStart=startlb, runEnd=endrun, lbEnd=endlb), pl)]
546 startiovindex = endiovindex
552 runlistNo = [run.runNr
for run
in runlist]
555 f = coolDbConn.GetDBConn(schema=self.schema, db=Selector.condDB()).getFolder(self.folder)
558 for ch
in sortedChannel:
560 chansel = cool.ChannelSelection(ch,ch,cool.ChannelSelection.sinceBeforeChannel)
562 chansel.addChannel(ch)
565 condData = defaultdict(list)
570 firstrun = runlist[runlistNo.index(rr[0])]
571 lastrun = runlist[runlistNo.index(rr[1])]
575 objs = f.browseObjects( iovmin, iovmax, chansel)
576 while objs.goToNext():
577 obj= objs.currentRef()
580 (channelNumber, resultKey, payloadKey) = chKey
581 if channelNumber != ch:
583 isBlob = (resultKey ==
'olc:bcidmask')
585 payloadvalue = obj.payload()[chKey[2]].
read()
587 payloadvalue = obj.payloadValue(chKey[2])
589 condData[resultKey].
append( (IOVRange(obj=obj, timebased=
True), payloadvalue) )
602 rejectSomething =
False
605 if run.runNr
not in condDataDict[k]:
606 run.addResult(k,
"n.a.")
609 datavec = condDataDict[k][run.runNr]
611 if not datavec
or len(datavec)==0:
612 run.addResult(k,
"n.a.")
615 if 'n.a.' in datavec:
616 run.showDataIncomplete=
True
618 for iov, data
in datavec:
621 run.addResult(k, self.
prettyValue(data,k), iov, reject=
True)
622 rejectSomething =
True
628 run.selDataIncomplete =
True
630 if not (rejectSomething
and self.
rejectRun(run)):
631 newrunlist += [run.runNr]
634 runlist = [r
for r
in runlist
if r.runNr
in newrunlist]
636 duration = time() - start
639 print (
" ==> %i runs found (%.2f sec)" % (len(runlist),duration))
641 print (
" ==> Done (%g sec)" % duration)
646 return run.data.isRejected