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