Project

General

Profile

TFileDirectory.cc

Kyle Knoepfel, 11/02/2017 09:06 AM

 
1
#include <ostream>
2

    
3
#include "TROOT.h"
4
#include "TFile.h"
5
#include "art/Framework/Services/Optional/TFileDirectory.h"
6
#include "canvas/Utilities/Exception.h"
7
#include "cetlib/exception.h"
8

    
9
using namespace std;
10

    
11
art::TFileDirectory::TFileDirectory(std::string const& dir,
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
19
art::TFileDirectory::invokeCallbacks()
20
{
21
  for (auto const& pr : callbacks_) {
22
    for (auto f : pr.second) {
23
      f();
24
    }
25
  }
26
}
27

    
28
void
29
art::TFileDirectory::registerCallback(Callback_t cb)
30
{
31
  callbacks_[fullPath()].push_back(cb);
32
}
33

    
34
void
35
art::TFileDirectory::cd() const
36
{
37
  auto const& fpath = fullPath();
38
  if (requireCallback_) {
39
    auto it = callbacks_.find(fpath);
40
    if (it == cend(callbacks_)) {
41
      throw Exception{errors::Configuration,
42
          "A TFileService error occured while attempting to make a directory or ROOT object.\n"}
43
      << "File-switching has been enabled for TFileService.  All modules must register\n"
44
      << "a callback function to be invoked whenever a file switch occurs.  The callback\n"
45
      << "must ensure that any pointers to ROOT objects have been updated.\n\n"
46
      << "  No callback has been registered for module '" << fpath << "'.\n\n"
47
      << "Contact artists@fnal.gov for guidance.";
48
    }
49
  }
50

    
51
  TDirectory* dir = file_->GetDirectory(fpath.c_str());
52
  if (dir == nullptr) {
53
    if (!path_.empty()) {
54
      dir = file_->GetDirectory(path_.c_str());
55
      if (dir == nullptr) {
56
        throw cet::exception("InvalidDirectory")
57
          << "Can't change directory to path: " << path_;
58
      }
59
    }
60
    else {
61
      dir = file_;
62
    }
63
    dir = dir->mkdir(dir_.c_str(), descr_.c_str());
64
    if (dir == nullptr) {
65
      throw cet::exception("InvalidDirectory")
66
        << "Can't create directory " << dir_ << " in path: " << path_;
67
    }
68
  }
69
  if (!file_->cd(fpath.c_str())) {
70
    throw cet::exception("InvalidDirectory")
71
      << "Can't change directory to path: " << fpath;
72
  }
73
}
74

    
75
std::string
76
art::TFileDirectory::fullPath() const
77
{
78
  return path_.empty() ? dir_ : path_ + "/" + dir_;
79
}
80

    
81
art::TFileDirectory
82
art::TFileDirectory::mkdir(std::string const& dir, std::string const& descr)
83
{
84
  detail::RootDirectorySentry sentry;
85
  cd();
86
  return TFileDirectory{dir, descr, file_, fullPath()};
87
}