CMS 3D CMS Logo

fileutil.cc
Go to the documentation of this file.
1 // .
2 // ..: P. Chang, philip@physics.ucsd.edu
3 
4 #include "fileutil.h"
5 
6 TChain* RooUtil::FileUtil::createTChain(TString name, TString inputs) {
7  using namespace std;
8 
9  // hadoopmap to bypass some of the broken files
10  ifstream infile("hadoopmap.txt");
11  std::map<TString, TString> _map;
12  if (infile.good()) {
13  ifstream mapfile;
14  mapfile.open("hadoopmap.txt");
15  std::string line, oldpath, newpath;
16  while (std::getline(mapfile, line)) {
17  mapfile >> oldpath >> newpath;
18  TString oldpath_tstr = oldpath.c_str();
19  TString newpath_tstr = newpath.c_str();
20  _map[oldpath_tstr] = newpath_tstr;
21  }
22  }
23 
24  // globbing if the provided path is only a directory
25  // It will check via looking at the last character == "/"
26  if (inputs.EndsWith("/")) {
27  std::string pattern = TString::Format("%s/*.root", inputs.Data()).Data();
29  }
30 
31  TChain* chain = new TChain(name);
32  inputs = inputs.ReplaceAll("\"", ""); // In case some rogue " or ' is left over
33  inputs = inputs.ReplaceAll("\'", ""); // In case some rogue " or ' is left over
34  char hostnamestupid[100];
35  gethostname(hostnamestupid, 100);
36  TString hostname(hostnamestupid);
37  std::cout << ">>> Hostname is " << hostname << std::endl;
38  bool useXrootd = inputs.BeginsWith("/store/");
39  // if (useXrootd and hostname.Contains("t2.ucsd.edu"))
40  // {
41  // if (inputs.Contains("/hadoop/cms"))
42  // inputs.ReplaceAll("/hadoop/cms", "root://redirector.t2.ucsd.edu/");
43  // else
44  // inputs.ReplaceAll("/store", "root://redirector.t2.ucsd.edu//store");
45  // }
46  // else
47  if (useXrootd) {
48  inputs.ReplaceAll("/store", "root://cmsxrootd.fnal.gov//store");
49  }
50  std::cout << "inputs : " << inputs.Data() << std::endl;
51  for (auto& ff : RooUtil::StringUtil::split(inputs, ",")) {
52  TString filepath = ff;
53  if (_map.find(ff) != _map.end())
54  filepath = _map[ff];
55  RooUtil::print(Form("Adding %s", filepath.Data()));
56  chain->Add(filepath);
57  }
58  return chain;
59 }
60 
61 TH1* RooUtil::FileUtil::get(TString name) { return (TH1*)gDirectory->Get(name); }
62 
63 std::map<TString, TH1*> RooUtil::FileUtil::getAllHistograms(TFile* f) {
64  std::map<TString, TH1*> hists;
65  for (int ikey = 0; ikey < f->GetListOfKeys()->GetEntries(); ++ikey) {
66  TString histname = f->GetListOfKeys()->At(ikey)->GetName();
67  hists[histname] = (TH1*)f->Get(histname);
68  }
69  return hists;
70 }
71 
72 void RooUtil::FileUtil::saveAllHistograms(std::map<TString, TH1*> allhists, TFile* ofile) {
73  ofile->cd();
74  for (auto& hist : allhists)
75  if (hist.second)
76  hist.second->Write();
77 }
78 
79 std::vector<TString> RooUtil::FileUtil::getFilePathsInDirectory(TString dirpath) {
80  std::vector<TString> rtn;
81  DIR* dir;
82  struct dirent* ent;
83  if ((dir = opendir(dirpath.Data())) != NULL) {
84  /* print all the files and directories within directory */
85  while ((ent = readdir(dir)) != NULL) {
86  if (!TString(ent->d_name).EqualTo(".") && !TString(ent->d_name).EqualTo(".."))
87  rtn.push_back(ent->d_name);
88  }
89  closedir(dir);
90  return rtn;
91  } else {
92  /* could not open directory */
93  error(TString::Format("Could not open directory = %s", dirpath.Data()));
94  return rtn;
95  }
96 }
97 
98 //https://stackoverflow.com/questions/8401777/simple-glob-in-c-on-unix-system
99 
100 std::vector<TString> RooUtil::FileUtil::glob(const std::string& pattern) {
101  using namespace std;
102 
103  // glob struct resides on the stack
104  glob_t glob_result;
105  memset(&glob_result, 0, sizeof(glob_result));
106 
107  // do the glob operation
108  int return_value = glob(pattern.c_str(), GLOB_TILDE, NULL, &glob_result);
109  if (return_value != 0) {
110  globfree(&glob_result);
111  stringstream ss;
112  ss << "glob() failed with return_value " << return_value << endl;
113  throw std::runtime_error(ss.str());
114  }
115 
116  // collect all the filenames into a std::list<std::string>
117  vector<TString> filenames;
118  for (size_t i = 0; i < glob_result.gl_pathc; ++i) {
119  filenames.push_back(string(glob_result.gl_pathv[i]));
120  }
121 
122  // cleanup
123  globfree(&glob_result);
124 
125  // done
126  return filenames;
127 }
void print(TString msg="", const char *fname="", int flush_before=0, int flush_after=0)
Definition: printutil.cc:23
vecTString split(TString in, TString separator=" ")
Definition: stringutil.cc:31
void saveAllHistograms(std::map< TString, TH1 *>, TFile *)
Definition: fileutil.cc:72
std::vector< TString > glob(const std::string &pattern)
Definition: fileutil.cc:100
double f[11][100]
std::map< TString, TH1 * > getAllHistograms(TFile *)
Definition: fileutil.cc:63
TString join(vecTString in, TString joiner=",", Int_t rm_blanks=1)
Definition: stringutil.cc:63
TChain * createTChain(TString, TString)
Definition: fileutil.cc:6
TH1 * get(TString)
Definition: fileutil.cc:61
std::vector< TString > getFilePathsInDirectory(TString dirpath)
Definition: fileutil.cc:79