CMS 3D CMS Logo

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