From 0dcd21471acf423f1e622a9ad1c76646096cbc95 Mon Sep 17 00:00:00 2001
From: John Derek Chapman <chapman@hep.phy.cam.ac.uk>
Date: Fri, 27 May 2022 18:48:18 +0200
Subject: [PATCH] Add missing required key:value pairs to
 /Digitization/Parameters metadata in CA-based configuration

Now that flags.Digitization.DigiSteeringConf is auto-configured based on metadata, it can be used directly.
---
 .../xAODTruthCnv/python/xAODTruthCnvConfigNew.py     | 12 +++---------
 .../Digitization/python/DigitizationConfigFlags.py   |  9 ++++++++-
 .../python/DigitizationParametersConfig.py           |  4 +++-
 .../G4Atlas/G4AtlasApps/python/SimConfigFlags.py     | 12 +++++++++++-
 Tools/PyUtils/python/MetaReader.py                   |  3 +++
 5 files changed, 28 insertions(+), 12 deletions(-)

diff --git a/Event/xAOD/xAODTruthCnv/python/xAODTruthCnvConfigNew.py b/Event/xAOD/xAODTruthCnv/python/xAODTruthCnvConfigNew.py
index fd79a44d9600..02abb885aeb3 100644
--- a/Event/xAOD/xAODTruthCnv/python/xAODTruthCnvConfigNew.py
+++ b/Event/xAOD/xAODTruthCnv/python/xAODTruthCnvConfigNew.py
@@ -17,15 +17,9 @@ def GEN_AOD2xAODCfg(flags, name="GEN_AOD2xAOD", **kwargs):
 
     writeInTimePileUpTruth=False
 
-    # Use digiSteeringConf from metadata to write full-PU truth
-    # Not available yet in metadata
-
-    from PyUtils.MetaReader import read_metadata
-    infile = flags.Input.Files[0]
-    thisFileMD = read_metadata(infile, None, 'full')
-    metadata = thisFileMD[infile]
-    digiSteeringConf = metadata['/Digitization/Parameters'].get("digiSteeringConf","")
-    if digiSteeringConf == 'StandardInTimeOnlyGeantinoTruthPileUpToolsAlg':
+    # Use digiSteeringConf from metadata to check whether full-PU
+    # truth should be written.
+    if flags.Digitization.DigiSteeringConf == 'StandardInTimeOnlyGeantinoTruthPileUpToolsAlg':
         writeInTimePileUpTruth = True
 
     kwargs.setdefault('WriteInTimePileUpTruth',  writeInTimePileUpTruth)
diff --git a/Simulation/Digitization/python/DigitizationConfigFlags.py b/Simulation/Digitization/python/DigitizationConfigFlags.py
index a68cc5ca98db..3eaaac9257be 100644
--- a/Simulation/Digitization/python/DigitizationConfigFlags.py
+++ b/Simulation/Digitization/python/DigitizationConfigFlags.py
@@ -33,7 +33,14 @@ def createDigitizationCfgFlags():
     flags = AthConfigFlags()
     # Digitization Steering - needed for easy comparison with the
     # old-style configuration, but can potentially drop
-    flags.addFlag("Digitization.DigiSteeringConf", "StandardPileUpToolsAlg")
+    def _checkDigiSteeringConf(prevFlags):
+        digiSteeringConf = "StandardPileUpToolsAlg"
+        if prevFlags.Input.Files:
+            from AthenaConfiguration.AutoConfigFlags import GetFileMD
+            digiSteeringConf = GetFileMD(prevFlags.Input.Files).get("digiSteeringConf", "StandardPileUpToolsAlg")
+        return digiSteeringConf
+
+    flags.addFlag("Digitization.DigiSteeringConf", _checkDigiSteeringConf)
     # Run Inner Detector noise simulation
     flags.addFlag("Digitization.DoInnerDetectorNoise", True)
     # Run pile-up digitization on one bunch crossing at a time?
diff --git a/Simulation/Digitization/python/DigitizationParametersConfig.py b/Simulation/Digitization/python/DigitizationParametersConfig.py
index 1337cb9b44e0..4716e19b5add 100644
--- a/Simulation/Digitization/python/DigitizationParametersConfig.py
+++ b/Simulation/Digitization/python/DigitizationParametersConfig.py
@@ -24,7 +24,9 @@ def writeDigitizationMetadata(ConfigFlags):
                           'IOVDbGlobalTag' : 'IOVDb.GlobalTag',
                           'DetDescrVersion' : 'GeoModel.AtlasVersion',
                           'finalBunchCrossing' : 'Digitization.PU.FinalBunchCrossing',
-                          'initialBunchCrossing' : 'Digitization.PU.InitialBunchCrossing'
+                          'initialBunchCrossing' : 'Digitization.PU.InitialBunchCrossing',
+                          'physicsList' : 'Sim.PhysicsList', #TODO migrate clients to use /Simulation/Parameters metadata?
+                          'digiSteeringConf' : 'Digitization.DigiSteeringConf'
                       }
     logDigitizationWriteMetadata.info('Filling Digitization MetaData')
     for testKey, testFlag in digitMetaDataKeys.items():
diff --git a/Simulation/G4Atlas/G4AtlasApps/python/SimConfigFlags.py b/Simulation/G4Atlas/G4AtlasApps/python/SimConfigFlags.py
index 06ea3ee8fb28..5e61640e10f0 100644
--- a/Simulation/G4Atlas/G4AtlasApps/python/SimConfigFlags.py
+++ b/Simulation/G4Atlas/G4AtlasApps/python/SimConfigFlags.py
@@ -56,7 +56,17 @@ def createSimConfigFlags():
 
     scf.addFlag("Sim.G4Version", _check_G4_version)
 
-    scf.addFlag("Sim.PhysicsList", "FTFP_BERT_ATL")
+    def _checkPhysicsListConf(prevFlags):
+        physicsList = "FTFP_BERT_ATL"
+        if prevFlags.Input.Files:
+            from AthenaConfiguration.AutoConfigFlags import GetFileMD
+            physicsList = GetFileMD(prevFlags.Input.Files).get("PhysicsList", "")
+            if not physicsList:
+                # Currently physicsList is also part of /Digitization/Parameters metadata. TODO migrate away from this.
+                physicsList = GetFileMD(prevFlags.Input.Files).get("physicsList", "FTFP_BERT_ATL")
+        return physicsList
+
+    scf.addFlag("Sim.PhysicsList", _checkPhysicsListConf)
     scf.addFlag("Sim.NeutronTimeCut", 150.) # Sets the value for the neutron out of time cut in G4
     scf.addFlag("Sim.NeutronEnergyCut", -1.) # Sets the value for the neutron energy cut in G4
     scf.addFlag("Sim.ApplyEMCuts", False) # Turns on the G4 option to apply cuts for EM physics
diff --git a/Tools/PyUtils/python/MetaReader.py b/Tools/PyUtils/python/MetaReader.py
index 6babbca967cc..8763380443a4 100644
--- a/Tools/PyUtils/python/MetaReader.py
+++ b/Tools/PyUtils/python/MetaReader.py
@@ -931,6 +931,7 @@ def make_peeker(meta_dict):
                 'TRTRangeCut',
                 'SimulationFlavour',
                 'Simulator',
+                'PhysicsList',
             ]
             for item in list(meta_dict[filename]['/Simulation/Parameters']):
                 if item not in keys_to_keep:
@@ -941,6 +942,8 @@ def make_peeker(meta_dict):
                 'numberOfCollisions',
                 'intraTrainBunchSpacing',
                 'BeamIntensityPattern'
+                'physicsList',
+                'digiSteeringConf',
             ]
             for item in list(meta_dict[filename]['/Digitization/Parameters']):
                 if item not in keys_to_keep:
-- 
GitLab