From af2a16ce75f8ba17241d832fc05a3f2c84cd83cf Mon Sep 17 00:00:00 2001
From: Tadej Novak <tadej.novak@cern.ch>
Date: Fri, 30 Oct 2020 16:30:16 +0100
Subject: [PATCH] Only run CutFlowSvc when explicitly configured and wanted

---
 .../share/DecisionPropagation_jo.py           |  2 ++
 .../share/EventNoSplit_jo.py                  |  2 ++
 .../share/EventSplitDecision_jo.py            |  2 ++
 .../share/EventSplit_jo.py                    |  2 ++
 .../share/SplittableData_jo.py                |  3 +++
 .../AthenaPoolExample_WriteJobOptions.py      |  4 +++
 .../EventBookkeeperTools/CutFlowSvc.h         |  3 +++
 .../EventBookkeeperTools/Root/CutFlowSvc.cxx  |  6 +++++
 .../python/CutFlowHelpers.py                  |  3 ++-
 .../python/EventBookkeeperToolsConfig.py      |  2 +-
 .../share/TestCutFlowSvcDummyAlg.py           | 25 ++++++++++++++++---
 .../share/RecExCommon_topOptions.py           | 24 ++++++++++++------
 12 files changed, 65 insertions(+), 13 deletions(-)

diff --git a/AtlasTest/DatabaseTest/AthenaPoolMultiTest/share/DecisionPropagation_jo.py b/AtlasTest/DatabaseTest/AthenaPoolMultiTest/share/DecisionPropagation_jo.py
index ec56a8509d87..738c24f3d9dc 100755
--- a/AtlasTest/DatabaseTest/AthenaPoolMultiTest/share/DecisionPropagation_jo.py
+++ b/AtlasTest/DatabaseTest/AthenaPoolMultiTest/share/DecisionPropagation_jo.py
@@ -19,6 +19,8 @@ from xAODEventInfoCnv.xAODEventInfoCnvConf import xAODMaker__EventInfoCnvAlg
 alg = xAODMaker__EventInfoCnvAlg()
 topSequence += alg
 
+from EventBookkeeperTools.CutFlowHelpers import CreateCutFlowSvc
+CreateCutFlowSvc( svcName="CutFlowSvc", seq=topSequence, addMetaDataToAllOutputFiles=True )
 
 from AthenaCommon.AppMgr import theApp
 theApp.EvtMax = 200000
diff --git a/AtlasTest/DatabaseTest/AthenaPoolMultiTest/share/EventNoSplit_jo.py b/AtlasTest/DatabaseTest/AthenaPoolMultiTest/share/EventNoSplit_jo.py
index e1470163db97..a264e9e70212 100755
--- a/AtlasTest/DatabaseTest/AthenaPoolMultiTest/share/EventNoSplit_jo.py
+++ b/AtlasTest/DatabaseTest/AthenaPoolMultiTest/share/EventNoSplit_jo.py
@@ -24,6 +24,8 @@ from xAODEventInfoCnv.xAODEventInfoCnvConf import xAODMaker__EventInfoCnvAlg
 alg = xAODMaker__EventInfoCnvAlg()
 topSequence += alg
 
+from EventBookkeeperTools.CutFlowHelpers import CreateCutFlowSvc
+CreateCutFlowSvc( svcName="CutFlowSvc", seq=topSequence, addMetaDataToAllOutputFiles=True )
 
 from AthenaCommon.AppMgr import theApp
 theApp.EvtMax = 200000
diff --git a/AtlasTest/DatabaseTest/AthenaPoolMultiTest/share/EventSplitDecision_jo.py b/AtlasTest/DatabaseTest/AthenaPoolMultiTest/share/EventSplitDecision_jo.py
index 3a9c4157cd1f..b106e24a2f2f 100755
--- a/AtlasTest/DatabaseTest/AthenaPoolMultiTest/share/EventSplitDecision_jo.py
+++ b/AtlasTest/DatabaseTest/AthenaPoolMultiTest/share/EventSplitDecision_jo.py
@@ -19,6 +19,8 @@ from xAODEventInfoCnv.xAODEventInfoCnvConf import xAODMaker__EventInfoCnvAlg
 alg = xAODMaker__EventInfoCnvAlg()
 topSequence += alg
 
+from EventBookkeeperTools.CutFlowHelpers import CreateCutFlowSvc
+CreateCutFlowSvc( svcName="CutFlowSvc", seq=topSequence, addMetaDataToAllOutputFiles=True )
 
 from AthenaCommon.AppMgr import theApp
 theApp.EvtMax = 200000
diff --git a/AtlasTest/DatabaseTest/AthenaPoolMultiTest/share/EventSplit_jo.py b/AtlasTest/DatabaseTest/AthenaPoolMultiTest/share/EventSplit_jo.py
index 4d604280b5f2..ed96960dd342 100755
--- a/AtlasTest/DatabaseTest/AthenaPoolMultiTest/share/EventSplit_jo.py
+++ b/AtlasTest/DatabaseTest/AthenaPoolMultiTest/share/EventSplit_jo.py
@@ -24,6 +24,8 @@ from xAODEventInfoCnv.xAODEventInfoCnvConf import xAODMaker__EventInfoCnvAlg
 alg = xAODMaker__EventInfoCnvAlg()
 topSequence += alg
 
+from EventBookkeeperTools.CutFlowHelpers import CreateCutFlowSvc
+CreateCutFlowSvc( svcName="CutFlowSvc", seq=topSequence, addMetaDataToAllOutputFiles=True )
 
 from AthenaCommon.AppMgr import theApp
 theApp.EvtMax = 200000
diff --git a/AtlasTest/DatabaseTest/AthenaPoolMultiTest/share/SplittableData_jo.py b/AtlasTest/DatabaseTest/AthenaPoolMultiTest/share/SplittableData_jo.py
index f2c5c32905b9..b05c5dc842aa 100755
--- a/AtlasTest/DatabaseTest/AthenaPoolMultiTest/share/SplittableData_jo.py
+++ b/AtlasTest/DatabaseTest/AthenaPoolMultiTest/share/SplittableData_jo.py
@@ -85,6 +85,9 @@ svcMgr += AthenaPoolCnvSvc()
 
 include( "AthenaPoolMultiTest/ExampleStreamConfig.py" )
 
+from EventBookkeeperTools.CutFlowHelpers import CreateCutFlowSvc
+CreateCutFlowSvc( svcName="CutFlowSvc", seq=topSequence, addMetaDataToAllOutputFiles=True )
+
 from AthenaPoolCnvSvc.WriteAthenaPool import AthenaPoolOutputStream
 DataStream = AthenaPoolOutputStream( "DataStream" , "AthenaPoolMultiTest_Splittable0.root", False, noTag=False )
 DataStream.ItemList    = exampleItemList  # comes from ExampleStreamConfig
diff --git a/Database/AthenaPOOL/AthenaPoolExample/AthenaPoolExampleAlgorithms/share/AthenaPoolExample_WriteJobOptions.py b/Database/AthenaPOOL/AthenaPoolExample/AthenaPoolExampleAlgorithms/share/AthenaPoolExample_WriteJobOptions.py
index 8cf09a1f04c7..08f1e5ceb931 100755
--- a/Database/AthenaPOOL/AthenaPoolExample/AthenaPoolExampleAlgorithms/share/AthenaPoolExample_WriteJobOptions.py
+++ b/Database/AthenaPOOL/AthenaPoolExample/AthenaPoolExampleAlgorithms/share/AthenaPoolExample_WriteJobOptions.py
@@ -100,6 +100,10 @@ svcMgr.EventSelector.RunNumber = 1
 #--------------------------------------------------------------
 # Private Application Configuration options
 #--------------------------------------------------------------
+# Load CutFlowSvc
+from EventBookkeeperTools.CutFlowHelpers import CreateCutFlowSvc
+CreateCutFlowSvc( svcName="CutFlowSvc", seq=topSequence, addMetaDataToAllOutputFiles=True )
+
 # Load "user algorithm" top algorithms to be run, and the libraries that house them
 from AthenaPoolExampleAlgorithms.AthenaPoolExampleAlgorithmsConf import AthPoolEx__WriteData
 topSequence += AthPoolEx__WriteData( "WriteData" )
diff --git a/Event/EventBookkeeperTools/EventBookkeeperTools/CutFlowSvc.h b/Event/EventBookkeeperTools/EventBookkeeperTools/CutFlowSvc.h
index 09a30f7ed3a5..2c6697964222 100644
--- a/Event/EventBookkeeperTools/EventBookkeeperTools/CutFlowSvc.h
+++ b/Event/EventBookkeeperTools/EventBookkeeperTools/CutFlowSvc.h
@@ -110,6 +110,9 @@ private:
   xAOD::CutBookkeeper* getCutBookkeeper(const CutIdentifier cutID,
                                         size_t index) const;
 
+  /// CutFlow service should be explicitly configured
+  Gaudi::Property<bool> m_configured{this, "Configured", false, ""};
+
   /// The input meta-data store
   ServiceHandle<StoreGateSvc> m_inMetaDataStore{this, "InputMetaDataStore", "StoreGateSvc/InputMetaDataStore", ""};
 
diff --git a/Event/EventBookkeeperTools/Root/CutFlowSvc.cxx b/Event/EventBookkeeperTools/Root/CutFlowSvc.cxx
index c23155dce2a0..099b1caf0002 100644
--- a/Event/EventBookkeeperTools/Root/CutFlowSvc.cxx
+++ b/Event/EventBookkeeperTools/Root/CutFlowSvc.cxx
@@ -33,6 +33,12 @@ CutFlowSvc::initialize()
 {
   ATH_MSG_DEBUG( "Initializing " << name() );
 
+  // Only run if explicitly configured
+  if (m_configured.value() == false) {
+    ATH_MSG_ERROR("CutFlowSvc should be explicitly configured!");
+    return StatusCode::FAILURE;
+  }
+
   //Get input MetaData StoreGate
   ATH_CHECK( m_inMetaDataStore.retrieve() );
 
diff --git a/Event/EventBookkeeperTools/python/CutFlowHelpers.py b/Event/EventBookkeeperTools/python/CutFlowHelpers.py
index 351ed7a8ee1b..dcef71543bda 100644
--- a/Event/EventBookkeeperTools/python/CutFlowHelpers.py
+++ b/Event/EventBookkeeperTools/python/CutFlowHelpers.py
@@ -44,7 +44,8 @@ def CreateCutFlowSvc( svcName="CutFlowSvc", seq=None, addMetaDataToAllOutputFile
     # Create the CutFlowSvc instance
     import AthenaCommon.CfgMgr as CfgMgr
     if not hasattr(svcMgr,"CutFlowSvc"): svcMgr += CfgMgr.CutFlowSvc()
-    svcMgr.CutFlowSvc.InputStream   = inputStreamName
+    svcMgr.CutFlowSvc.Configured  = True
+    svcMgr.CutFlowSvc.InputStream = inputStreamName
 
     # Make sure MetaDataSvc is ready
     if not hasattr(svcMgr,'MetaDataSvc'):
diff --git a/Event/EventBookkeeperTools/python/EventBookkeeperToolsConfig.py b/Event/EventBookkeeperTools/python/EventBookkeeperToolsConfig.py
index 4359575d21a8..0465d1f5b3cf 100644
--- a/Event/EventBookkeeperTools/python/EventBookkeeperToolsConfig.py
+++ b/Event/EventBookkeeperTools/python/EventBookkeeperToolsConfig.py
@@ -27,7 +27,7 @@ def CutFlowSvcCfg(flags):
     # Determine current input stream name
     # inputStreamName = GetCurrentStreamName( msg=msg )
 
-    acc.addService(CompFactory.CutFlowSvc())
+    acc.addService(CompFactory.CutFlowSvc(Configured=True))
     # svcMgr.CutFlowSvc.InputStream   = inputStreamName
 
     acc.merge(BookkeeperToolCfg(flags))
diff --git a/Event/EventBookkeeperTools/share/TestCutFlowSvcDummyAlg.py b/Event/EventBookkeeperTools/share/TestCutFlowSvcDummyAlg.py
index f821ab936129..311c938002af 100644
--- a/Event/EventBookkeeperTools/share/TestCutFlowSvcDummyAlg.py
+++ b/Event/EventBookkeeperTools/share/TestCutFlowSvcDummyAlg.py
@@ -1,8 +1,27 @@
 # Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
 
+# setup the input
+from AthenaCommon.AthenaCommonFlags import athenaCommonFlags
+if 'inputFiles' in dir():
+    athenaCommonFlags.FilesInput = inputFiles.split(',')
+    del inputFiles
+else:
+    athenaCommonFlags.FilesInput = [
+        '/cvmfs/atlas-nightlies.cern.ch/repo/data/data-art/CommonInputs/mc16_13TeV.410501.PowhegPythia8EvtGen_A14_ttbar_hdamp258p75_nonallhad.merge.AOD.e5458_s3126_r9364_r9315/AOD.11182705._000001.pool.root.1'
+    ]
+
+theApp.EvtMax = 10
+
 from AthenaCommon.AlgSequence import AlgSequence
-from EventBookkeeperTools.PyFilterTester import PyFilterTester
 topSequence = AlgSequence()
-topSequence += PyFilterTester('myFilter')
 
-theApp.EvtMax = 10
+from xAODEventInfoCnv.xAODEventInfoCnvConf import xAODMaker__EventInfoCnvAlg
+topSequence += xAODMaker__EventInfoCnvAlg()
+
+# setup the CutFlowSvc
+from EventBookkeeperTools.CutFlowHelpers import CreateCutFlowSvc
+CreateCutFlowSvc(svcName="CutFlowSvc", seq=topSequence, addMetaDataToAllOutputFiles=True)
+
+# setup the test alg
+from EventBookkeeperTools.PyFilterTester import PyFilterTester
+topSequence += PyFilterTester('myFilter')
diff --git a/Reconstruction/RecExample/RecExCommon/share/RecExCommon_topOptions.py b/Reconstruction/RecExample/RecExCommon/share/RecExCommon_topOptions.py
index fb953b93aecd..a9025e610881 100644
--- a/Reconstruction/RecExample/RecExCommon/share/RecExCommon_topOptions.py
+++ b/Reconstruction/RecExample/RecExCommon/share/RecExCommon_topOptions.py
@@ -943,14 +943,22 @@ if rec.doFileMetaData():
 
     # Add the needed stuff for cut-flow bookkeeping.
     # Only the configurables that are not already present will be created
-    from EventBookkeeperTools.CutFlowHelpers import CreateCutFlowSvc
-    logRecExCommon_topOptions.debug("Going to call CreateCutFlowSvc")
-    CreateCutFlowSvc( svcName="CutFlowSvc", seq=topSequence, addMetaDataToAllOutputFiles=True )
-    if rec.readAOD() or rec.readESD():
-        #force CutFlowSvc execution (necessary for file merging)
-        theApp.CreateSvc+=['CutFlowSvc']
-        logRecExCommon_topOptions.debug("Added CutFlowSvc to theApp")
-        pass    
+    hasBookkeepers = False
+    if 'metadata_items' in metadata:
+        metadata_items = metadata['metadata_items']
+        if 'xAOD::CutBookkeeperContainer_v1' in set(metadata_items.values()):
+            logRecExCommon_topOptions.debug("Existing CutBookkeeperContainer found")
+            hasBookkeepers = True
+    if hasBookkeepers or hasattr(runArgs, "reductionConf"): # TODO: no other way to detect we are running derivations
+        # TODO: check all DAOD workflows
+        from EventBookkeeperTools.CutFlowHelpers import CreateCutFlowSvc
+        logRecExCommon_topOptions.debug("Going to call CreateCutFlowSvc")
+        CreateCutFlowSvc( svcName="CutFlowSvc", seq=topSequence, addMetaDataToAllOutputFiles=True )
+        if rec.readAOD() or rec.readESD():
+            #force CutFlowSvc execution (necessary for file merging)
+            theApp.CreateSvc+=['CutFlowSvc']
+            logRecExCommon_topOptions.debug("Added CutFlowSvc to theApp")
+            pass    
 
     try:
         # ByteStreamMetadata
-- 
GitLab