From 5cb48d35e51b9e6859d706e77633e4128a23c359 Mon Sep 17 00:00:00 2001
From: Emily Anne Thompson <emily.anne.thompson@cern.ch>
Date: Tue, 1 Dec 2020 11:03:19 +0100
Subject: [PATCH] Migrate RpvElectronD0Tool to use DataHandles

---
 .../RpvElectronD0Tool.h                       |  4 +++-
 .../src/RpvElectronD0Tool.cxx                 | 24 ++++++-------------
 2 files changed, 10 insertions(+), 18 deletions(-)

diff --git a/PhysicsAnalysis/SUSYPhys/LongLivedParticleDPDMaker/LongLivedParticleDPDMaker/RpvElectronD0Tool.h b/PhysicsAnalysis/SUSYPhys/LongLivedParticleDPDMaker/LongLivedParticleDPDMaker/RpvElectronD0Tool.h
index c9706099ca91..1f4e8d0e3d9b 100644
--- a/PhysicsAnalysis/SUSYPhys/LongLivedParticleDPDMaker/LongLivedParticleDPDMaker/RpvElectronD0Tool.h
+++ b/PhysicsAnalysis/SUSYPhys/LongLivedParticleDPDMaker/LongLivedParticleDPDMaker/RpvElectronD0Tool.h
@@ -16,6 +16,8 @@
 
 // DerivationFramework includes
 #include "DerivationFrameworkInterfaces/IAugmentationTool.h"
+#include "xAODEgamma/ElectronContainer.h"
+#include "StoreGate/ReadHandleKey.h"
 
 namespace DerivationFramework {
 
@@ -43,7 +45,7 @@ namespace DerivationFramework {
     virtual StatusCode addBranches() const;
 
   private:
-    std::string m_collName;
+    SG::ReadHandleKey<xAOD::ElectronContainer> m_collName { this, "ElectronContainerKey", "Electrons", ""};
     std::string m_sgPrefix;
 
   }; 
diff --git a/PhysicsAnalysis/SUSYPhys/LongLivedParticleDPDMaker/src/RpvElectronD0Tool.cxx b/PhysicsAnalysis/SUSYPhys/LongLivedParticleDPDMaker/src/RpvElectronD0Tool.cxx
index 6f0fac01a517..9d39324afe55 100644
--- a/PhysicsAnalysis/SUSYPhys/LongLivedParticleDPDMaker/src/RpvElectronD0Tool.cxx
+++ b/PhysicsAnalysis/SUSYPhys/LongLivedParticleDPDMaker/src/RpvElectronD0Tool.cxx
@@ -10,7 +10,6 @@
 // Writes result to SG for later selection by string parser
 
 #include "LongLivedParticleDPDMaker/RpvElectronD0Tool.h"
-#include "xAODEgamma/ElectronContainer.h"
 #include <vector>
 #include <string>
 
@@ -19,11 +18,9 @@ DerivationFramework::RpvElectronD0Tool::RpvElectronD0Tool( const std::string& t,
 							   const std::string& n,
 							   const IInterface* p ) :
   AthAlgTool(t,n,p),
-  m_collName("Electrons"),
   m_sgPrefix("")
   {
     declareInterface<DerivationFramework::IAugmentationTool>(this);
-    declareProperty("CollectionName", m_collName);
     declareProperty("SGPrefix", m_sgPrefix);
   }
  
@@ -35,6 +32,7 @@ DerivationFramework::RpvElectronD0Tool::~RpvElectronD0Tool() {
 StatusCode DerivationFramework::RpvElectronD0Tool::initialize()
 {
      ATH_MSG_VERBOSE("initialize() ...");
+     ATH_CHECK(m_collName.initialize());
      return StatusCode::SUCCESS;
 }
 StatusCode DerivationFramework::RpvElectronD0Tool::finalize()
@@ -48,14 +46,16 @@ StatusCode DerivationFramework::RpvElectronD0Tool::addBranches() const
 {
 
      // Retrieve data
-     const xAOD::ElectronContainer* electrons =  evtStore()->retrieve< const xAOD::ElectronContainer >( m_collName );
-     if( ! electrons ) {
+     SG::ReadHandle<xAOD::ElectronContainer> electrons(m_collName);
+     if( !electrons.isValid() ) {
 	ATH_MSG_ERROR("Couldn't retrieve e-gamma container with key: " << m_collName);
 	return StatusCode::FAILURE;
      }
 	
-     // Make a vector for the cut results
-     std::vector<float>* d0vec = new std::vector<float>();
+     // Write decision to SG for access by downstream algs 
+     std::string sgKey(m_sgPrefix+"D0");
+     SG::WriteHandle< std::vector<float> > d0vec(sgKey);
+     ATH_CHECK(d0vec.record(std::make_unique< std::vector<float> >()));
 
      // Loop over electrons, set decisions
      for (xAOD::ElectronContainer::const_iterator eIt = electrons->begin(); eIt!=electrons->end(); ++eIt) {
@@ -67,16 +67,6 @@ StatusCode DerivationFramework::RpvElectronD0Tool::addBranches() const
        d0vec->push_back(d0);
      
      }     
-
-     // Write decision to SG for access by downstream algs 
-     std::string sgKey(m_sgPrefix+"D0");
-
-     if (evtStore()->contains<std::vector<float> >(sgKey)) {
-       ATH_MSG_ERROR("Tool is attempting to write a StoreGate key " << sgKey << " which already exists. Please use a different key");
-       delete d0vec; // avoid mem leak
-       return StatusCode::FAILURE;
-     }
-     CHECK(evtStore()->record(d0vec, sgKey));       
      
      return StatusCode::SUCCESS;
 
-- 
GitLab