From e4ff1288054b79c7e1a314663dc43225e905bb80 Mon Sep 17 00:00:00 2001
From: Rafal Bielski <rafal.bielski@cern.ch>
Date: Tue, 15 Dec 2020 13:02:59 +0100
Subject: [PATCH] Disable Trigger in RecExCommon unless really needed

---
 .../share/RecExCommon_topOptions.py           | 65 ++++++++++++-------
 .../RecExConfig/python/RecAlgsFlags.py        |  4 +-
 .../share/skeleton.ESDtoAOD_tf.py             | 20 ++----
 3 files changed, 48 insertions(+), 41 deletions(-)

diff --git a/Reconstruction/RecExample/RecExCommon/share/RecExCommon_topOptions.py b/Reconstruction/RecExample/RecExCommon/share/RecExCommon_topOptions.py
index 6529e23a8e2..9988b612aec 100644
--- a/Reconstruction/RecExample/RecExCommon/share/RecExCommon_topOptions.py
+++ b/Reconstruction/RecExample/RecExCommon/share/RecExCommon_topOptions.py
@@ -554,42 +554,57 @@ if rec.readESD() and rec.doESD():
     rec.doTrigger=False
     recAlgs.doTrigger=False
     logRecExCommon_topOptions.info("detected re-reconstruction from ESD, will switch trigger OFF !")
-#try:
+
+# Disable Trigger output reading in MC if there is none, unless running Trigger selection algorithms
+if not globalflags.InputFormat.is_bytestream() and not recAlgs.doTrigger:
+    try:
+        from RecExConfig.ObjKeyStore import cfgKeyStore
+        from PyUtils.MetaReaderPeeker import convert_itemList
+        cfgKeyStore.addManyTypesInputFile(convert_itemList(layout='#join'))
+        # Check for Run-1, Run-2 or Run-3 Trigger content in the input file
+        if not cfgKeyStore.isInInputFile("HLT::HLTResult", "HLTResult_EF") \
+                and not cfgKeyStore.isInInputFile("xAOD::TrigNavigation", "TrigNavigation") \
+                and not cfgKeyStore.isInInputFile("xAOD::TrigCompositeContainer", "HLTNav_Summary"):
+            logRecExCommon_topOptions.info('Disabled rec.doTrigger because recAlgs.doTrigger=False and there is no Trigger content in the input file')
+            rec.doTrigger = False
+    except Exception:
+        logRecExCommon_topOptions.warning('Failed to check input file for Trigger content, leaving rec.doTrigger value unchanged (%s)', rec.doTrigger)
+
 if rec.doTrigger:
-    if globalflags.DataSource() == 'data'and globalflags.InputFormat == 'bytestream':
+    if globalflags.DataSource() == 'data' and globalflags.InputFormat == 'bytestream':
         try:
             include("TriggerJobOpts/BStoESD_Tier0_HLTConfig_jobOptions.py")
         except Exception:
             treatException("Could not import TriggerJobOpts/BStoESD_Tier0_HLTConfig_jobOptions.py . Switching trigger off !" )
-            recAlgs.doTrigger=False
+            rec.doTrigger = recAlgs.doTrigger = False
     else:
         try:
             from TriggerJobOpts.TriggerGetter import TriggerGetter
             triggerGetter = TriggerGetter()
         except Exception:
             treatException("Could not import TriggerJobOpts.TriggerGetter . Switched off !" )
-            recAlgs.doTrigger=False
-
-# Run-3 Trigger Outputs: Don't run any trigger - only pass the HLT contents from ESD to AOD
-if rec.readESD() and rec.doAOD():
-    from AthenaConfiguration.AllConfigFlags import ConfigFlags
-    # The simplest protection in case ConfigFlags.Input.Files is not set, doesn't cover all cases:
-    if ConfigFlags.Input.Files == ['_ATHENA_GENERIC_INPUTFILE_NAME_'] and athenaCommonFlags.FilesInput():
-        ConfigFlags.Input.Files = athenaCommonFlags.FilesInput()
-
-    if ConfigFlags.Trigger.EDMVersion == 3:
-        # Add HLT output
-        from TriggerJobOpts.HLTTriggerResultGetter import HLTTriggerResultGetter
-        hltOutput = HLTTriggerResultGetter()
-        # Add Trigger menu metadata
-        if rec.doFileMetaData():
-            from RecExConfig.ObjKeyStore import objKeyStore
-            metadataItems = [ "xAOD::TriggerMenuContainer#TriggerMenu",
-                              "xAOD::TriggerMenuAuxContainer#TriggerMenuAux." ]
-            objKeyStore.addManyTypesMetaData( metadataItems )
-        # Add L1 output (to be consistent with R2)
-        from TrigEDMConfig.TriggerEDM import getLvl1AODList
-        objKeyStore.addManyTypesStreamAOD(getLvl1AODList())
+            rec.doTrigger = recAlgs.doTrigger = False
+
+    # ESDtoAOD Run-3 Trigger Outputs: Don't run any trigger - only pass the HLT contents from ESD to AOD
+    if rec.readESD() and rec.doAOD():
+        from AthenaConfiguration.AllConfigFlags import ConfigFlags
+        # The simplest protection in case ConfigFlags.Input.Files is not set, doesn't cover all cases:
+        if ConfigFlags.Input.Files == ['_ATHENA_GENERIC_INPUTFILE_NAME_'] and athenaCommonFlags.FilesInput():
+            ConfigFlags.Input.Files = athenaCommonFlags.FilesInput()
+
+        if ConfigFlags.Trigger.EDMVersion == 3:
+            # Add HLT output
+            from TriggerJobOpts.HLTTriggerResultGetter import HLTTriggerResultGetter
+            hltOutput = HLTTriggerResultGetter()
+            # Add Trigger menu metadata
+            if rec.doFileMetaData():
+                from RecExConfig.ObjKeyStore import objKeyStore
+                metadataItems = [ "xAOD::TriggerMenuContainer#TriggerMenu",
+                                "xAOD::TriggerMenuAuxContainer#TriggerMenuAux." ]
+                objKeyStore.addManyTypesMetaData( metadataItems )
+            # Add L1 output (to be consistent with R2)
+            from TrigEDMConfig.TriggerEDM import getLvl1AODList
+            objKeyStore.addManyTypesStreamAOD(getLvl1AODList())
 
 AODFix_postTrigger()
 
diff --git a/Reconstruction/RecExample/RecExConfig/python/RecAlgsFlags.py b/Reconstruction/RecExample/RecExConfig/python/RecAlgsFlags.py
index 720352aba5d..77ae0617c99 100755
--- a/Reconstruction/RecExample/RecExConfig/python/RecAlgsFlags.py
+++ b/Reconstruction/RecExample/RecExConfig/python/RecAlgsFlags.py
@@ -102,9 +102,11 @@ class doEgammaBremReco(JobProperty):
     StoredValue=False 
 class doTrigger(JobProperty):
     """ Switch for trigger"""
+    # TODO: Remove this flag and assume False in all client configuration
+    # once the Run-2 trigger execution is removed from release 22
     statusOn=True 
     allowedTypes=['bool']
-    StoredValue=True 
+    StoredValue=False
 class doAtlfast(JobProperty):
     """ Switch for fast simulation (but normally run at ESD->AOD stage)"""
     statusOn=True
diff --git a/Reconstruction/RecJobTransforms/share/skeleton.ESDtoAOD_tf.py b/Reconstruction/RecJobTransforms/share/skeleton.ESDtoAOD_tf.py
index 7014c91ffcc..443fd878689 100644
--- a/Reconstruction/RecJobTransforms/share/skeleton.ESDtoAOD_tf.py
+++ b/Reconstruction/RecJobTransforms/share/skeleton.ESDtoAOD_tf.py
@@ -52,21 +52,11 @@ if hasattr(runArgs,"outputAODFile"):
     rec.doAOD.set_Value_and_Lock( True )
     rec.doWriteAOD.set_Value_and_Lock( True ) 
     athenaCommonFlags.PoolAODOutput.set_Value_and_Lock( runArgs.outputAODFile )
-    # Begin temporary block for Run-3 Trigger outputs
-    if ConfigFlags.Trigger.EDMVersion == 3:
-        # Lock DQ configuration to prevent downstream override
-        from AthenaMonitoring.DQMonFlags import DQMonFlags
-        print('DQMonFlags override')
-        if not rec.doTrigger():
-            DQMonFlags.useTrigger.set_Value_and_Lock(False)
-        if DQMonFlags.useTrigger() and rec.doTrigger():
-            DQMonFlags.useTrigger.set_Value_and_Lock(True)
-        # Don't run any trigger - only pass the HLT contents from ESD to AOD
-        # Configure here, and extract HLT content in RecExCommon_topOptions
-        # after the rest of the job is configured
-        from RecExConfig.RecAlgsFlags import recAlgs
-        recAlgs.doTrigger.set_Value_and_Lock( False )
-        rec.doTrigger.set_Value_and_Lock( False )
+    # Lock DQ configuration to prevent downstream override
+    # RB 15/12/2020: This logic was added in !36737, not sure if still needed
+    from AthenaMonitoring.DQMonFlags import DQMonFlags
+    print('DQMonFlags.useTrigger override')
+    DQMonFlags.useTrigger.set_Value_and_Lock(rec.doTrigger() and DQMonFlags.useTrigger())
 
 if hasattr(runArgs,"outputTAGFile"):
     # should be used as outputTAGFile_e2a=myTAG.root so that it does not trigger AODtoTAG
-- 
GitLab