1 #ifndef CombineTools_TFileIO_h
2 #define CombineTools_TFileIO_h
6 #include "boost/algorithm/string.hpp"
9 #include "TDirectory.h"
14 std::unique_ptr<TH1>
GetClonedTH1(TFile* file, std::string
const& path);
17 void WriteToTFile(T * ptr, TFile* file, std::string
const& path);
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());
43 gDirectory->cd(as_vec[i].c_str());
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());
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"));
62 filepath = fullpath.substr(0, pos);
63 objectpath = fullpath.substr(pos + 1);
65 TFile file(filepath.c_str());
67 throw std::runtime_error(
FNERROR(
"File is invalid"));
70 T* obj_ptr =
dynamic_cast<T*
>(gDirectory->Get(objectpath.c_str()));
72 throw std::runtime_error(
73 FNERROR(
"Object " + objectpath +
" is missing or of wrong type"));
80 if (!file || !file->IsOpen()) {
81 throw std::runtime_error(
FNERROR(
"File is null or invalid"));
84 T* obj_ptr =
dynamic_cast<T*
>(gDirectory->Get(path.c_str()));
86 throw std::runtime_error(
87 FNERROR(
"Object " + path +
" is missing or of wrong type"));
#define LAUNCH_FUNCTION_TIMER(x, y)
Conveniently initialise a ch::FnTimer instance.
void WriteToTFile(T *ptr, TFile *file, std::string const &path)
std::unique_ptr< TH1 > GetClonedTH1(TFile *file, std::string const &path)
T OpenFromTFile(std::string const &fullpath)