ATLAS Offline Software
Loading...
Searching...
No Matches
GenerateMenuMT.py
Go to the documentation of this file.
1# Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
2
3import importlib, string
4
5from TriggerMenuMT.HLT.Config.Utility.HLTMenuConfig import HLTMenuConfig
6
7from AthenaCommon.Logging import logging
8log = logging.getLogger(__name__)
9
10# _maxAllowedCustomCH: this variable keeps track of the number of custom ComboHypo that are named differently as expected from the ControlFlow rule (which sets one ComboHypo per step, both named the same). These violations are already investigated and allowed because the ComboHypos are able to handle the decisions internally. Any change of this parameter needs to be discussed with experts
11_maxAllowedCustomCH = 10
12
14 return ['Streaming','Monitor','Beamspot','Cosmic', 'Calib', 'EnhancedBias']
15
17 return ['MinBias','Electron','Photon','Muon','Tau','Jet', 'Bjet','MET','UnconventionalTracking','HeavyIon']
18
20 return ['Bjet', 'Egamma', 'Combined']
21
23 return ['Streaming']
24
26 return ['Test']
27
29 return ['Bphysics']
30
36 """Standard chain filter"""
37 def __init__(self, flags):
38 for f in ("enabledSignatures", "disabledSignatures", "selectChains", "disableChains", "selectGroups", "disableGroups"):
39 # Ensure flag values have correct type
40 value = getattr(flags.Trigger, f)
41 assert isinstance(value, list), f"Flag Trigger.{f}={value!r} is not of type list"
42 # Store flag
43 setattr(self, f, value)
44
45 if self.selectGroups and self.disableGroups:
46 raise RuntimeError("Cannot set both selectGroups and disableGroups.")
47
48 if not flags.Trigger.Offline.SA.Muon.usePhaseIIGeoSetup or \
49 not flags.Trigger.Offline.SA.Muon.scheduleActsReco:
50 if self.selectGroups:
51 if 'MuonPhaseIIReco' in self.selectGroups:
52 raise RuntimeError("Muon Phase-II reconstruction software is not scheduled to run, but selectGroups includes 'MuonPhaseIIReco'.")
53 elif 'MuonPhaseIIReco' not in self.disableGroups:
54 log.info("Muon Phase-II reconstruction software is not scheduled to run, disabling Muon chains that require it.")
55 self.disableGroups.append('MuonPhaseIIReco')
56
57 def __call__(self, signame, chain):
58 # Check if the signature is enabled and not disabled
59 if signame not in self.enabledSignatures or signame in self.disabledSignatures:
60 return False
61 # Check if the chain is explicitly selected or disabled
62 if (self.selectChains and chain.name not in self.selectChains) or \
63 chain.name in self.disableChains:
64 return False
65 # Check if the chain's groups are selected or disabled
66 if self.selectGroups:
67 return any(g in self.selectGroups for g in chain.groups)
68 if self.disableGroups:
69 return not any(g in self.disableGroups for g in chain.groups)
70 return True
71
72 def __str__(self) -> str:
73 return f'FilterChainsToGenerate(enabledSignatures={self.enabledSignatures!r}, disabledSignatures={self.disabledSignatures!r}, selectChains={self.selectChains!r}, disableChains={self.disableChains!r})'
74
75
76class Singleton(type):
77 _instances = {}
78 def __call__(cls, *args, **kwargs):
79 if cls not in cls._instances:
80 cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
81 return cls._instances[cls]
82
83 def clear(cls):
84 cls._instances.clear()
85
86
87class GenerateMenuMT(metaclass=Singleton):
88 """Singleton class for the Trigger Menu"""
89 def __init__(self):
90 self.base_menu_name: str = ''
91 self.prescale_set_name: str | None = None
92
93 self.chainsInMenu = {} # signature : [chains]
94
95 self.allChainsForAlignment = []
96 self.chainDicts = []
97 self.combinationsInMenu = []
98 self.alignmentGroupsToAlign = set()
99 self.configLengthDict = {}
100
101 self.signaturesOverwritten = False
102 self.L1Prescales = None
103 self.HLTPrescales = None
104
105 self.chainFilter = None
106 self.availableSignatures = []
107
108 self.sigDicts = {}
109
110 self.chainDefModule = {} # Generate[SIG]ChainDefs module for each SIGnature
111 self.defaultFlagsForSignature = {}
112
113
114 # Define which signatures (folders) are required for each slice
115 def getRequiredSignatures(theslice):
116 allSigs = allSignatures()
117 signatureDeps = {sig:[sig] for sig in allSigs}
118 # Special cases
119 signatureDeps.update({
120 # Bjet always requires jet
121 'Bjet': ['Bjet','Jet'],
122 # Egamma contains two signatures
123 'Egamma': ['Electron','Photon'],
124 'Combined': combinedSignatures(),
125 })
126 return set(signatureDeps[theslice]+defaultSignatures()) # always allow streamers
127
128
129 def setChainFilter(self, f):
130 """Set chain filter for menu generation.
131
132 This can be any callable object taking two
133 arguments for signature and chain object and returning a boolean.
134 E.g. to only generate Egamma chains:
135 menu.setChainFilter(lambda slice,chain : slice=='Egamma').
136
137 In the special case that f is a functor with the list attributes
138 selectChains and/or disableChains, the contents will be explicitly
139 checked to be in the menu.
140 """
141 fname = f.__class__.__name__ if isinstance(f,object) else f.__name__
142 import inspect
143 if len(inspect.signature(f).parameters)!=2:
144 log.error('%s is not a valid chain filter. Function/callable needs take two arguments '
145 'for signature and chain name and return a boolean', fname)
146 else:
147 log.info('Setting chain filter to: %s', f)
148 self.chainFilter = f
149
150
151 def getChainDicts(self, flags):
152
153 def validSignature(currentSig, chainSig):
154 """Check if chain is assigned to the correct signature"""
155 reqd = GenerateMenuMT.getRequiredSignatures(currentSig)
156 isValid = chainSig.issubset( reqd )
157 log.debug("Chain signatures: %s, required signatures: %s",chainSig,reqd)
158 if not isValid:
159 log.error("Chain signatures %s not a subset of required signatures %s",set(chainSig),reqd)
160 return isValid
161
162 from TriggerMenuMT.HLT.Config.Utility.DictFromChainName import dictFromChainName
163
164 chainCounter = 0
165 invalid = False
166 for sig, chains in self.chainsInMenu.items():
167 for chain in chains:
168 log.debug("Now processing chain: %s from signature %s", chain, sig)
169 chainCounter += 1
170 chainDict = dictFromChainName(flags, chain)
171 chainDict['chainCounter'] = chainCounter
172 chainDict['prescale'] = 1 # set default chain prescale
173
174 # Pick out the folder and subsignature directories to import
175 for sigfo, subsig in chainDict['sigDicts'].items():
176 if sigfo not in self.sigDicts:
177 self.sigDicts[sigfo] = subsig
178 else:
179 for ss in subsig:
180 if ss not in self.sigDicts[sigfo]:
181 self.sigDicts[sigfo].append(ss)
182
183 self.chainDicts.append(chainDict)
184
185 if not validSignature(sig, set(chainDict['signatures'])):
186 invalid=True
187 log.error('Chain %s assigned to signature %s but creates %s',
188 chainDict['chainName'], sig, set(chainDict['signatures']))
189 if invalid:
190 raise RuntimeError('Incorrect assignment of chains to slices -- see preceding messages.')
191
192 def importSignaturesToGenerate(self):
193 """check if all the signature files can be imported and then import them"""
194
195 for sig, subSigs in self.sigDicts.items():
196 try:
197 for ss in subSigs:
198 import_module = 'TriggerMenuMT.HLT.' + sig +'.Generate' + ss + 'ChainDefs'
199 self.chainDefModule[ss] = importlib.import_module(import_module)
200
201 if ss not in self.availableSignatures:
202 self.availableSignatures.append(ss)
203
204 except ImportError:
205 log.exception('Problems when importing ChainDef generating code for %s', sig)
206 import traceback
207 traceback.print_exc()
208
209 log.info('Available signature(s) for chain generation: %s', self.availableSignatures)
210
211 return
212
213 def generateChains(self, flags):
214 all_chains = []
215 combinations_in_menu = []
216 alignmentGroups_to_align = set()
217 length_of_configs = {}
218
219 nchainDicts = len(self.chainDicts)
220 notify_increment = max(int(nchainDicts / 10),1)
221 for ichainDict, chainDict in enumerate(self.chainDicts):
222 log.debug("Next: getting chain configuration for chain %s ", chainDict['chainName'])
223 if ichainDict % notify_increment==0:
224 log.info("Generating HLT chain %d / %d", ichainDict+1, nchainDicts)
225 chainConfig,lengthOfChainConfigs = self.__generateChainConfig(flags, chainDict)
226 all_chains += [(chainDict,chainConfig,lengthOfChainConfigs)]
227
228 #update the alignment group length dictionary if we have a longer number of steps
229 #or the signature isn't registered in the dictionary yet
230 for config_length, config_grp in lengthOfChainConfigs:
231 if config_grp in length_of_configs:
232 if config_length > length_of_configs[config_grp]:
233 length_of_configs[config_grp] = config_length
234 else:
235 length_of_configs[config_grp] = config_length
236
237 # find the chains that contain more than one alignment group, to keep track
238 # of what combinations do we need to deal with.
239 # we're using sets here so we don't end up with duplicates
240 if len(set(chainDict['alignmentGroups'])) > 1:
241 combinations_in_menu += [list(set(chainDict['alignmentGroups']))]
242 for align_group in list(set(chainDict['alignmentGroups'])):
243 alignmentGroups_to_align.update([align_group])
244
245 self.allChainsForAlignment = all_chains
246 self.combinationsInMenu = combinations_in_menu
247 self.alignmentGroupsToAlign = alignmentGroups_to_align
248 self.configLengthDict = length_of_configs
249
250 return
251
252
253 def generateAllChainConfigs(self, flags):
254 """
255 == Obtains chain configs for all chains in menu
256 """
257
258 from TriggerMenuMT.HLT.Config.Utility.MenuAlignmentTools import MenuAlignment
259 from TriggerMenuMT.HLT.CommonSequences import EventBuildingSequences, TLABuildingSequences
260
261 # get all chain names from menu
262 log.info("Will now get chains from the menu")
263 self.getChainsFromMenu(flags)
264
265 # decoding of the chain name
266 log.info("Will now get chain dictionaries for each chain")
267 self.getChainDicts(flags)
268
269 if flags.Trigger.disableCPS:
270 log.warning('Removing all CPS group because the flag Trigger.disableCPS is set')
271 for chainDict in self.chainDicts:
272 chainDict['groups'] = [g for g in chainDict['groups'] if not g.startswith('RATE:CPS_')]
273
274 #import the necessary signatures
275 log.debug("Importing the necessary signatures")
276 self.importSignaturesToGenerate()
277
278 log.info("Will now generate the chain configuration for each chain")
279 self.generateChains(flags)
280
281 log.info("Will now calculate the alignment parameters")
282 #dict of signature: set it belongs to
283 #e.g. {'Electron': ['Electron','Muon','Photon']}
284 menuAlignment = MenuAlignment(self.combinationsInMenu,
285 self.alignmentGroupsToAlign,
286 self.configLengthDict)
287 menuAlignment.analyse_combinations()
288
289 # alignmentGroups_to_align = menuAlignment.groupsToAlign
290 # lengthOfChainConfigs = self.configLengthDict
291 # combinationsInMenu = menuAlignment.combinationsInMenu
292 # alignmentGroup_sets_to_align = menuAlignment.setsToAlign
293
294 log.info('Aligning the following signatures: %s',sorted(menuAlignment.sets_to_align))
295 log.debug('Length of each of the alignment groups: %s',self.configLengthDict)
296
297 chainConfigs = []
298
299 for chainDict,chainConfig,lengthOfChainConfigs in self.allChainsForAlignment:
300
301 # start by ordering electron, photon, muon by having e+mu, g+mu, e+g chains
302 # desired ordering: electron, photon, muon, tau, jet, met, b-jet
303
304 # lengthOfChainConfigs is something like this: [(4, 'Photon'), (5, 'Muon')]
305 # needs to match up with the maximum number of steps in a signature in the menu (length_of_configs)
306 # start with electron! Only need to add post-steps for combined electron chains if the max length in a combined chain
307 # is greater than the number of electron steps combined chain. Assume that the max length of an electron chain occurs
308 # in a combined chain.
309
310 log.debug("[generateAllChainConfigs] chain %s has config lengths %s and alignment groups %s", chainDict['chainName'], lengthOfChainConfigs, chainDict['alignmentGroups'])
311
312 alignmentGroups = chainDict['alignmentGroups']
313
314 #parallel-merged single-signature chains or single signature chains. Anything that needs no splitting!
315 if len(set(alignmentGroups)) == 1:
316 alignedChainConfig = menuAlignment.single_align(chainDict, chainConfig)
317 HLTMenuConfig.registerChain( chainDict )
318 chainConfigs.append( alignedChainConfig )
319
320 elif len(alignmentGroups) >= 2:
321 alignedChainConfig = menuAlignment.multi_align(chainDict, chainConfig, lengthOfChainConfigs)
322 HLTMenuConfig.registerChain( chainDict )
323 chainConfigs.append( alignedChainConfig )
324
325 else:
326 log.error("Menu can't deal with combined chains with more than two alignmentGroups at the moment. oops...")
327 raise NotImplementedError("more than three alignment groups still needs implementing in ChainMerging.py, ATR-22206")
328
329 if not HLTMenuConfig.isChainRegistered(chainDict['chainName']):
330 log.error("Chain %s has not been registered in the menu!", chainDict['chainName'])
331 import pprint
332 pp = pprint.PrettyPrinter(indent=4, depth=8)
333 log.error('The chain dictionary is: %s', pp.pformat(chainDict))
334 raise Exception("Please fix the menu or the chain.")
335
336 # align event building sequences
337 log.info("[generateAllChainConfigs] general alignment complete, will now align TLA chains")
338 TLABuildingSequences.alignTLASteps(chainConfigs, HLTMenuConfig.dicts())
339 log.info("[generateAllChainConfigs] general and TLA alignment complete, will now align PEB chains")
340 EventBuildingSequences.alignEventBuildingSteps(chainConfigs, HLTMenuConfig.dicts())
341
342 log.info("[generateAllChainConfigs] all chain configurations have been generated.")
343 return chainConfigs
344
345
346 def getChainsFromMenu(self, flags):
347 """
348 == Returns the list of chain names that are in the menu
349 """
350
351 log.info(f'Menu name: {flags.Trigger.triggerMenuSetup}')
352
353 # Get the base menu name (without prescale set suffix)
354 from TriggerMenuMT.HLT.Config.Utility.MenuPrescaleSet import AutoPrescaleSetGen
355 self.base_menu_name, self.prescale_set_name = AutoPrescaleSetGen.split_menu_name(flags.Trigger.triggerMenuSetup)
356 log.debug('Base menu name: %s', self.base_menu_name)
357
358 # Generate the list of chains from the basic menu (terminated in a version number)
359 try:
360 menu_module = importlib.import_module(f'TriggerMenuMT.HLT.Menu.{self.base_menu_name}')
361 except Exception as e:
362 log.fatal(f'Failed to import menu module "{self.base_menu_name}" inferred from menu "{flags.Trigger.triggerMenuSetup}"')
363 raise e
364
365 # Load Menu
366 self.chainsInMenu = menu_module.setupMenu()
367
368 # Temporary HLT->EF migration helper block.
369 # Replaces EF_ with HLT_ just after the menu is read, before it is processed any further.
370 if flags.Trigger.useHLTPrefixForRun4:
371 import dataclasses
372 for grp in self.chainsInMenu:
373 for i, chn in enumerate(self.chainsInMenu[grp]):
374 if chn.name.startswith("EF_"):
375 self.chainsInMenu[grp][i] = dataclasses.replace(chn, name="HLT_" + chn.name[3:])
376
377 # Filter chains if requested
378 if self.chainFilter is not None:
379 self.signaturesOverwritten = True
380
381 # Verify that if the chain filter has lists of chains
382 # they are all in the menu
383 chainsToCheck = []
384 if hasattr(self.chainFilter,'selectChains'):
385 chainsToCheck += self.chainFilter.selectChains
386 if hasattr(self.chainFilter,'disableChains'):
387 chainsToCheck += self.chainFilter.disableChains
388 for chain in chainsToCheck:
389 inMenu = False
390 for signame in self.chainsInMenu:
391 if chain in [c.name for c in self.chainsInMenu[signame]]:
392 inMenu = True
393 break
394 if not inMenu:
395 raise RuntimeError(f'Request to enable/disable chain {chain} that is not in menu')
396
397 for signame in self.chainsInMenu:
398 self.chainsInMenu[signame] = [c for c in self.chainsInMenu[signame]
399 if self.chainFilter(signame, c)]
400
401 if not self.chainsInMenu:
402 log.warning("There seem to be no chains in the menu - please check")
403 elif log.isEnabledFor(logging.DEBUG):
404 import pprint
405 log.debug("The following chains were found in the menu:")
406 pprint.pprint(self.chainsInMenu)
407
408
409 def __generateChainConfig(self, flags, mainChainDict):
410 """
411 # Assembles the chain configuration and returns a chain object with (name, L1see and list of ChainSteps)
412 """
413
414 from TriggerMenuMT.HLT.Config.Utility.ChainDictTools import splitInterSignatureChainDict
415 from TriggerMenuMT.HLT.Config.Utility.ComboHypoHandling import addTopoInfo, comboConfigurator, topoLegIndices, anomdetWPIndices, topo3VarLegIndices
416 from TriggerMenuMT.HLT.Config.Utility.ChainMerging import mergeChainDefs
417 from TriggerMenuMT.HLT.CommonSequences import EventBuildingSequences, TLABuildingSequences
418
419 # split the the chainDictionaries for each chain and print them in a pretty way
420 chainDicts = splitInterSignatureChainDict(mainChainDict)
421
422 if log.isEnabledFor(logging.DEBUG):
423 import pprint
424 pp = pprint.PrettyPrinter(indent=4, depth=8)
425 log.debug('dictionary is: %s', pp.pformat(chainDicts))
426
427 # Loop over all chainDicts and send them off to their respective assembly code
428 listOfChainConfigs = []
429 perSig_lengthOfChainConfigs = []
430
431 for chainPartDict in chainDicts:
432 chainPartConfig = None
433 currentSig = chainPartDict['signature']
434 currentAlignGroup = None
435 if len(chainPartDict['chainParts']) == 1:
436 currentAlignGroup = chainPartDict['chainParts'][0]['alignmentGroup']
437
438 chainName = chainPartDict['chainName']
439 log.debug('Checking chainDict for chain %s in signature %s, alignment group %s' , chainName, currentSig, currentAlignGroup)
440
441 if currentSig in self.availableSignatures:
442 try:
443 log.debug("[__generateChainConfigs] Trying to get chain config for %s", currentSig)
444 if currentSig in self.defaultFlagsForSignature:
445 sigFlags = self.defaultFlagsForSignature[currentSig]
446 else:
447 try:
448 sigFlags = self.chainDefModule[currentSig].prepareDefaultSignatureFlags(flags)
449 except AttributeError:
450 log.debug("prepareDefaultSignatureFlags not present")
451 sigFlags = flags
452 except Exception as e:
453 log.error(f"Unexpected error invoking prepareDefaultSignatureFlags {e}")
454 sigFlags = flags
455
456 self.defaultFlagsForSignature[currentSig] = sigFlags
457
458 if currentSig in ['Electron', 'Photon', 'Muon', 'Tau', 'Bphysics'] :
459 chainPartConfig, perSig_lengthOfChainConfigs = self.chainDefModule[currentSig].generateChainConfigs(sigFlags, chainPartDict, perSig_lengthOfChainConfigs)
460 else:
461 chainPartConfig = self.chainDefModule[currentSig].generateChainConfigs(sigFlags, chainPartDict)
462 if currentSig == 'Test' and isinstance(chainPartConfig, tuple):
463 chainPartConfig = chainPartConfig[0]
464 except Exception:
465 log.error('[__generateChainConfigs] Problems creating ChainDef for chain %s ', chainName)
466 log.error('[__generateChainConfigs] I am in chain part\n %s ', chainPartDict)
467 log.exception('[__generateChainConfigs] Full chain dictionary is\n %s ', mainChainDict)
468 raise Exception('[__generateChainConfigs] Stopping menu generation. Please investigate the exception shown above.')
469 else:
470 log.error('Chain %s cannot be generated - Signature "%s" not available', chainPartDict['chainName'], currentSig)
471 log.error('Available signature(s): %s', self.availableSignatures)
472 raise Exception('Stopping the execution. Please correct the configuration.')
473
474 log.debug("Chain %s \n chain config: %s",chainPartDict['chainName'],chainPartConfig)
475
476 listOfChainConfigs.append(chainPartConfig)
477 log.debug("[__generateChainConfigs] adding to the perSig_lengthOfChainConfigs list (%s, %s)",chainPartConfig.nSteps,chainPartConfig.alignmentGroups)
478 perSig_lengthOfChainConfigs.append((chainPartConfig.nSteps,chainPartConfig.alignmentGroups))
479
480 # this will be a list of lists for inter-sig combined chains and a list with one
481 # multi-element list for intra-sig combined chains
482 # here, we flatten it accordingly (works for both cases!)
483 lengthOfChainConfigs = []
484 for nSteps, aGrps in perSig_lengthOfChainConfigs:
485 if len(nSteps) != len(aGrps):
486 log.error("Chain part has %s steps and %s alignment groups - these don't match!",nSteps,aGrps)
487 else:
488 for a,b in zip(nSteps,aGrps):
489 lengthOfChainConfigs.append((a,b))
490
491
492 # This part is to deal with combined chains between different signatures
493 try:
494 if len(listOfChainConfigs) == 0:
495 raise Exception('[__generateChainConfigs] No Chain Configuration found for {0}'.format(mainChainDict['chainName']))
496 else:
497 if len(listOfChainConfigs)>1:
498 log.debug("Merging strategy from dictionary: %s", mainChainDict["mergingStrategy"])
499 theChainConfig, perSig_lengthOfChainConfigs = mergeChainDefs(listOfChainConfigs, mainChainDict, perSig_lengthOfChainConfigs)
500 lengthOfChainConfigs = []
501 for nSteps, aGrps in perSig_lengthOfChainConfigs:
502 if len(nSteps) != len(aGrps):
503 log.error("Post-merged chain part has %s steps and %s alignment groups - these don't match!",nSteps,aGrps)
504 else:
505 for a,b in zip(nSteps,aGrps):
506 lengthOfChainConfigs.append((a,b))
507 else:
508 theChainConfig = listOfChainConfigs[0]
509
510 for topoID in range(len(mainChainDict['extraComboHypos'])):
511 thetopo = mainChainDict['extraComboHypos'][topoID].strip(string.digits).rstrip(topoLegIndices)
512
513
514 if "anomdet" in thetopo:
515 thetopo = thetopo.rstrip(anomdetWPIndices)
516
517 if "masswiso" in thetopo:
518 thetopo = thetopo.rstrip(topo3VarLegIndices)
519
520 theChainConfig.addTopo((comboConfigurator[thetopo],thetopo))
521
522 # Now we know where the topos should go, we can insert them in the right steps
523 if len(theChainConfig.topoMap) > 0:
524 log.debug("Trying to add extra ComboHypoTool for %s",mainChainDict['extraComboHypos'])
525 addTopoInfo(theChainConfig,mainChainDict,listOfChainConfigs,lengthOfChainConfigs)
526 except RuntimeError:
527 log.error('[__generateChainConfigs] Problems creating ChainDef for chain %s ', chainName)
528 log.error('[__generateChainConfigs] I am in the extraComboHypos section, for %s ', mainChainDict['extraComboHypos'])
529 log.exception('[__generateChainConfigs] Full chain dictionary is\n %s ', mainChainDict)
530 raise Exception('[__generateChainConfigs] Stopping menu generation. Please investigate the exception shown above.')
531 except AttributeError:
532 raise Exception('[__generateChainConfigs] Stopping menu generation. Please investigate the exception shown above.')
533
534 # Configure event building strategy
535 eventBuildType = mainChainDict['eventBuildType']
536 TLAEventBuildTypes = ('PhysicsTLA', 'FTagPEBTLA', 'EgammaPEBTLA', 'DarkJetPEBTLA')
537 if eventBuildType:
538 try:
539 if any(ebtype in eventBuildType for ebtype in TLAEventBuildTypes):
540 log.debug("Adding TLA Step for chain %s", mainChainDict['chainName'])
541 TLABuildingSequences.addTLAStep(flags, theChainConfig, mainChainDict)
542 log.debug('Configuring event building sequence %s for chain %s', eventBuildType, mainChainDict['chainName'])
543 EventBuildingSequences.addEventBuildingSequence(flags, theChainConfig, eventBuildType, mainChainDict)
544 except TypeError as ex:
545 log.error(ex)
546 raise Exception('[__generateChainConfigs] Stopping menu generation for EventBuilding/TLA sequences. Please investigate the exception shown above.')
547
548 log.debug('[__generateChainConfigs] lengthOfChainConfigs %s, ChainConfigs %s ', lengthOfChainConfigs, theChainConfig)
549 return theChainConfig,lengthOfChainConfigs
550
551
552 def resolveEmptySteps(self,chainConfigs):
553 max_steps = max([len(cc.steps) for cc in chainConfigs], default=0)
554 steps_are_empty = [True for i in range(0,max_steps)]
555 emptySteps = []
556 for cc in chainConfigs:
557 for istep, the_step in enumerate(cc.steps):
558 if not the_step.isEmpty:
559 steps_are_empty[istep] = False
560 else:
561 emptySteps.append(the_step)
562
563 log.debug("Are there any fully empty steps? %s", steps_are_empty)
564 log.debug("The empty step(s) and associated chain(s) are: %s", emptySteps)
565 empty_step_indices = [i for i,is_empty in enumerate(steps_are_empty) if is_empty]
566
567 if len(empty_step_indices) == 0:
568 return chainConfigs
569
570 special_test_menu = self.chainFilter and ( getattr(self.chainFilter, "selectChains", False) or \
571 getattr(self.chainFilter, "disableChains", False) or \
572 getattr(self.chainFilter, "disabledSignatures", False) or \
573 getattr(self.chainFilter, "enabledSignatures", False) )
574
575
576 if len(self.availableSignatures) != 1 and not special_test_menu:
577 raise Exception("[resolveEmptySteps] Please find the reason for this empty step and resolve it / remove it from the menu: %s", emptySteps)
578
579 log.info("Will now delete steps %s (indexed from zero)",empty_step_indices)
580
581 for cc in chainConfigs:
582 new_steps = []
583 #only add non-empty steps to the new steps list!
584 for istep,step in enumerate(cc.steps):
585 if istep not in empty_step_indices:
586 new_steps += [step]
587 cc.steps = new_steps
588
589 return chainConfigs
590
591 def generatePrescales(self, flags):
592 '''Add prescales for disabling items (e.g. MC production)'''
593 log.info(f'Generating automatic prescale set: {self.prescale_set_name}')
594
595 if self.prescale_set_name is not None:
596 from TriggerMenuMT.HLT.Menu.MenuPrescaleConfig import menu_prescale_set_gens
597 gen = menu_prescale_set_gens[self.prescale_set_name]
598 else:
599 from TriggerMenuMT.HLT.Config.Utility.MenuPrescaleSet import AutoPrescaleSetGen
600 gen = AutoPrescaleSetGen()
601
602 # Generate prescale set
603 ps_set = gen.generate(flags, store=True)
604 self.L1Prescales = ps_set.l1_prescales
605 self.HLTPrescales = ps_set.hlt_prescales
606
607
608
609def generateMenuMT(flags):
610 """
611 == Main function to generate the L1, L1Topo and HLT menu configs and CA, using the GenerateMenuMT class
612 """
613 # Generate L1 menu
614 # The L1Menu json file is produced here
615 from TrigConfigSvc.TrigConfigSvcCfg import generateL1Menu
616 generateL1Menu(flags)
617
618
619 menu = GenerateMenuMT()
620
621 # Apply generation filter (enable/disable chains and signatures)
622 chains_gen_filter = FilterChainsToGenerate(flags)
623 menu.setChainFilter(chains_gen_filter)
624 log.debug('Filtering chains: %d', menu.chainFilter is not None)
625
626 # Generate all chains configuration
627 finalListOfChainConfigs = menu.generateAllChainConfigs(flags)
628
629 checkNumberOfLegs = [chain.checkNumberOfLegs() for chain in finalListOfChainConfigs]
630 if 0 in checkNumberOfLegs:
631 log.error('There is a chain with unexpected number of legs. Revisit your configuration')
632
633 log.info('Number of configured chains: %d', len(finalListOfChainConfigs))
634
635 from TriggerMenuMT.HLT.Config import MenuComponents
636 if len(MenuComponents._CustomComboHypoAllowed)> _maxAllowedCustomCH:
637 log.error(f'Found {len(MenuComponents._CustomComboHypoAllowed)} ComboHypo algorithms violating the one-CH-per-step rule, only {_maxAllowedCustomCH} are allowed (which are BLS ComboHypos). This is the list of current violations: {MenuComponents._CustomComboHypoAllowed}. Please consolidate your choice of ComboHypo, by checking that it is able to handle decisions internally; if yes eventually increase the limit set by _maxAllowedCustomCH, after discussing with experts')
638 # Generate and apply the automatic prescale sets (e.g. for disabling items in an MC production)
639 menu.generatePrescales(flags)
640
641
642 # Remove any remaining steps that are fully empty in all chains
643 finalListOfChainConfigs = menu.resolveEmptySteps(finalListOfChainConfigs)
644 log.debug("finalListOfChainConfig: %s", finalListOfChainConfigs)
645
646 # Make the HLT configuration tree
647 # The HLTMenu json file is produced here.
648 log.info("Making the HLT configuration tree")
649 menuAcc, CFseq_list = makeHLTTree(flags, finalListOfChainConfigs)
650
651 # Post-generation checks:
652 log.info("Checking the L1HLTConsistency...")
653 from TriggerMenuMT.HLT.Config.Validation.CheckL1HLTConsistency import checkL1HLTConsistency
654 checkL1HLTConsistency(flags)
655
656 log.info("Checking the Coherent Prescale assignments...")
657 from TriggerMenuMT.HLT.Config.Validation.CheckCPSGroups import checkCPSGroups
658 checkCPSGroups(HLTMenuConfig.dictsList())
659
660 log.info("Checking that all chains streamed in express have a signature or detctor monGroup")
661 from TriggerMenuMT.HLT.Config.Validation.CheckMonGroups import checkMonGroups
662 checkMonGroups(HLTMenuConfig.dictsList())
663
664 # Cleanup menu singletons to allow garbage collection (ATR-28855)
665 GenerateMenuMT.clear()
666 from TriggerMenuMT.HLT.Config import MenuComponents
667 MenuComponents._ComboHypoPool.clear()
668 MenuComponents._CustomComboHypoAllowed.clear()
669
670 return menuAcc
671
672
673def makeHLTTree(flags, chainConfigs):
674 """
675 Generate appropriate Control Flow Graph wiht all HLT algorithms
676 """
677 from TriggerMenuMT.HLT.Config.ControlFlow.HLTCFConfig import decisionTreeFromChains, sequenceScanner
678 from TriggerJobOpts.TriggerConfig import collectViewMakers
679 from AthenaConfiguration.ComponentAccumulator import ComponentAccumulator
680 from AthenaCommon.CFElements import seqAND
681
682 acc = ComponentAccumulator()
683 steps = seqAND('HLTAllSteps')
684 finalDecisions, CFseq_list, menuAcc = decisionTreeFromChains(flags, steps, chainConfigs, HLTMenuConfig.dictsList())
685 if log.getEffectiveLevel() <= logging.DEBUG:
686 menuAcc.printConfig()
687
688 acc.merge(menuAcc)
689 successful_scan = sequenceScanner( steps )
690 if not successful_scan:
691 raise Exception("[makeHLTTree] At least one sequence is expected in more than one step. Check error messages and fix!")
692
693 flatDecisions=[]
694 for step in finalDecisions:
695 flatDecisions.extend (step)
696
697 viewMakers = collectViewMakers(steps)
698 viewMakerMap = {vm.name:vm for vm in viewMakers}
699 for vmname, vm in viewMakerMap.items():
700 log.debug(f"[makeHLTTree] {vmname} InputMakerOutputDecisions: {vm.InputMakerOutputDecisions}")
701 if vmname.endswith("_probe"):
702 try:
703 log.debug(f"Setting InputCachedViews on {vmname} to read decisions from tag leg {vmname[:-6]}: {vm.InputMakerOutputDecisions}")
704 vm.InputCachedViews = viewMakerMap[vmname[:-6]].InputMakerOutputDecisions
705 except KeyError: # We may be using a probe leg that has different reco from the tag
706 log.debug(f"Tag leg does not match probe: '{vmname[:-6]}', will not use cached views")
707
708
709 # Generate JSON representation of the config
710 from TriggerMenuMT.HLT.Config.JSON.HLTMenuJSON import generateJSON
711 generateJSON(flags, HLTMenuConfig.dictsList(), menuAcc.getSequence("HLTAllSteps"))
712
713 # Store the HLTMonitoring json file
714 from TriggerMenuMT.HLT.Config.JSON.HLTMonitoringJSON import generateDefaultMonitoringJSON
715 generateDefaultMonitoringJSON(flags, HLTMenuConfig.dictsList())
716
717 return acc, CFseq_list
STL class.