CombineHarvester
TFileIO.cc
Go to the documentation of this file.
2 #include <memory>
3 #include <string>
4 #include <vector>
5 #include "TFile.h"
6 #include "TH1.h"
7 #include "TDirectory.h"
9 
10 namespace ch {
11 
12 std::unique_ptr<TH1> GetClonedTH1(TFile* file, std::string const& path) {
13  if (!file) {
14  throw std::runtime_error(FNERROR("Supplied ROOT file pointer is null"));
15  }
16  TDirectory* backup_dir = gDirectory;
17  file->cd();
18  bool cur_status = TH1::AddDirectoryStatus();
19  TH1::AddDirectory(kFALSE);
20  TObject *obj = gDirectory->Get(path.c_str());
21  if (!obj) {
22  gDirectory = backup_dir;
23  throw std::runtime_error(
24  FNERROR("TH1 " + path + " not found in " + file->GetName()));
25  }
26  std::unique_ptr<TH1> res(
27  dynamic_cast<TH1*>(obj));
28  TH1::AddDirectory(cur_status);
29  if (!res) {
30  gDirectory = backup_dir;
31  throw std::runtime_error(FNERROR("Object " + path + " in " +
32  file->GetName() + " is not of type TH1"));
33  }
34  // res->SetDirectory(0);
35  gDirectory = backup_dir;
36  return res;
37 }
38 }
#define FNERROR(x)
Definition: Logging.h:9
Definition: Algorithm.h:10
std::unique_ptr< TH1 > GetClonedTH1(TFile *file, std::string const &path)
Definition: TFileIO.cc:12