susy_cfa  b611ccad937ea179f86a1f5663960264616c0a20
skim_ntuples.cxx
Go to the documentation of this file.
1 // skim_ntuples.cxx: Skims reduced trees
2 // USAGE: ./plot/skim_ntuples.exe infolder outfolder [cuts=\"ht>500&&met>200\"] [njobs=-1] [ijob=-1]
3 
4 
5 #include "utilities.hpp"
6 
7 #include <fstream>
8 #include <iostream>
9 #include <cmath>
10 #include <string>
11 #include <sstream>
12 #include <vector>
13 #include <stdlib.h> /* atoi */
14 
15 #include "TChain.h"
16 #include "TFile.h"
17 #include "TString.h"
18 #include "TMath.h"
19 #include "TSystem.h"
20 #include "TDirectory.h"
21 
22 using namespace std;
23 using std::cout;
24 using std::endl;
25 
26 void onefile_skim(TString infiles, TString outfolder, TString cuts);
27 
28 int main(int argc, char *argv[]){
29 
30  if(argc < 3) {
31  cout<<endl<<"Required at least 2 arguments: "
32  <<"./plot/skim_ntuples.exe infolder outfolder [cuts=\"ht>500&&met>200\"] "
33  <<"[njobs=-1] [ijob=-1]"<<endl<<endl;;
34  return 1;
35  }
36  TString folder(argv[1]), outfolder(argv[2]), cuts="ht>500&&met>200";
37  if(argc >= 4) cuts = argv[3];
38  unsigned njobs(0), ijob(0);
39  if(argc >= 6) {
40  njobs = atoi(argv[4]);
41  ijob = atoi(argv[5]);
42  }
43  vector<TString> files = dirlist(folder, ".root");
44  unsigned nfiles(files.size()), ini(0), end(nfiles);
45  if(njobs>0){
46  if(ijob<1 || ijob>njobs){
47  cout<<endl<<"You need to set the 5th argument between 1 and "<<njobs<<endl<<endl;
48  return 1;
49  }
50  unsigned jobfiles = (nfiles+njobs-1)/njobs;
51  unsigned nbigjobs = (nfiles+njobs-1)%njobs+1;
52  if(ijob <= nbigjobs){
53  ini = jobfiles*(ijob-1);
54  end = ini + jobfiles;
55  } else {
56  ini = nbigjobs*jobfiles+(jobfiles-1)*(ijob-1-nbigjobs);
57  end = ini + jobfiles-1;
58  }
59  }
60  cout<<"Doing files "<<ini+1<<" to "<<end<<" out of "<<nfiles<<endl;
61  for(unsigned file(ini); file < end; file++){
62  onefile_skim(folder+"/"+files[file], outfolder, cuts);
63  }
64  return 0;
65 }
66 
67 void onefile_skim(TString infiles, TString outfolder, TString cuts){
68  TString folder(infiles), outfile(infiles);
69  folder.Remove(folder.Last('/')+1, folder.Length());
70 
71  // Finding outfile name
72  outfile.Remove(0, outfile.Last('/')); outfile.ReplaceAll("*","");
73  if(outfile.Contains(".root")) outfile.ReplaceAll(".root","_"+cuts+".root");
74  else outfile += ("_"+cuts+".root");
75  outfile.ReplaceAll(">=","ge"); outfile.ReplaceAll("<=","se"); outfile.ReplaceAll("&","_");
76  outfile.ReplaceAll(">","g"); outfile.ReplaceAll("<","s"); outfile.ReplaceAll("=","");
77  outfile.ReplaceAll("(",""); outfile.ReplaceAll(")",""); outfile.ReplaceAll("+","");
78  outfile.ReplaceAll("[",""); outfile.ReplaceAll("]",""); outfile.ReplaceAll("|","_");
79  outfile = outfolder+outfile;
80 
81  // Checking if output file exists
82  TString outname(outfile);
83  outname.ReplaceAll(outfolder, ""); outname.ReplaceAll("/", "");
84  vector<TString> outfiles = dirlist(outfolder, outname);
85  if(outfiles.size()>0) {
86  cout<<"File "<<outfile<<" exists. Exiting"<<endl;
87  return;
88  }
89 
90  gSystem->mkdir(outfolder, kTRUE);
91  TFile out_rootfile(outfile, "CREATE");
92  if(out_rootfile.IsZombie() || !out_rootfile.IsOpen()) return;
93  out_rootfile.cd();
94  TChain tree("tree");
95  int nfiles = tree.Add(infiles);
96  TChain treeglobal("treeglobal");
97  treeglobal.Add(infiles);
98 
99  //cout<<"Skimming the "<<nfiles<<" files in "<<infiles<<endl;
100  long nentries(tree.GetEntries());
101  TTree *ctree = tree.CopyTree(cuts);
102  TTree *ctreeglobal = treeglobal.CopyTree("1");
103  if(ctree) ctree->Write();
104  else cout<<"Could not find tree in "<<infiles<<endl;
105  if(ctreeglobal) ctreeglobal->Write();
106  else cout<<"Could not find treeglobal in "<<infiles<<endl;
107  out_rootfile.Close();
108  cout<<"Written "<<outfile<<" from "<<nfiles<<" files and "<<nentries<<" entries."<<endl;
109 }
110 
string files
Definition: data_combine.py:33
string outfolder
Definition: data_combine.py:17
void onefile_skim(TString infiles, TString outfolder, TString cuts)
STL namespace.
std::vector< TString > dirlist(const TString &folder, const TString &inname="dir", const TString &tag="")
Definition: utilities.cpp:182
int main(int argc, char *argv[])