Go to the documentation of this file.00001 #include <ostream>
00002 #include <iostream>
00003
00004 #include "CommonTools/Utils/interface/TFileDirectory.h"
00005
00006 #include "boost/regex.hpp"
00007
00008
00009
00010 using namespace std;
00011
00012 TDirectory *
00013 TFileDirectory::getBareDirectory (const string &subdir) const
00014 {
00015 return _cd (subdir, false);
00016 }
00017
00018 TH1*
00019 TFileDirectory::getHist (const string &histname, const string &subdir) const
00020 {
00021 TObject *objPtr = _getObj (histname, subdir);
00022
00023 TH1* retval = dynamic_cast< TH1* > ( objPtr );
00024 if ( ! retval )
00025 {
00026
00027 throw
00028 cms::Exception ("ObjectNotHistogram")
00029 << "Object named " << histname << " is not a histogram";
00030 }
00031 return retval;
00032 }
00033
00034 bool
00035 TFileDirectory::cd () const
00036 {
00037 _cd ("", false);
00038 return true;
00039 }
00040
00041 TDirectory *
00042 TFileDirectory::_cd (const string &subdir, bool createNeededDirectories) const
00043 {
00044 string fpath = fullPath();
00045 if (subdir.length())
00046 {
00047
00048 if (fpath.length())
00049 {
00050
00051 fpath += "/" + subdir;
00052 } else {
00053
00054 fpath = subdir;
00055 }
00056 }
00057 TDirectory * dir = file_->GetDirectory( fpath.c_str() );
00058 if ( dir == 0 )
00059 {
00060
00061
00062
00063
00064 if (! createNeededDirectories)
00065 {
00066 cout << "here " << fpath << endl;
00067 throw
00068 cms::Exception( "InvalidDirectory" )
00069 << "directory " << fpath << " doesn't exist.";
00070 }
00071 if ( ! path_.empty() )
00072 {
00073 dir = file_->GetDirectory( path_.c_str() );
00074 if ( dir == 0 )
00075 {
00076 throw
00077 cms::Exception( "InvalidDirectory" )
00078 << "Can't change directory to path: " << path_;
00079 }
00080 } else
00081 {
00082
00083
00084 dir = file_;
00085 }
00086
00087
00088
00089
00090 if (subdir.length())
00091 {
00092 throw
00093 cms::Exception( "InvalidDirectory" )
00094 << "directory " << fpath << " doesn't exist.";
00095 }
00096
00097
00098
00099
00100 dir = _mkdir (dir, dir_, descr_);
00101 }
00102 bool ok = file_->cd( fpath.c_str() );
00103 if ( ! ok )
00104 {
00105 throw
00106 cms::Exception( "InvalidDirectory" )
00107 << "Can't change directory to path: " << fpath;
00108 }
00109 return dir;
00110 }
00111
00112 TDirectory*
00113 TFileDirectory::_mkdir (TDirectory *dirPtr,
00114 const string &subdirName,
00115 const string &description) const
00116 {
00117
00118 TDirectory *subdirPtr = dirPtr->GetDirectory (subdirName.c_str());
00119 if (subdirPtr)
00120 {
00121 subdirPtr->cd();
00122 return subdirPtr;
00123 }
00124
00125
00126 const boost::regex subdirRE ("(.+?)/([^/]+)");
00127 boost::smatch matches;
00128 TDirectory *parentDir = 0;
00129 string useName = subdirName;
00130 if( boost::regex_match (subdirName, matches, subdirRE) )
00131 {
00132 parentDir = _mkdir (dirPtr, matches[1], description);
00133 useName = matches[2];
00134 } else {
00135
00136 parentDir = dirPtr;
00137 }
00138 subdirPtr = parentDir->mkdir (useName.c_str());
00139 if ( ! subdirPtr )
00140 {
00141 throw
00142 cms::Exception( "InvalidDirectory" )
00143 << "Can't create directory " << dir_ << " in path: " << path_;
00144 }
00145 subdirPtr->cd();
00146 return subdirPtr;
00147 }
00148
00149 TObject*
00150 TFileDirectory::_getObj (const string &objname, const string &subdir) const
00151 {
00152 TObject *objPtr = getBareDirectory (subdir)->Get (objname.c_str());
00153 if ( ! objPtr)
00154 {
00155
00156 if (subdir.length())
00157 {
00158 throw
00159 cms::Exception ("ObjectNotFound")
00160 << "Can not find object named " << objname
00161 << " in subdir " << subdir;
00162 } else {
00163 throw
00164 cms::Exception ("ObjectNotFound")
00165 << "Can not find object named " << objname;
00166 }
00167 }
00168 return objPtr;
00169 }
00170
00171 std::string
00172 TFileDirectory::fullPath() const
00173 {
00174 return string( path_.empty() ? dir_ : path_ + "/" + dir_ );
00175 }
00176
00177 TFileDirectory
00178 TFileDirectory::mkdir( const std::string & dir, const std::string & descr )
00179 {
00180 TH1AddDirectorySentry sentry;
00181 _cd();
00182 return TFileDirectory( dir, descr, file_, fullPath() );
00183 }