babymaker  e95a6a9342d4604277fe7cc6149b6b5b24447d89
merge_ntuples.cxx
Go to the documentation of this file.
1 // merge_ntuples.cxx: Merges ntuples tree and treeglobal
2 
3 #include <ctime>
4 #include <fstream>
5 #include <iostream>
6 
7 #include "TChain.h"
8 #include "TFile.h"
9 #include "TString.h"
10 #include "TSystem.h"
11 
12 using namespace std;
13 using std::cout;
14 using std::endl;
15 
16 int main(int argc, char *argv[]){
17  time_t startTime, endtime;
18  time(&startTime);
19 
20  if(argc < 4) {
21  cout<<endl<<"Requires at least 3 arguments: ./plot/merge_ntuples.exe infolder outfolder ntuples_tag <out_tag>"<<endl<<endl;;
22  return 1;
23  }
24 
25  TString folder(argv[1]), outfolder(argv[2]), ntuples_tag(argv[3]), out_tag("");
26  gSystem->mkdir(outfolder, kTRUE);
27  if(argc>4) out_tag = argv[4];
28  TString ntuples = folder+"/*"+ntuples_tag+"*";
29 
30  // Merging tree TTrees
31  TChain chain("tree");
32  int nfiles = chain.Add(ntuples);
33  TString outname = outfolder+"/mergedbaby_"+ntuples_tag+"_"+out_tag+"_nfiles_"+to_string(nfiles)+".root";
34  cout<<endl<<"Merging "<<ntuples<<" with "<<chain.GetEntries()<<" entries into "<<outname<<endl;
35  chain.Merge(outname);
36 
37  // Merging treeglobal TTrees
38  TChain chaing("treeglobal");
39  chaing.Add(ntuples);
40  TTree *tglobal = chaing.CopyTree("1");
41  tglobal->SetDirectory(0);
42  TFile rootfile(outname, "UPDATE");
43  rootfile.cd();
44  tglobal->Write();
45  rootfile.Close();
46 
47  time(&endtime);
48  cout<<"Took "<<difftime(endtime, startTime)<<" seconds to merge "<<ntuples<<" into "<<outname<<endl<<endl;
49  }
STL namespace.
int main(int argc, char *argv[])