File indexing completed on 2024-11-19 23:20:31
0001
0002
0003
0004 #include "fileutil.h"
0005
0006 TChain* RooUtil::FileUtil::createTChain(TString name, TString inputs) {
0007 using namespace std;
0008
0009
0010 ifstream infile("hadoopmap.txt");
0011 std::map<TString, TString> _map;
0012 if (infile.good()) {
0013 ifstream mapfile;
0014 mapfile.open("hadoopmap.txt");
0015 std::string line, oldpath, newpath;
0016 while (std::getline(mapfile, line)) {
0017 mapfile >> oldpath >> newpath;
0018 TString oldpath_tstr = oldpath.c_str();
0019 TString newpath_tstr = newpath.c_str();
0020 _map[oldpath_tstr] = newpath_tstr;
0021 }
0022 }
0023
0024
0025
0026 if (inputs.EndsWith("/")) {
0027 std::string pattern = TString::Format("%s/*.root", inputs.Data()).Data();
0028 inputs = RooUtil::StringUtil::join(glob(pattern));
0029 }
0030
0031 TChain* chain = new TChain(name);
0032 inputs = inputs.ReplaceAll("\"", "");
0033 inputs = inputs.ReplaceAll("\'", "");
0034 char hostnamestupid[100];
0035 gethostname(hostnamestupid, 100);
0036 TString hostname(hostnamestupid);
0037 std::cout << ">>> Hostname is " << hostname << std::endl;
0038 bool useXrootd = inputs.BeginsWith("/store/");
0039
0040
0041
0042
0043
0044
0045
0046
0047 if (useXrootd) {
0048 inputs.ReplaceAll("/store", "root://cmsxrootd.fnal.gov//store");
0049 }
0050 std::cout << "inputs : " << inputs.Data() << std::endl;
0051 for (auto& ff : RooUtil::StringUtil::split(inputs, ",")) {
0052 TString filepath = ff;
0053 if (_map.find(ff) != _map.end())
0054 filepath = _map[ff];
0055 RooUtil::print(Form("Adding %s", filepath.Data()));
0056 chain->Add(filepath);
0057 }
0058 return chain;
0059 }
0060
0061 TH1* RooUtil::FileUtil::get(TString name) { return (TH1*)gDirectory->Get(name); }
0062
0063 std::map<TString, TH1*> RooUtil::FileUtil::getAllHistograms(TFile* f) {
0064 std::map<TString, TH1*> hists;
0065 for (int ikey = 0; ikey < f->GetListOfKeys()->GetEntries(); ++ikey) {
0066 TString histname = f->GetListOfKeys()->At(ikey)->GetName();
0067 hists[histname] = (TH1*)f->Get(histname);
0068 }
0069 return hists;
0070 }
0071
0072 void RooUtil::FileUtil::saveAllHistograms(std::map<TString, TH1*> allhists, TFile* ofile) {
0073 ofile->cd();
0074 for (auto& hist : allhists)
0075 if (hist.second)
0076 hist.second->Write();
0077 }
0078
0079 std::vector<TString> RooUtil::FileUtil::getFilePathsInDirectory(TString dirpath) {
0080 std::vector<TString> rtn;
0081 DIR* dir;
0082 struct dirent* ent;
0083 if ((dir = opendir(dirpath.Data())) != NULL) {
0084
0085 while ((ent = readdir(dir)) != NULL) {
0086 if (!TString(ent->d_name).EqualTo(".") && !TString(ent->d_name).EqualTo(".."))
0087 rtn.push_back(ent->d_name);
0088 }
0089 closedir(dir);
0090 return rtn;
0091 } else {
0092
0093 error(TString::Format("Could not open directory = %s", dirpath.Data()));
0094 return rtn;
0095 }
0096 }
0097
0098
0099
0100 std::vector<TString> RooUtil::FileUtil::glob(const std::string& pattern) {
0101 using namespace std;
0102
0103
0104 glob_t glob_result;
0105 memset(&glob_result, 0, sizeof(glob_result));
0106
0107
0108 int return_value = glob(pattern.c_str(), GLOB_TILDE, NULL, &glob_result);
0109 if (return_value != 0) {
0110 globfree(&glob_result);
0111 stringstream ss;
0112 ss << "glob() failed with return_value " << return_value << endl;
0113 throw std::runtime_error(ss.str());
0114 }
0115
0116
0117 vector<TString> filenames;
0118 for (size_t i = 0; i < glob_result.gl_pathc; ++i) {
0119 filenames.push_back(string(glob_result.gl_pathv[i]));
0120 }
0121
0122
0123 globfree(&glob_result);
0124
0125
0126 return filenames;
0127 }