ATLAS Offline Software
selection.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
2 
3 from DQUtils.quick_retrieve import make_fieldselection, make_selection_vector
4 
5 from pyparsing import (Word, Literal, oneOf, QuotedString, Optional,
6  operatorPrecedence, opAssoc, nums, alphanums, StringStart, StringEnd,
7  Combine, CaselessLiteral)
8 
9 # Taken from "Good usage of setWhitespaceChars" on pyparsing's discussion
11  if t.is_float:
12  return float(t[0])
13  else:
14  return int(t[0])
15 
16 # Number defintion
17 sign = oneOf('+ -')
18 integer = Word(nums)
19 
20 number = Combine(
21  Optional(sign)
22  + ( ( integer + Optional('.')('is_float') + Optional(integer))
23  | ( Literal('.')('is_float') + Optional(integer))
24  )
25  + Optional( CaselessLiteral('E') + Optional(sign) + integer)('is_float')
26  ).setParseAction(convertNumber)
27 
28 boolValue = (Literal("True") .setParseAction(lambda t: True) |
29  Literal("False").setParseAction(lambda t: False))
30 stringValue = QuotedString("'") | QuotedString('"')
31 value = boolValue | number | stringValue
32 
33 variable = Word(alphanums, min=1)
34 
35 def make_parser(payload_specification):
36  """
37  The parser has to be payload_specification specific because it needs to know
38  the types of the records to build a FieldSelection.
39  """
40 
41  def make_selection(string, location, tokens):
42  variable, comparator, value = tokens
43  if payload_specification and variable not in payload_specification:
44  raise RuntimeError("%s is not a value in the folder payload "
45  "specification")
46  elif not payload_specification:
47  return (variable, comparator, value)
48  typeid = payload_specification[variable].storageType().id()
49  return make_fieldselection(variable, typeid, comparator, value)
50 
51  from PyCool import cool
52  EQ = Literal("==").setParseAction(lambda t: cool.FieldSelection.EQ)
53  NE = Literal("!=").setParseAction(lambda t: cool.FieldSelection.NE)
54  GE = Literal(">=").setParseAction(lambda t: cool.FieldSelection.GE)
55  LE = Literal("<=").setParseAction(lambda t: cool.FieldSelection.LE)
56  GT = Literal(">") .setParseAction(lambda t: cool.FieldSelection.GT)
57  LT = Literal("<") .setParseAction(lambda t: cool.FieldSelection.LT)
58  comparator = EQ | NE | GE | LE | GT | LT
59  operand = (variable + comparator + value).setParseAction(make_selection)
60 
61  orop = Literal("or").suppress()
62  andop = Literal("and").suppress()
63 
64  def logic_builder(connective):
65  def thunk(string, location, tokens):
66  vec = make_selection_vector()
67  for token in tokens[0]:
68  vec.push_back(token)
69 
70  return cool.CompositeSelection(connective, vec)
71  return thunk
72 
73  expr = StringStart() + operatorPrecedence( operand,
74  [(andop, 2, opAssoc.LEFT, logic_builder(cool.CompositeSelection.AND)),
75  (orop, 2, opAssoc.LEFT, logic_builder(cool.CompositeSelection.OR)),]
76  ) + StringEnd()
77 
78  def go(selection):
79  return expr.parseString(selection)[0]
80  return go
81 
82 def make_browse_objects_selection(folder, selection):
83  parser = make_parser(folder.folderSpecification().payloadSpecification())
84  return parser(selection)
python.CaloScaleNoiseConfig.parser
parser
Definition: CaloScaleNoiseConfig.py:75
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
python.quick_retrieve.make_selection_vector
make_selection_vector
Definition: quick_retrieve.py:20
python.selection.convertNumber
def convertNumber(t)
Definition: selection.py:10
python.doZLumi.go
def go(fname)
Definition: doZLumi.py:78
id
SG::auxid_t id
Definition: Control/AthContainers/Root/debug.cxx:220
python.selection.make_browse_objects_selection
def make_browse_objects_selection(folder, selection)
Definition: selection.py:82
python.selection.make_parser
def make_parser(payload_specification)
Definition: selection.py:35
python.quick_retrieve.make_fieldselection
make_fieldselection
Definition: quick_retrieve.py:19
readCCLHist.float
float
Definition: readCCLHist.py:83