From 5d23f059db55bc03d5df41e6ad56bbbbd6001fd1 Mon Sep 17 00:00:00 2001
From: Will Buttinger <will@cern.ch>
Date: Wed, 29 Aug 2018 09:41:07 +0100
Subject: [PATCH] added new getOutputFile method for obtaining the TFile
 pointer to a given output stream name (of the THistSvc)

Former-commit-id: 82076563f29e24f46e3dc30be4e96065848def20
---
 .../AthAnalysisBaseComps/AthAnalysisHelper.h  |  7 ++++
 .../src/AthAnalysisHelper.cxx                 | 38 +++++++++++++++++++
 2 files changed, 45 insertions(+)

diff --git a/Control/AthAnalysisBaseComps/AthAnalysisBaseComps/AthAnalysisHelper.h b/Control/AthAnalysisBaseComps/AthAnalysisBaseComps/AthAnalysisHelper.h
index 2c8fffe7579..4694a71fbf1 100644
--- a/Control/AthAnalysisBaseComps/AthAnalysisBaseComps/AthAnalysisHelper.h
+++ b/Control/AthAnalysisBaseComps/AthAnalysisBaseComps/AthAnalysisHelper.h
@@ -31,6 +31,8 @@
 
 #include "GaudiKernel/ToolHandle.h"
 
+#include "TFile.h"
+
 class AthAnalysisHelper { //thought about being a namespace but went for static methods instead, in case I want private data members in future
 
 public:
@@ -358,6 +360,11 @@ public:
 
 
 
+  //Obtain TFile pointer to THistSvc's output file with the given streamName
+  static TFile* getOutputFile(const std::string& streamName);
+
+
+
 
    ///we keep a static handle to the joboptionsvc, since it's very useful
    ///can e.g. do: AAH::joSvc->readOptions("myJob.opts","$JOBOPTSEARCHPATH")
diff --git a/Control/AthAnalysisBaseComps/src/AthAnalysisHelper.cxx b/Control/AthAnalysisBaseComps/src/AthAnalysisHelper.cxx
index 21bb0fed605..e639c8aa521 100644
--- a/Control/AthAnalysisBaseComps/src/AthAnalysisHelper.cxx
+++ b/Control/AthAnalysisBaseComps/src/AthAnalysisHelper.cxx
@@ -6,6 +6,10 @@
 
 #include "AthContainers/AuxTypeRegistry.h"
 
+#include "GaudiKernel/AttribStringParser.h"
+#include "TROOT.h"
+#include "boost/algorithm/string/case_conv.hpp"
+
 const std::string AthAnalysisHelper::UNDEFINED = "__UNDEFINED__";
 
 ServiceHandle<IJobOptionsSvc> AthAnalysisHelper::joSvc = ServiceHandle<IJobOptionsSvc>("JobOptionsSvc","AthAnalysisHelper");
@@ -106,3 +110,37 @@ void AthAnalysisHelper::dumpProperties(const IProperty& component) {
     std::cout << p->name() << " = " << p->toString() << std::endl;
   }
 }
+
+
+TFile* AthAnalysisHelper::getOutputFile(const std::string& streamName) {
+  ServiceHandle<IProperty> histSvc("THistSvc","");
+  auto& prop = histSvc->getProperty("Output");
+
+  std::vector<std::string> outputs;
+  if( Gaudi::Parsers::parse(outputs,prop.toString()).isFailure() ) {
+    return nullptr;
+  }
+
+  //extract the DATAFILE part of the string
+  std::string fileName="";
+  for(std::string& output : outputs) {
+    if( output.substr(0,output.find(" "))!=streamName ) continue;
+
+    //got here .. means we found the stream ...
+    for(auto attrib : Gaudi::Utils::AttribStringParser(output.substr(output.find(" ")+1))) {
+      auto TAG = boost::algorithm::to_upper_copy(attrib.tag);
+     
+      if(TAG=="FILE" || TAG=="DATAFILE") {
+	fileName = attrib.value;
+	break;
+      }
+
+    }
+    if(fileName.length()) {
+      return static_cast<TFile*>(gROOT->GetListOfFiles()->FindObject(fileName.c_str()));
+    }
+
+  }
+  return 0;
+
+}
-- 
GitLab