CMS 3D CMS Logo

TFileDirectory.cc
Go to the documentation of this file.
1 #include <ostream>
2 #include <iostream>
3 
5 
6 #include "boost/regex.hpp"
7 
8 using namespace std;
9 
10 TDirectory *TFileDirectory::getBareDirectory(const string &subdir) const { return _cd(subdir, false); }
11 
12 bool TFileDirectory::cd() const {
13  _cd("", false);
14  return true;
15 }
16 
17 TDirectory *TFileDirectory::_cd(const string &subdir, bool createNeededDirectories) const {
18  string fpath = fullPath();
19  if (subdir.length()) {
20  // not empty, we need to append it to path
21  if (fpath.length()) {
22  // path is also not empty, so add a slash and let's get going.
23  fpath += "/" + subdir;
24  } else {
25  // path doesn't exist, so just use subdir
26  fpath = subdir;
27  }
28  }
29  TDirectory *dir = file_->GetDirectory(fpath.c_str());
30  if (dir == nullptr) {
31  // we didn't find the directory
32  //
33  // If we're not supposed to create the diretory, then we should
34  // complain now that it doesn't exist.
35  if (!createNeededDirectories) {
36  cout << "here " << fpath << endl;
37  throw cms::Exception("InvalidDirectory") << "directory " << fpath << " doesn't exist.";
38  }
39  if (!path_.empty()) {
40  dir = file_->GetDirectory(path_.c_str());
41  if (dir == nullptr) {
42  throw cms::Exception("InvalidDirectory") << "Can't change directory to path: " << path_;
43  }
44  } else {
45  // the base path 'path_' is empty, so just use the pointer to
46  // the file.
47  dir = file_;
48  }
49  // if a subdirectory was passed in, then this directory better
50  // already exist (since you shoudln't be cd'ing into a directory
51  // before making it and the cd with a subdir is only used to get
52  // histograms that are already made).
53  if (subdir.length()) {
54  throw cms::Exception("InvalidDirectory") << "directory " << fpath << " doesn't exist.";
55  }
56  // if we're here, then that means that this is the first time
57  // that we are calling cd() on this directory AND cd() has not
58  // been called with a subdirectory, so go ahead and make the
59  // directory.
60  dir = _mkdir(dir, dir_, descr_);
61  }
62  bool ok = file_->cd(fpath.c_str());
63  if (!ok) {
64  throw cms::Exception("InvalidDirectory") << "Can't change directory to path: " << fpath;
65  }
66  return dir;
67 }
68 
69 TDirectory *TFileDirectory::_mkdir(TDirectory *dirPtr, const string &subdirName, const string &description) const {
70  // do we have this one already
71  TDirectory *subdirPtr = dirPtr->GetDirectory(subdirName.c_str());
72  if (subdirPtr) {
73  subdirPtr->cd();
74  return subdirPtr;
75  }
76  // if we're here, then this directory doesn't exist. Is this
77  // directory a subdirectory?
78  const boost::regex subdirRE("(.+?)/([^/]+)");
79  boost::smatch matches;
80  TDirectory *parentDir = nullptr;
81  string useName = subdirName;
82  if (boost::regex_match(subdirName, matches, subdirRE)) {
83  parentDir = _mkdir(dirPtr, matches[1], description);
84  useName = matches[2];
85  } else {
86  // This is not a subdirectory, so we're golden
87  parentDir = dirPtr;
88  }
89  subdirPtr = parentDir->mkdir(useName.c_str());
90  if (!subdirPtr) {
91  throw cms::Exception("InvalidDirectory") << "Can't create directory " << dir_ << " in path: " << path_;
92  }
93  subdirPtr->cd();
94  return subdirPtr;
95 }
96 
97 TObject *TFileDirectory::_getObj(const string &objname, const string &subdir) const {
98  TObject *objPtr = getBareDirectory(subdir)->Get(objname.c_str());
99  if (!objPtr) {
100  // no histogram found by that name. Sorry dude.
101  if (subdir.length()) {
102  throw cms::Exception("ObjectNotFound") << "Can not find object named " << objname << " in subdir " << subdir;
103  } else {
104  throw cms::Exception("ObjectNotFound") << "Can not find object named " << objname;
105  }
106  } // if nothing found
107  return objPtr;
108 }
109 
110 std::string TFileDirectory::fullPath() const { return string(path_.empty() ? dir_ : path_ + "/" + dir_); }
111 
113  TH1AddDirectorySentry sentry;
114  _cd();
115  return TFileDirectory(dir, descr, file_, fullPath());
116 }
TDirectory * _cd(const std::string &subdir="", bool createNeededDirectories=true) const
static const char dir_[]
TObject * _getObj(const std::string &objname, const std::string &subdir="") const
bool cd() const
TDirectory * getBareDirectory(const std::string &subdir="") const
TDirectory * _mkdir(TDirectory *dirPtr, const std::string &dir, const std::string &description) const
TFileDirectory mkdir(const std::string &dir, const std::string &descr="")
create a new subdirectory
std::string & path_
std::string fullPath() const
return the full path of the stored histograms