CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch13/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 TH1*
00019 TFileDirectory::getHist (const string &histname, const string &subdir) const
00020 {
00021    TObject *objPtr = _getObj (histname, subdir);
00022    // Ok, we've got it.  Let's see if it's a histogram
00023    TH1* retval = dynamic_cast< TH1* > ( objPtr );
00024    if ( ! retval )
00025    {
00026       // object isn't a derivative of TH1 so it's not a histogram
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       // not empty, we need to append it to path
00048       if (fpath.length())
00049       {
00050          // path is also not empty, so add a slash and let's get going.
00051          fpath += "/" + subdir;
00052       } else {
00053          // path doesn't exist, so just use subdir
00054          fpath = subdir;
00055       }
00056    }
00057    TDirectory * dir = file_->GetDirectory( fpath.c_str() );
00058    if ( dir == 0 ) 
00059    {      
00060       // we didn't find the directory
00061       //
00062       // If we're not supposed to create the diretory, then we should
00063       // complain now that it doesn't exist.
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          // the base path 'path_' is empty, so just use the pointer to
00083          // the file.
00084          dir = file_;
00085       }
00086       // if a subdirectory was passed in, then this directory better
00087       // already exist (since you shoudln't be cd'ing into a directory
00088       // before making it and the cd with a subdir is only used to get
00089       // histograms that are already made).
00090       if (subdir.length())
00091       {
00092          throw 
00093             cms::Exception( "InvalidDirectory" ) 
00094             << "directory " << fpath << " doesn't exist.";
00095       }
00096       // if we're here, then that means that this is the first time
00097       // that we are calling cd() on this directory AND cd() has not
00098       // been called with a subdirectory, so go ahead and make the
00099       // directory.
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    // do we have this one already
00118    TDirectory *subdirPtr = dirPtr->GetDirectory (subdirName.c_str());
00119    if (subdirPtr)
00120    {
00121       subdirPtr->cd();
00122       return subdirPtr;
00123    }
00124    // if we're here, then this directory doesn't exist.  Is this
00125    // directory a subdirectory?
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       // This is not a subdirectory, so we're golden
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       // no histogram found by that name.  Sorry dude.
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    } // if nothing found
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 }