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 bool
00019 TFileDirectory::cd () const
00020 {
00021 _cd ("", false);
00022 return true;
00023 }
00024
00025 TDirectory *
00026 TFileDirectory::_cd (const string &subdir, bool createNeededDirectories) const
00027 {
00028 string fpath = fullPath();
00029 if (subdir.length())
00030 {
00031
00032 if (fpath.length())
00033 {
00034
00035 fpath += "/" + subdir;
00036 } else {
00037
00038 fpath = subdir;
00039 }
00040 }
00041 TDirectory * dir = file_->GetDirectory( fpath.c_str() );
00042 if ( dir == 0 )
00043 {
00044
00045
00046
00047
00048 if (! createNeededDirectories)
00049 {
00050 cout << "here " << fpath << endl;
00051 throw
00052 cms::Exception( "InvalidDirectory" )
00053 << "directory " << fpath << " doesn't exist.";
00054 }
00055 if ( ! path_.empty() )
00056 {
00057 dir = file_->GetDirectory( path_.c_str() );
00058 if ( dir == 0 )
00059 {
00060 throw
00061 cms::Exception( "InvalidDirectory" )
00062 << "Can't change directory to path: " << path_;
00063 }
00064 } else
00065 {
00066
00067
00068 dir = file_;
00069 }
00070
00071
00072
00073
00074 if (subdir.length())
00075 {
00076 throw
00077 cms::Exception( "InvalidDirectory" )
00078 << "directory " << fpath << " doesn't exist.";
00079 }
00080
00081
00082
00083
00084 dir = _mkdir (dir, dir_, descr_);
00085 }
00086 bool ok = file_->cd( fpath.c_str() );
00087 if ( ! ok )
00088 {
00089 throw
00090 cms::Exception( "InvalidDirectory" )
00091 << "Can't change directory to path: " << fpath;
00092 }
00093 return dir;
00094 }
00095
00096 TDirectory*
00097 TFileDirectory::_mkdir (TDirectory *dirPtr,
00098 const string &subdirName,
00099 const string &description) const
00100 {
00101
00102 TDirectory *subdirPtr = dirPtr->GetDirectory (subdirName.c_str());
00103 if (subdirPtr)
00104 {
00105 subdirPtr->cd();
00106 return subdirPtr;
00107 }
00108
00109
00110 const boost::regex subdirRE ("(.+?)/([^/]+)");
00111 boost::smatch matches;
00112 TDirectory *parentDir = 0;
00113 string useName = subdirName;
00114 if( boost::regex_match (subdirName, matches, subdirRE) )
00115 {
00116 parentDir = _mkdir (dirPtr, matches[1], description);
00117 useName = matches[2];
00118 } else {
00119
00120 parentDir = dirPtr;
00121 }
00122 subdirPtr = parentDir->mkdir (useName.c_str());
00123 if ( ! subdirPtr )
00124 {
00125 throw
00126 cms::Exception( "InvalidDirectory" )
00127 << "Can't create directory " << dir_ << " in path: " << path_;
00128 }
00129 subdirPtr->cd();
00130 return subdirPtr;
00131 }
00132
00133 TObject*
00134 TFileDirectory::_getObj (const string &objname, const string &subdir) const
00135 {
00136 TObject *objPtr = getBareDirectory (subdir)->Get (objname.c_str());
00137 if ( ! objPtr)
00138 {
00139
00140 if (subdir.length())
00141 {
00142 throw
00143 cms::Exception ("ObjectNotFound")
00144 << "Can not find object named " << objname
00145 << " in subdir " << subdir;
00146 } else {
00147 throw
00148 cms::Exception ("ObjectNotFound")
00149 << "Can not find object named " << objname;
00150 }
00151 }
00152 return objPtr;
00153 }
00154
00155 std::string
00156 TFileDirectory::fullPath() const
00157 {
00158 return string( path_.empty() ? dir_ : path_ + "/" + dir_ );
00159 }
00160
00161 TFileDirectory
00162 TFileDirectory::mkdir( const std::string & dir, const std::string & descr )
00163 {
00164 TH1AddDirectorySentry sentry;
00165 _cd();
00166 return TFileDirectory( dir, descr, file_, fullPath() );
00167 }