From 4398fed3d40c7c17aee3c5d8f588ff8b286e8ae5 Mon Sep 17 00:00:00 2001
From: Baptiste Ravina <baptiste.ravina@cern.ch>
Date: Wed, 29 May 2024 14:44:26 +0200
Subject: [PATCH] CPAlgorithms: additional variables for photons

CPAlgorithms: additional variables for photons
---
 .../python/ConfigFactory.py                   |  3 ++
 .../Root/ComponentFactoryPreloader.cxx        |  2 +
 .../EgammaAnalysisAlgorithmsDict.h            |  1 +
 .../PhotonExtraVariablesAlg.h                 | 35 ++++++++++++++++
 .../EgammaAnalysisAlgorithms/selection.xml    |  1 +
 .../Root/PhotonExtraVariablesAlg.cxx          | 42 +++++++++++++++++++
 .../python/PhotonExtraVariablesConfig.py      | 21 ++++++++++
 .../EgammaAnalysisAlgorithms_entries.cxx      |  3 +-
 8 files changed, 107 insertions(+), 1 deletion(-)
 create mode 100644 PhysicsAnalysis/Algorithms/EgammaAnalysisAlgorithms/EgammaAnalysisAlgorithms/PhotonExtraVariablesAlg.h
 create mode 100644 PhysicsAnalysis/Algorithms/EgammaAnalysisAlgorithms/Root/PhotonExtraVariablesAlg.cxx
 create mode 100644 PhysicsAnalysis/Algorithms/EgammaAnalysisAlgorithms/python/PhotonExtraVariablesConfig.py

diff --git a/PhysicsAnalysis/Algorithms/AnalysisAlgorithmsConfig/python/ConfigFactory.py b/PhysicsAnalysis/Algorithms/AnalysisAlgorithmsConfig/python/ConfigFactory.py
index c598dc45b70c..c845985421fe 100644
--- a/PhysicsAnalysis/Algorithms/AnalysisAlgorithmsConfig/python/ConfigFactory.py
+++ b/PhysicsAnalysis/Algorithms/AnalysisAlgorithmsConfig/python/ConfigFactory.py
@@ -257,6 +257,9 @@ class ConfigFactory():
         from EgammaAnalysisAlgorithms.PhotonAnalysisConfig import PhotonWorkingPointConfig
         self.addAlgConfigBlock(algName="WorkingPoint", alg=PhotonWorkingPointConfig,
             superBlocks="Photons")
+        from EgammaAnalysisAlgorithms.PhotonExtraVariablesConfig import PhotonExtraVariablesBlock
+        self.addAlgConfigBlock(algName="ExtraVariables", alg=PhotonExtraVariablesBlock,
+            superBlocks="Photons")
 
         # muons
         from MuonAnalysisAlgorithms.MuonAnalysisConfig import MuonCalibrationConfig
diff --git a/PhysicsAnalysis/Algorithms/ComponentFactoryPreloader/Root/ComponentFactoryPreloader.cxx b/PhysicsAnalysis/Algorithms/ComponentFactoryPreloader/Root/ComponentFactoryPreloader.cxx
index 84e3ac428837..a528e16ef2d5 100644
--- a/PhysicsAnalysis/Algorithms/ComponentFactoryPreloader/Root/ComponentFactoryPreloader.cxx
+++ b/PhysicsAnalysis/Algorithms/ComponentFactoryPreloader/Root/ComponentFactoryPreloader.cxx
@@ -43,6 +43,7 @@
 #include <EgammaAnalysisAlgorithms/EgammaIsolationCorrectionAlg.h>
 #include <EgammaAnalysisAlgorithms/EgammaIsolationSelectionAlg.h>
 #include <EgammaAnalysisAlgorithms/PhotonOriginCorrectionAlg.h>
+#include <EgammaAnalysisAlgorithms/PhotonExtraVariablesAlg.h>
 #include <ElectronPhotonFourMomentumCorrection/EgammaCalibrationAndSmearingTool.h>
 #include <ElectronPhotonSelectorTools/AsgDeadHVCellRemovalTool.h>
 #include <EventSelectionAlgorithms/MissingETPlusTransverseMassSelectorAlg.h>
@@ -123,6 +124,7 @@ namespace CP
     ANA_CHECK (asg::registerAlgorithmFactory<CP::EgammaCalibrationAndSmearingAlg>("CP::EgammaCalibrationAndSmearingAlg"));
     ANA_CHECK (asg::registerAlgorithmFactory<CP::EgammaIsolationCorrectionAlg>("CP::EgammaIsolationCorrectionAlg"));
     ANA_CHECK (asg::registerAlgorithmFactory<CP::EgammaIsolationSelectionAlg>("CP::EgammaIsolationSelectionAlg"));
+    ANA_CHECK (asg::registerAlgorithmFactory<CP::PhotonExtraVariablesAlg>("CP::PhotonExtraVariablesAlg"));
     ANA_CHECK (asg::registerAlgorithmFactory<CP::EventFlagSelectionAlg>("CP::EventFlagSelectionAlg"));
     ANA_CHECK (asg::registerAlgorithmFactory<CP::EventStatusSelectionAlg>("CP::EventStatusSelectionAlg"));
     ANA_CHECK (asg::registerAlgorithmFactory<CP::JetCalibrationAlg>("CP::JetCalibrationAlg"));
diff --git a/PhysicsAnalysis/Algorithms/EgammaAnalysisAlgorithms/EgammaAnalysisAlgorithms/EgammaAnalysisAlgorithmsDict.h b/PhysicsAnalysis/Algorithms/EgammaAnalysisAlgorithms/EgammaAnalysisAlgorithms/EgammaAnalysisAlgorithmsDict.h
index c9d30fd3f4d9..d0c271e6c678 100644
--- a/PhysicsAnalysis/Algorithms/EgammaAnalysisAlgorithms/EgammaAnalysisAlgorithms/EgammaAnalysisAlgorithmsDict.h
+++ b/PhysicsAnalysis/Algorithms/EgammaAnalysisAlgorithms/EgammaAnalysisAlgorithms/EgammaAnalysisAlgorithmsDict.h
@@ -19,5 +19,6 @@
 #include <EgammaAnalysisAlgorithms/PhotonOriginCorrectionAlg.h>
 #include <EgammaAnalysisAlgorithms/EgammaFSRForMuonsCollectorAlg.h>
 #include <EgammaAnalysisAlgorithms/ElectronSiHitDecAlg.h>
+#include <EgammaAnalysisAlgorithms/PhotonExtraVariablesAlg.h>
 
 #endif
diff --git a/PhysicsAnalysis/Algorithms/EgammaAnalysisAlgorithms/EgammaAnalysisAlgorithms/PhotonExtraVariablesAlg.h b/PhysicsAnalysis/Algorithms/EgammaAnalysisAlgorithms/EgammaAnalysisAlgorithms/PhotonExtraVariablesAlg.h
new file mode 100644
index 000000000000..d2e612e7db48
--- /dev/null
+++ b/PhysicsAnalysis/Algorithms/EgammaAnalysisAlgorithms/EgammaAnalysisAlgorithms/PhotonExtraVariablesAlg.h
@@ -0,0 +1,35 @@
+/*
+  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
+*/
+
+/// @author Nils Abicht
+
+#ifndef PHOTON_EXTRAVARIABLES_ALG__H
+#define PHOTON_EXTRAVARIABLES_ALG__H
+
+#include <AnaAlgorithm/AnaAlgorithm.h>
+#include <SystematicsHandles/SysListHandle.h>
+#include <SystematicsHandles/SysReadHandle.h>
+#include <SystematicsHandles/SysWriteDecorHandle.h>
+
+#include <xAODEgamma/PhotonContainer.h>
+
+namespace CP {
+
+  class PhotonExtraVariablesAlg final : public EL::AnaAlgorithm {
+
+  public:
+    using EL::AnaAlgorithm::AnaAlgorithm;
+    virtual StatusCode initialize() override;
+    virtual StatusCode execute() override;
+
+  private:
+    CP::SysListHandle m_systematicsList {this};
+    CP::SysReadHandle<xAOD::PhotonContainer> m_photonsHandle { this, "photons", "", "the input photon container" };
+    CP::SysWriteDecorHandle<int> m_conversionTypeHandle { this, "conversionType", "conversionType_%SYS%", "decoration name for photon conversionType" };
+    CP::SysWriteDecorHandle<float> m_caloEta2Handle { this, "caloEta2", "caloEta2_%SYS%", "decoration name for photon caloEtaBE(2)" };
+  };
+
+} // namespace
+
+#endif
diff --git a/PhysicsAnalysis/Algorithms/EgammaAnalysisAlgorithms/EgammaAnalysisAlgorithms/selection.xml b/PhysicsAnalysis/Algorithms/EgammaAnalysisAlgorithms/EgammaAnalysisAlgorithms/selection.xml
index 03fcf69cc1d4..ad6db72bb3f1 100644
--- a/PhysicsAnalysis/Algorithms/EgammaAnalysisAlgorithms/EgammaAnalysisAlgorithms/selection.xml
+++ b/PhysicsAnalysis/Algorithms/EgammaAnalysisAlgorithms/EgammaAnalysisAlgorithms/selection.xml
@@ -11,5 +11,6 @@
    <class name="CP::PhotonOriginCorrectionAlg" />
    <class name="CP::EgammaFSRForMuonsCollectorAlg" />
    <class name="CP::ElectronSiHitDecAlg" />
+   <class name="CP::PhotonExtraVariablesAlg" />
    
 </lcgdict>
diff --git a/PhysicsAnalysis/Algorithms/EgammaAnalysisAlgorithms/Root/PhotonExtraVariablesAlg.cxx b/PhysicsAnalysis/Algorithms/EgammaAnalysisAlgorithms/Root/PhotonExtraVariablesAlg.cxx
new file mode 100644
index 000000000000..41b9f7672eea
--- /dev/null
+++ b/PhysicsAnalysis/Algorithms/EgammaAnalysisAlgorithms/Root/PhotonExtraVariablesAlg.cxx
@@ -0,0 +1,42 @@
+/*
+  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
+*/
+
+/// @author Nils Abicht
+
+#include <EgammaAnalysisAlgorithms/PhotonExtraVariablesAlg.h>
+
+namespace CP {
+
+  StatusCode PhotonExtraVariablesAlg::initialize() {
+
+    ANA_CHECK(m_photonsHandle.initialize(m_systematicsList));
+
+    ANA_CHECK(m_conversionTypeHandle.initialize(m_systematicsList, m_photonsHandle));
+    ANA_CHECK(m_caloEta2Handle.initialize(m_systematicsList, m_photonsHandle));
+
+    ANA_CHECK(m_systematicsList.initialize());
+
+    return StatusCode::SUCCESS;
+  }
+
+  StatusCode PhotonExtraVariablesAlg::execute() {
+
+    for (const auto &sys : m_systematicsList.systematicsVector()) {
+
+      const xAOD::PhotonContainer *photons = nullptr;
+      ANA_CHECK(m_photonsHandle.retrieve(photons, sys));
+
+      for (const xAOD::Photon *photon : *photons) {
+        int conversionType = photon->conversionType();
+        m_conversionTypeHandle.set(*photon, conversionType, sys);
+
+        float caloEta2 = photon->caloCluster()->etaBE(2);
+        m_caloEta2Handle.set(*photon, caloEta2, sys);
+      }
+    }
+
+    return StatusCode::SUCCESS;
+  }
+
+} // namespace
diff --git a/PhysicsAnalysis/Algorithms/EgammaAnalysisAlgorithms/python/PhotonExtraVariablesConfig.py b/PhysicsAnalysis/Algorithms/EgammaAnalysisAlgorithms/python/PhotonExtraVariablesConfig.py
new file mode 100644
index 000000000000..aab54b45ff85
--- /dev/null
+++ b/PhysicsAnalysis/Algorithms/EgammaAnalysisAlgorithms/python/PhotonExtraVariablesConfig.py
@@ -0,0 +1,21 @@
+# Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
+
+from AnalysisAlgorithmsConfig.ConfigBlock import ConfigBlock
+
+class PhotonExtraVariablesBlock(ConfigBlock):
+    """a ConfigBlock for additional photon output variables"""
+    """Decorates the output photons with the conversion type and calorimeter eta"""
+    """and writes them to the output. Useful e.g. for photon-fake studies."""
+ 
+    def __init__(self):
+        super(PhotonExtraVariablesBlock, self).__init__()
+        self.addOption('containerName', None, type=str, info='the input photon container')
+    
+    def makeAlgs(self, config):
+
+        alg = config.createAlgorithm('CP::PhotonExtraVariablesAlg', 'PhotonExtraVariables' + self.containerName)
+        alg.photons = config.readName(self.containerName)
+        alg.affectingSystematicsFilter = '.*'
+
+        config.addOutputVar(self.containerName, 'conversionType_%SYS%', 'conversionType', noSys=True)
+        config.addOutputVar(self.containerName, 'caloEta2_%SYS%', 'caloEta2', noSys=True)
diff --git a/PhysicsAnalysis/Algorithms/EgammaAnalysisAlgorithms/src/components/EgammaAnalysisAlgorithms_entries.cxx b/PhysicsAnalysis/Algorithms/EgammaAnalysisAlgorithms/src/components/EgammaAnalysisAlgorithms_entries.cxx
index 6f78d9d05c97..93935ff1c0f2 100644
--- a/PhysicsAnalysis/Algorithms/EgammaAnalysisAlgorithms/src/components/EgammaAnalysisAlgorithms_entries.cxx
+++ b/PhysicsAnalysis/Algorithms/EgammaAnalysisAlgorithms/src/components/EgammaAnalysisAlgorithms_entries.cxx
@@ -11,6 +11,7 @@
 #include <EgammaAnalysisAlgorithms/PhotonOriginCorrectionAlg.h>
 #include <EgammaAnalysisAlgorithms/EgammaFSRForMuonsCollectorAlg.h>
 #include <EgammaAnalysisAlgorithms/ElectronSiHitDecAlg.h>
+#include <EgammaAnalysisAlgorithms/PhotonExtraVariablesAlg.h>
 
 DECLARE_COMPONENT (CP::EgammaCalibrationAndSmearingAlg)
 DECLARE_COMPONENT (CP::EgammaIsGoodOQSelectionTool)
@@ -23,4 +24,4 @@ DECLARE_COMPONENT (CP::ElectronLRTMergingAlg)
 DECLARE_COMPONENT (CP::PhotonOriginCorrectionAlg)
 DECLARE_COMPONENT (CP::EgammaFSRForMuonsCollectorAlg)
 DECLARE_COMPONENT (CP::ElectronSiHitDecAlg)
-
+DECLARE_COMPONENT (CP::PhotonExtraVariablesAlg)
-- 
GitLab