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