ATLAS Offline Software
ConditionsToolSetterFastReduction.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
2 
3 """Instantiates TrigJetHypoToolConfig_fastreduction AlgTool
4 from a hypo tree."""
5 
6 from __future__ import print_function
7 
8 from collections import defaultdict
9 
10 from AthenaCommon.Logging import logging
11 log = logging.getLogger( 'ConditionsToolSetterFastReduction' )
12 
13 def is_leaf(node):
14  return node.scenario in ('simple', 'dijet', 'qjet', 'agg')
15 
16 
17 def is_inner(node):
18  return node.scenario in ('root', 'all')
19 
20 
22 
23  """Visitor to set instantiated AlgTools to a jet hypo tree"""
24 
25 
26  def __init__(self, algToolFactory):
27 
28  self.algToolFactory = algToolFactory
29 
30  # map conaining parent child ids for the node
31  self.treeMap = {0: 0}
32 
33  # map containing the a list of Condition factory AlgTools for scenario
34  self.conditionMakers = defaultdict(list)
35 
36  def _set_conditions(self, node):
37  """attach Conditions to leaf nodes"""
38 
39  self._mod_leaf(node)
40 
41  for cn in node.children:
42  self._set_conditions(cn)
43 
44 
45  def _make_el_condition_tools(self, conf_dict):
46  """conf_dict: a dict containing names of elemental conditions
47  and min, max valies. These will be used to instantiate
48  conditon building AlgTools, one for eac conditon
49 
50  for 2j80_2j60, the dictionaries are:
51  {'et': {'min': '80000.0', 'max': 'inf'},
52  'eta': {'min': '0.0', 'max': '3.2'}}
53 
54  and
55 
56 
57  {'et': {'min': '60000.0', 'max': 'inf'},
58  'eta': {'min': '0.0', 'max': '3.2'}})
59 
60  """
61 
62  condition_tools = [] # a list of AlgTools that build elemental Conds.
63 
64  for k, v in conf_dict.items(): # loop over elemental conditions
65  # k in the condition name, v contains its min, max values.
66 
67  # create the AlgTool that will build the elemental condition
68  condition_tool = self.algToolFactory(k)
69  for lim, val in v.items(): # lim: min, max
70  setattr(condition_tool, lim, val)
71 
72  # SPECIAL CASE: Moment tool needs the name of the
73  # moment as well as the min. max cuts:
74  if (k.startswith ('mom')):
75  moment = k[len('mom'):]
76  if moment in self.JetMoments:
77  condition_tool.moment = self.JetMoments[moment]
78  else: raise RuntimeError('%s not supported' % (moment))
79 
80  # END SPECIAL CASE
81 
82  condition_tools.append(condition_tool)
83 
84  return condition_tools
85 
86 
87  def _make_filter_condition_tool(self, node):
88 
89  """Condition filters use a list of CompoundCondition containing
90  single jet elemental conditions select a subset of the reco
91  jets to send to the a Condition"""
92 
93  el_condition_tools = []
94 
95  for fc in node.filter_dicts:
96 
97  assert len(fc) == 1 # 1 elemental condition
98  el_condition_tools.extend(self._make_el_condition_tools(fc))
99 
100  condition_tool = self.algToolFactory('repeated')
101 
102  condition_tool.conditionMakers = el_condition_tools
103  condition_tool.multiplicity = 1
104 
105  return condition_tool
106 
107 
109  """For each element of node.conf_attrs, construct a
110  ConditionContainer. Example for chain HLT_2j80_3j60_L1J15:
111 
112  First leaf node has
113  conf_attrs [1]:
114  (defaultdict(<class 'dict'>, {
115  'et': {'min': '80000.0', 'max': 'inf'},
116  'eta': {'min': '0.0', 'max': '3.2'}}), 2)
117 
118  Second leaf node has
119  conf_attrs [1]:
120  (defaultdict(<class 'dict'>, {'et': {'min': '60000.0', 'max': 'inf'},
121  'eta': {'min': '0.0', 'max': '3.2'}}), 3)
122  """
123 
124  # compound_condition_tools:
125  # elemental condition maker AlgToolshelper by the compound condition
126  # AlgTool
127  outer_condition_tools = []
128 
129  # loop over elements of node.conf_attrs. The elements are (dict, int)
130  # int is multiplicity, dict holds Condition parameters.
131 
132  assert len(node.conf_attrs) == 1
133  mult = node.multiplicity
134  for i in range(len(node.conf_attrs)):
135  c = node.conf_attrs[i]
136  cpi = ''
137 
138  if node.chainpartinds:
139  cpi = node.chainpartinds[i]
140 
141  el_condition_tools = self._make_el_condition_tools(c)
142 
143  # create condition from elemental conditions
144  condition_tool =self.algToolFactory('repeated')
145 
146  if cpi:
147 
148  # convert label from string to int for more efficient
149  # processing in C++ land.
150  condition_tool.chainPartInd = int(cpi[len('leg'):])
151 
152  condition_tool.conditionMakers = el_condition_tools
153  condition_tool.multiplicity = mult
154  # add condition container to list
155  outer_condition_tools.append(condition_tool)
156 
157  return outer_condition_tools
158 
159 
160  def _mod_leaf(self, node):
161  """ Add Condition tools to For a leaf node."""
162 
163  if not is_leaf(node):
164  return
165 
166  # parameters: (10et,0eta320)(20et)
167  # conf_attrs: [2]: (is a list of length 2)
168  # defaultdict(<type 'dict'>, {'et': {'max': 'inf', 'min': '10000.0'},
169  # 'eta': {'max': '3.2', 'min': '0.0'}})
170  # defaultdict(<type 'dict'>, {'et': {'max': 'inf', 'min': '20000.0'}})
171 
172 
173  # make a config tool and provide it with condition makers
174 
175 
176  node.compound_condition_tools = self._make_compound_condition_tools(
177  node)
178 
179  node.filter_condition_tool = self._make_filter_condition_tool(
180  node)
181 
182  def report(self):
183  return str(self.algToolFactory)
184 
185  def _fill_tree_map(self, node, tmap):
186  tmap[node.node_id] = node.parent_id
187  for cn in node.children:
188  self._fill_tree_map(cn, tmap)
189 
190 
191  def _fill_conditions_map(self, node, cmap, fmap):
192  if is_leaf(node):
193 
194  assert (len(node.compound_condition_tools) == 1)
195  cmap[node.node_id] = node.compound_condition_tools[0]
196 
197  fmap[node.node_id] = node.filter_condition_tool
198 
199  else:
200 
201  cmap[node.node_id] = self.algToolFactory('repeated')
202  cmap[node.node_id].conditionMakers = [self.algToolFactory('all')]
203  cmap[node.node_id].multiplicity = 1
204 
205  fmap[node.node_id] = self.algToolFactory('repeated')
206  fmap[node.node_id].conditionMakers = []
207  fmap[node.node_id].multiplicity = 1
208 
209 
210  for cn in node.children:
211  self._fill_conditions_map(cn, cmap, fmap)
212 
213 
214  def _map_2_vec(self, amap):
215 
216  vec = [0 for i in range(len(amap))]
217  for nid, value in amap.items():
218  vec[nid] = value
219  return vec
220 
221  def _check_scenarios(self, node):
222  if not(is_inner(node) or is_leaf(node)):
223  raise RuntimeError(
224  'ConditionsToolSetter: illegal scenario: %s' % node.scenario)
225 
226  for cn in node.children:
227  self._check_scenarios(cn)
228 
229  def mod(self, tree):
230  """Entry point for this module.
231  Modifies a (usually compound) hypo tree node to
232  reduce it to form from whuch the treevector, and conditionsVector
233  These will be used to initialise FastReductionMatcher.
234 
235  In particular: all leaf nodes will have a single ConmpoundCondition
236  All other nodes will be assigned an AcceptAll condition.
237  """
238 
239  # navigate the tree filling in node-parent and node- Condtion factory
240  # relations
241 
242  self._check_scenarios(tree)
243 
244  # add Condition builders to leaf nodes.
245  self._set_conditions(tree)
246 
247 
248  tree_map = {} # tree of node indices
249  self._fill_tree_map(tree, tree_map)
250 
251  treeVec = self._map_2_vec(tree_map)
252 
253  conditionsMap = {}
254  filterConditionsMap = {}
255 
256  self._fill_conditions_map(tree, conditionsMap, filterConditionsMap)
257 
258  # conditionVec is an attribute as it will be used directly
259  # to make prefilter tools, so hold onto it here
260  self.conditionMakersVec = self._map_2_vec(conditionsMap)
261  filterConditionsVec = self._map_2_vec(filterConditionsMap)
262 
263  # make a config tool and provide it with condition makers
264  config_tool = self.algToolFactory('fastreduction')
265 
266 
267  config_tool.conditionMakers = self.conditionMakersVec
268  config_tool.filtConditionsMakers = filterConditionsVec
269  config_tool.treeVector = treeVec
270  self.config_tool = config_tool
271 
python.ConditionsToolSetterFastReduction.ConditionsToolSetterFastReduction._make_filter_condition_tool
def _make_filter_condition_tool(self, node)
Definition: ConditionsToolSetterFastReduction.py:87
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
python.ConditionsToolSetterFastReduction.ConditionsToolSetterFastReduction.algToolFactory
algToolFactory
Definition: ConditionsToolSetterFastReduction.py:28
python.ConditionsToolSetterFastReduction.ConditionsToolSetterFastReduction._mod_leaf
def _mod_leaf(self, node)
Definition: ConditionsToolSetterFastReduction.py:160
python.ConditionsToolSetterFastReduction.ConditionsToolSetterFastReduction.treeMap
treeMap
Definition: ConditionsToolSetterFastReduction.py:31
python.ConditionsToolSetterFastReduction.ConditionsToolSetterFastReduction._fill_conditions_map
def _fill_conditions_map(self, node, cmap, fmap)
Definition: ConditionsToolSetterFastReduction.py:191
plotBeamSpotVxVal.range
range
Definition: plotBeamSpotVxVal.py:195
python.ConditionsToolSetterFastReduction.ConditionsToolSetterFastReduction._map_2_vec
def _map_2_vec(self, amap)
Definition: ConditionsToolSetterFastReduction.py:214
python.ConditionsToolSetterFastReduction.ConditionsToolSetterFastReduction.conditionMakersVec
conditionMakersVec
Definition: ConditionsToolSetterFastReduction.py:260
python.ConditionsToolSetterFastReduction.ConditionsToolSetterFastReduction._make_el_condition_tools
def _make_el_condition_tools(self, conf_dict)
Definition: ConditionsToolSetterFastReduction.py:45
python.ConditionsToolSetterFastReduction.ConditionsToolSetterFastReduction.report
def report(self)
Definition: ConditionsToolSetterFastReduction.py:182
python.ConditionsToolSetterFastReduction.ConditionsToolSetterFastReduction._check_scenarios
def _check_scenarios(self, node)
Definition: ConditionsToolSetterFastReduction.py:221
python.ConditionsToolSetterFastReduction.ConditionsToolSetterFastReduction._set_conditions
def _set_conditions(self, node)
Definition: ConditionsToolSetterFastReduction.py:36
python.ConditionsToolSetterFastReduction.is_leaf
def is_leaf(node)
Definition: ConditionsToolSetterFastReduction.py:13
python.ConditionsToolSetterFastReduction.ConditionsToolSetterFastReduction
Definition: ConditionsToolSetterFastReduction.py:21
python.ConditionsToolSetterFastReduction.is_inner
def is_inner(node)
Definition: ConditionsToolSetterFastReduction.py:17
pickleTool.object
object
Definition: pickleTool.py:30
python.ConditionsToolSetterFastReduction.ConditionsToolSetterFastReduction.__init__
def __init__(self, algToolFactory)
Definition: ConditionsToolSetterFastReduction.py:26
str
Definition: BTagTrackIpAccessor.cxx:11
python.ConditionsToolSetterFastReduction.ConditionsToolSetterFastReduction._fill_tree_map
def _fill_tree_map(self, node, tmap)
Definition: ConditionsToolSetterFastReduction.py:185
python.ConditionsToolSetterFastReduction.ConditionsToolSetterFastReduction.conditionMakers
conditionMakers
Definition: ConditionsToolSetterFastReduction.py:34
python.ConditionsToolSetterFastReduction.ConditionsToolSetterFastReduction._make_compound_condition_tools
def _make_compound_condition_tools(self, node)
Definition: ConditionsToolSetterFastReduction.py:108
python.ConditionsToolSetterFastReduction.ConditionsToolSetterFastReduction.config_tool
config_tool
Definition: ConditionsToolSetterFastReduction.py:270
python.ConditionsToolSetterFastReduction.ConditionsToolSetterFastReduction.mod
def mod(self, tree)
Definition: ConditionsToolSetterFastReduction.py:229