From aa1d1f454e66b4451cc073b022db60efc556f4c2 Mon Sep 17 00:00:00 2001
From: Rafal Bielski <rafal.bielski@cern.ch>
Date: Wed, 9 Sep 2020 19:36:37 +0200
Subject: [PATCH] TriggerMenuMT: Align PEB steps

---
 .../EventBuildingSequenceSetup.py             | 35 +++++++++++++++++++
 .../HLTMenuConfig/Menu/GenerateMenuMT.py      |  7 ++--
 2 files changed, 40 insertions(+), 2 deletions(-)

diff --git a/Trigger/TriggerCommon/TriggerMenuMT/python/HLTMenuConfig/CommonSequences/EventBuildingSequenceSetup.py b/Trigger/TriggerCommon/TriggerMenuMT/python/HLTMenuConfig/CommonSequences/EventBuildingSequenceSetup.py
index ba9d34e8bd2..4bf4949af66 100644
--- a/Trigger/TriggerCommon/TriggerMenuMT/python/HLTMenuConfig/CommonSequences/EventBuildingSequenceSetup.py
+++ b/Trigger/TriggerCommon/TriggerMenuMT/python/HLTMenuConfig/CommonSequences/EventBuildingSequenceSetup.py
@@ -128,3 +128,38 @@ def pebSequence(eventBuildType, inputMaker):
     if findAlgorithm(seq, inputMaker.name()) != inputMaker:
         seq += inputMaker
     return seq
+
+
+def findEventBuildingStep(chainConfig):
+    pebSteps = [s for s in chainConfig.steps if 'PEBInfoWriter' in s.name and 'EmptyPEBAlign' not in s.name]
+    if len(pebSteps) == 0:
+        return None
+    elif len(pebSteps) > 1:
+        raise RuntimeError('Multiple Event Building steps in one chain are not supported but found in chain ' + chainConfig.name)
+    return pebSteps[0]
+
+
+def alignEventBuildingSteps(all_chains):
+    def is_peb(chainData):
+        return len(chainData[0]['eventBuildType']) > 0
+    all_peb_chains = list(filter(is_peb, all_chains))
+    maxPebStepPosition = {} # {eventBuildType: N}
+    def getPebStepPosition(chainConfig):
+        pebStep = findEventBuildingStep(chainConfig)
+        return chainConfig.steps.index(pebStep) + 1
+
+    # First loop to find the maximal PEB step positions to which we need to align
+    for chainDict, chainConfig, lengthOfChainConfigs in all_peb_chains:
+        pebStepPosition = getPebStepPosition(chainConfig)
+        ebt = chainDict['eventBuildType']
+        if ebt not in maxPebStepPosition or pebStepPosition > maxPebStepPosition[ebt]:
+            maxPebStepPosition[ebt] = pebStepPosition
+
+    # Second loop to insert empty steps before the PEB steps where needed
+    for chainDict, chainConfig, lengthOfChainConfigs in all_peb_chains:
+        pebStepPosition = getPebStepPosition(chainConfig)
+        ebt = chainDict['eventBuildType']
+        if pebStepPosition < maxPebStepPosition[ebt]:
+            numStepsNeeded = maxPebStepPosition[ebt] - pebStepPosition
+            log.debug('Aligning PEB step for chain %s by adding %d empty steps', chainDict['chainName'], numStepsNeeded)
+            chainConfig.insertEmptySteps(chainDict,'EmptyPEBAlign', numStepsNeeded, pebStepPosition-1)
diff --git a/Trigger/TriggerCommon/TriggerMenuMT/python/HLTMenuConfig/Menu/GenerateMenuMT.py b/Trigger/TriggerCommon/TriggerMenuMT/python/HLTMenuConfig/Menu/GenerateMenuMT.py
index 8b6283e8a72..fd6cedfbc4a 100755
--- a/Trigger/TriggerCommon/TriggerMenuMT/python/HLTMenuConfig/Menu/GenerateMenuMT.py
+++ b/Trigger/TriggerCommon/TriggerMenuMT/python/HLTMenuConfig/Menu/GenerateMenuMT.py
@@ -17,6 +17,7 @@ from TriggerMenuMT.HLTMenuConfig.Menu.ChainDictTools import splitInterSignatureC
 from TriggerMenuMT.HLTMenuConfig.Menu.MenuPrescaleConfig import MenuPrescaleConfig, applyHLTPrescale
 from TriggerMenuMT.HLTMenuConfig.Menu.ChainMerging import mergeChainDefs
 from TriggerMenuMT.HLTMenuConfig.Menu.MenuAlignmentTools import analyseCombinations, groupSignatures, setChainSignatures
+from TriggerMenuMT.HLTMenuConfig.CommonSequences import EventBuildingSequenceSetup
 
 from AthenaCommon.Logging import logging
 log = logging.getLogger( __name__ )
@@ -201,6 +202,9 @@ class GenerateMenuMT(object):
                 for sig in list(set(chainDict['signatures'])):
                     signatures_to_align.update([sig])
         
+        # align event building sequences
+        EventBuildingSequenceSetup.alignEventBuildingSteps(all_chains)
+
         #will likely always be true, but the grouping could be redefined
         groupPeskySignatures = True
         
@@ -497,8 +501,7 @@ class GenerateMenuMT(object):
         eventBuildType = mainChainDict['eventBuildType']
         if eventBuildType:
             log.debug('Configuring event building sequence %s for chain %s', eventBuildType, mainChainDict['chainName'])
-            from TriggerMenuMT.HLTMenuConfig.CommonSequences.EventBuildingSequenceSetup import addEventBuildingSequence
-            addEventBuildingSequence(theChainConfig, eventBuildType)
+            EventBuildingSequenceSetup.addEventBuildingSequence(theChainConfig, eventBuildType)
 
         log.debug('ChainConfigs  %s ', theChainConfig)
         return theChainConfig,lengthOfChainConfigs
-- 
GitLab