Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-11-19 23:20:31

0001 //  .
0002 // ..: P. Chang, philip@physics.ucsd.edu
0003 
0004 #include "fileutil.h"
0005 
0006 TChain* RooUtil::FileUtil::createTChain(TString name, TString inputs) {
0007   using namespace std;
0008 
0009   // hadoopmap to bypass some of the broken files
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   // globbing if the provided path is only a directory
0025   // It will check via looking at the last character == "/"
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("\"", "");  // In case some rogue " or ' is left over
0033   inputs = inputs.ReplaceAll("\'", "");  // In case some rogue " or ' is left over
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   // if (useXrootd and hostname.Contains("t2.ucsd.edu"))
0040   // {
0041   //     if (inputs.Contains("/hadoop/cms"))
0042   //         inputs.ReplaceAll("/hadoop/cms", "root://redirector.t2.ucsd.edu/");
0043   //     else
0044   //         inputs.ReplaceAll("/store", "root://redirector.t2.ucsd.edu//store");
0045   // }
0046   // else
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     /* print all the files and directories within directory */
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     /* could not open directory */
0093     error(TString::Format("Could not open directory = %s", dirpath.Data()));
0094     return rtn;
0095   }
0096 }
0097 
0098 //https://stackoverflow.com/questions/8401777/simple-glob-in-c-on-unix-system
0099 
0100 std::vector<TString> RooUtil::FileUtil::glob(const std::string& pattern) {
0101   using namespace std;
0102 
0103   // glob struct resides on the stack
0104   glob_t glob_result;
0105   memset(&glob_result, 0, sizeof(glob_result));
0106 
0107   // do the glob operation
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   // collect all the filenames into a std::list<std::string>
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   // cleanup
0123   globfree(&glob_result);
0124 
0125   // done
0126   return filenames;
0127 }