From 72553685aaa3731b5f34f0fa25a2e023320c2809 Mon Sep 17 00:00:00 2001
From: Frank Winklmeier <frank.winklmeier@cern.ch>
Date: Mon, 6 Feb 2023 11:22:59 +0100
Subject: [PATCH 1/2] TriggerMenuMT: allow Hypo generators to take flags as
 first argument

Make it possible to pass flags as the first argument to the Hypo
generators. The argument has to be named `flags`, e.g.:
```
def myToolFromDict(flags, chainDict)
```
---
 .../HLT/Config/ControlFlow/HLTCFComponents.py | 10 ++---
 .../HLT/Config/ControlFlow/HLTCFConfig.py     |  6 +--
 .../Config/ControlFlow/HLTCFConfig_newJO.py   |  4 +-
 .../python/HLT/Config/MenuComponents.py       | 40 +++++++++++--------
 4 files changed, 32 insertions(+), 28 deletions(-)

diff --git a/Trigger/TriggerCommon/TriggerMenuMT/python/HLT/Config/ControlFlow/HLTCFComponents.py b/Trigger/TriggerCommon/TriggerMenuMT/python/HLT/Config/ControlFlow/HLTCFComponents.py
index 045b576e0c11..6296d9692cf7 100644
--- a/Trigger/TriggerCommon/TriggerMenuMT/python/HLT/Config/ControlFlow/HLTCFComponents.py
+++ b/Trigger/TriggerCommon/TriggerMenuMT/python/HLT/Config/ControlFlow/HLTCFComponents.py
@@ -1,4 +1,4 @@
-# Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
+# Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
 
 from TriggerMenuMT.HLT.Config.MenuComponents import AlgNode
 from TriggerMenuMT.HLT.Config.ControlFlow.MenuComponentsNaming import CFNaming
@@ -155,7 +155,7 @@ class CFSequence(object):
             log.debug("CFSequence.connectCombo: adding output to  %s: %s",  self.combo.Alg.getName(), combo_output)
 
     
-    def createHypoTools(self, chain, newstep):       
+    def createHypoTools(self, flags, chain, newstep):
         """ set and create HypoTools accumulated on the self.step from an input step configuration
         """
         if self.step.combo is None:
@@ -174,9 +174,9 @@ class CFSequence(object):
             hypoToolConf=seq.getHypoToolConf()
             if hypoToolConf is not None: # avoid empty sequences
                 hypoToolConf.setConf( onePartChainDict )
-                myseq.hypo.addHypoTool(hypoToolConf) #this creates the HypoTools            
+                myseq.hypo.addHypoTool(flags, hypoToolConf) #this creates the HypoTools
         chainDict = HLTMenuConfig.getChainDictFromChainName(chain)
-        self.combo.createComboHypoTools(chainDict, newstep.comboToolConfs)
+        self.combo.createComboHypoTools(flags, chainDict, newstep.comboToolConfs)
 
     def __repr__(self):
         return "--- CFSequence ---\n + Filter: %s \n + decisions: %s\n +  %s \n"%(\
@@ -211,5 +211,3 @@ class CFSequenceCA(CFSequence):
     @lru_cache(None)
     def findComboHypoAlg(self):
         return findAlgorithmByPredicate(self.seq, lambda alg: compName(alg) == self.step.Alg.getName() and isComboHypoAlg(alg))
-        
-    
\ No newline at end of file
diff --git a/Trigger/TriggerCommon/TriggerMenuMT/python/HLT/Config/ControlFlow/HLTCFConfig.py b/Trigger/TriggerCommon/TriggerMenuMT/python/HLT/Config/ControlFlow/HLTCFConfig.py
index 0f7c6f2d46fe..b3f290c41298 100644
--- a/Trigger/TriggerCommon/TriggerMenuMT/python/HLT/Config/ControlFlow/HLTCFConfig.py
+++ b/Trigger/TriggerCommon/TriggerMenuMT/python/HLT/Config/ControlFlow/HLTCFConfig.py
@@ -376,7 +376,7 @@ def decisionTreeFromChains(flags, HLTNode, chains, allDicts, newJO):
         return ([], acc)
     
 
-    (finalDecisions, CFseq_list) = createDataFlow(chains, allDicts)
+    (finalDecisions, CFseq_list) = createDataFlow(flags, chains, allDicts)
     acc = createControlFlow(flags, HLTNode, CFseq_list)
     
 
@@ -396,7 +396,7 @@ def decisionTreeFromChains(flags, HLTNode, chains, allDicts, newJO):
     return (finalDecisions,acc)
 
 
-def createDataFlow(chains, allDicts):
+def createDataFlow(flags, chains, allDicts):
     """ Creates the filters and connect them to the menu sequences"""
 
     # find tot nsteps
@@ -482,7 +482,7 @@ def createDataFlow(chains, allDicts):
                 log.debug("Combo not implemented if it's empty step")
 
             # add HypoTools to this step (cumulating all same steps)
-            lastCFseq.createHypoTools(chain.name,chainStep)
+            lastCFseq.createHypoTools(flags,chain.name,chainStep)
 
             if len(chain.steps) == nstep+1:
                 log.debug("Adding finalDecisions for chain %s at step %d:", chain.name, nstep+1)
diff --git a/Trigger/TriggerCommon/TriggerMenuMT/python/HLT/Config/ControlFlow/HLTCFConfig_newJO.py b/Trigger/TriggerCommon/TriggerMenuMT/python/HLT/Config/ControlFlow/HLTCFConfig_newJO.py
index ea7b1efed897..11040ec9bef8 100644
--- a/Trigger/TriggerCommon/TriggerMenuMT/python/HLT/Config/ControlFlow/HLTCFConfig_newJO.py
+++ b/Trigger/TriggerCommon/TriggerMenuMT/python/HLT/Config/ControlFlow/HLTCFConfig_newJO.py
@@ -1,4 +1,4 @@
-# Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
+# Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
 from functools import lru_cache
 from AthenaCommon.CFElements import findAllAlgorithms, parOR, seqOR, seqAND, isSequence
 from AthenaCommon.Logging import logging
@@ -454,7 +454,7 @@ def generateDecisionTree(flags, HLTMenuConfig):
 
                 for sequence, chainLegDict in chainDictForSequence(chain.name, step.sequences):
                     if not isinstance(sequence, EmptyMenuSequence):
-                        hypoTool = sequence._hypoToolConf.confAndCreate( chainLegDict )
+                        hypoTool = sequence._hypoToolConf.confAndCreate( flags, chainLegDict )
                         assert isinstance(hypoTool, GaudiConfig2._configurables.Configurable), "The Hypo Tool for {} is not Configurable2".format(chain.name)
                         sequence.hypo.Alg.HypoTools.append( hypoTool )
                         # # if the chain requires special combo tools
diff --git a/Trigger/TriggerCommon/TriggerMenuMT/python/HLT/Config/MenuComponents.py b/Trigger/TriggerCommon/TriggerMenuMT/python/HLT/Config/MenuComponents.py
index a317b278c59a..80e5d364d6c8 100644
--- a/Trigger/TriggerCommon/TriggerMenuMT/python/HLT/Config/MenuComponents.py
+++ b/Trigger/TriggerCommon/TriggerMenuMT/python/HLT/Config/MenuComponents.py
@@ -27,6 +27,7 @@ from AthenaConfiguration.ComponentFactory import isComponentAccumulatorCfg
 
 from collections.abc import MutableSequence
 import collections.abc
+import inspect
 import re
 
 from AthenaCommon.Logging import logging
@@ -134,6 +135,8 @@ class AlgNode(Node):
 class HypoToolConf(object):
     """ Class to group info on hypotools for ChainDict"""
     def __init__(self, hypoToolGen):
+        # Check if the generator function takes flags:
+        self.hasFlags = 'flags' in inspect.signature(hypoToolGen).parameters
         self.hypoToolGen = hypoToolGen
         self.name=hypoToolGen.__name__
 
@@ -142,14 +145,17 @@ class HypoToolConf(object):
             raise RuntimeError("Configuring hypo with %s, not good anymore, use chainDict" % str(chainDict) )
         self.chainDict = chainDict
 
-    def create(self):
+    def create(self, flags):
         """creates instance of the hypo tool"""
-        return self.hypoToolGen( self.chainDict )
-    
-    def confAndCreate(self, chainDict):
+        if self.hasFlags:
+            return self.hypoToolGen( flags, self.chainDict )
+        else:
+            return self.hypoToolGen( self.chainDict )
+
+    def confAndCreate(self, flags, chainDict):
         """sets the configuration and creates instance of the hypo tool"""
         self.setConf(chainDict)
-        return self.create()
+        return self.create(flags)
 
 
 class HypoAlgNode(AlgNode):
@@ -171,10 +177,10 @@ class HypoAlgNode(AlgNode):
                       self.name, outputs[0])
 
 
-    def addHypoTool (self, hypoToolConf):
+    def addHypoTool (self, flags, hypoToolConf):
         log.debug("Adding HypoTool %s for chain %s to %s", hypoToolConf.name, hypoToolConf.chainDict['chainName'], self.Alg.getName())        
         try:
-            self.Alg.HypoTools = self.Alg.HypoTools + [hypoToolConf.create()]  # see ATEAM-773
+            self.Alg.HypoTools = self.Alg.HypoTools + [hypoToolConf.create(flags)]  # see ATEAM-773
             if isComponentAccumulatorCfg():
                 assert isinstance(self.Alg.HypoTools[-1], GaudiConfig2._configurables.Configurable), "The Hypo Tool for {} is not Configurable2".format(hypoToolConf.chainDict['chainName'])
 
@@ -280,7 +286,7 @@ class ComboMaker(AlgNode):
         return cval.keys()
 
 
-    def createComboHypoTools(self, chainDict, comboToolConfs):
+    def createComboHypoTools(self, flags, chainDict, comboToolConfs):
          """Created the ComboHypoTools"""
          if not len(comboToolConfs):
              return
@@ -289,7 +295,7 @@ class ComboMaker(AlgNode):
          for conf in confs:
              log.debug("ComboMaker.createComboHypoTools adding %s", conf)
              tools = self.Alg.ComboHypoTools
-             self.Alg.ComboHypoTools = tools + [ conf.confAndCreate( chainDict ) ]
+             self.Alg.ComboHypoTools = tools + [ conf.confAndCreate( flags, chainDict ) ]
  
 
 ##########################################################
@@ -508,15 +514,15 @@ class MenuSequence(object):
         self._maker.addInput(outfilter)        
     
   
-    def createHypoTools(self, chainDict):
+    def createHypoTools(self, flags, chainDict):
         if type(self._hypoToolConf) is list:
             log.warning ("This sequence %s has %d multiple HypoTools ",self.sequence.name, len(self.hypoToolConf))
             for hypo, hypoToolConf in zip(self._hypo, self._hypoToolConf):
                 hypoToolConf.setConf( chainDict )
-                hypo.addHypoTool(self._hypoToolConf)
+                hypo.addHypoTool(flags, self._hypoToolConf)
         else:
             self._hypoToolConf.setConf( chainDict )
-            self._hypo.addHypoTool(self._hypoToolConf) #this creates the HypoTools 
+            self._hypo.addHypoTool(flags, self._hypoToolConf) #this creates the HypoTools
             
     def getHypoToolConf(self) :
         return self._hypoToolConf
@@ -724,7 +730,7 @@ class Chain(object):
         for step in self.steps:
             step.setSeedsToSequences()
     
-    def createHypoTools(self):
+    def createHypoTools(self, flags):
         """ This is extrapolating the hypotool configuration from the chain name"""
         log.debug("createHypoTools for chain %s", self.name)        
         
@@ -737,9 +743,9 @@ class Chain(object):
             for seq, onePartChainDict in zip(step.sequences, step.stepDicts):
                 log.debug('    seq: %s, onePartChainDict:', seq.name)
                 log.debug('    %s', onePartChainDict)
-                seq.createHypoTools( onePartChainDict )
+                seq.createHypoTools( flags, onePartChainDict )
             
-            step.createComboHypoTools(self.name) 
+            step.createComboHypoTools(flags, self.name)
 
     # Receives a pair with the topo config function and an identifier string,
     # optionally also a target step name
@@ -891,9 +897,9 @@ class ChainStep(object):
         self.combo = _ComboHypoPool[key]
         
 
-    def createComboHypoTools(self, chainName):      
+    def createComboHypoTools(self, flags, chainName):
         chainDict = HLTMenuConfig.getChainDictFromChainName(chainName)
-        self.combo.createComboHypoTools(chainDict, self.comboToolConfs)
+        self.combo.createComboHypoTools(flags, chainDict, self.comboToolConfs)
             
     def getChainLegs(self):
         """ This is extrapolating the chain legs from the step dictionaries"""       
-- 
GitLab


From dff5f9c0d9f375b71154ee2c897a32a192969345 Mon Sep 17 00:00:00 2001
From: Frank Winklmeier <frank.winklmeier@cern.ch>
Date: Mon, 6 Feb 2023 11:24:54 +0100
Subject: [PATCH 2/2] TriggerMenuMT: pass flags to GenericMonitoringTool

---
 .../HLT/Config/Utility/ComboHypoHandling.py      |  4 ++--
 .../HLT/Electron/ElectronChainConfiguration.py   | 16 ++++++++--------
 .../python/HLT/MinBias/AFPMenuSequence.py        |  4 ++--
 .../HLT/MinBias/MinBiasChainConfiguration.py     |  4 ++--
 .../HLT/Photon/PhotonChainConfiguration.py       | 12 ++++++------
 5 files changed, 20 insertions(+), 20 deletions(-)

diff --git a/Trigger/TriggerCommon/TriggerMenuMT/python/HLT/Config/Utility/ComboHypoHandling.py b/Trigger/TriggerCommon/TriggerMenuMT/python/HLT/Config/Utility/ComboHypoHandling.py
index d7ec6fec6eed..6d92710f1305 100644
--- a/Trigger/TriggerCommon/TriggerMenuMT/python/HLT/Config/Utility/ComboHypoHandling.py
+++ b/Trigger/TriggerCommon/TriggerMenuMT/python/HLT/Config/Utility/ComboHypoHandling.py
@@ -53,7 +53,7 @@ allowed_obs = {
 from TriggerMenuMT.HLT.MinBias.MinBiasChainConfiguration import TrigAFPDijetComboHypoToolCfg
 from TriggerMenuMT.HLT.Muon.MuonChainConfiguration import TrigMuonEFIdtpInvMassHypoToolCfg
 
-def TrigComboHypoToolFromDict(chainDict):
+def TrigComboHypoToolFromDict(flags, chainDict):
     from AthenaMonitoringKernel.GenericMonitoringTool import GenericMonitoringTool
 
     chainName = chainDict['chainName']
@@ -146,7 +146,7 @@ def TrigComboHypoToolFromDict(chainDict):
         else:
             monToolName = f"MonTool_{chainName}_{chainDict['extraComboHypos'][iTopo]}"
         histNameTag = var
-        monTool = GenericMonitoringTool(monToolName)
+        monTool = GenericMonitoringTool(flags, monToolName)
         monTool.defineHistogram(histNameTag+'OfAccepted', type='TH1F', path='EXPERT',
                                 title=var+" in accepted combinations; {}".format(var),
                                 xbins=allowed_obs[var]['hist_nbins'],
diff --git a/Trigger/TriggerCommon/TriggerMenuMT/python/HLT/Electron/ElectronChainConfiguration.py b/Trigger/TriggerCommon/TriggerMenuMT/python/HLT/Electron/ElectronChainConfiguration.py
index 6f85775b80ce..b18d14c0415b 100644
--- a/Trigger/TriggerCommon/TriggerMenuMT/python/HLT/Electron/ElectronChainConfiguration.py
+++ b/Trigger/TriggerCommon/TriggerMenuMT/python/HLT/Electron/ElectronChainConfiguration.py
@@ -88,9 +88,9 @@ def precisionElectronSequenceCfg_lrt( flags, is_probe_leg=False):
 from TrigEgammaHypo.TrigEgammaHypoConf import TrigEgammaTopoHypoTool
 
 
-def _diElectronMassComboHypoToolFromDict(chainDict, mass_range): 
+def _diElectronMassComboHypoToolFromDict(flags, chainDict, mass_range):
     name = chainDict['chainName']
-    monTool = GenericMonitoringTool("MonTool_"+name,
+    monTool = GenericMonitoringTool(flags, "MonTool_"+name,
                                     HistPath = 'EgammaMassHypo/'+name)
     monTool.defineHistogram('DphiOfProcessed', type='TH1F', path='EXPERT', title="PrecisionCalo Hypo entries per Phi;Phi", xbins=128, xmin=-3.2, xmax=3.2)
     monTool.defineHistogram('MassOfProcessed', type='TH1F', path='EXPERT', title="Mass in accepted combinations [MeV]", xbins=75, xmin=0, xmax=150000)
@@ -106,14 +106,14 @@ def _diElectronMassComboHypoToolFromDict(chainDict, mass_range):
     return tool
 
 
-def diElectronZeeMassComboHypoToolFromDict(chainDict):
-    return _diElectronMassComboHypoToolFromDict(chainDict, (50000, 130000))
+def diElectronZeeMassComboHypoToolFromDict(flags, chainDict):
+    return _diElectronMassComboHypoToolFromDict(flags, chainDict, (50000, 130000))
 
-def diElectronJpsieeMassComboHypoToolFromDict(chainDict):
-    return _diElectronMassComboHypoToolFromDict(chainDict, (1000, 5000))
+def diElectronJpsieeMassComboHypoToolFromDict(flags, chainDict):
+    return _diElectronMassComboHypoToolFromDict(flags, chainDict, (1000, 5000))
 
-def diEgammaHegMassComboHypoToolFromDict(chainDict):
-    return _diElectronMassComboHypoToolFromDict(chainDict, (90000, 1400000))
+def diEgammaHegMassComboHypoToolFromDict(flags, chainDict):
+    return _diElectronMassComboHypoToolFromDict(flags, chainDict, (90000, 1400000))
 
 def electronFastCaloCfg_fwd( flags, is_probe_leg=False ):
     return fastCaloMenuSequence_FWD(flags, "Electron", is_probe_leg=is_probe_leg)
diff --git a/Trigger/TriggerCommon/TriggerMenuMT/python/HLT/MinBias/AFPMenuSequence.py b/Trigger/TriggerCommon/TriggerMenuMT/python/HLT/MinBias/AFPMenuSequence.py
index 238e0cbad3f4..925bfe3dc036 100644
--- a/Trigger/TriggerCommon/TriggerMenuMT/python/HLT/MinBias/AFPMenuSequence.py
+++ b/Trigger/TriggerCommon/TriggerMenuMT/python/HLT/MinBias/AFPMenuSequence.py
@@ -58,12 +58,12 @@ def AFPTrkSequenceCfg(flags):
                         Hypo     = hypoAlg,
                         HypoToolGen = trigStreamerAFPHypoTool)
 
-def TestTrigAFPDijetHypoToolGen(chainDict):
+def TestTrigAFPDijetHypoToolGen(flags, chainDict):
     from TrigAFPHypo.TrigAFPHypoConf import TestTrigAFPDijetHypoTool
     hypo = TestTrigAFPDijetHypoTool(chainDict["chainName"])
 
     from AthenaMonitoringKernel.GenericMonitoringTool import GenericMonitoringTool
-    monTool = GenericMonitoringTool("MonTool_AFP_Hypo")
+    monTool = GenericMonitoringTool(flags, "MonTool_AFP_Hypo")
     monTool.defineHistogram('DijetMass', type='TH1F', path='EXPERT', title="Dijet mass", xbins=100, xmin=0, xmax=2000)
     monTool.defineHistogram('DijetRapidity', type='TH1F', path='EXPERT', title="Dijet rapidity", xbins=100, xmin=-5, xmax=5)
 
diff --git a/Trigger/TriggerCommon/TriggerMenuMT/python/HLT/MinBias/MinBiasChainConfiguration.py b/Trigger/TriggerCommon/TriggerMenuMT/python/HLT/MinBias/MinBiasChainConfiguration.py
index 02b26f671eb0..ab080dc72d2f 100644
--- a/Trigger/TriggerCommon/TriggerMenuMT/python/HLT/MinBias/MinBiasChainConfiguration.py
+++ b/Trigger/TriggerCommon/TriggerMenuMT/python/HLT/MinBias/MinBiasChainConfiguration.py
@@ -36,12 +36,12 @@ def MinBiasMbtsEmptySequenceCfg(flags):
 def MinBiasZFindEmptySequenceCfg(flags):
     return EmptyMenuSequence("EmptyZFind")
 
-def TrigAFPDijetComboHypoToolCfg(chainDict):
+def TrigAFPDijetComboHypoToolCfg(flags, chainDict):
     from TrigAFPHypo.TrigAFPHypoConf import TrigAFPDijetComboHypoTool
     name = chainDict['chainName']
     tool = TrigAFPDijetComboHypoTool(name)
 
-    monTool = GenericMonitoringTool("MonTool_"+name,
+    monTool = GenericMonitoringTool(flags, "MonTool_"+name,
                                     HistPath = 'AFPComboHypo/'+tool.getName())
     monTool.defineHistogram('DijetMass', type='TH1F', path='EXPERT', title="Dijet mass", xbins=100, xmin=0, xmax=2000)
     monTool.defineHistogram('DijetRapidity', type='TH1F', path='EXPERT', title="Dijet rapidity", xbins=100, xmin=-5, xmax=5)
diff --git a/Trigger/TriggerCommon/TriggerMenuMT/python/HLT/Photon/PhotonChainConfiguration.py b/Trigger/TriggerCommon/TriggerMenuMT/python/HLT/Photon/PhotonChainConfiguration.py
index 528b188aff8b..3de71da39291 100644
--- a/Trigger/TriggerCommon/TriggerMenuMT/python/HLT/Photon/PhotonChainConfiguration.py
+++ b/Trigger/TriggerCommon/TriggerMenuMT/python/HLT/Photon/PhotonChainConfiguration.py
@@ -33,9 +33,9 @@ def fastPhotonCaloSequenceCfg( flags, doRinger = False ):
 def fastPhotonSequenceCfg( flags ):    
     return fastPhotonMenuSequence( flags )
 
-def _diPhotonComboHypoToolFromDict(chainDict, lowermass=80000,uppermass=-999,dphi=1.5,applymass=False,applydphi=False): 
+def _diPhotonComboHypoToolFromDict(flags, chainDict, lowermass=80000,uppermass=-999,dphi=1.5,applymass=False,applydphi=False):
     name = chainDict['chainName']
-    monTool = GenericMonitoringTool("MonTool_"+name,
+    monTool = GenericMonitoringTool(flags, "MonTool_"+name,
                                     HistPath = 'EgammaMassHypo/'+name)
     monTool.defineHistogram('DphiOfAccepted', type='TH1F', path='EXPERT', title="PrecisionCalo Hypo entries per Phi;Phi", xbins=128, xmin=-3.2, xmax=3.2)
     monTool.defineHistogram('MassOfAccepted', type='TH1F', path='EXPERT', title="Mass in accepted combinations [MeV]", xbins=75, xmin=0, xmax=150000)
@@ -50,11 +50,11 @@ def _diPhotonComboHypoToolFromDict(chainDict, lowermass=80000,uppermass=-999,dph
                                  MonTool = monTool)
     return tool
 
-def diphotonDPhiHypoToolFromDict(chainDict):
-    return _diPhotonComboHypoToolFromDict(chainDict,lowermass=80000,uppermass=-999,dphi=1.5,applymass=False,applydphi=True)
+def diphotonDPhiHypoToolFromDict(flags, chainDict):
+    return _diPhotonComboHypoToolFromDict(flags,chainDict,lowermass=80000,uppermass=-999,dphi=1.5,applymass=False,applydphi=True)
 
-def diphotonDPhiMassHypoToolFromDict(chainDict):
-    return _diPhotonComboHypoToolFromDict(chainDict,lowermass=80000,uppermass=-999,dphi=1.5,applymass=True,applydphi=True)
+def diphotonDPhiMassHypoToolFromDict(flags, chainDict):
+    return _diPhotonComboHypoToolFromDict(flags,chainDict,lowermass=80000,uppermass=-999,dphi=1.5,applymass=True,applydphi=True)
 
 
 #----------------------------------------------------------------
-- 
GitLab