From 30bceda6b09811a338acb9e61021a81983e254bc Mon Sep 17 00:00:00 2001
From: Tadej Novak <tadej.novak@cern.ch>
Date: Sun, 10 Jul 2022 15:02:06 +0200
Subject: [PATCH] Merge branch 'CPAlgsFixRequiredCopies' into 'master'

Add a const getCopy method to the SysCopyHandle and use it in the AsgSelectionAlg

See merge request atlas/athena!54929

(cherry picked from commit 00c75befd85fc2dc4ba3d2bafc7ff67865533c65)

7a033d23 Add a const getCopy method to the SysCopyHandle and use it in the AsgSelectionAlg
---
 .../Root/AsgSelectionAlg.cxx                  |  4 +-
 .../SystematicsHandles/SysCopyHandle.h        |  6 +++
 .../SystematicsHandles/SysCopyHandle.icc      | 37 +++++++++++++++++++
 3 files changed, 45 insertions(+), 2 deletions(-)

diff --git a/PhysicsAnalysis/Algorithms/AsgAnalysisAlgorithms/Root/AsgSelectionAlg.cxx b/PhysicsAnalysis/Algorithms/AsgAnalysisAlgorithms/Root/AsgSelectionAlg.cxx
index 7e6dc7abd966..b169e0218ff9 100644
--- a/PhysicsAnalysis/Algorithms/AsgAnalysisAlgorithms/Root/AsgSelectionAlg.cxx
+++ b/PhysicsAnalysis/Algorithms/AsgAnalysisAlgorithms/Root/AsgSelectionAlg.cxx
@@ -63,9 +63,9 @@ namespace CP
       if (m_systematicsTool)
         ANA_CHECK (m_systematicsTool->applySystematicVariation (sys));
 
-      xAOD::IParticleContainer *particles = nullptr;
+      const xAOD::IParticleContainer *particles = nullptr;
       ANA_CHECK (m_particlesHandle.getCopy (particles, sys));
-      for (xAOD::IParticle *particle : *particles)
+      for (const xAOD::IParticle *particle : *particles)
       {
         if (m_preselection.getBool (*particle, sys))
         {
diff --git a/PhysicsAnalysis/Algorithms/SystematicsHandles/SystematicsHandles/SysCopyHandle.h b/PhysicsAnalysis/Algorithms/SystematicsHandles/SystematicsHandles/SysCopyHandle.h
index 6d04d24bcbca..f480fcfed01a 100644
--- a/PhysicsAnalysis/Algorithms/SystematicsHandles/SystematicsHandles/SysCopyHandle.h
+++ b/PhysicsAnalysis/Algorithms/SystematicsHandles/SystematicsHandles/SysCopyHandle.h
@@ -91,6 +91,12 @@ namespace CP
                           const CP::SystematicSet& sys) const;
 
 
+    /// \brief const retrieve the object for the given name
+  public:
+    ::StatusCode getCopy (const T*& object,
+                          const CP::SystematicSet& sys) const;
+
+
 
     //
     // inherited interface
diff --git a/PhysicsAnalysis/Algorithms/SystematicsHandles/SystematicsHandles/SysCopyHandle.icc b/PhysicsAnalysis/Algorithms/SystematicsHandles/SystematicsHandles/SysCopyHandle.icc
index 42ea9f3df09f..7c676a918276 100644
--- a/PhysicsAnalysis/Algorithms/SystematicsHandles/SystematicsHandles/SysCopyHandle.icc
+++ b/PhysicsAnalysis/Algorithms/SystematicsHandles/SystematicsHandles/SysCopyHandle.icc
@@ -117,6 +117,43 @@ namespace CP
     }
   }
 
+  template<typename T> ::StatusCode SysCopyHandle<T> ::
+  getCopy (const T*& object, const CP::SystematicSet& sys) const
+  {
+    auto cache = m_nameCache.find (sys);
+    if (cache == m_nameCache.end())
+    {
+      if (m_nameCache.empty())
+        throw std::logic_error ("uninitialized SysCopyHandle (" + m_inputName + ")");
+      else
+        throw std::logic_error ("unsupported systematic in SysCopyHandle (" + m_inputName + "): (" + sys.name() + ")");
+    }
+    assert (m_evtStore);
+    if (std::get<1>(cache->second).empty())
+    {
+      // if no output name is configured, act like an update handle
+      return m_evtStore->retrieve (object, std::get<0>(cache->second));
+    } else
+    {
+      // if an output name is configured, retrieve the input object as
+      // a const object, (shallow) copy it, record the copy and return
+      // it.
+
+      const T *inputObject = nullptr;
+      if (m_evtStore->retrieve (inputObject, std::get<0>(cache->second)).isFailure())
+        return StatusCode::FAILURE;
+
+      T *tmpObject = nullptr;
+
+      if (detail::ShallowCopy<T>::getCopy
+        (msg(), *m_evtStore, tmpObject, inputObject,
+         std::get<1>(cache->second), std::get<2>(cache->second)).isFailure())
+        return StatusCode::FAILURE;
+      object = tmpObject;
+      return StatusCode::SUCCESS;
+    }
+  }
+
 
 
   template<typename T> CP::SystematicSet SysCopyHandle<T> ::
-- 
GitLab