14 from __future__
import print_function
15 from functools
import reduce
18 from CoolRunQuery.utils.AtlRunQueryLookup
import InitDetectorMaskDecoder, DecodeDetectorMask, DQChannels
20 from DQDefects
import DefectsDB
23 dqFolderList = [
'SHIFTOFL',
'DQCALCOFL',
'DQMFOFL',
'DQMFOFLH',
'DCSOFL',
'TISUMM',
'LBSUMM',
'MUONCALIB',
24 'DQMFONL',
'DQMFONLLB',
'SHIFTONL' ]
34 self.
sortedKeys = [
"run",
"time",
"duration",
"events",
"allevents",
"projectTag",
"partition",
"readyforphysics",
35 "trigkeys",
"trigger",
"release",
36 "ami",
"magnets",
"larcond",
"db",
"ctag",
"lhc",
"lumi",
"dq",
37 "streams",
"detector",
"datasets",
"all",
"summary",
"dqeff",
"cosmics",
"heavyions" ]
40 'r(uns) [format: "run 91000", "runs 91000-92000", "runs 91000-(+)" (this run and all before (after))]',
""),
42 'all [format: only available as "show" option]',
""),
44 'summary [format: only available as "show" option]',
""),
46 'dqsum(mary) [format: only available as "show" option]',
""),
48 'dqpl(ots) [format: only available as "show" option]',
""),
50 'dqeff [format: only available as "show" option]',
""),
52 'ev(ents) [format: "events 10000+", ... (same as for run number)]',
""),
54 'alle(vents) [format: only available as "show" option]',
""),
56 't(ime) [format: "time 10.9.2008-30.9.2008", "time 15.10.2008-(+)" (this date and all before (after))]',
""),
58 'dur(ation) [format: "dur 10d/h/m/s+/-", "2000s+" (run duration more than 2000 sec), "dur 3h-" (less than 3 hours)]',
""),
60 'm(agnet) [format: "magnet s(olenoid)", "magnet t(oroid)", "not magnet t(oroid)", ...]',
""),
62 'det(ector) [format: "detector Pixel B" (for Pixel Barrel), "detector Pixel" (for Pixel B & EC), "all" (for all detectors, detector mask = 72551459979255)',
""),
64 'smk [format: "smk 398", "smk 398,399 (super-master-key labelling trigger menu, format like for run ranges)]',
""),
66 'trigk(eys) [format: "tkeys" (show all trigger keys of run: SMK, L1 and HLT prescale keys)]',
""),
68 'rel(ease) [format: "release 15.1.*" ',
""),
70 'ptag [format: "ptag data08_cos,data08_cosmag,data09_cos", "ptag data08_cos*,data09_cos" (note: the projectTag in dataset name / denoted "filenamtTag" in COOL)]',
"data0*,data1*,data2*"),
72 'p(artition) [format: "partition ATLAS"]',
"ATLAS"),
74 'ready(forphysics) [format: "readyforphysics T(rue)"]',
""),
76 'db [format: "db <DB>, where <DB> is either DATA, MC, COMP200, CONDBR2, or OFLP200',
""),
78 'ctag [format: "ctag COMCOND-HLTC-001-00" ',
""),
80 'st(reams) [format: "stream RPCwBeam,TGCwBeam", "stream physics*" ',
""),
82 'dq [format: "dq <FLAG> <status>", where the data quality status is "g(reen)", "y(ellow)", "r(ed)", "u(known)", example: dq PIXB y+ (means yellow or green status)]',
""),
84 'tr(igger) [format: "trigger *Electron*" ',
""),
86 'lu(minosity) [format: "lumi FOLDER", default: LBLESTOFL]" ',
""),
88 'olc [format: "olc" (OLC = Online Luminosity Calculator)]" ',
""),
90 'bs [format: "bs"]" ',
""),
92 'bpm [format: "show bpm"]" ',
""),
94 'da(tasets) [format: show datasets *NTUP*"]" ',
""),
96 'ami [format: show ami"]" ',
""),
98 'lar(cond) [format: larcond nsamples 7 / show larcond"]" ',
""),
100 'lhc [format: lhc fill 7 / show lhc"]" ',
""),
102 'trigr(ates) [format: sh trigrates L1_EM*"]" ',
""),
104 'dqsumgrl [format: "dqsumgrl PHYS_StandardGRL_All_Good_25ns", Define tolerable/intolerable defects for DQ summary relative to GRL or other virtual defect, default is PHYS_StandardGRL_All_Good_25ns]',
"PHYS_StandardGRL_All_Good_25ns"),
106 'cos(mics) [format: only available as "show" option]',
""),
108 'heavy(ions) [format: only available as "show" option]',
""),
110 'logictag [format: "logictag "HEAD", Define logic tag for defect database used for DQ summary relative to GRL or other virtual defect, default is "HEAD"]',
"HEAD"),
112 'defecttag [format: "defecttag "HEAD", Define defect tag for defect database used for DQ summary relative to GRL or other virtual defect, default is "HEAD"]',
"HEAD") }
120 print (
'Parser usage: python %s <string_argument>' % sys.argv[0])
122 print (
'A query starts with the keyword "f(ind)", several queries can be combined with "and", "(and) not"')
123 print (
'The requested response information is given through the keyword "sh(ow)" (the query must be terminated by a "/")')
125 print (
'Query words:')
129 print (
'Parts of a keyword written in paranthesis are optional')
133 print (
' find run 90272 and runs 90275-91900 and magnet toroid and magnet solenoid / show run and events')
134 print (
' f r 90272 and r 90275-91900 and m t and m s / sh r and ev [same as above in short-hand version]')
135 print (
' find runs 90275-91900 and not run 90280 and not dq EMEC r and show run and dq EMEC' )
139 return n.replace(
'k',
'000').
replace(
'm',
'000000')
144 ptag, period = name.split(
'.')
147 letter = period[period.find(
'period')+6:][0]
148 return ptag.strip(), period.strip(), letter
153 arg: 'run data10_7TeV.periodA' or 'run periodA' (where 'data11_7TeV' is assumed)
154 # or 'data10_7TeV.periodA-periodC' or 'data10_7TeV.periodA,data10_7TeV.periodB,...'
155 # This is case sensitive !!
157 arg = arg.split(
None,1)[1]
159 from CoolRunQuery.AtlRunQueryInterpretDataPeriods
import GetRuns
163 if len(list_of_runs)==0:
164 print (
"No runs matching pattern")
167 return "--run " +
','.
join([
str(r)
for r
in list_of_runs])
175 return "--" + atlqarg.strip() +
' "' + rge.strip().
replace(
' ',
'') +
'"'
182 return "--" + atlqarg.strip() +
' "' + rge.strip().
replace(
' ',
'') +
'"'
186 arg = arg.partition(
' ')[2]
187 arg = arg.replace(
'%',
'*')
188 arg = arg.strip().
replace(
' ',
'')
190 atlqarg = atlqarg.strip()
191 if atlqarg==
'db' and (arg==
'MC' or arg==
'OFLP'):
195 if atlqarg ==
'readyforphysics' and not arg:
199 if atlqarg ==
'dqsumgrl' and not arg:
200 arg =
'PHYS_StandardGRL_All_Good_25ns'
203 if atlqarg ==
'logictag' and not arg:
205 if atlqarg ==
'defecttag' and not arg:
208 return '--%s "%s"' % (atlqarg, arg)
212 cmd,sep,arg = arg.partition(
' ')
213 arg = arg.replace(
'%',
'*')
217 if cmd.lower() ==
'olc' and 'lumi' in arg.lower():
223 print (
'ERROR in argument of command "olc lumi": no argument given' )
227 if cmd.lower() ==
'lumi':
230 print (
'ERROR in argument of command "olc lumi": no argument given' )
234 if cmd.lower() ==
'lhc':
235 if 'stablebeams' in arg.lower():
236 arg,sep,val = arg.partition(
' ')
237 val = val.strip().
upper()
238 if val ==
'1' or val ==
'T':
240 elif val ==
'0' or val ==
'F':
242 elif val !=
'TRUE' and val !=
'FALSE':
243 print (
'ERROR in argument of command "lhc": "%s". Value must be boolean (true/false or 1/0)' % val)
247 atlqarg = atlqarg.strip()
248 if atlqarg==
'db' and (arg==
'MC' or arg==
'OFLP'):
251 return '--%s "%s"' % (atlqarg, arg)
255 arg = arg.split()[1:]
256 retstr =
"--" + atlqarg.strip() +
' "' + arg[0].
replace(
'%',
'*')
264 key, sep, arg = arg.partition(
' ')
265 units = {
'sec': 1,
's': 1,
'm': 60,
'h': 3600,
'd': 86400 }
267 for u, fac
in units.items():
269 arg = arg.replace(u,
'')
274 arg =
"%i%s" % (
int(arg.replace(sign,
'').strip())*fac,sign)
277 self.
ParseError(
"Unit missing in duration tag: '%s' - should be 's', 'm', 'h', or 'd'" % arg )
282 key, s, arg = arg.partition(
' ')
285 if arg.strip() ==
'':
286 self.
ParseError(
'Keyword "mag(net)" requires argument' )
300 self.
ParseError(
"cannot interpret of magnetic field: '%s'" % arg )
306 return "--" + atlqarg.strip() +
' "' + newarg.strip().
replace(
' ',
'') +
'"'
312 arglist = sys.argv[1].
split(
" ")
315 for idx, item
in enumerate(arglist):
316 if item ==
"logictag":
317 logictag = arglist[idx+1]
318 if item ==
"defecttag":
319 defecttag = arglist[idx+1]
323 if defecttag
not in [
'HEAD'] + ddb.defects_tags:
324 print(
'WARNING (DQGetFolder): The defined defect tag "%s" is not defined in defect database. Will use "HEAD" tag instead!' %(defecttag))
327 if logictag
not in [
'HEAD'] + ddb.logics_tags:
328 print(
'WARNING (DQGetFolder): The defined logic tag "%s" is not defined in defect database. Will use "HEAD" tag instead!' %(logictag))
331 ddb = DefectsDB(tag=(defecttag, logictag))
334 if logictag !=
"HEAD" and defecttag !=
"HEAD":
335 logic_revision =
int(logictag.split(
"-")[-1])
336 defect_part =
"-".
join(defecttag.split(
"-")[1:])
337 htag =
"DetStatus-v%02i-%s" % (logic_revision, defect_part)
340 """interpret dq show command
341 * args are all arguments after dq codeword
343 possible formats for 'dq args':
355 D is a komma-separated list of defects or DQ-flags or *, each optionally proceeded by a ! or followed by a (defect,defect,defect,...)
356 F an folder name [default DEFECTS]
357 T a cool tag [default HEAD]
359 If no folder is specified the new defects folder is used
362 d = {
'flag':
'',
'folder':
'',
'tag':
''}
365 m = re.match(
r'(?P<flag>[!.*\w,\-\$\\\]*?)\s*?(?P<folder>\w*)#(?P<tag>[\w-]*)', args)
367 m = re.match(
r'(?P<flag>[!\w,\-\$\\\]*?)\s*?(?P<folder>\w*)#(?P<tag>[\w-]*)', args)
369 m = re.match(
r'(?P<flag>[!\w,\-\$\\\]*)\s*(?P<folder>[\w-]*)', args)
372 d.update(m.groupdict())
374 flag = d[
'flag'].lower()
375 folder = d[
'folder'].
upper()
379 folder = flag.upper()
392 folder = folder + tag
394 return flag, folder, tag
400 args = arg.partition(
' ')[2]
403 flags, folder, tag = self.
DQGetFolder( args, allowWildCards =
True )
407 if folder.startswith(
'DEFECTS'):
410 return '--show "dq DEFECTS%s" ' % tag
413 return ' '.
join([
'--show "dq %s DEFECTS%s"' % (fl.upper(), tag)
for fl
in flags.split(
',')])
416 flags = flags.replace(
'lar',
'emba,embc,emeca,emecc,heca,hecc,fcala,fcalc' )
419 flags = flags.replace(
'tile',
'tigb,tilba,tilbc,tieba,tiebc' )
422 flags = flags.replace(
'trig',
'l1cal,l1mub,l1mue,l1ctp,trcal,trbjt,trbph,trcos,trele,trgam,trjet,trmet,trmbi,trmuo,trtau,tridt' )
427 for key, dqchan
in dqitems:
428 newarg +=
'--show "dq %s %s" ' % (dqchan, folder)
431 argList = flags.split(
',')
433 a = a.strip().lower()
435 newarg +=
'--show "dq %s %s" ' % (a.upper(), folder)
437 for key, dqchan
in dqitems:
440 if dc[0:2]==
"tr" and dc[2]!=
't':
441 newarg +=
'--show "dq %s %s" ' % (dqchan, folder)
444 newarg +=
'--show "dq %s %s" ' % (dqchan, folder)
455 args = args.split()[1:]
457 hasColor = len(args)>0
and re.match(
'(n.a.|u|r|y|g|b)[+-]{0,1}$',args[-1].lower())
467 useAny = len(args)>0
and (args[0] ==
'any')
476 if folder.startswith(
'DEFECTS'):
478 newarg =
"--dq !ANY " + folder
481 dqchans = flags.upper().
split(
',')
485 for dqchan
in dqchans:
490 self.
ParseError(
'DQ flags need to be assigned a value ("n.a.", "u", "g", "y", "r"), eg, "dq PIXB,TIL yellow+" or "dq g"' )
493 flags = flags.replace(
'lar',
'emba,embc,emeca,emecc,heca,hecc,fcala,fcalc' )
494 flags = flags.replace(
'tile',
'tigb,tilba,tilbc,tieba,tiebc' )
495 flags = flags.replace(
'trig',
'l1cal,l1mub,l1mue,l1ctp,trcal,trbjt,trbph,trcos,trele,trgam,trjet,trmet,trmbi,trmuo,trtau,tridt' )
501 dqchans = [y
for (x,y)
in dqitems]
505 for dqchan
in dqchans:
511 for a
in flags.upper().
split(
','):
516 tmpdqchans = [dqchan
for (k,dqchan)
in dqitems
if a
in dqchan]
517 if 'LUMI' == a
and 'LUMIONL' in tmpdqchans:
518 tmpdqchans.remove(
'LUMIONL')
519 if 'IDVX' == a
and 'MMUIDVX' in tmpdqchans:
520 tmpdqchans.remove(
'MMUIDVX')
521 if len(tmpdqchans)==0:
522 self.
ParseError(
'Unknown DQ channel: "%s"' % a )
523 dqchans += tmpdqchans
528 for dqchan
in dqchans:
540 self.
ParseError(
'ERROR in DQ argument: "%s" --> need <Flag> AND <Status> (len is: %i)' % (atlqarg, len(args)) )
541 flag, col, folder = args
547 if not folder.startswith(
'DEFECTS'):
561 self.
ParseError(
'ERROR in DQ argument: "%s" --> uknown status: %s' % (args, col) )
582 self.
ParseError(
'ERROR in DQ argument: "%s" --> uknown status: %s' % (atlqarg, col) )
585 return "--" + atlqarg.strip() +
' "' + flag +
' ' + trcol +
' ' + folder +
'"'
588 arg = (arg.partition(
' ')[2]).lower().strip()
591 arg = arg.replace(
'any',
'').strip()
597 for sarg
in arg.split(
","):
598 for i
in range(0,len(self.dName)):
600 if i
in self.vetoedbits:
604 d = self.dName[i].lower() + self.NotInAll[i]
605 if not (
'unknown' in d
or 'removed' in d):
607 if (sarg ==
'all' and 'NotInAll' not in d)
or sarg
in d:
612 print (
'ERROR: could not find detector: "%s" in detector list' % arg)
614 print (
'Note: search is case INSENSITIVE')
624 return "--" + atlqarg +
' "%i%s' % (mask,anyflag) +
'"'
628 print (
'ERROR in argument: "%s"' % self.
const_arg)
629 print (
"%s" % errtext)
637 shortNoPar = short.replace(
'(',
'').
replace(
')',
'')
639 short = short[:short.find(
'(')]
640 matchesArg = shortNoPar.startswith(arg.lower())
and arg.lower().startswith(short)
647 firstword, findarg = findarg.split(
None,1)
648 if not 'find'.startswith(firstword):
649 self.
ParseError(
"Argument must begin with 'f(ind)'", 0 )
651 argList = [x.strip()
for x
in findarg.split(
" and ")]
659 arg = arg.replace(
"not",
"").strip()
664 self.
ParseError(
"Could not find predefined keyword for arg: '%s' in 'find' block" % (arg) )
666 short, atlqarg, function = self.
queryArgs[key][0:3]
667 newarg += function( atlqarg, arg, negation ) +
' '
674 arglist = self.
default_show + [a.strip()
for a
in showarg.split(
' and ')
if a.strip()!=
'']
678 idx = arglist.index(
'all')
679 arglist[idx:idx+1] = [
'ready',
'lhc',
'trigk',
'rel',
'streams',
'det']
681 if 'summary' in arglist:
682 arglist += [
'dur',
'allev',
'str',
'dq']
684 if any([arg.startswith(
'lhc')
for arg
in arglist]):
687 tmpArgList = [arg.startswith(
'dqsum')
or arg.startswith(
'dqpl')
for arg
in arglist]
689 idx = tmpArgList.index(
True)
690 arglist[idx+1:idx+1] = [
'ready',
'lumi',
'lhc stablebeams',
'dq',
'ptag',
'trigrates L1_EM30',
'trigrates L1_EM5',
'trigrates L1_EM12']
697 self.
ParseError(
"Could not find predefined keyword for arg: '%s' in 'show' block" % (arg) )
700 (short, atlqarg, dummy, function) = self.
queryArgs[key][0:4]
701 newarg += function( atlqarg, short, arg )
707 return '--show ' + atlqarg +
' '
710 if len(arg.split())==1:
712 return '--show olclumi --show olclbdata --show olcfillparams '
713 elif atlqarg ==
'luminosity':
714 return '--show "lhc min" --show "%s 0" ' % (atlqarg)
716 return '--show %s ' % atlqarg
719 return '--show "olclumi %s" --show olcfillparams --show olclbdata ' % arg.partition(
' ')[2]
720 elif atlqarg ==
'luminosity':
721 return '--show "lhc min" --show "%s %s" ' % (atlqarg, arg.partition(
' ')[2])
723 return '--show "%s %s" ' % (atlqarg, arg.partition(
' ')[2])
728 if len(showargs.split(
'lhc')) >= 3:
729 showargs = showargs.replace(
'--show "lhc min"',
'')
732 duples = [
'--show %s' % d.strip()
for d
in showargs.split(
'--show')
if d.strip()!=
'']
733 uniques =
reduce(
lambda first, second: (second
not in first
and first.append(second))
or first, duples, [])
734 showargs =
' '.
join(uniques)
744 findarg, showarg, otherarg = [x.strip()
for x
in (self.
const_arg.
split(
'/')+[
'',
''])[0:3]]
748 if showarg[0:2] !=
'sh':
749 self.
ParseError(
"Show block must begin with 'sh(ow)'", 0 )
750 showarg = showarg[showarg.index(
' ')+1:]
752 if ' sh ' in findarg
or ' sho ' in findarg
or ' show ' in findarg:
753 self.
ParseError(
"Show block must be separated by '/' from find condition'", 0 )
761 if '--olclumi' in queryarg
and 'stablebeams' not in queryarg:
762 queryarg +=
'--lhc "stablebeams TRUE" '
768 oargs = otherarg.split()
770 extraargs = {
'verbose' :
'--verbose'}
772 while idx < len(oargs):
775 extraargs[
'verbose'] =
'--verbose'
777 extraargs[
'noroot'] =
'--noroot'
779 extraargs[
'root'] =
'--root'
781 extraargs[
'nohtml'] =
'--nohtml'
783 extraargs[
'html'] =
'--html'
785 extraargs[
'utc'] =
'--utc'
787 extraargs[
'nogrl'] =
'--nogrl'
788 elif oa ==
'grl' or oa ==
'xmlfile':
789 extraargs[
'xmlfile'] =
'--xmlfile MyLBCollection.xml:MyLBCollection'
792 elif idx>0
and (oargs[idx-1]==
'grl' or oargs[idx-1]==
'xmlfile'):
793 extraargs[
'xmlfile'] =
'--xmlfile %s' % oa
795 print (
"Extra argument '%s' unknown. Exiting." % oa)
799 if 'verbose' in extraargs:
801 print (
' find argument: "%s"' % findarg.strip())
802 print (
' show argument: "%s"' % showarg.strip())
803 print (
' extra argument: "%s"' % otherarg.strip())
811 for key, argument, defaultvalue
in [ (x[0],x[1][1],x[1][5])
for x
in self.
queryArgs.
items()]:
812 if argument
not in queryarg
and defaultvalue !=
"":
813 queryarg +=
" --" + argument +
' "' + defaultvalue +
'"'
815 extraarg =
' '.
join(extraargs.values())
817 fullarg =
'%s %s %s' % (queryarg, shwarg, extraarg)
823 if __name__==
'__main__':
828 if len(sys.argv) <= 1:
829 print (
'No query argument given')
832 if sys.argv[1].lower() ==
'detmask':
833 print (
'Detector mask %s correspond to:\n%s' % (sys.argv[2],
DecodeDetectorMask(
int(sys.argv[2] ))))
836 if sys.argv[1].lower() ==
'help':
840 atlqueryarg = ap.ParseArgument(
' '.
join(sys.argv[1:]) )
841 print (
"\nAtlRunQuery.py %s\n" % atlqueryarg)