CombineHarvester
TFileIO.h
Go to the documentation of this file.
1 #ifndef CombineTools_TFileIO_h
2 #define CombineTools_TFileIO_h
3 #include <memory>
4 #include <string>
5 #include <vector>
6 #include "boost/algorithm/string.hpp"
7 #include "TFile.h"
8 #include "TH1.h"
9 #include "TDirectory.h"
11 
12 namespace ch {
13 
14 std::unique_ptr<TH1> GetClonedTH1(TFile* file, std::string const& path);
15 
16 template <class T>
17 void WriteToTFile(T * ptr, TFile* file, std::string const& path);
18 
19 // Extracts objects from the form:
20 // "path/to/a/file.root:path/to/object"
21 template <class T>
22 T OpenFromTFile(std::string const& fullpath);
23 
24 template <class T>
25 T OpenFromTFile(TFile* file, std::string const& path);
26 }
27 
28 // Template function implementation
29 // ------------------------------------------------------------------
30 template <class T>
31 void ch::WriteToTFile(T * ptr, TFile* file, std::string const& path) {
32  #ifdef TIME_FUNCTIONS
33  LAUNCH_FUNCTION_TIMER(__timer__, __token__)
34  #endif
35  file->cd();
36  std::vector<std::string> as_vec;
37  boost::split(as_vec, path, boost::is_any_of("/"));
38  if (as_vec.size() >= 1) {
39  for (unsigned i = 0; i < as_vec.size() - 1; ++i) {
40  if (!gDirectory->GetDirectory(as_vec[i].c_str())) {
41  gDirectory->mkdir(as_vec[i].c_str());
42  }
43  gDirectory->cd(as_vec[i].c_str());
44  }
45  if (!gDirectory->FindKey(as_vec.back().c_str())) {
46  ptr->SetName(as_vec.back().c_str());
47  gDirectory->WriteTObject(ptr, as_vec.back().c_str());
48  }
49  gDirectory->cd("/");
50  }
51 }
52 
53 template <class T>
54 T ch::OpenFromTFile(std::string const& fullpath) {
55  std::size_t pos = fullpath.find(':');
56  std::string filepath = "";
57  std::string objectpath = "";
58  if (pos == std::string::npos || pos == 0 || pos == (fullpath.size() - 1)) {
59  throw std::runtime_error(
60  FNERROR("Input path must of the format file.root:object"));
61  } else {
62  filepath = fullpath.substr(0, pos);
63  objectpath = fullpath.substr(pos + 1);
64  }
65  TFile file(filepath.c_str());
66  if (!file.IsOpen()) {
67  throw std::runtime_error(FNERROR("File is invalid"));
68  }
69  file.cd();
70  T* obj_ptr = dynamic_cast<T*>(gDirectory->Get(objectpath.c_str()));
71  if (!obj_ptr) {
72  throw std::runtime_error(
73  FNERROR("Object " + objectpath + " is missing or of wrong type"));
74  }
75  return *obj_ptr;
76 }
77 
78 template <class T>
79 T ch::OpenFromTFile(TFile* file, std::string const& path) {
80  if (!file || !file->IsOpen()) {
81  throw std::runtime_error(FNERROR("File is null or invalid"));
82  }
83  file->cd();
84  T* obj_ptr = dynamic_cast<T*>(gDirectory->Get(path.c_str()));
85  if (!obj_ptr) {
86  throw std::runtime_error(
87  FNERROR("Object " + path + " is missing or of wrong type"));
88  }
89  return *obj_ptr;
90 }
91 
92 #endif
LAUNCH_FUNCTION_TIMER
#define LAUNCH_FUNCTION_TIMER(x, y)
Conveniently initialise a ch::FnTimer instance.
Definition: Logging.h:67
FNERROR
#define FNERROR(x)
Definition: Logging.h:9
ch::WriteToTFile
void WriteToTFile(T *ptr, TFile *file, std::string const &path)
Definition: TFileIO.h:31
ch::OpenFromTFile
T OpenFromTFile(std::string const &fullpath)
Definition: TFileIO.h:54
ch
Definition: Algorithm.h:10
ch::GetClonedTH1
std::unique_ptr< TH1 > GetClonedTH1(TFile *file, std::string const &path)
Definition: TFileIO.cc:12
Logging.h