From 66a26309700e9da98b066f1d89091fa96c7701cf Mon Sep 17 00:00:00 2001
From: Jean-Baptiste De Vivie De Regie <devivie@lpsc.in2p3.fr>
Date: Thu, 28 Oct 2021 11:34:51 +0200
Subject: [PATCH] Prepare egamma output and LRT

---
 .../python/EMGSFCaloExtensionBuilderConfig.py |  17 +-
 .../python/egammaBuilderConfig.py             |  11 +-
 .../egammaConfig/python/egammaConfigFlags.py  |  12 +-
 .../python/egammaLRTBuilderConfig.py          | 152 +++++++++++++++
 .../python/egammaWriteOutputConfig.py         | 176 ++++++++++++++++++
 .../python/egammaTrackToolsConfig.py          |  46 +++--
 6 files changed, 371 insertions(+), 43 deletions(-)
 create mode 100644 Reconstruction/egamma/egammaConfig/python/egammaLRTBuilderConfig.py
 create mode 100644 Reconstruction/egamma/egammaConfig/python/egammaWriteOutputConfig.py

diff --git a/Reconstruction/egamma/egammaAlgs/python/EMGSFCaloExtensionBuilderConfig.py b/Reconstruction/egamma/egammaAlgs/python/EMGSFCaloExtensionBuilderConfig.py
index 6a3e41868a51..cd0026c4faeb 100644
--- a/Reconstruction/egamma/egammaAlgs/python/EMGSFCaloExtensionBuilderConfig.py
+++ b/Reconstruction/egamma/egammaAlgs/python/EMGSFCaloExtensionBuilderConfig.py
@@ -3,7 +3,6 @@
 __doc__ = """ Instantiate the EMGSFCaloExtensionBuilder
 with default configuration """
 
-from TrackToCalo.TrackToCaloConfig import ParticleCaloExtensionToolCfg
 from AthenaConfiguration.ComponentAccumulator import ComponentAccumulator
 from AthenaConfiguration.ComponentFactory import CompFactory
 from AthenaCommon.Logging import logging
@@ -19,23 +18,15 @@ def EMGSFCaloExtensionBuilderCfg(
 
     acc = ComponentAccumulator()
 
-    # should use egammaCaloExtrapolator for the two below...
+    from egammaTrackTools.egammaTrackToolsConfig import (
+        EMParticleCaloExtensionToolCfg, EMLastCaloExtensionToolCfg)
     if "PerigeeCaloExtensionTool" not in kwargs:
-        perigeeCaloExtrapAcc = ParticleCaloExtensionToolCfg(
-            flags,
-            name="PerigeeCaloExtensionTool",
-            ParticleType="electron",
-            StartFromPerigee=True)
         kwargs["PerigeeCaloExtensionTool"] = acc.popToolsAndMerge(
-            perigeeCaloExtrapAcc)
+            EMParticleCaloExtensionToolCfg(flags))
 
     if "LastCaloExtensionTool" not in kwargs:
-        lastCaloExtrapAcc = ParticleCaloExtensionToolCfg(
-            flags,
-            name="LastCaloExtensionTool",
-            ParticleType="electron")
         kwargs["LastCaloExtensionTool"] = acc.popToolsAndMerge(
-            lastCaloExtrapAcc)
+            EMLastCaloExtensionToolCfg(flags))
 
     kwargs.setdefault(
         "GFFTrkPartContainerName",
diff --git a/Reconstruction/egamma/egammaConfig/python/egammaBuilderConfig.py b/Reconstruction/egamma/egammaConfig/python/egammaBuilderConfig.py
index 73976f6e986e..c83533ea7d7e 100644
--- a/Reconstruction/egamma/egammaConfig/python/egammaBuilderConfig.py
+++ b/Reconstruction/egamma/egammaConfig/python/egammaBuilderConfig.py
@@ -23,7 +23,6 @@ def EGammaReconstructionCfg(flags, name="EGammaReconstruction"):
 
     # Add e/gamma tracking algorithms
     if flags.Egamma.doGSF:
-
         from egammaAlgs.egammaSelectedTrackCopyConfig import (
             egammaSelectedTrackCopyCfg)
         acc.merge(egammaSelectedTrackCopyCfg(flags))
@@ -38,14 +37,12 @@ def EGammaReconstructionCfg(flags, name="EGammaReconstruction"):
 
     # Add e/gamma conversion finding
     if flags.Egamma.doConversionBuilding:
-
         from egammaAlgs.EMVertexBuilderConfig import (
             EMVertexBuilderCfg)
         acc.merge(EMVertexBuilderCfg(flags))
 
     # Add calo seeded central algorithms
     if flags.Egamma.doCaloSeeded:
-
         from egammaAlgs.egammaRecBuilderConfig import (
             egammaRecBuilderCfg)
         acc.merge(egammaRecBuilderCfg(flags))
@@ -65,7 +62,6 @@ def EGammaReconstructionCfg(flags, name="EGammaReconstruction"):
 
     # Add calo seeded forward algorithms
     if flags.Egamma.doForwardSeeded:
-
         from egammaAlgs.egammaForwardBuilderConfig import (
             egammaForwardBuilderCfg)
         acc.merge(egammaForwardBuilderCfg(flags))
@@ -76,7 +72,6 @@ def EGammaReconstructionCfg(flags, name="EGammaReconstruction"):
 
     # Add truth association
     if flags.Egamma.doTruthAssociation:
-
         from egammaAlgs.egammaTruthAssociationConfig import (
             egammaTruthAssociationCfg)
         acc.merge(egammaTruthAssociationCfg(flags))
@@ -84,11 +79,15 @@ def EGammaReconstructionCfg(flags, name="EGammaReconstruction"):
     # Add e/gamma track thinning
     # (although we call the Alg slimming)
     if flags.Egamma.doTrackThinning:
-
         from egammaAlgs.egammaTrackSlimmerConfig import (
             egammaTrackSlimmerCfg)
         acc.merge(egammaTrackSlimmerCfg(flags))
 
+    # Add e/gamma and related containers to the output stream
+    from egammaConfig.egammaWriteOutputConfig import (
+        egammaWriteOutputCfg)
+    acc.merge(egammaWriteOutputCfg(flags))
+
     mlog.info("EGamma reconstruction configured")
 
     return acc
diff --git a/Reconstruction/egamma/egammaConfig/python/egammaConfigFlags.py b/Reconstruction/egamma/egammaConfig/python/egammaConfigFlags.py
index 9edcf9accfa6..b529db8192a7 100644
--- a/Reconstruction/egamma/egammaConfig/python/egammaConfigFlags.py
+++ b/Reconstruction/egamma/egammaConfig/python/egammaConfigFlags.py
@@ -43,12 +43,7 @@ def createEgammaConfigFlags():
                  'v12phiflip_supercluster')
     egcf.addFlag("Egamma.Calib.MVAVersion", 'egammaMVACalib/offline/v7')
 
-    ##################################################
-    # The keys: should update to use file peeking info
-    # Also not sure what's the best way to do this, might want to revisit
-    # one idea is to make the keys have tuples with type, name, etc
-    ##################################################
-
+    # The output keys
     egcf.addFlag("Egamma.Keys.Input.CaloCells", 'AllCalo')
     egcf.addFlag("Egamma.Keys.Input.TopoClusters",
                  'CaloTopoClusters')  # input topoclusters
@@ -83,11 +78,6 @@ def createEgammaConfigFlags():
     egcf.addFlag("Egamma.Keys.Output.CaloClustersSuppESD", '')
     egcf.addFlag("Egamma.Keys.Output.CaloClustersSuppAOD", '')
 
-    egcf.addFlag("Egamma.Keys.Output.TopoSeededClusters",
-                 'egammaTopoSeededClusters')
-    egcf.addFlag("Egamma.Keys.Output.TopoSeededClustersSuppESD", '')
-    egcf.addFlag("Egamma.Keys.Output.TopoSeededClustersSuppAOD", '-CellLink')
-
     egcf.addFlag("Egamma.Keys.Output.Electrons", 'Electrons')
     egcf.addFlag("Egamma.Keys.Output.ElectronsSuppESD", '')
     egcf.addFlag("Egamma.Keys.Output.ElectronsSuppAOD",
diff --git a/Reconstruction/egamma/egammaConfig/python/egammaLRTBuilderConfig.py b/Reconstruction/egamma/egammaConfig/python/egammaLRTBuilderConfig.py
new file mode 100644
index 000000000000..bf2a51cd310a
--- /dev/null
+++ b/Reconstruction/egamma/egammaConfig/python/egammaLRTBuilderConfig.py
@@ -0,0 +1,152 @@
+# Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
+
+__doc__ = """
+          Instantiate the EGamma LRT reconstruction.
+          Note that
+          egammaTopoClusterCopier is scheduled in TrackRecoConfig
+          """
+
+from AthenaCommon.Logging import logging
+from AthenaConfiguration.ComponentAccumulator import ComponentAccumulator
+from egammaTrackTools.egammaTrackToolsConfig import (
+    EMExtrapolationToolsLRTCommonCacheCfg, EMExtrapolationToolsLRTCacheCfg)
+
+
+def EGammaLRTReconstructionCfg(flags, name="EGammaLRTReconstruction"):
+
+    mlog = logging.getLogger(name)
+    mlog.info('Starting EGamma LRT reconstruction configuration')
+
+    acc = ComponentAccumulator()
+
+    # if large radius tracking not enabled add nothing
+    if not flags.InDet.doR3LargeD0 or not flags.Egamma.enabled:
+        if not flags.InDet.doR3LargeD0 and flags.Egamma.enabled:
+            mlog.info('Large radius tracking not enabled. Do nothing')
+        return acc
+
+    # Add e/gamma tracking algorithms
+    if flags.Egamma.doGSF:
+
+        from egammaAlgs.egammaSelectedTrackCopyConfig import (
+            egammaSelectedTrackCopyCfg)
+        emextLRTCommonCache = acc.popToolsAndMerge(
+            EMExtrapolationToolsLRTCommonCacheCfg(flags))
+        acc.merge(egammaSelectedTrackCopyCfg(
+            flags,
+            name="egammaSelectedLRTTrackCopy",
+            TrackParticleContainerName="InDetLargeD0TrackParticles",
+            OutputTrkPartContainerName="LRTegammaSelectedTrackParticles",
+            ExtrapolationToolCommonCache=emextLRTCommonCache)
+        )
+
+        from egammaAlgs.EMBremCollectionBuilderConfig import (
+            EMBremCollectionBuilderCfg)
+        acc.merge(EMBremCollectionBuilderCfg(
+            flags,
+            name='EMLRTBremCollectionBuilder',
+            TrackParticleContainerName='InDetLargeD0TrackParticles',
+            SelectedTrackParticleContainerName='LRTegammaSelectedTrackParticles',
+            OutputTrkPartContainerName='LRT'+flags.Egamma.Keys.Output.GSFTrackParticles,
+            OutputTrackContainerName='LRT'+flags.Egamma.Keys.Output.GSFTracks)
+        )
+
+        from egammaAlgs.EMGSFCaloExtensionBuilderConfig import (
+            EMGSFCaloExtensionBuilderCfg)
+        acc.merge(EMGSFCaloExtensionBuilderCfg(
+            flags,
+            name='EMGSFLRTCaloExtensionBuilder',
+            GSFPerigeeCache='LRTGSFPerigeeCaloExtension',
+            GSFLastCache='LRTGSFLastCaloExtension',
+            GFFTrkPartContainerName='LRT'+flags.Egamma.Keys.Output.GSFTrackParticles)
+        )
+
+    # Add calo seeded central algorithms
+    if flags.Egamma.doCaloSeeded:
+
+        from egammaAlgs.egammaRecBuilderConfig import (
+            egammaRecBuilderCfg)
+        from egammaTools.EMTrackMatchBuilderConfig import (
+            EMTrackMatchBuilderCfg)
+        emextLRTCache = acc.popToolsAndMerge(
+            EMExtrapolationToolsLRTCacheCfg(flags))
+        lrtemtrackmatch = acc.popToolsAndMerge(EMTrackMatchBuilderCfg(
+            flags,
+            name='LRTEMTrackMatchBuilder',
+            TrackParticlesName='LRT'+flags.Egamma.Keys.Output.GSFTrackParticles,
+            ExtrapolationTool=emextLRTCache)
+        )
+        acc.merge(egammaRecBuilderCfg(
+            flags,
+            name='LRTegammaRecBuilder',
+            egammaRecContainer="LRT"+flags.Egamma.Keys.Internal.EgammaRecs,
+            TrackMatchBuilderTool=lrtemtrackmatch,
+            doConversions=False)
+        )
+
+        from egammaAlgs.egammaSuperClusterBuilderConfig import (
+            electronSuperClusterBuilderCfg)
+        acc.merge(electronSuperClusterBuilderCfg(
+            flags,
+            name='LRTelectronSuperClusterBuilder',
+            InputEgammaRecContainerName='LRT'+flags.Egamma.Keys.Internal.EgammaRecs,
+            SuperElectronRecCollectionName='LRT' +
+            flags.Egamma.Keys.Internal.ElectronSuperRecs,
+            SuperClusterCollectionName='LRTElectronSuperClusters',
+            TrackMatchBuilderTool=lrtemtrackmatch)
+        )
+
+        from egammaAlgs.topoEgammaBuilderConfig import (
+            topoEgammaBuilderCfg)
+        from egammaTools.EMClusterToolConfig import (
+            EMClusterToolCfg)
+        LRTEMClusterTool = acc.popToolsAndMerge(EMClusterToolCfg(
+            flags,
+            name='LRTEMClusterTool',
+            OutputClusterContainerName='LRT'+flags.Egamma.Keys.Output.CaloClusters)
+        )
+        acc.merge(topoEgammaBuilderCfg(
+            flags,
+            name='LRTtopoEgammaBuilder',
+            InputElectronRecCollectionName='LRT' +
+            flags.Egamma.Keys.Internal.ElectronSuperRecs,
+            ElectronOutputName='LRT'+flags.Egamma.Keys.Output.Electrons,
+            EMClusterTool=LRTEMClusterTool,
+            doPhotons=False)
+        )
+
+    # Add truth association
+    if flags.Egamma.doTruthAssociation:
+
+        from egammaAlgs.egammaTruthAssociationConfig import (
+            egammaTruthAssociationCfg)
+        acc.merge(egammaTruthAssociationCfg(
+            flags,
+            name='LRTegammaTruthAssociationAlg',
+            ElectronContainerName='LRT'+flags.Egamma.Keys.Output.Electrons,
+            EgammaTruthContainerName='LRT'+flags.Egamma.Keys.Output.TruthParticles,
+            MatchPhotons=False,
+            MatchForwardElectrons=False)
+        )
+
+    mlog.info("EGamma LRT reconstruction configured")
+
+    return acc
+
+
+if __name__ == "__main__":
+    from AthenaCommon.Configurable import Configurable
+    Configurable.configurableRun3Behavior = True
+    from AthenaConfiguration.AllConfigFlags import ConfigFlags as flags
+    from AthenaConfiguration.TestDefaults import defaultTestFiles
+    from AthenaConfiguration.MainServicesConfig import MainServicesCfg
+    flags.Input.Files = defaultTestFiles.RDO
+    flags.Output.doWriteAOD = True  # To test the AOD parts
+
+    acc = MainServicesCfg(flags)
+    acc.merge(EGammaLRTReconstructionCfg(flags))
+    acc.printConfig(withDetails=True,
+                    printDefaults=True)
+
+    with open("egammalrtbuilderconfig.pkl", "wb") as f:
+        acc.store(f)
diff --git a/Reconstruction/egamma/egammaConfig/python/egammaWriteOutputConfig.py b/Reconstruction/egamma/egammaConfig/python/egammaWriteOutputConfig.py
new file mode 100644
index 000000000000..97a1887c1531
--- /dev/null
+++ b/Reconstruction/egamma/egammaConfig/python/egammaWriteOutputConfig.py
@@ -0,0 +1,176 @@
+# Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
+
+__doc__ = "Prepare EGamma output list"
+
+from AthenaCommon.Logging import logging
+from AthenaConfiguration.ComponentAccumulator import ComponentAccumulator
+
+
+def egammaWriteOutputCfg(flags, name="EGOutputList"):
+
+    acc = ComponentAccumulator()
+
+    mlog = logging.getLogger(name)
+
+    # if not enabled add nothing
+    if not flags.Egamma.enabled:
+        return acc
+
+    outFlags = flags.Egamma.Keys.Output
+
+    toESD = []
+    toAOD = []
+
+    if flags.Egamma.doCaloSeeded:
+        toESD += [
+            f"xAOD::ElectronContainer#{outFlags.Electrons}",
+            f"xAOD::ElectronAuxContainer#{outFlags.Electrons}"
+            f"Aux.{outFlags.ElectronsSuppESD}"]
+        toESD += [
+            f"xAOD::PhotonContainer#{outFlags.Photons}",
+            f"xAOD::PhotonAuxContainer#{outFlags.Photons}"
+            f"Aux.{outFlags.PhotonsSuppESD}"]
+        toESD += [
+            f"xAOD::CaloClusterContainer#{outFlags.CaloClusters}",
+            f"xAOD::CaloClusterAuxContainer#{outFlags.CaloClusters}"
+            f"Aux.{outFlags.CaloClustersSuppESD}"]
+        toESD += [
+            f"xAOD::CaloClusterContainer#{outFlags.EgammaLargeClusters}",
+            f"xAOD::CaloClusterAuxContainer#{outFlags.EgammaLargeClusters}"
+            f"Aux.{outFlags.EgammaLargeClustersSuppESD}"]
+        toESD += [
+            f"CaloClusterCellLinkContainer#{outFlags.CaloClusters}"
+            "_links"]
+        toESD += [
+            f"CaloClusterCellLinkContainer#{outFlags.EgammaLargeClusters}"
+            "_links"]
+
+        toAOD += [
+            f"xAOD::ElectronContainer#{outFlags.Electrons}",
+            f"xAOD::ElectronAuxContainer#{outFlags.Electrons}"
+            f"Aux.{outFlags.ElectronsSuppAOD}"]
+        toAOD += [
+            f"xAOD::PhotonContainer#{outFlags.Photons}",
+            f"xAOD::PhotonAuxContainer#{outFlags.Photons}"
+            f"Aux.{outFlags.PhotonsSuppAOD}"]
+        toAOD += [
+            f"xAOD::CaloClusterContainer#{outFlags.CaloClusters}",
+            f"xAOD::CaloClusterAuxContainer#{outFlags.CaloClusters}"
+            f"Aux.{outFlags.CaloClustersSuppAOD}"]
+        toAOD += [
+            f"CaloClusterCellLinkContainer#{outFlags.CaloClusters}_links"]
+
+    if flags.Egamma.doForwardSeeded:
+        toESD += [
+            f"xAOD::ElectronContainer#{outFlags.ForwardElectrons}",
+            f"xAOD::ElectronAuxContainer#{outFlags.ForwardElectrons}"
+            f"Aux.{outFlags.ForwardElectronsSuppESD}"]
+        toESD += [
+            f"xAOD::CaloClusterContainer#{outFlags.ForwardClusters}",
+            f"xAOD::CaloClusterAuxContainer#{outFlags.ForwardClusters}"
+            f"Aux.{outFlags.ForwardClustersSuppESD}"]
+        toESD += [
+            f"xAOD::CaloClusterContainer#{outFlags.EgammaLargeFWDClusters}",
+            f"xAOD::CaloClusterAuxContainer#{outFlags.EgammaLargeFWDClusters}"
+            f"Aux.{outFlags.EgammaLargeFWDClustersSuppESD}"]
+        toESD += [
+            f"CaloClusterCellLinkContainer#{outFlags.ForwardClusters}"
+            "_links"]
+        toESD += [
+            f"CaloClusterCellLinkContainer#{outFlags.EgammaLargeFWDClusters}"
+            "_links"]
+
+        toAOD += [
+            f"xAOD::ElectronContainer#{outFlags.ForwardElectrons}",
+            f"xAOD::ElectronAuxContainer#{outFlags.ForwardElectrons}"
+            f"Aux.{outFlags.ForwardElectronsSuppAOD}"]
+        toAOD += [
+            f"xAOD::CaloClusterContainer#{outFlags.ForwardClusters}",
+            f"xAOD::CaloClusterAuxContainer#{outFlags.ForwardClusters}"
+            f"Aux.{outFlags.ForwardClustersSuppAOD}"]
+        toAOD += [
+            f"CaloClusterCellLinkContainer#{outFlags.ForwardClusters}"
+            "_links"]
+
+    if flags.Egamma.doGSF:
+        toESD += [
+            f"xAOD::TrackParticleContainer#{outFlags.GSFTrackParticles}",
+            f"xAOD::TrackParticleAuxContainer#{outFlags.GSFTrackParticles}"
+            f"Aux.{outFlags.GSFTrackParticlesSuppESD}"]
+        toESD += [
+            f"TrackCollection#{outFlags.GSFTracks}"]
+
+        toAOD += [
+            f"xAOD::TrackParticleContainer#{outFlags.GSFTrackParticles}",
+            f"xAOD::TrackParticleAuxContainer#{outFlags.GSFTrackParticles}"
+            f"Aux.{outFlags.GSFTrackParticlesSuppAOD}"]
+
+    if flags.Egamma.doConversionBuilding:
+        toESD += [
+            f"xAOD::VertexContainer#{outFlags.ConversionVertices}",
+            f"xAOD::VertexAuxContainer#{outFlags.ConversionVertices}"
+            f"Aux.{outFlags.ConversionVerticesSuppESD}"]
+
+        toAOD += [
+            f"xAOD::VertexContainer#{outFlags.ConversionVertices}",
+            f"xAOD::VertexAuxContainer#{outFlags.ConversionVertices}"
+            f"Aux.{outFlags.ConversionVerticesSuppAOD}"]
+
+    if flags.Egamma.doTruthAssociation:
+        toESD += [
+            f"TruthParticleContainer#{outFlags.TruthParticles}",
+            f"TruthParticleAuxContainer#{outFlags.TruthParticles}"
+            f"Aux.{outFlags.TruthParticlesSuppESD}"]
+
+        toAOD += [f"TruthParticleContainer#{outFlags.TruthParticles}",
+                  f"TruthParticleAuxContainer#{outFlags.TruthParticles}"
+                  f"Aux.{outFlags.TruthParticlesSuppAOD}"]
+
+    if flags.InDet.doR3LargeD0:
+        toESD += [
+            f"xAOD::ElectronContainer#LRT{outFlags.Electrons}",
+            f"xAOD::ElectronAuxContainer#LRT{outFlags.Electrons}"
+            f"Aux.{outFlags.ElectronsSuppESD}"]
+        toESD += [
+            f"xAOD::CaloClusterContainer#LRT{outFlags.CaloClusters}",
+            f"xAOD::CaloClusterAuxContainer#LRT{outFlags.CaloClusters}"
+            f"Aux.{outFlags.CaloClustersSuppESD}"]
+        toESD += [
+            f"xAOD::CaloClusterContainer#LRT{outFlags.EgammaLargeClusters}",
+            f"xAOD::CaloClusterAuxContainer#LRT{outFlags.EgammaLargeClusters}"
+            f"Aux.{outFlags.EgammaLargeClustersSuppESD}"]
+        toESD += [
+            f"CaloClusterCellLinkContainer#LRT{outFlags.CaloClusters}"
+            "_links"]
+        toESD += [
+            f"CaloClusterCellLinkContainer#LRT{outFlags.EgammaLargeClusters}"
+            "_links"]
+        toESD += [
+            f"xAOD::TrackParticleContainer#LRT{outFlags.GSFTrackParticles}",
+            f"xAOD::TrackParticleAuxContainer#LRT{outFlags.GSFTrackParticles}"
+            f"Aux.{outFlags.GSFTrackParticlesSuppESD}"]
+
+        toAOD += [
+            f"xAOD::ElectronContainer#LRT{outFlags.Electrons}",
+            f"xAOD::ElectronAuxContainer#LRT{outFlags.Electrons}"
+            f"Aux.{outFlags.ElectronsSuppAOD}"]
+        toAOD += [
+            f"xAOD::CaloClusterContainer#LRT{outFlags.CaloClusters}",
+            f"xAOD::CaloClusterAuxContainer#LRT{outFlags.CaloClusters}"
+            f"Aux.{outFlags.CaloClustersSuppAOD}"]
+        toAOD += [
+            f"CaloClusterCellLinkContainer#LRT{outFlags.CaloClusters}"
+            "_links"]
+        toAOD += [
+            f"xAOD::TrackParticleContainer#LRT{outFlags.GSFTrackParticles}",
+            f"xAOD::TrackParticleAuxContainer#LRT{outFlags.GSFTrackParticles}"
+            f"Aux.{outFlags.GSFTrackParticlesSuppAOD}"]
+
+    mlog.info('egammaESDList: %s \n', toESD)
+    mlog.info('egammaAODList: %s \n', toAOD)
+
+    from OutputStreamAthenaPool.OutputStreamConfig import (addToAOD, addToESD)
+    acc.merge(addToESD(flags, toESD))
+    acc.merge(addToAOD(flags, toAOD))
+
+    return acc
diff --git a/Reconstruction/egamma/egammaTrackTools/python/egammaTrackToolsConfig.py b/Reconstruction/egamma/egammaTrackTools/python/egammaTrackToolsConfig.py
index 7102be2f3d20..deee9c8631e9 100644
--- a/Reconstruction/egamma/egammaTrackTools/python/egammaTrackToolsConfig.py
+++ b/Reconstruction/egamma/egammaTrackTools/python/egammaTrackToolsConfig.py
@@ -11,6 +11,22 @@ from TrkConfig.AtlasExtrapolatorConfig import (
 from TrackToCalo.TrackToCaloConfig import ParticleCaloExtensionToolCfg
 
 
+def EMLastCaloExtensionToolCfg(flags, **kwargs):
+    kwargs.setdefault("name", "EMLastCaloExtensionTool")
+    kwargs.setdefault("ParticleType", "electron")
+    if "Extrapolator" not in kwargs:
+        acc = ComponentAccumulator()
+        extrapAcc = egammaCaloExtrapolatorCfg(flags)
+        kwargs["Extrapolator"] = acc.popToolsAndMerge(extrapAcc)
+    return ParticleCaloExtensionToolCfg(flags, **kwargs)
+
+
+def EMParticleCaloExtensionToolCfg(flags, **kwargs):
+    kwargs.setdefault("name", "EMParticleCaloExtensionTool")
+    kwargs.setdefault("StartFromPerigee", True)
+    return EMLastCaloExtensionToolCfg(flags, **kwargs)
+
+
 def EMExtrapolationToolsCfg(flags, **kwargs):
 
     mlog = logging.getLogger('EMExtrapolationTools')
@@ -24,23 +40,12 @@ def EMExtrapolationToolsCfg(flags, **kwargs):
         kwargs["Extrapolator"] = acc.popToolsAndMerge(extrapAcc)
 
     if "PerigeeCaloExtensionTool" not in kwargs:
-        perigeeCaloExtrapAcc = ParticleCaloExtensionToolCfg(
-            flags,
-            name="EMParticleCaloExtensionTool",
-            Extrapolator=kwargs["Extrapolator"],
-            ParticleType="electron",
-            StartFromPerigee=True)
         kwargs["PerigeeCaloExtensionTool"] = acc.popToolsAndMerge(
-            perigeeCaloExtrapAcc)
+            EMParticleCaloExtensionToolCfg(flags))
 
     if "LastCaloExtensionTool" not in kwargs:
-        lastCaloExtrapAcc = ParticleCaloExtensionToolCfg(
-            flags,
-            name="EMLastCaloExtensionTool",
-            ParticleType="electron",
-            Extrapolator=kwargs["Extrapolator"])
         kwargs["LastCaloExtensionTool"] = acc.popToolsAndMerge(
-            lastCaloExtrapAcc)
+            EMLastCaloExtensionToolCfg(flags))
 
     emExtrapolationTools = EMExtrapolationTools(**kwargs)
     acc.setPrivateTools(emExtrapolationTools)
@@ -64,6 +69,21 @@ def EMExtrapolationToolsCommonCacheCfg(flags, **kwargs):
     return EMExtrapolationToolsCfg(flags, **kwargs)
 
 
+def EMExtrapolationToolsLRTCacheCfg(flags, **kwargs):
+    kwargs.setdefault("name", "EMExtrapolationToolsLRTCache")
+    kwargs.setdefault("PerigeeCache", "LRTGSFPerigeeCaloExtension")
+    kwargs.setdefault("LastCache", "LRTGSFLastCaloExtension")
+    return EMExtrapolationToolsCacheCfg(flags, **kwargs)
+
+
+def EMExtrapolationToolsLRTCommonCacheCfg(flags, **kwargs):
+    kwargs.setdefault("name", "EMExtrapolationToolsLRTCommonCache")
+    kwargs.setdefault("LastCache", "ParticleCaloExtension_LRT")
+    kwargs.setdefault("useCaching", False)
+    kwargs.setdefault("useLastCaching", True)
+    return EMExtrapolationToolsCfg(flags, **kwargs)
+
+
 def egammaTrkRefitterToolCfg(flags,
                              name='GSFRefitterTool',
                              **kwargs):
-- 
GitLab