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

---
 .../VHLowTrackJetFilterTool.h                 | 26 ++++++-----
 .../src/VHLowTrackJetFilterTool.cxx           | 45 ++++++++-----------
 2 files changed, 35 insertions(+), 36 deletions(-)

diff --git a/PhysicsAnalysis/SUSYPhys/LongLivedParticleDPDMaker/LongLivedParticleDPDMaker/VHLowTrackJetFilterTool.h b/PhysicsAnalysis/SUSYPhys/LongLivedParticleDPDMaker/LongLivedParticleDPDMaker/VHLowTrackJetFilterTool.h
index d18e447a54d8..1446d6bd8bf4 100644
--- a/PhysicsAnalysis/SUSYPhys/LongLivedParticleDPDMaker/LongLivedParticleDPDMaker/VHLowTrackJetFilterTool.h
+++ b/PhysicsAnalysis/SUSYPhys/LongLivedParticleDPDMaker/LongLivedParticleDPDMaker/VHLowTrackJetFilterTool.h
@@ -19,6 +19,10 @@
 #include "DerivationFrameworkInterfaces/ISkimmingTool.h"
 
 #include "MuonAnalysisInterfaces/IMuonSelectionTool.h"
+#include "xAODEgamma/ElectronContainer.h"
+#include "xAODJet/JetContainer.h"
+#include "xAODMuon/MuonContainer.h"
+#include "StoreGate/ReadHandleKey.h"
 
 
 namespace DerivationFramework {
@@ -33,21 +37,22 @@ namespace DerivationFramework {
     virtual ~VHLowTrackJetFilterTool() = default;
     
     // Athena algtool's Hooks
+    StatusCode  initialize();
     StatusCode  finalize() override;
     
     /** Check that the current event passes this filter */
     virtual bool eventPassesFilter() const override;
     
   private:
-    mutable unsigned int m_nEventsTotal;
-    mutable unsigned int m_nEventsPass;
-    mutable unsigned int m_nEventsPassJet;
-    mutable unsigned int m_nEventsPassElectron;
-    mutable unsigned int m_nEventsPassMuon;
-    mutable unsigned int m_nJetsPassAlphaMax;
-    mutable unsigned int m_nJetsPassCHF;
+    mutable std::atomic<unsigned int> m_nEventsTotal;
+    mutable std::atomic<unsigned int> m_nEventsPass;
+    mutable std::atomic<unsigned int> m_nEventsPassJet;
+    mutable std::atomic<unsigned int> m_nEventsPassElectron;
+    mutable std::atomic<unsigned int> m_nEventsPassMuon;
+    mutable std::atomic<unsigned int> m_nJetsPassAlphaMax;
+    mutable std::atomic<unsigned int> m_nJetsPassCHF;
     bool m_debug;
-    std::string m_jetSGKey;
+    SG::ReadHandleKey<xAOD::JetContainer> m_jetSGKey { this, "JetContainerKey", "AntiKt4EMTopoJets", ""};
     float m_jetPtCut;
     float m_jetEtaCut;
     float m_TrackMinPt;
@@ -56,13 +61,14 @@ namespace DerivationFramework {
     float m_AlphaMaxCut;
     float m_CHFCut;
     float m_nJetsReq;
-    std::string m_electronSGKey;
+    SG::ReadHandleKey<xAOD::ElectronContainer> m_electronSGKey { this, "ElectronContainerKey", "Electrons", ""};
     std::string m_electronIDKey;
     float m_electronPtCut;
     ToolHandle<CP::IMuonSelectionTool> m_muonSelectionTool;
-    std::string m_muonSGKey;
+    SG::ReadHandleKey<xAOD::MuonContainer> m_muonSGKey { this, "MuonContainerKey", "Muons", ""};
     std::string m_muonIDKey;
     float m_muonPtCut;
+
   }; 
   
 }
diff --git a/PhysicsAnalysis/SUSYPhys/LongLivedParticleDPDMaker/src/VHLowTrackJetFilterTool.cxx b/PhysicsAnalysis/SUSYPhys/LongLivedParticleDPDMaker/src/VHLowTrackJetFilterTool.cxx
index 2358a0586497..90a54388f2a2 100644
--- a/PhysicsAnalysis/SUSYPhys/LongLivedParticleDPDMaker/src/VHLowTrackJetFilterTool.cxx
+++ b/PhysicsAnalysis/SUSYPhys/LongLivedParticleDPDMaker/src/VHLowTrackJetFilterTool.cxx
@@ -7,14 +7,10 @@
 ///////////////////////////////////////////////////////////////////
 
 #include "LongLivedParticleDPDMaker/VHLowTrackJetFilterTool.h"
-#include "xAODEgamma/ElectronContainer.h"
 #include "xAODEventInfo/EventInfo.h"
-#include "xAODJet/JetContainer.h"
-#include "xAODMuon/MuonContainer.h"
 #include "xAODTracking/TrackParticleContainer.h"
 #include "xAODTracking/TrackingPrimitives.h"
 #include "xAODTracking/TrackParticlexAODHelpers.h"
-#include "xAODTracking/VertexContainer.h"
 #include "xAODBTagging/BTaggingContainer.h"
 #include "xAODBTagging/BTaggingUtilities.h"
 
@@ -32,7 +28,6 @@ m_nEventsPassMuon(0),
 m_nJetsPassAlphaMax(0),
 m_nJetsPassCHF(0),
 m_debug(true),
-m_jetSGKey("AntiKt4EMTopoJets"),
 m_jetPtCut(0),
 m_jetEtaCut(2.1),
 m_TrackMinPt(400.0),
@@ -41,11 +36,9 @@ m_TrackD0Max(0.5),
 m_AlphaMaxCut(0.05),
 m_CHFCut(0.045),
 m_nJetsReq(0),
-m_electronSGKey("Electrons"),
 m_electronIDKey("LHMedium"),
 m_electronPtCut(0),
 m_muonSelectionTool("CP::MuonSelectionTool/MuonSelectionTool"),
-m_muonSGKey("Muons"),
 m_muonIDKey("Medium"),
 m_muonPtCut(0)
 
@@ -54,7 +47,6 @@ m_muonPtCut(0)
   declareInterface<DerivationFramework::ISkimmingTool>(this);
   declareProperty("Debug", m_debug);
   
-  declareProperty("JetContainerKey", m_jetSGKey);
   declareProperty("JetPtCut", m_jetPtCut);
   declareProperty("JetEtaCut", m_jetEtaCut);
   declareProperty("TrackMinPt", m_TrackMinPt);
@@ -64,16 +56,23 @@ m_muonPtCut(0)
   declareProperty("JetCHFCut", m_CHFCut);
   declareProperty("NJetsRequired", m_nJetsReq);
   
-  declareProperty("ElectronContainerKey", m_electronSGKey);
   declareProperty("ElectronIDKey", m_electronIDKey);
   declareProperty("ElectronPtCut", m_electronPtCut);
   
-  declareProperty("MuonContainerKey", m_muonSGKey);
   declareProperty("MuonIDKey", m_muonIDKey);
   declareProperty("MuonPtCut", m_muonPtCut);
   
 }
 
+// Athena initialize
+StatusCode DerivationFramework::VHLowTrackJetFilterTool::initialize()
+{
+     ATH_MSG_VERBOSE("initialize() ...");
+     ATH_CHECK(m_electronSGKey.initialize());
+     ATH_CHECK(m_muonSGKey.initialize());
+     ATH_CHECK(m_jetSGKey.initialize());
+     return StatusCode::SUCCESS;
+}
 // Athena finalize
 StatusCode DerivationFramework::VHLowTrackJetFilterTool::finalize()
 {
@@ -99,16 +98,14 @@ bool DerivationFramework::VHLowTrackJetFilterTool::eventPassesFilter() const
   bool passesEl=false, passesMu=false, passesJet=false;
   m_nEventsTotal++;
   
-  const xAOD::EventInfo* eventInfo(0);
-  StatusCode sc = evtStore()->retrieve(eventInfo,"EventInfo");
-  if (sc.isFailure()) {
+  SG::ReadHandle<xAOD::EventInfo> eventInfo("EventInfo"); 
+  if( !eventInfo.isValid() ) {
     ATH_MSG_ERROR( "Could not retrieve event info" );
   }
   
   //Vertex Container
-  const xAOD::VertexContainer* vertices(0);
-  sc = evtStore()->retrieve(vertices, "PrimaryVertices");
-  if (sc.isFailure()) {
+  SG::ReadHandle<xAOD::VertexContainer> vertices("PrimaryVertices"); 
+  if( !eventInfo.isValid() ) {
     ATH_MSG_FATAL("No vertex collection with name PrimaryVertices found in StoreGate!");
     return false;
   }
@@ -117,9 +114,8 @@ bool DerivationFramework::VHLowTrackJetFilterTool::eventPassesFilter() const
 
 
   //electron portion
-  const xAOD::ElectronContainer* electrons(0);
-  sc = evtStore()->retrieve(electrons,m_electronSGKey);
-  if (sc.isFailure()) {
+  SG::ReadHandle<xAOD::ElectronContainer> electrons(m_electronSGKey); 
+  if( !electrons.isValid() ) {
     ATH_MSG_FATAL("No electron collection with name " << m_electronSGKey << " found in StoreGate!");
     return false;
   }
@@ -169,9 +165,8 @@ bool DerivationFramework::VHLowTrackJetFilterTool::eventPassesFilter() const
   
   //muon portion
 
-  const xAOD::MuonContainer* muons(0);
-  sc = evtStore()->retrieve(muons,m_muonSGKey);
-  if (sc.isFailure()) {
+  SG::ReadHandle<xAOD::MuonContainer> muons(m_muonSGKey); 
+  if( !muons.isValid() ) {
     ATH_MSG_FATAL("No muon collection with name " << m_muonSGKey << " found in StoreGate!");
     return false;
   }
@@ -215,9 +210,8 @@ bool DerivationFramework::VHLowTrackJetFilterTool::eventPassesFilter() const
   //Jet portion
   
   int nJetsPassed=0;
-  const xAOD::JetContainer* jets(0);
-  sc=evtStore()->retrieve(jets,m_jetSGKey);
-  if( sc.isFailure() || !jets ) {
+  SG::ReadHandle<xAOD::JetContainer> jets(m_jetSGKey); 
+  if( !jets.isValid() ) {
     ATH_MSG_WARNING("No Jet container found, will skip this event");
     return false;
   }
@@ -265,7 +259,6 @@ bool DerivationFramework::VHLowTrackJetFilterTool::eventPassesFilter() const
     
     TLorentzVector CHFNum = TLorentzVector(0.0,0.0,0.0,0.0);
     const xAOD::BTagging *bjet(nullptr);
-    //bjet = jet->btagging();
     bjet = xAOD::BTaggingUtilities::getBTagging( *jet );
     TrackLinks assocTracks = bjet->auxdata<TrackLinks>("BTagTrackToJetAssociator");
     
-- 
GitLab