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
0021 if (!fpath.empty()) {
0022
0023 fpath += "/" + subdir;
0024 } else {
0025
0026 fpath = subdir;
0027 }
0028 }
0029 TDirectory *dir = file_->GetDirectory(fpath.c_str());
0030 if (dir == nullptr) {
0031
0032
0033
0034
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
0046
0047 dir = file_;
0048 }
0049
0050
0051
0052
0053 if (!subdir.empty()) {
0054 throw cms::Exception("InvalidDirectory") << "directory " << fpath << " doesn't exist.";
0055 }
0056
0057
0058
0059
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
0071 TDirectory *subdirPtr = dirPtr->GetDirectory(subdirName.c_str());
0072 if (subdirPtr) {
0073 subdirPtr->cd();
0074 return subdirPtr;
0075 }
0076
0077
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
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
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 }
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 }