CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_2_7_hltpatch2/src/CommonTools/Utils/src/TFileDirectory.cc

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       // not empty, we need to append it to path
00032       if (fpath.length())
00033       {
00034          // path is also not empty, so add a slash and let's get going.
00035          fpath += "/" + subdir;
00036       } else {
00037          // path doesn't exist, so just use subdir
00038          fpath = subdir;
00039       }
00040    }
00041    TDirectory * dir = file_->GetDirectory( fpath.c_str() );
00042    if ( dir == 0 ) 
00043    {      
00044       // we didn't find the directory
00045       //
00046       // If we're not supposed to create the diretory, then we should
00047       // complain now that it doesn't exist.
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          // the base path 'path_' is empty, so just use the pointer to
00067          // the file.
00068          dir = file_;
00069       }
00070       // if a subdirectory was passed in, then this directory better
00071       // already exist (since you shoudln't be cd'ing into a directory
00072       // before making it and the cd with a subdir is only used to get
00073       // histograms that are already made).
00074       if (subdir.length())
00075       {
00076          throw 
00077             cms::Exception( "InvalidDirectory" ) 
00078             << "directory " << fpath << " doesn't exist.";
00079       }
00080       // if we're here, then that means that this is the first time
00081       // that we are calling cd() on this directory AND cd() has not
00082       // been called with a subdirectory, so go ahead and make the
00083       // directory.
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    // do we have this one already
00102    TDirectory *subdirPtr = dirPtr->GetDirectory (subdirName.c_str());
00103    if (subdirPtr)
00104    {
00105       subdirPtr->cd();
00106       return subdirPtr;
00107    }
00108    // if we're here, then this directory doesn't exist.  Is this
00109    // directory a subdirectory?
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       // This is not a subdirectory, so we're golden
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       // no histogram found by that name.  Sorry dude.
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    } // if nothing found
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 }