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

---
 .../LongLivedParticleDPDMaker/RpvMuonD0Tool.h |  4 +-
 .../src/RpvMuonD0Tool.cxx                     | 40 ++++++-------------
 2 files changed, 15 insertions(+), 29 deletions(-)

diff --git a/PhysicsAnalysis/SUSYPhys/LongLivedParticleDPDMaker/LongLivedParticleDPDMaker/RpvMuonD0Tool.h b/PhysicsAnalysis/SUSYPhys/LongLivedParticleDPDMaker/LongLivedParticleDPDMaker/RpvMuonD0Tool.h
index acd092f8e57a..0acd94b4e038 100644
--- a/PhysicsAnalysis/SUSYPhys/LongLivedParticleDPDMaker/LongLivedParticleDPDMaker/RpvMuonD0Tool.h
+++ b/PhysicsAnalysis/SUSYPhys/LongLivedParticleDPDMaker/LongLivedParticleDPDMaker/RpvMuonD0Tool.h
@@ -16,6 +16,8 @@
 
 // DerivationFramework includes
 #include "DerivationFrameworkInterfaces/IAugmentationTool.h"
+#include "xAODMuon/MuonContainer.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::MuonContainer> m_collName { this, "MuonContainerKey", "Muons", ""};
     std::string m_sgPrefix;
 
   }; 
diff --git a/PhysicsAnalysis/SUSYPhys/LongLivedParticleDPDMaker/src/RpvMuonD0Tool.cxx b/PhysicsAnalysis/SUSYPhys/LongLivedParticleDPDMaker/src/RpvMuonD0Tool.cxx
index b3481ecc0392..cc984add262e 100644
--- a/PhysicsAnalysis/SUSYPhys/LongLivedParticleDPDMaker/src/RpvMuonD0Tool.cxx
+++ b/PhysicsAnalysis/SUSYPhys/LongLivedParticleDPDMaker/src/RpvMuonD0Tool.cxx
@@ -10,7 +10,6 @@
 // Writes result to SG for later selection by string parser
 
 #include "LongLivedParticleDPDMaker/RpvMuonD0Tool.h"
-#include "xAODMuon/MuonContainer.h"
 #include <vector>
 #include <string>
 
@@ -19,11 +18,9 @@ DerivationFramework::RpvMuonD0Tool::RpvMuonD0Tool( const std::string& t,
 						   const std::string& n,
 						   const IInterface* p ) :
   AthAlgTool(t,n,p),
-  m_collName("Muons"),
   m_sgPrefix("")
   {
     declareInterface<DerivationFramework::IAugmentationTool>(this);
-    declareProperty("CollectionName", m_collName);
     declareProperty("SGPrefix", m_sgPrefix);
   }
  
@@ -35,6 +32,7 @@ DerivationFramework::RpvMuonD0Tool::~RpvMuonD0Tool() {
 StatusCode DerivationFramework::RpvMuonD0Tool::initialize()
 {
      ATH_MSG_VERBOSE("initialize() ...");
+     ATH_CHECK(m_collName.initialize());
      return StatusCode::SUCCESS;
 }
 StatusCode DerivationFramework::RpvMuonD0Tool::finalize()
@@ -48,15 +46,22 @@ StatusCode DerivationFramework::RpvMuonD0Tool::addBranches() const
 {
 
      // Retrieve data
-     const xAOD::MuonContainer* muons =  evtStore()->retrieve< const xAOD::MuonContainer >( m_collName );
-     if( ! muons ) {
+     SG::ReadHandle<xAOD::MuonContainer> muons(m_collName);
+     if( !muons.isValid() ) {
 	ATH_MSG_ERROR("Couldn't retrieve muon container with key: " << m_collName);
 	return StatusCode::FAILURE;
      }
 	
      // Make a vector for the cut results
-     std::vector<float>* d0vec = new std::vector<float>();
-     std::vector<int>* isCombinedVec = new std::vector<int>();
+     // Write decision to SG for access by downstream algs 
+     std::string sgKeyd0(m_sgPrefix+"D0");
+     SG::WriteHandle< std::vector<float> > d0vec(sgKeyd0);
+     ATH_CHECK(d0vec.record(std::make_unique< std::vector<float> >()));
+
+     std::string sgKeycomb(m_sgPrefix+"isCombined");
+     SG::WriteHandle< std::vector<int> > isCombinedVec(sgKeycomb);
+     ATH_CHECK(isCombinedVec.record(std::make_unique< std::vector<int> >()));
+
 
      // Loop over muons, set decisions
      for (xAOD::MuonContainer::const_iterator muIt = muons->begin(); muIt!=muons->end(); ++muIt) {
@@ -86,27 +91,6 @@ StatusCode DerivationFramework::RpvMuonD0Tool::addBranches() const
        }     
      }
      
-     // 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");
-       // avoid mem leak
-       delete d0vec; 
-       delete isCombinedVec;
-       return StatusCode::FAILURE;
-     }
-     CHECK(evtStore()->record(d0vec, sgKey)); 
-
-     sgKey = m_sgPrefix+"isCombined";
-
-     if (evtStore()->contains<std::vector<int> >(sgKey)) {
-       ATH_MSG_ERROR("Tool is attempting to write a StoreGate key " << sgKey << " which already exists. Please use a different key");
-       delete isCombinedVec; // avoid mem leak
-       return StatusCode::FAILURE;
-     }
-     CHECK(evtStore()->record(isCombinedVec, sgKey));       
-     
      return StatusCode::SUCCESS;
 }
 
-- 
GitLab