3 from __future__
import print_function
5 """Provides Trigger related selectors:
7 TriggerSelector Trigger menu from TriggerDB
10 L1TrigKeySelector L1PSK
11 HLTTrigKeySelector HLTPSK
12 RatesSelector L1 rates from COOL
19 from collections
import defaultdict
21 from PyCool
import cool
23 from CoolRunQuery.selector.AtlRunQuerySelectorBase
import Selector, RunLBBasedCondition
24 from CoolRunQuery.utils.AtlRunQueryUtils
import coolDbConn, GetRanges
35 super(TrigKeySelector,self).
__init__(name=name,
36 dbfolderkey=
'COOLONL_TRIGGER::/TRIGGER/HLT/HltConfigKeys',
37 channelKeys = [(0,
'SMK',
'MasterConfigurationKey'),
38 (0,
'Release',
'ConfigSource')])
55 super(TrigKeySelector,self).setShowOutput(
'SMK')
58 super(TrigKeySelector,self).setShowOutput(
'Release')
62 major, minor, sub, patch = ([
int(x)
for x
in r.split(
'.')] + [0,0,0,0])[0:4]
65 return 1000000*major+10000*minor+100*sub+patch
70 return 'SELOUT Checking if SMK matches "%s" and release matches "%s"' % (self.
trigkeys,self.
release)
72 return 'SELOUT Checking if SMK matches "%s"' % self.
trigkeys
74 return 'SELOUT Checking if release matches "%s"' % (self.
release)
77 return "Retrieving SMK and release version"
79 return "Retrieving SMK"
81 return "Retrieving release version"
93 if val>=cr[0]
and val<=cr[1]:
108 if val>=cr[0]
and val<=cr[1]:
118 if key==
'Release' and value!=
"n.a." and ',' in value:
119 return value.split(
',')[1]
123 if key==
'Release' and ';' in condData:
124 [db,rel,relfull] = condData.split(
';')
125 return {
"db" : db,
"release" : rel,
"releaseFullName" : relfull}
131 return {
"value": smk}
135 from CoolRunQuery.utils.AtlRunQueryTriggerUtils
import getSmkNames, getRandom, triggerDBAlias
142 smkNamesByLHCRun = {}
146 smk =
int(run.result[
'SMK'])
147 except (ValueError, KeyError):
151 smksByLHCRun[run.lhcRun].
add(smk)
154 dbAlias = run.data[
'Release'][0][
"db"]
156 smk = run.data[
'SMK'][0].value
157 dbAlias =
"TRIGGERDB_RUN3" if smk>=3000
else "TRIGGERDBDEV1_I8"
158 smksByLHCRun[run.lhcRun][dbAlias].
add(smk)
162 smkNamesByLHCRun[3] = {}
163 for dbAlias
in smksByLHCRun[3]:
168 smk =
int(run.result[
'SMK'])
169 except (ValueError, KeyError):
172 if smk
in smkNamesByLHCRun[run.lhcRun]:
173 info =
list(smkNamesByLHCRun[run.lhcRun][smk])
175 info = [
"Name unknown", 0,
"no comment"]
176 if info[2]==
"" or info[2]==
"~":
187 super(BGSKeySelector,self).
__init__(name=name,
188 dbfolderkey=
'COOLONL_TRIGGER::/TRIGGER/LVL1/BunchGroupKey',
189 channelKeys = [(0,
'BGS Key',
'Lvl1BunchGroupConfigurationKey')])
191 return "Retrieving Bunch group set key"
197 if not str.isdigit(value):
202 for k
in self.ResultKey():
204 run.stats[k] = {
"blocks" : [],
"first" : 0 }
205 entries = run.data[k]
209 for entry
in entries:
211 if len(blocks) > 0
and blocks[-1][0]==v
and blocks[-1][2]==entry.startlb:
212 blocks[-1][2] = entry.endlb
214 blocks += [ [v, entry.startlb, entry.endlb] ]
215 run.stats[k] = {
"blocks" : blocks,
"first" : entries[0].value }
221 super(L1TrigKeySelector,self).
__init__(name=name,
222 dbfolderkey=
'COOLONL_TRIGGER::/TRIGGER/LVL1/Lvl1ConfigKey',
223 channelKeys = [(0,
'L1 PSK',
'Lvl1PrescaleConfigurationKey')])
226 return "Retrieving L1 PSK"
232 return int(value)
if str.isdigit(value)
else None
235 for k
in self.ResultKey():
237 run.stats[k] = {
"blocks" : [],
"first" : 0 }
238 entries = run.data[k]
242 for entry
in entries:
246 if len(blocks) > 0
and blocks[-1][0]==v
and blocks[-1][2]==entry.startlb:
247 blocks[-1][2] = entry.endlb
249 blocks += [ [v, entry.startlb, entry.endlb] ]
250 run.stats[k] = {
"blocks" : blocks,
"first" : entries[0].value }
251 HLTTrigKeySelector.combineHltL1Keys(runlist)
257 if Selector.condDB() ==
'OFLP200':
258 super(HLTTrigKeySelector,self).
__init__(name=name,
259 dbfolderkey=
'COOLONL_TRIGGER::/TRIGGER/HLT/HltConfigKeys',
260 channelKeys = [(0,
'HLT PSK',
'HltPrescaleConfigurationKey')])
262 super(HLTTrigKeySelector,self).
__init__(name=name,
263 dbfolderkey=
'COOLONL_TRIGGER::/TRIGGER/HLT/PrescaleKey',
264 channelKeys = [(0,
'HLT PSK',
'HltPrescaleKey')])
267 return "Retrieving HLT PSK"
273 if not str.isdigit(value):
278 for k
in self.ResultKey():
282 for entry
in run.data[k]:
286 if len(blocks) > 0
and blocks[-1][0]==v
and blocks[-1][2]==entry.startlb:
287 blocks[-1][2] = entry.endlb
289 blocks += [ [v, entry.startlb, entry.endlb] ]
290 run.stats[k] = {
"blocks" : blocks,
"first" : run.data[k][0].value }
292 HLTTrigKeySelector.combineHltL1Keys(runlist)
296 from CoolRunQuery.utils.AtlRunQueryTriggerUtils
import isTriggerRun2
297 l1keysByDbAlias = defaultdict(set)
298 hltkeysByDbAlias = defaultdict(set)
307 dbAlias = run.data[
'Release'][0][
"db"]
309 smk = run.data[
'SMK'][0].value
310 dbAlias =
"TRIGGERDB_RUN3" if smk>=3000
else "TRIGGERDBDEV1_I8"
311 if 'L1 PSK' in run.stats:
312 l1keysByDbAlias[dbAlias].
update( [x[0]
for x
in run.stats[
'L1 PSK'][
'blocks']] )
313 if 'HLT PSK' in run.stats:
314 hltkeysByDbAlias[dbAlias].
update( [x[0]
for x
in run.stats[
'HLT PSK'][
'blocks']] )
316 if 'L1 PSK' in run.stats:
317 l1keysRun2.update( [x[0]
for x
in run.stats[
'L1 PSK'][
'blocks']] )
318 if 'HLT PSK' in run.stats:
319 hltkeysRun2.update( [x[0]
for x
in run.stats[
'HLT PSK'][
'blocks']] )
321 if 'L1 PSK' in run.stats:
322 l1keysRun1.update( [x[0]
for x
in run.stats[
'L1 PSK'][
'blocks']] )
323 if 'HLT PSK' in run.stats:
324 hltkeysRun1.update( [x[0]
for x
in run.stats[
'HLT PSK'][
'blocks']] )
327 if len(l1keysRun1)+len(l1keysRun2)+len(l1keysByDbAlias) == 0:
329 if len(hltkeysRun1)+len(hltkeysRun2)+len(hltkeysByDbAlias) == 0:
332 from CoolRunQuery.utils.AtlRunQueryTriggerUtils
import getL1PskNames, getHLTPskNames
337 l1NamesByDbAlias = {}
338 hltNamesByDbAlias = {}
339 for dbAlias
in l1keysByDbAlias:
340 l1NamesByDbAlias[dbAlias] =
getL1PskNames(l1keysByDbAlias[dbAlias], dbAlias = dbAlias)
341 for dbAlias
in hltkeysByDbAlias:
342 hltNamesByDbAlias[dbAlias] =
getHLTPskNames(hltkeysByDbAlias[dbAlias], dbAlias = dbAlias)
347 dbAlias = run.data[
'Release'][0][
"db"]
349 smk = run.data[
'SMK'][0].value
350 dbAlias =
"TRIGGERDB_RUN3" if smk>=3000
else "TRIGGERDBDEV1_I8"
351 l1names = l1NamesByDbAlias[dbAlias]
352 hltnames = hltNamesByDbAlias[dbAlias]
354 l1names = l1namesRun2
355 hltnames = hltnamesRun2
357 l1names = l1namesRun1
358 hltnames = hltnamesRun1
359 if not (
'L1 PSK' in run.stats
and 'HLT PSK' in run.stats):
363 for l1key, l1beg, l1end
in run.stats[
'L1 PSK' ][
'blocks']:
364 for hltkey, hltbeg, hltend
in run.stats[
'HLT PSK' ][
'blocks']:
365 lbmin =
max(ic,
min(l1beg,hltbeg))
366 lbmax =
min(l1end-1,hltend-1)
367 if lbmin > lbmax
or lbmin == 0
or lbmax == 0:
371 l1name = l1names[l1key]
if l1key
else ""
372 hltname = hltnames[hltkey]
if hltkey
else ""
373 elm = (lbmin, lbmax, l1key, hltkey, l1name, hltname)
375 run.stats[
'PSK' ] = {
'blocks' :
sorted(
list(blocks)) }
385 dbfolderkey=
'COOLONL_TRIGGER::/TRIGGER/LUMI/LVL1COUNTERS',
386 channelKeys = [(-1,
'TriggerRates',
'TriggerName')]
390 from CoolRunQuery.AtlRunQuerySelectorWorker
import SelectorWorker
391 self.
trigSel = SelectorWorker.getRetrieveSelector(
'trigger',
'TriggerSelector')
393 self.
trigSel.addShowTriggerPattern(
','.
join(trigger))
396 if pattern==
"" or pattern==
"L1":
397 pattern=
"L1_EM5,L1_TAU5,L1_XE10,L1_J5,L1_MBTS_4_4,L1_MU6"
398 self.
trigSel.addShowTriggerPattern(pattern)
399 self.
trigger += pattern.split(
',')
403 return "Retrieving trigger rates for %r" %
','.
join(self.
trigger)
410 fL1R = coolDbConn.GetDBConn(self.schema,db=Selector.condDB()).getFolder(self.folder)
413 menu = r.result[
'TriggerMenu']
415 namelookup = 256*[
'']
418 if not tr.name.startswith(
"L1_"):
421 namelookup[ch] = tr.name
422 channellist.append(ch)
424 if len(channellist)>50:
425 r.addResult(self.ResultKey()[0],
"Too many L1 triggers requested (%i), maximum is 50" % len(channellist))
428 if len(channellist)==0:
429 r.addResult(self.ResultKey()[0],
"No match")
434 chanselL1 = cool.ChannelSelection(ch,ch,cool.ChannelSelection.sinceBeforeChannel)
435 for ch
in channellist[1:]:
436 chanselL1.addChannel(ch)
438 iovmin=(r.runNr << 32)+0
439 iovmax=((r.runNr+1) << 32)-1
441 countsholder = defaultdict(list)
444 objs = fL1R.browseObjects( iovmin, iovmax, chanselL1)
445 while objs.goToNext():
446 obj=objs.currentRef()
448 countsholder[namelookup[ch]].
append((obj.since()&0xFFFFFFFF,
449 obj.payloadValue(
'BeforePrescale'),
450 obj.payloadValue(
'AfterPrescale'),
451 obj.payloadValue(
'L1Accept')))
453 r.addResult(self.ResultKey()[0], countsholder)
455 duration = time() - start
457 if self.applySelection:
458 print (
" ==> %i runs found (%.2f sec)" % (len(runlist),duration))
460 print (
" ==> Done (%g sec)" % duration)
472 from CoolRunQuery.AtlRunQuerySelectorWorker
import SelectorWorker
473 SelectorWorker.getRetrieveSelector(
'trigkey',
'TrigKeySelector')
474 SelectorWorker.getRetrieveSelector(
'l1trigkey',
'L1TrigKeySelector')
475 SelectorWorker.getRetrieveSelector(
'hlttrigkey',
'HLTTrigKeySelector')
477 super(TriggerSelector,self).
__init__(name=name,
478 dbfolderkey=
'COOLONL_TRIGGER::/TRIGGER/HLT/Menu',
479 channelKeys = [(-1,
'TriggerMenu',
'ChainName')])
493 if triggerpattern==
"":
499 return 'SELOUT Checking if the trigger name matches "%s"' % self.
triggers
506 runl1psks is a dict run -> [l1psk,...]
508 from CoolRunQuery.utils.AtlRunQueryTriggerUtils
import getL1Prescales
510 for run_number, pss
in runl1psks.items():
512 l1pscache[(run_number,l1psk)] =
getL1Prescales(l1psk,run_number = run_number)
517 runhltpsks is a dict run -> [hltpsk,...]
519 from CoolRunQuery.utils.AtlRunQueryTriggerUtils
import getHLTPrescales
522 for run_number, pss
in runhltpsks.items():
524 l2pscache[(run_number,psk)], efpscache[(run_number,psk)] =
getHLTPrescales(psk, run_number = run_number)
526 return l2pscache, efpscache
530 from CoolRunQuery.utils.AtlRunQueryTriggerUtils
import getL1Menu, getHLTMenu
533 l2menucache = defaultdict(dict)
534 efmenucache = defaultdict(dict)
537 for smk
in set(smks):
541 l1menucache[smk] = l1items
545 d2 = l2menucache[smk] = dict([(c.counter,c)
for c
in l2chains])
548 for chain
in efchains:
549 if chain.lowername==
"":
552 if (cc
in d2)
and (chain.lowername == d2[cc].name):
554 chain.lowercounter = cc
556 for l2ch
in l2chains:
557 if chain.lowername == l2ch.name:
559 chain.lowercounter = l2ch.counter
562 for chain
in l2chains:
563 if chain.lowername ==
"":
565 for l1item
in l1items:
566 if l1item
and chain.lowername == l1item.name:
568 chain.lowercounter = l1item.counter
572 for item
in l1items + l2chains + efchains:
578 for item
in l1items + l2chains + efchains:
582 return l1menucache, l2menucache, efmenucache
600 smks = [
int(r.result[
'SMK'])
for r
in runlist]
604 runl1psks[r.runNr] = [l1psk
for (l1psk,firstlb,lastlb)
in r.stats[
'L1 PSK'][
'blocks']]
605 runhltpsks[r.runNr] = [hltpsk
for (hltpsk,firstlb,lastlb)
in r.stats[
'HLT PSK'][
'blocks']]
615 smk =
int(run.result[
'SMK'])
616 psks = [(x[2],x[3])
for x
in run.stats[
'PSK'][
'blocks']]
618 l1items = [it
for it
in l1menucache[smk]
if it
and (it.forshow
or it.forselect)]
619 l2chains = [ch
for ch
in l2menucache[smk].
values()
if (ch.forshow
or ch.forselect)
and not ch.multiseeded]
620 efchains = [ch
for ch
in efmenucache[smk].
values()
if (ch.forshow
or ch.forselect)
and not ch.multiseeded
and ch.lower
and not ch.lower.multiseeded]
622 value = defaultdict(list)
625 for l1psk,hltpsk
in psks:
629 itemps = l1pscache[(run.runNr,l1psk)][item.counter]
630 value[item].
append(itemps
if itemps>=0
else -1)
632 for l2chain
in l2chains:
633 for l1psk,hltpsk
in psks:
634 if not l1psk
or not hltpsk:
635 value[l2chain].
append(
None)
637 chainps,chainpt = l2pscache[(smk,hltpsk)][l2chain.counter]
641 chainps *= l1pscache[(run.runNr,l1psk)][l2chain.lowercounter]
642 value[l2chain].
append(chainps
if chainps>=0
else -1)
644 for efchain
in efchains:
645 l2chain = efchain.lower
646 for l1psk,hltpsk
in psks:
647 if not l1psk
or not hltpsk:
648 value[efchain].
append(
None)
650 chainps,chainpt = efpscache[(smk,hltpsk)][efchain.counter]
654 l2chainps,l2chainpt = l2pscache[(smk,hltpsk)][l2chain.counter]
659 chainps *= l1pscache[(run.runNr,l1psk)][l2chain.lowercounter]
660 value[efchain].
append(chainps
if chainps>=0
else -1)
663 for tr,pslist
in value.items():
664 if not any([ps
is None or ps>=0
for ps
in pslist]):
669 newrunlist += [run.runNr]
671 run.addResult(self.ResultKey()[0], value)
673 runlist = [r
for r
in runlist
if r.runNr
in newrunlist]
675 duration = time() - start
678 print (
" ==> %i runs found (%.2f sec)" % (len(runlist),duration))
680 print (
" ==> Done (%g sec)" % duration)
698 form += [
"%s (%s)" % (v[0],
','.
join(v[1]))]
700 form += [
"%s (%s|%s)" % v]
701 return ', '.
join(form)
705 for chain
in triggers: