susy_cfa  b611ccad937ea179f86a1f5663960264616c0a20
divide_ntuples.cxx
Go to the documentation of this file.
1 // divide_ntuples: Divides each ntuple in infolder into nDiv exclusive subsets
2 
3 #include <ctime>
4 
5 #include <vector>
6 #include <iostream>
7 #include <string>
8 #include <unistd.h>
9 #include <string>
10 
11 #include "TFile.h"
12 #include "TSystem.h"
13 #include "TString.h"
14 #include "TChain.h"
15 #include "TDirectory.h"
16 
17 #include "utilities.hpp"
18 
19 using namespace std;
20 
21 int main(int argc, char *argv[]){
22  time_t startTime, curTime;
23  time(&startTime);
24 
25  TString infolder(""), outfolder("");
26  int c(0);
27  unsigned nDiv(1);
28  while((c=getopt(argc, argv, "i:o:n:"))!=-1){
29  switch(c){
30  case 'n':
31  nDiv=atoi(optarg);
32  break;
33  case 'i':
34  infolder=optarg;
35  break;
36  case 'o':
37  outfolder=optarg;
38  break;
39  default:
40  break;
41  }
42  }
43  vector<TString> files = dirlist(infolder, ".root");
44  unsigned nFiles(files.size());
45 
46  if(nDiv == 1 || nFiles==0){
47  cout<<"nDiv = "<<nDiv<<" and nFiles = "<<nFiles<<". Nothing to do, exiting."<<endl<<endl;
48  return 1;
49  }
50 
51  TString ntuplename, ntupleout, tag;
52  gSystem->mkdir(outfolder, kTRUE);
53  cout<<"Dividing "<<nFiles<<" files into "<<nDiv<<" divisions. outfolder = "<<outfolder<<endl;
54  for(unsigned file(0); file < nFiles; file++){
55  files[file] = "/"+files[file];
56  ntuplename = infolder+files[file];
57  TChain chain("tree");
58  chain.Add(ntuplename);
59  cout<<file<<": "<<ntuplename<<" has "<<chain.GetEntries()<<" entries"<<endl;
60  vector<TChain *> divChains;
61  for(unsigned div(0); div < nDiv; div++){
62  divChains.push_back(static_cast<TChain *>(chain.CloneTree(0)));
63  } // Loop over number of divisions
64  for(int entry(0); entry < chain.GetEntries(); entry++){
65  chain.GetEntry(entry);
66  divChains[entry%nDiv]->Fill();
67  } // Loop over chain entries
68  for(unsigned div(0); div < nDiv; div++){
69  tag = "_"; tag += div+1; tag += "of"; tag += nDiv;
70  ntupleout = outfolder+files[file];
71  ntupleout.ReplaceAll(".root", tag+".root");
72  TFile divFile(ntupleout,"RECREATE");
73  divFile.cd();
74  divChains[div]->Write();
75  divFile.Close();
76  //cout<<"Written "<<ntupleout<<endl;
77  } // Loop over number of divisions
78  } // Loop over files
79 
80  time(&curTime);
81  cout<<"Dividing ntuples took "<<difftime(curTime,startTime)<<" seconds"<<endl<<endl;
82 
83  return 0;
84 }
string files
Definition: data_combine.py:33
string outfolder
Definition: data_combine.py:17
STL namespace.
int main(int argc, char *argv[])
std::vector< TString > dirlist(const TString &folder, const TString &inname="dir", const TString &tag="")
Definition: utilities.cpp:182