LArSoft  v06_85_00
Liquid Argon Software toolkit - http://larsoft.org/
TFileDirectory.cc
Go to the documentation of this file.
1 #include <ostream>
2 
3 #include "TFile.h"
4 #include "TROOT.h"
7 #include "cetlib_except/exception.h"
8 
9 using namespace std;
10 
12  std::string const& descr,
13  TFile* file,
14  std::string const& path)
15  : file_{file}, dir_{dir}, descr_{descr}, path_{path}
16 {}
17 
18 void
20 {
21  for (auto const& pr : callbacks_) {
22  dir_ = pr.first;
23  for (auto f : pr.second) {
24  f();
25  }
26  }
27 }
28 
29 void
31 {
32  callbacks_[dir_].push_back(cb);
33 }
34 
35 void
37 {
38  auto const& fpath = fullPath();
39  if (requireCallback_) {
40  auto it = callbacks_.find(dir_);
41  if (it == cend(callbacks_)) {
43  "A TFileService error occured while attempting to make a "
44  "directory or ROOT object.\n"}
45  << "File-switching has been enabled for TFileService. All modules "
46  "must register\n"
47  << "a callback function to be invoked whenever a file switch occurs. "
48  "The callback\n"
49  << "must ensure that any pointers to ROOT objects have been "
50  "updated.\n\n"
51  << " No callback has been registered for module '" << fpath << "'.\n\n"
52  << "Contact artists@fnal.gov for guidance.";
53  }
54  }
55 
56  TDirectory* dir = file_->GetDirectory(fpath.c_str());
57  if (dir == nullptr) {
58  if (!path_.empty()) {
59  dir = file_->GetDirectory(path_.c_str());
60  if (dir == nullptr) {
61  throw cet::exception("InvalidDirectory")
62  << "Can't change directory to path: " << path_;
63  }
64  } else {
65  dir = file_;
66  }
67  dir = dir->mkdir(dir_.c_str(), descr_.c_str());
68  if (dir == nullptr) {
69  throw cet::exception("InvalidDirectory")
70  << "Can't create directory " << dir_ << " in path: " << path_;
71  }
72  }
73  if (!file_->cd(fpath.c_str())) {
74  throw cet::exception("InvalidDirectory")
75  << "Can't change directory to path: " << fpath;
76  }
77 }
78 
79 std::string
81 {
82  return path_.empty() ? dir_ : path_ + "/" + dir_;
83 }
84 
86 art::TFileDirectory::mkdir(std::string const& dir, std::string const& descr)
87 {
89  cd();
90  return TFileDirectory{dir, descr, file_, fullPath()};
91 }
std::function< void()> Callback_t
STL namespace.
TFileDirectory mkdir(std::string const &dir, std::string const &descr="")
TFile f
Definition: plotHisto.C:6
void registerCallback(Callback_t)
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
TDirectory * dir
Definition: macro.C:5
TFile * file
std::string fullPath() const
std::map< std::string, std::vector< Callback_t > > callbacks_
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
TFileDirectory(std::string const &dir, std::string const &descr, TFile *file, std::string const &path)
Create a new TFileDirectory object.