374 def __generateChainConfig(self, flags, mainChainDict):
375 """
376 # Assembles the chain configuration and returns a chain object with (name, L1see and list of ChainSteps)
377 """
378
379 from TriggerMenuMT.HLT.Config.Utility.ChainDictTools import splitInterSignatureChainDict
380 from TriggerMenuMT.HLT.Config.Utility.ComboHypoHandling import addTopoInfo, comboConfigurator, topoLegIndices, anomdetWPIndices, topo3VarLegIndices
381 from TriggerMenuMT.HLT.Config.Utility.ChainMerging import mergeChainDefs
382 from TriggerMenuMT.HLT.CommonSequences import EventBuildingSequences, TLABuildingSequences
383
384
385 chainDicts = splitInterSignatureChainDict(mainChainDict)
386
387 if log.isEnabledFor(logging.DEBUG):
388 import pprint
389 pp = pprint.PrettyPrinter(indent=4, depth=8)
390 log.debug('dictionary is: %s', pp.pformat(chainDicts))
391
392
393 listOfChainConfigs = []
394 perSig_lengthOfChainConfigs = []
395
396 for chainPartDict in chainDicts:
397 chainPartConfig = None
398 currentSig = chainPartDict['signature']
399 currentAlignGroup = None
400 if len(chainPartDict['chainParts']) == 1:
401 currentAlignGroup = chainPartDict['chainParts'][0]['alignmentGroup']
402
403 chainName = chainPartDict['chainName']
404 log.debug('Checking chainDict for chain %s in signature %s, alignment group %s' , chainName, currentSig, currentAlignGroup)
405
406 if currentSig in self.availableSignatures:
407 try:
408 log.debug("[__generateChainConfigs] Trying to get chain config for %s", currentSig)
409 if currentSig in self.defaultFlagsForSignature:
410 sigFlags = self.defaultFlagsForSignature[currentSig]
411 else:
412 try:
413 sigFlags = self.chainDefModule[currentSig].prepareDefaultSignatureFlags(flags)
414 except AttributeError:
415 log.debug("prepareDefaultSignatureFlags not present")
416 sigFlags = flags
417 except Exception as e:
418 log.error(f"Unexpected error invoking prepareDefaultSignatureFlags {e}")
419 sigFlags = flags
420
421 self.defaultFlagsForSignature[currentSig] = sigFlags
422
423 if currentSig in ['Electron', 'Photon', 'Muon', 'Tau', 'Bphysics'] :
424 chainPartConfig, perSig_lengthOfChainConfigs = self.chainDefModule[currentSig].generateChainConfigs(sigFlags, chainPartDict, perSig_lengthOfChainConfigs)
425 else:
426 chainPartConfig = self.chainDefModule[currentSig].generateChainConfigs(sigFlags, chainPartDict)
427 if currentSig == 'Test' and isinstance(chainPartConfig, tuple):
428 chainPartConfig = chainPartConfig[0]
429 except Exception:
430 log.error('[__generateChainConfigs] Problems creating ChainDef for chain %s ', chainName)
431 log.error('[__generateChainConfigs] I am in chain part\n %s ', chainPartDict)
432 log.exception('[__generateChainConfigs] Full chain dictionary is\n %s ', mainChainDict)
433 raise Exception('[__generateChainConfigs] Stopping menu generation. Please investigate the exception shown above.')
434 else:
435 log.error('Chain %s cannot be generated - Signature "%s" not available', chainPartDict['chainName'], currentSig)
436 log.error('Available signature(s): %s', self.availableSignatures)
437 raise Exception('Stopping the execution. Please correct the configuration.')
438
439 log.debug("Chain %s \n chain config: %s",chainPartDict['chainName'],chainPartConfig)
440
441 listOfChainConfigs.append(chainPartConfig)
442 log.debug("[__generateChainConfigs] adding to the perSig_lengthOfChainConfigs list (%s, %s)",chainPartConfig.nSteps,chainPartConfig.alignmentGroups)
443 perSig_lengthOfChainConfigs.append((chainPartConfig.nSteps,chainPartConfig.alignmentGroups))
444
445
446
447
448 lengthOfChainConfigs = []
449 for nSteps, aGrps in perSig_lengthOfChainConfigs:
450 if len(nSteps) != len(aGrps):
451 log.error("Chain part has %s steps and %s alignment groups - these don't match!",nSteps,aGrps)
452 else:
453 for a,b in zip(nSteps,aGrps):
454 lengthOfChainConfigs.append((a,b))
455
456
457
458 try:
459 if len(listOfChainConfigs) == 0:
460 raise Exception('[__generateChainConfigs] No Chain Configuration found for {0}'.format(mainChainDict['chainName']))
461 else:
462 if len(listOfChainConfigs)>1:
463 log.debug("Merging strategy from dictionary: %s", mainChainDict["mergingStrategy"])
464 theChainConfig, perSig_lengthOfChainConfigs = mergeChainDefs(listOfChainConfigs, mainChainDict, perSig_lengthOfChainConfigs)
465 lengthOfChainConfigs = []
466 for nSteps, aGrps in perSig_lengthOfChainConfigs:
467 if len(nSteps) != len(aGrps):
468 log.error("Post-merged chain part has %s steps and %s alignment groups - these don't match!",nSteps,aGrps)
469 else:
470 for a,b in zip(nSteps,aGrps):
471 lengthOfChainConfigs.append((a,b))
472 else:
473 theChainConfig = listOfChainConfigs[0]
474
475 for topoID in range(len(mainChainDict['extraComboHypos'])):
476 thetopo = mainChainDict[
'extraComboHypos'][topoID].
strip(string.digits).rstrip(topoLegIndices)
477
478
479 if "anomdet" in thetopo:
480 thetopo = thetopo.rstrip(anomdetWPIndices)
481
482 if "masswiso" in thetopo:
483 thetopo = thetopo.rstrip(topo3VarLegIndices)
484
485 theChainConfig.addTopo((comboConfigurator[thetopo],thetopo))
486
487
488 if len(theChainConfig.topoMap) > 0:
489 log.debug("Trying to add extra ComboHypoTool for %s",mainChainDict['extraComboHypos'])
490 addTopoInfo(theChainConfig,mainChainDict,listOfChainConfigs,lengthOfChainConfigs)
491 except RuntimeError:
492 log.error('[__generateChainConfigs] Problems creating ChainDef for chain %s ', chainName)
493 log.error('[__generateChainConfigs] I am in the extraComboHypos section, for %s ', mainChainDict['extraComboHypos'])
494 log.exception('[__generateChainConfigs] Full chain dictionary is\n %s ', mainChainDict)
495 raise Exception('[__generateChainConfigs] Stopping menu generation. Please investigate the exception shown above.')
496 except AttributeError:
497 raise Exception('[__generateChainConfigs] Stopping menu generation. Please investigate the exception shown above.')
498
499
500 eventBuildType = mainChainDict['eventBuildType']
501 TLAEventBuildTypes = ('PhysicsTLA', 'FTagPEBTLA', 'EgammaPEBTLA', 'DarkJetPEBTLA')
502 if eventBuildType:
503 try:
504 if any(ebtype in eventBuildType for ebtype in TLAEventBuildTypes):
505 log.debug("Adding TLA Step for chain %s", mainChainDict['chainName'])
506 TLABuildingSequences.addTLAStep(flags, theChainConfig, mainChainDict)
507 log.debug('Configuring event building sequence %s for chain %s', eventBuildType, mainChainDict['chainName'])
508 EventBuildingSequences.addEventBuildingSequence(flags, theChainConfig, eventBuildType, mainChainDict)
509 except TypeError as ex:
510 log.error(ex)
511 raise Exception('[__generateChainConfigs] Stopping menu generation for EventBuilding/TLA sequences. Please investigate the exception shown above.')
512
513 log.debug('[__generateChainConfigs] lengthOfChainConfigs %s, ChainConfigs %s ', lengthOfChainConfigs, theChainConfig)
514 return theChainConfig,lengthOfChainConfigs
515
516