ATLAS Offline Software
LogicalFilterCombiner.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
2 
3 
15 
16 __doc__ = """This algorithm combines the results of one or more skim filters"""
17 __version__ = "0.0.1"
18 __author__ = "Marcello Barisonzi <Marcello.Barisonzi@desy.de>"
19 
20 import AthenaPython.PyAthena as PyAthena
21 
22 import tokenize
23 from io import StringIO
24 
25 class LogicalFilterCombiner( PyAthena.AthFilterAlgorithm ):
26 
27  def __init__ ( self, name = 'LogicalFilterCombiner', **kw ):
28 
29  kw['name'] = name
30  super(LogicalFilterCombiner, self).__init__(**kw)
33 
34  self.cmdstring = kw.get('cmdstring', '')
35  self.prefix = kw.get('prefix', '')
36 
37  # Create the algorithm dictionary
38  self.algdict = {}
39 
40  return
41 
42 
43  def initialize(self):
44  self.msg.info( '************************************' )
45  self.msg.info( '==> initialize %s...', self.name() )
46  self.msg.info( '************************************' )
47  self.cutFlowSvc().setFilterDescription(self.cutID,self.cmdstring)
48 
49  # look if parentheses are matched
50  if self.cmdstring.count("(") != self.cmdstring.count(")"):
51  self.msg.fatal("Mismatched parentheses in filter string: %s" % self.cmdstring)
52  return False
53 
54  # these parentheses are not logically correct
55  if self.cmdstring.count("{") != 0 or \
56  self.cmdstring.count("}") != 0 or \
57  self.cmdstring.count("[") != 0 or \
58  self.cmdstring.count("]") != 0:
59  self.msg.fatal("Wrong type of parentheses in filter string: %s" % self.cmdstring)
60  return False
61 
62  try:
63  tokenobj = tokenize.generate_tokens(StringIO(self.cmdstring).readline)
64 
65  self.algdict = {}
66 
67  result = []
68 
69  for toknum, tokval, _, _, _ in tokenobj:
70  if toknum == tokenize.NAME and \
71  tokval != 'or' and \
72  tokval != 'not' and \
73  tokval != 'and' and \
74  tokval != 'True' and \
75  tokval != 'False':
76 
77  if self.prefix != '':
78  tokval = "%s_%s" % (self.prefix, tokval)
79 
80  self.msg.info("Adding algorithm: %s", tokval)
81 
82 
83  _alg = None
84 
85  _alg = PyAthena.py_alg(tokval)
86 
87  self.algdict.update({tokval : _alg})
88 
89  if not _alg:
90  self.msg.error("Algorithm %s not found" , tokval)
91  return False
92  else:
93  self.msg.debug("Found algorithm: %s -> %s", tokval, _alg)
94 
95  exec('self.%s = _alg' % tokval)
96  self.cutFlowSvc().declareChildFilter(tokval,self.cutID)
97 
98  result.extend([
99  (tokenize.STRING, 'self.%s' % tokval),
100  (tokenize.OP, '.'),
101  (tokenize.STRING, 'filterPassed'),
102  (tokenize.OP, '('),
103  (tokenize.OP, ')')
104  ])
105 
106  else:
107  result.append((toknum, tokval))
108 
109  self.cmd = tokenize.untokenize(result)
110 
111  self.msg.debug("String changed internally to:\n%s", self.cmd)
112 
113  #execute command once to validate
114  eval(self.cmd)
115  except Exception as e:
116  self.msg.fatal("Not a valid Python string. Exception: %s" % e)
117  import traceback
118  self.msg.fatal(traceback.format_exc())
119  return False
120 
121  self.msg.info("Filter string validated")
122 
123  return True
124 
125  def execute(self):
126 
127  self.nEventsProcessed+=1
128  for k,v in self.algdict.iteritems():
129  self.msg.debug("Alg %s : %s", k, v.filterPassed())
130  pass
131 
132 
133 
134  response = bool(eval(self.cmd))
135  if response:
136  self.nEventsPassed+=1
137  self.msg.debug("Response is: %s", response)
138  self.setFilterPassed(response)
139  return True
140 
141  def finalize(self):
142  self.msg.info("nEventsProcessed is: %s", self.nEventsProcessed)
143  self.msg.info("nEventsPassed is: %s", self.nEventsPassed)
144  return True
145 
146 
python.LogicalFilterCombiner.LogicalFilterCombiner.cmd
cmd
Definition: LogicalFilterCombiner.py:109
grepfile.info
info
Definition: grepfile.py:38
python.LogicalFilterCombiner.LogicalFilterCombiner.__init__
def __init__(self, name='LogicalFilterCombiner', **kw)
Definition: LogicalFilterCombiner.py:27
python.LogicalFilterCombiner.LogicalFilterCombiner.nEventsProcessed
nEventsProcessed
initialize base class
Definition: LogicalFilterCombiner.py:31
python.LogicalFilterCombiner.LogicalFilterCombiner
Definition: LogicalFilterCombiner.py:25
python.Bindings.iteritems
iteritems
Definition: Control/AthenaPython/python/Bindings.py:812
python.LogicalFilterCombiner.LogicalFilterCombiner.prefix
prefix
Definition: LogicalFilterCombiner.py:35
XMLtoHeader.count
count
Definition: XMLtoHeader.py:85
python.LogicalFilterCombiner.LogicalFilterCombiner.algdict
algdict
Definition: LogicalFilterCombiner.py:38
python.LogicalFilterCombiner.LogicalFilterCombiner.finalize
def finalize(self)
Definition: LogicalFilterCombiner.py:141
LArG4FSStartPointFilter.exec
exec
Definition: LArG4FSStartPointFilter.py:103
python.LogicalFilterCombiner.LogicalFilterCombiner.nEventsPassed
nEventsPassed
Definition: LogicalFilterCombiner.py:32
python.LogicalFilterCombiner.LogicalFilterCombiner.cmdstring
cmdstring
Definition: LogicalFilterCombiner.py:34
python.LogicalFilterCombiner.LogicalFilterCombiner.execute
def execute(self)
Definition: LogicalFilterCombiner.py:125
debug
const bool debug
Definition: MakeUncertaintyPlots.cxx:53
dqt_zlumi_pandas.update
update
Definition: dqt_zlumi_pandas.py:42
python.LogicalFilterCombiner.LogicalFilterCombiner.initialize
def initialize(self)
Definition: LogicalFilterCombiner.py:43
xAOD::bool
setBGCode setTAP setLVL2ErrorBits bool
Definition: TrigDecision_v1.cxx:60
error
Definition: IImpactPoint3dEstimator.h:70