From 2725eae6a9079c0f64888554ccd74d43835e14d0 Mon Sep 17 00:00:00 2001
From: Yun Sang Chow <yun.sang.chow@cern.ch>
Date: Wed, 2 Nov 2016 23:57:58 +0100
Subject: [PATCH] CaloMuonLikelihood histogram retrieval method change
 (CaloTrkMuIdTools-00-15-18)

2016-11-02 Yun Sang Chow
	* CaloMuonLikelihood histogram retrieval method change
	* CaloTrkMuIdTools-00-15-18

2016-10-18 Yun Sang Chow


Former-commit-id: 7850850cf3a1309ac5c47d6cc26be573613528e9
---
 .../CaloTrkMuIdTools/CaloMuonLikelihoodTool.h |  2 -
 .../share/CaloTrkMuIdTools_jobOptions.py      | 31 -----------
 .../src/CaloMuonLikelihoodTool.cxx            | 55 ++++++++++---------
 3 files changed, 28 insertions(+), 60 deletions(-)

diff --git a/Reconstruction/MuonIdentification/CaloTrkMuIdTools/CaloTrkMuIdTools/CaloMuonLikelihoodTool.h b/Reconstruction/MuonIdentification/CaloTrkMuIdTools/CaloTrkMuIdTools/CaloMuonLikelihoodTool.h
index 6da8c4dacca..14f2f186a56 100644
--- a/Reconstruction/MuonIdentification/CaloTrkMuIdTools/CaloTrkMuIdTools/CaloMuonLikelihoodTool.h
+++ b/Reconstruction/MuonIdentification/CaloTrkMuIdTools/CaloTrkMuIdTools/CaloMuonLikelihoodTool.h
@@ -9,7 +9,6 @@
 #include "ICaloTrkMuIdTools/ITrackEnergyInCaloTool.h"
 
 #include "AthenaBaseComps/AthAlgTool.h"
-#include "GaudiKernel/ITHistSvc.h"
 #include "GaudiKernel/ToolHandle.h"
 #include "GaudiKernel/ServiceHandle.h"
 #include "StoreGate/StoreGateSvc.h"
@@ -52,7 +51,6 @@ private:
   std::string               m_caloClusterContainerName;
 
   ToolHandle<ITrackEnergyInCaloTool>  m_trkEnergyInCalo;
-  ServiceHandle<ITHistSvc>            m_histSvc;
 };
 
 #endif
diff --git a/Reconstruction/MuonIdentification/CaloTrkMuIdTools/share/CaloTrkMuIdTools_jobOptions.py b/Reconstruction/MuonIdentification/CaloTrkMuIdTools/share/CaloTrkMuIdTools_jobOptions.py
index eba57a7e710..56cd2d207bc 100755
--- a/Reconstruction/MuonIdentification/CaloTrkMuIdTools/share/CaloTrkMuIdTools_jobOptions.py
+++ b/Reconstruction/MuonIdentification/CaloTrkMuIdTools/share/CaloTrkMuIdTools_jobOptions.py
@@ -13,20 +13,6 @@ from AthenaCommon.Logging import log
 # Finds the root file @rootFileName and creates a one-item list for THistSvc.Input
 ###############################################################################
 
-def defineCaloLhrPdf(rootFileName, streamName=""):
-   import os
-   from AthenaCommon.Utils.unixtools import FindFile
-   rootFile = FindFile(filename=rootFileName, pathlist=os.environ['DATAPATH'].split(os.pathsep), access=os.R_OK)
-   if streamName=="":
-       pos1 = rootFileName.find("CaloMuonLikelihood.PDF.")
-       pos2 = rootFileName.find(".root")
-       if pos1!=-1 and pos2!=-1:
-         streamName = rootFileName.replace(".", "_")
-         streamName = streamName[0:pos2]
-       else:
-           raise ValueError("in CaloTrkMuIdTools_jobOptions.py: defineCaloLhrPdf(...): rootFileName: " + rootFileName + " is not an expected value (should be CaloMuonLikelihood.PDF.xx.root).")
-   return [streamName + " DATAFILE='" + str(rootFile) + "' TYP='ROOT' OPT='READ'"]
- 
 ###############################################################################
 ## jobOptions
 ###############################################################################
@@ -59,23 +45,6 @@ else:
 ToolSvc += CaloTrkMuIdTools_Extrapolator
 #print      CaloTrkMuIdTools_Extrapolator
 
-from AthenaCommon.AppMgr import ServiceMgr
-if not hasattr(ServiceMgr, 'THistSvc'):
-   ServiceMgr += CfgMgr.THistSvc()
-
-from AthenaCommon.AthenaCommonFlags import athenaCommonFlags
-if athenaCommonFlags.isOnline == False:
-    log.error('THistSvc.Input = %s. Reading ntuples CaloTrkMuIdTools.' % ServiceMgr.THistSvc.Input)
-    ServiceMgr.THistSvc.Input += defineCaloLhrPdf("CaloMuonLikelihood.PDF.A0.root");            ### PDFs for barrel region low pT
-    ServiceMgr.THistSvc.Input += defineCaloLhrPdf("CaloMuonLikelihood.PDF.A1.root");            ### PDFs for barrel region medium pT
-    ServiceMgr.THistSvc.Input += defineCaloLhrPdf("CaloMuonLikelihood.PDF.A2.root");            ### PDFs for barrel region high pT
-    ServiceMgr.THistSvc.Input += defineCaloLhrPdf("CaloMuonLikelihood.PDF.B0.root");            ### PDFs for transition region low pT
-    ServiceMgr.THistSvc.Input += defineCaloLhrPdf("CaloMuonLikelihood.PDF.B1.root");            ### PDFs for transition region medium pT
-    ServiceMgr.THistSvc.Input += defineCaloLhrPdf("CaloMuonLikelihood.PDF.B2.root");            ### PDFs for transition region high pT
-    ServiceMgr.THistSvc.Input += defineCaloLhrPdf("CaloMuonLikelihood.PDF.C0.root");            ### PDFs for endcap region low pT
-    ServiceMgr.THistSvc.Input += defineCaloLhrPdf("CaloMuonLikelihood.PDF.C1.root");            ### PDFs for endcap region medium pT
-    ServiceMgr.THistSvc.Input += defineCaloLhrPdf("CaloMuonLikelihood.PDF.C2.root");            ### PDFs for endcap region high pT
-
 ### Configure TrackEnergyInCaloTool ###
 from CaloTrkMuIdTools.CaloTrkMuIdToolsConf import TrackEnergyInCaloTool as ConfiguredTrackEnergyInCaloTool
 TrackEnergyInCaloTool = ConfiguredTrackEnergyInCaloTool(name = "TrackEnergyInCaloTool", ExtrapolatorHandle = CaloTrkMuIdTools_Extrapolator)
diff --git a/Reconstruction/MuonIdentification/CaloTrkMuIdTools/src/CaloMuonLikelihoodTool.cxx b/Reconstruction/MuonIdentification/CaloTrkMuIdTools/src/CaloMuonLikelihoodTool.cxx
index d4bcde350ea..d9ad0f07866 100644
--- a/Reconstruction/MuonIdentification/CaloTrkMuIdTools/src/CaloMuonLikelihoodTool.cxx
+++ b/Reconstruction/MuonIdentification/CaloTrkMuIdTools/src/CaloMuonLikelihoodTool.cxx
@@ -35,23 +35,22 @@ namespace Units = Athena::Units;
 ///////////////////////////////////////////////////////////////////////////////
 CaloMuonLikelihoodTool::CaloMuonLikelihoodTool(const std::string& type, const std::string& name, const IInterface* parent) : 
   AthAlgTool(type,name,parent),
-  m_trkEnergyInCalo("TrackEnergyInCaloTool"),
-  m_histSvc("THistSvc/THistSvc", name)//,
+  m_trkEnergyInCalo("TrackEnergyInCaloTool")//,
   // m_clusCont(0)
 {
   declareInterface<ICaloMuonLikelihoodTool>(this);  
 
   m_fileNames.clear();
   
-  m_fileNames.push_back("/CaloMuonLikelihood_PDF_A0/");
-  m_fileNames.push_back("/CaloMuonLikelihood_PDF_A1/");
-  m_fileNames.push_back("/CaloMuonLikelihood_PDF_A2/");  
-  m_fileNames.push_back("/CaloMuonLikelihood_PDF_B0/");
-  m_fileNames.push_back("/CaloMuonLikelihood_PDF_B1/");
-  m_fileNames.push_back("/CaloMuonLikelihood_PDF_B2/");  
-  m_fileNames.push_back("/CaloMuonLikelihood_PDF_C0/");
-  m_fileNames.push_back("/CaloMuonLikelihood_PDF_C1/");
-  m_fileNames.push_back("/CaloMuonLikelihood_PDF_C2/");  
+  m_fileNames.push_back("CaloMuonLikelihood.PDF.A0.root");
+  m_fileNames.push_back("CaloMuonLikelihood.PDF.A1.root");
+  m_fileNames.push_back("CaloMuonLikelihood.PDF.A2.root");  
+  m_fileNames.push_back("CaloMuonLikelihood.PDF.B0.root");
+  m_fileNames.push_back("CaloMuonLikelihood.PDF.B1.root");
+  m_fileNames.push_back("CaloMuonLikelihood.PDF.B2.root");  
+  m_fileNames.push_back("CaloMuonLikelihood.PDF.C0.root");
+  m_fileNames.push_back("CaloMuonLikelihood.PDF.C1.root");
+  m_fileNames.push_back("CaloMuonLikelihood.PDF.C2.root");  
   
   declareProperty("RootFileNames", m_fileNames);
   declareProperty("TrackEnergyInCaloTool",m_trkEnergyInCalo);
@@ -81,10 +80,6 @@ StatusCode CaloMuonLikelihoodTool::initialize() {
     return StatusCode::FAILURE;
   }
   
-  if (m_histSvc.retrieve().isFailure()) {
-    ATH_MSG_FATAL("Could not retrieve THistSvc.");
-  }
-  
   if (retrieveHistograms().isFailure()) { 
     ATH_MSG_FATAL("Could not open PDF root fles");
     return StatusCode::FAILURE;
@@ -218,29 +213,35 @@ StatusCode CaloMuonLikelihoodTool::retrieveHistograms() {
       m_TH1F_sig[iFile][iHist] = 0;
       m_TH1F_bkg[iFile][iHist] = 0;
     }
-    
-    TList histList;
-    if ( m_histSvc->getTHists(fileName, histList).isFailure() ) {
-      ATH_MSG_FATAL("Could not retrieve histograms for " << fileName);
+
+    // --- Retrieving root files and list of keys ---
+    std::string rootFilePath = PathResolver::find_file(fileName, "DATAPATH");
+    TFile* rootFile = TFile::Open(rootFilePath.c_str(), "READ");	
+    if (!rootFile) {
+      ATH_MSG_FATAL("Could not retrieve root file: " << fileName);
       return StatusCode::FAILURE;
     }
-    ATH_MSG_DEBUG(histList.GetSize() << "histograms found in " << fileName);
-    if ( histList.GetSize()>22 ) {
+    TList* listOfKeys = rootFile->GetListOfKeys();
+    if (!listOfKeys) {
+      ATH_MSG_FATAL("Could not retrieve key list: " << fileName);
+      return StatusCode::FAILURE;
+    }
+    ATH_MSG_DEBUG(listOfKeys->GetSize() << "histogram keys found in " << fileName);
+    if (listOfKeys->GetSize() > 22) {
       ATH_MSG_FATAL("This exceeds the maximum allowed number of histograms");
       return StatusCode::FAILURE;
     }
-    
+
     int numKeysSignal = 0;
     int numKeysBkg    = 0;
     // --- Retrieving individual histograms ---
-    for ( int iHist = 0; iHist < histList.GetSize(); iHist++ ) {
-      TH1F* hist = 0;
-      hist = dynamic_cast<TH1F*>(histList.At(iHist));
+    for ( int iHist = 0; iHist < listOfKeys->GetSize(); iHist++ ) {
+      const std::string histName = listOfKeys->At(iHist)->GetName();
+      TH1F* hist = (TH1F*)(rootFile->Get(histName.c_str()));
       bool isSignal = false;
       if ( !hist ) {
-        ATH_MSG_ERROR("dynamic_cast<TH1F*> failed for TObject: " << histList.At(iHist));
+        ATH_MSG_ERROR("cannot retrieve hist " << histName);
       }
-      const std::string histName = hist->GetName();
       size_t endOfKey = histName.find("_signal", 0);
       if ( endOfKey!=std::string::npos ) {
         isSignal = true;
-- 
GitLab