From 33ba1a597fea524f00f9ceb89e03df1e39974c6e Mon Sep 17 00:00:00 2001
From: Will Leight <wleight@cern.ch>
Date: Wed, 21 Mar 2018 13:18:03 +0100
Subject: [PATCH] Migrate MuonTruthSummaryTool to use ReadHandles for data
 access

Additionally, added a configurable flag to control whether the tool uses NSW or CSC hits. Mentioning ATLASRECTS-4406 for bookkeeping purposes.


Former-commit-id: 697547f2ccbc8110ad41e7779a64c76d460ea6ff
---
 .../src/MuonTruthSummaryTool.cxx              | 51 ++++++++-----------
 .../MuonTruthTools/src/MuonTruthSummaryTool.h | 12 ++---
 2 files changed, 26 insertions(+), 37 deletions(-)

diff --git a/MuonSpectrometer/MuonReconstruction/MuonRecTools/MuonTruthTools/src/MuonTruthSummaryTool.cxx b/MuonSpectrometer/MuonReconstruction/MuonRecTools/MuonTruthTools/src/MuonTruthSummaryTool.cxx
index 8ad41171aeb..76bad77ea86 100644
--- a/MuonSpectrometer/MuonReconstruction/MuonRecTools/MuonTruthTools/src/MuonTruthSummaryTool.cxx
+++ b/MuonSpectrometer/MuonReconstruction/MuonRecTools/MuonTruthTools/src/MuonTruthSummaryTool.cxx
@@ -6,7 +6,6 @@
 #include "MuonSegment/MuonSegment.h"
 #include "TrkTrack/Track.h"
 #include "TrkMeasurementBase/MeasurementBase.h"
-#include "TrkTruthData/PRD_MultiTruthCollection.h"
 #include "HepMC/GenParticle.h"
 #include <iostream>
 #include "TTree.h"
@@ -29,16 +28,11 @@ namespace Muon {
     m_level(0)
   {
     declareInterface<IMuonTruthSummaryTool>(this);
-    declareProperty("CSC_TruthName",    m_CSC_TruthName = "CSC_TruthMap");
-    declareProperty("RPC_TruthName",    m_RPC_TruthName = "RPC_TruthMap");
-    declareProperty("TGC_TruthName",    m_TGC_TruthName = "TGC_TruthMap");
-    declareProperty("MDT_TruthName",    m_MDT_TruthName = "MDT_TruthMap");
-    declareProperty("MM_TruthName",     m_MM_TruthName  = "MM_TruthMap");
-    declareProperty("STGC_TruthName",   m_STGC_TruthName = "STGC_TruthMap");
     declareProperty("WriteNtuple",      m_writeTree);
     declareProperty("NtupleTreeName",   m_treeName = "MuonTruthSummaryTree");
     declareProperty("HistStream",       m_histStream = "Summary");
     declareProperty("SelectedPdgId",    m_selectedPdgId = 13, "Should be positive as absolute value is used" );
+    declareProperty("UseNSW",           m_useNSW=false);
   }
 
   StatusCode  MuonTruthSummaryTool::initialize()
@@ -93,6 +87,13 @@ namespace Muon {
       ATH_MSG_WARNING("SelectedPdgId should be positive, taking the absolute value");
       m_selectedPdgId = abs(m_selectedPdgId);
     }
+
+    if(m_useNSW){
+      m_TruthNames.emplace_back("MM_TruthMap");
+      m_TruthNames.emplace_back("STGC_TruthMap");
+    }
+    else m_TruthNames.emplace_back("CSC_TruthMap");
+    ATH_CHECK(m_TruthNames.initialize());
     return StatusCode::SUCCESS;
   }
 
@@ -113,35 +114,25 @@ namespace Muon {
   }
 
   void MuonTruthSummaryTool::init() const {
-    getTruth(m_CSC_TruthName);
-    getTruth(m_RPC_TruthName);
-    getTruth(m_TGC_TruthName);
-    getTruth(m_MDT_TruthName);
-    getTruth(m_MM_TruthName);
-    getTruth(m_STGC_TruthName);
+    getTruth();
     
     m_wasInit = true;
     ATH_MSG_DEBUG(" Total collected muon truth hits " << m_truthHits.size() ); 
   }
 
-  void MuonTruthSummaryTool::getTruth(std::string name ) const {
-    const PRD_MultiTruthCollection* col = 0;
-
-    if( !evtStore()->contains<PRD_MultiTruthCollection>(name) ) return; 
+  void MuonTruthSummaryTool::getTruth() const {
 
-    if( evtStore()->retrieve(col, name).isFailure() || !col ) {
-      ATH_MSG_WARNING(  "PRD_MultiTruthCollection " << name << " NOT found");
-      return;
-    }
-    ATH_MSG_DEBUG(  "PRD_MultiTruthCollection " << name << " found");
-    PRD_MultiTruthCollection::const_iterator it = col->begin();
-    PRD_MultiTruthCollection::const_iterator it_end = col->end();
-    for( ;it!=it_end;++it ){
-      const HepMcParticleLink& link = it->second;
-      if( link.cptr() && 
-          (abs(link.cptr()->pdg_id()) ==  m_selectedPdgId || abs(link.cptr()->pdg_id()) == 13 ) ) {
-        m_truthHits[it->first] = link.cptr()->barcode();
-        m_pdgIdLookupFromBarcode[link.cptr()->barcode()]=link.cptr()->pdg_id();
+    for(SG::ReadHandle<PRD_MultiTruthCollection>& col : m_TruthNames.makeHandles()){
+      ATH_MSG_DEBUG(  "PRD_MultiTruthCollection " << col.key() << " found");
+      PRD_MultiTruthCollection::const_iterator it = col->begin();
+      PRD_MultiTruthCollection::const_iterator it_end = col->end();
+      for( ;it!=it_end;++it ){
+	const HepMcParticleLink& link = it->second;
+	if( link.cptr() && 
+	    (abs(link.cptr()->pdg_id()) ==  m_selectedPdgId || abs(link.cptr()->pdg_id()) == 13 ) ) {
+	  m_truthHits[it->first] = link.cptr()->barcode();
+	  m_pdgIdLookupFromBarcode[link.cptr()->barcode()]=link.cptr()->pdg_id();
+	}
       }
     }
   }
diff --git a/MuonSpectrometer/MuonReconstruction/MuonRecTools/MuonTruthTools/src/MuonTruthSummaryTool.h b/MuonSpectrometer/MuonReconstruction/MuonRecTools/MuonTruthTools/src/MuonTruthSummaryTool.h
index d7f5b3143d7..2b3b4a689dc 100644
--- a/MuonSpectrometer/MuonReconstruction/MuonRecTools/MuonTruthTools/src/MuonTruthSummaryTool.h
+++ b/MuonSpectrometer/MuonReconstruction/MuonRecTools/MuonTruthTools/src/MuonTruthSummaryTool.h
@@ -11,7 +11,9 @@
 #include "MuonIdHelpers/MuonIdHelperTool.h"
 #include "MuonRecHelperTools/MuonEDMPrinterTool.h"
 #include "MuonRecHelperTools/MuonEDMHelperTool.h"
+#include "TrkTruthData/PRD_MultiTruthCollection.h"
 #include "GaudiKernel/IIncidentListener.h"
+#include "StoreGate/ReadHandleKeyArray.h"
 #include <string>
 #include <set>
 #include <map>
@@ -69,7 +71,7 @@ namespace Muon {
 
     /** add measurements */
     void add( const std::vector<const Trk::MeasurementBase*>& measurements, int level );
-    void getTruth(std::string name ) const;
+    void getTruth() const;
     std::string printSummary( const std::set<Identifier>& truth, const std::set<Identifier>& found );
 
     ToolHandle<MuonIdHelperTool>                m_idHelper;
@@ -77,13 +79,9 @@ namespace Muon {
     ToolHandle<MuonEDMPrinterTool>              m_printer;
     ServiceHandle< IIncidentSvc >               m_incidentSvc;
     mutable bool m_wasInit;
+    bool m_useNSW;
 
-    std::string m_CSC_TruthName;
-    std::string m_RPC_TruthName;
-    std::string m_TGC_TruthName;
-    std::string m_MDT_TruthName;
-    std::string m_MM_TruthName;
-    std::string m_STGC_TruthName;
+    SG::ReadHandleKeyArray<PRD_MultiTruthCollection> m_TruthNames{this,"TruthNames",{"RPC_TruthMap","TGC_TruthMap","MDT_TruthMap"},"truth names"};
 
     mutable std::map<int,int>                           m_pdgIdLookupFromBarcode;
     mutable std::map<Identifier,int>                    m_truthHits; // map containing truth hits associated with muons, stores barcode as second element
-- 
GitLab