69def TrigComboHypoToolFromDict(flags, chainDict):
70 from AthenaMonitoringKernel.GenericMonitoringTool import GenericMonitoringTool
71
72 chainName = chainDict['chainName']
73 log.debug("[TrigComboHypoToolFromDict] chain %s, combo hypos to be processed: %s, t", chainName, chainDict['extraComboHypos'])
74
75 topoDefs = []
76
77
78
79
80
81 theregex = fr"(\d*)({'|'.join(allowed_obs.keys())})([{topoLegIndices}])([{topoLegIndices}])(\d*)"
82 matcher = re.compile(theregex)
83
84 for iTopo, topoInfo in enumerate(chainDict['extraComboHypos']):
85 log.debug("[TrigComboHypoToolFromDict] new combo hypo for chain: %s, topoInfo = %s", chainName, topoInfo)
86
87 result = matcher.match(topoInfo)
88 if not result:
89 log.error("[TrigComboHypoToolFromDict] Topo expression %s does not conform to format (min?)(var)(legA)(legB)(max?).",topoInfo)
90 log.error("[TrigComboHypoToolFromDict] Must use leg IDs in %s, vars in {allowed_obs.keys()}",topoLegIndices)
91 raise ValueError(f"[TrigComboHypoToolFromDict] Invalid topo expression {topoInfo} received in 'extraComboHypos'!")
92
93
94 str_min, var, char_legA, char_legB, str_max = result.groups()
95
96
97 use_min = bool(str_min)
98 use_max = bool(str_max)
99 if not (use_min or use_max):
100 log.error("[TrigComboHypoToolFromDict] Topo expression %s does not specify a min or max cut value.",topoInfo)
101 raise ValueError(f"[TrigComboHypoToolFromDict] Invalid topo expression {topoInfo} received in 'extraComboHypos'!")
102
103 if var in ['dR','dphi','deta']:
104 cut_min = float(str_min)/10. if use_min else float('nan')
105 cut_max = float(str_max)/10. if use_max else float('nan')
106 else:
107 cut_min = float(str_min) if use_min else float('nan')
108 cut_max = float(str_max) if use_max else float('nan')
109
110
111 i_legA = topoLegIndices.find(char_legA)
112 i_legB = topoLegIndices.find(char_legB)
113
114
115
116 legInfo = []
117 for ileg in [i_legA,i_legB]:
118 cpart = chainDict['chainParts'][ileg]
119 legname = f"leg{ileg:03d}_{chainName}"
120 legInfo.append({
121 'index' : ileg,
122 'legname' : legname,
123 'HLTId' : string2hash(legname),
124 'isMET' : cpart['signature']=='MET',
125 'multiplicity': int(cpart['multiplicity'])
126 })
127
128
129 n_MET_legs = legInfo[0]['isMET'] + legInfo[1]['isMET']
130
131
132
133
134 skipLegCheck = False
135 if i_legA==i_legB:
136 if legInfo[0]['multiplicity'] != 2:
137 log.error("[TrigComboHypoToolFromDict] Error configuring topo for chain %s!",chainName)
138 log.error("[TrigComboHypoToolFromDict] Topo selection %s requires multiplicity 2 on leg %d, found %d!",topoInfo,i_legA,legInfo[0]['multiplicity'])
139 raise Exception("[TrigComboHypoToolFromDict] Invalid multiplicity")
140 if n_MET_legs==2:
141 log.error("[TrigComboHypoToolFromDict] Configured with the same MET leg on both sides -- impossible to satisfy!")
142 raise Exception("[TrigComboHypoToolFromDict] Identical MET legs for topo selection")
143 if len(chainDict['chainParts'])==1:
144 skipLegCheck=True
145 else:
146 for li in legInfo:
147 if li['multiplicity'] != 1:
148 log.error("[TrigComboHypoToolFromDict] Error configuring topo for chain %s!",chainName)
149 log.error("[TrigComboHypoToolFromDict] Topo selection %s requires multiplicity 1 on leg %d, found %d!",topoInfo,li['index'],li['multiplicity'])
150 raise Exception("[TrigComboHypoToolFromDict] Invalid multiplicity")
151
152
153 if n_MET_legs not in allowed_obs[var]['n_MET_legs']:
154 log.error("[TrigComboHypoToolFromDict] Attempting var %s with %d MET legs, %s allowed", var, n_MET_legs, allowed_obs[var]['n_MET_legs'])
155 raise Exception("[TrigComboHypoToolFromDict] Attempting to use the MET leg in var")
156
157 if len(chainDict['extraComboHypos'])==1:
158 monToolName = "MonTool_"+chainName
159 else:
160 monToolName = f"MonTool_{chainName}_{chainDict['extraComboHypos'][iTopo]}"
161 histNameTag = var
163 monTool.defineHistogram(histNameTag+'OfAccepted', type='TH1F', path='EXPERT',
164 title=var+" in accepted combinations; {}".format(var),
165 xbins=allowed_obs[var]['hist_nbins'],
166 xmin=allowed_obs[var]['hist_min'],
167 xmax=allowed_obs[var]['hist_max'])
168 monTool.defineHistogram(histNameTag+'OfProcessed', type='TH1F', path='EXPERT',
169 title=var+" in processed combinations; {}".format(var),
170 xbins=allowed_obs[var]['hist_nbins'],
171 xmin=allowed_obs[var]['hist_min'],
172 xmax=allowed_obs[var]['hist_max'])
173 log.debug("[TrigComboHypoToolFromDict] tool configured for hypo name: %s, topoInfo = %s", chainName, topoInfo)
174 log.debug("[TrigComboHypoToolFromDict] histName = %s", histNameTag)
175
176 if len(chainDict['extraComboHypos'])==1:
177 monTool.HistPath = f'ComboHypo/{chainName}'
178 else:
179 subDirNameTag = f"{var}leg{i_legA:03d}leg{i_legB:03d}"
180 monTool.HistPath = f'ComboHypo/{chainName}/detail_{subDirNameTag}'
181
182
183 singleTopoDef = {
184 "Variables" : var,
185 "UseMinVec" : use_min,
186 "UseMaxVec" : use_max,
187 "LowerCutVec" : cut_min,
188 "UpperCutVec" : cut_max,
189 "LegAVec" : legInfo[0]["HLTId"],
190 "LegBVec" : legInfo[1]["HLTId"],
191 "IsLegA_METVec": legInfo[0]["isMET"],
192 "IsLegB_METVec": legInfo[1]["isMET"],
193 "MonTools" : monTool,
194 }
195 topoDefs.append(singleTopoDef)
196
197
198 log.debug("[TrigComboHypoToolFromDict] tool configured for hypo name: %s, topoInfo = %s", chainName, topoInfo)
199 log.debug("[TrigComboHypoToolFromDict] var = %s", singleTopoDef['Variables'])
200 log.debug("[TrigComboHypoToolFromDict] legA = %s", singleTopoDef['LegAVec'])
201 log.debug("[TrigComboHypoToolFromDict] legB = %s", singleTopoDef['LegBVec'])
202 if use_min:
203 log.debug("[TrigComboHypoToolFromDict] min = %10.3f", singleTopoDef['LowerCutVec'])
204 if use_max:
205 log.debug("[TrigComboHypoToolFromDict] max = %10.3f", singleTopoDef['UpperCutVec'])
206
207
208
209
210 toolProps = {k:[thedef[k] for thedef in topoDefs] for k in topoDefs[0]}
211 tool = CompFactory.TrigComboHypoTool(chainName, SkipLegCheck=skipLegCheck, **toolProps)
212
213 return tool
214