Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-12-20 03:13:22

0001 #include <ostream>
0002 #include <iostream>
0003 
0004 #include "CommonTools/Utils/interface/TFileDirectory.h"
0005 
0006 #include "boost/regex.hpp"
0007 
0008 using namespace std;
0009 
0010 TDirectory *TFileDirectory::getBareDirectory(const string &subdir) const { return _cd(subdir, false); }
0011 
0012 bool TFileDirectory::cd() const {
0013   _cd("", false);
0014   return true;
0015 }
0016 
0017 TDirectory *TFileDirectory::_cd(const string &subdir, bool createNeededDirectories) const {
0018   string fpath = fullPath();
0019   if (!subdir.empty()) {
0020     // not empty, we need to append it to path
0021     if (!fpath.empty()) {
0022       // path is also not empty, so add a slash and let's get going.
0023       fpath += "/" + subdir;
0024     } else {
0025       // path doesn't exist, so just use subdir
0026       fpath = subdir;
0027     }
0028   }
0029   TDirectory *dir = file_->GetDirectory(fpath.c_str());
0030   if (dir == nullptr) {
0031     // we didn't find the directory
0032     //
0033     // If we're not supposed to create the diretory, then we should
0034     // complain now that it doesn't exist.
0035     if (!createNeededDirectories) {
0036       cout << "here " << fpath << endl;
0037       throw cms::Exception("InvalidDirectory") << "directory " << fpath << " doesn't exist.";
0038     }
0039     if (!path_.empty()) {
0040       dir = file_->GetDirectory(path_.c_str());
0041       if (dir == nullptr) {
0042         throw cms::Exception("InvalidDirectory") << "Can't change directory to path: " << path_;
0043       }
0044     } else {
0045       // the base path 'path_' is empty, so just use the pointer to
0046       // the file.
0047       dir = file_;
0048     }
0049     // if a subdirectory was passed in, then this directory better
0050     // already exist (since you shoudln't be cd'ing into a directory
0051     // before making it and the cd with a subdir is only used to get
0052     // histograms that are already made).
0053     if (!subdir.empty()) {
0054       throw cms::Exception("InvalidDirectory") << "directory " << fpath << " doesn't exist.";
0055     }
0056     // if we're here, then that means that this is the first time
0057     // that we are calling cd() on this directory AND cd() has not
0058     // been called with a subdirectory, so go ahead and make the
0059     // directory.
0060     dir = _mkdir(dir, dir_, descr_);
0061   }
0062   bool ok = file_->cd(fpath.c_str());
0063   if (!ok) {
0064     throw cms::Exception("InvalidDirectory") << "Can't change directory to path: " << fpath;
0065   }
0066   return dir;
0067 }
0068 
0069 TDirectory *TFileDirectory::_mkdir(TDirectory *dirPtr, const string &subdirName, const string &description) const {
0070   // do we have this one already
0071   TDirectory *subdirPtr = dirPtr->GetDirectory(subdirName.c_str());
0072   if (subdirPtr) {
0073     subdirPtr->cd();
0074     return subdirPtr;
0075   }
0076   // if we're here, then this directory doesn't exist.  Is this
0077   // directory a subdirectory?
0078   const boost::regex subdirRE("(.+?)/([^/]+)");
0079   boost::smatch matches;
0080   TDirectory *parentDir = nullptr;
0081   string useName = subdirName;
0082   if (boost::regex_match(subdirName, matches, subdirRE)) {
0083     parentDir = _mkdir(dirPtr, matches[1], description);
0084     useName = matches[2];
0085   } else {
0086     // This is not a subdirectory, so we're golden
0087     parentDir = dirPtr;
0088   }
0089   subdirPtr = parentDir->mkdir(useName.c_str());
0090   if (!subdirPtr) {
0091     throw cms::Exception("InvalidDirectory") << "Can't create directory " << dir_ << " in path: " << path_;
0092   }
0093   subdirPtr->cd();
0094   return subdirPtr;
0095 }
0096 
0097 TObject *TFileDirectory::_getObj(const string &objname, const string &subdir) const {
0098   TObject *objPtr = getBareDirectory(subdir)->Get(objname.c_str());
0099   if (!objPtr) {
0100     // no histogram found by that name.  Sorry dude.
0101     if (!subdir.empty()) {
0102       throw cms::Exception("ObjectNotFound") << "Can not find object named " << objname << " in subdir " << subdir;
0103     } else {
0104       throw cms::Exception("ObjectNotFound") << "Can not find object named " << objname;
0105     }
0106   }  // if nothing found
0107   return objPtr;
0108 }
0109 
0110 std::string TFileDirectory::fullPath() const { return string(path_.empty() ? dir_ : path_ + "/" + dir_); }
0111 
0112 TFileDirectory TFileDirectory::mkdir(const std::string &dir, const std::string &descr) {
0113   TH1AddDirectorySentry sentry;
0114   _cd();
0115   return TFileDirectory(dir, descr, file_, fullPath());
0116 }