babymaker  e95a6a9342d4604277fe7cc6149b6b5b24447d89
skim_scan.cxx
Go to the documentation of this file.
1 // skim_scan.cxx: Separate signal scan by mass point
2 // USAGE: ./plot/skim_ntuples.exe infolder outfolder [cuts=\"ht>500&&met>200\"] [njobs=-1] [ijob=-1]
3 
4 
5 #include "utilities.hh"
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 #include "TH2.h"
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=\"\"] "
33  <<"[njobs=-1] [ijob=-1]"<<endl<<endl;;
34  return 1;
35  }
36  TString folder(argv[1]), outfolder(argv[2]), cuts="";
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  TChain tree("tree");
69  int nfiles = tree.Add(infiles);
70  TChain treeglobal("treeglobal");
71  treeglobal.Add(infiles);
72 
73  //Project tree into mass plane to find points for skim
74  TH2F * mass_plane = new TH2F("mglu_vs_mlsp","mglu_vs_mlsp",3000,-0.5,2999.5,3000,-0.5,2999.5);
75  tree.Project("mglu_vs_mlsp","mgluino:mlsp","","colz");
76 
77  //find relevant range to loop over
78  int ini_x = mass_plane->FindFirstBinAbove(0,1);
79  int last_x = mass_plane->FindLastBinAbove(0,1);
80  int ini_y = mass_plane->FindFirstBinAbove(0,2);
81  int last_y = mass_plane->FindLastBinAbove(0,2);
82 
83  //load all pairs as cuts into vector
84  vector<TString> pair_cuts;
85  for(int iy=ini_y; iy<=last_y; iy++){
86  for(int ix=ini_x; ix<=last_x; ix++){
87  if(mass_plane->GetBinContent(ix,iy) > 0){
88  int mglui = static_cast<int>(mass_plane->GetYaxis()->GetBinCenter(iy));
89  int mlsp = static_cast<int>(mass_plane->GetXaxis()->GetBinCenter(ix));
90  if(cuts =="") pair_cuts.push_back(Form("mgluino==%i&&mlsp==%i",mglui,mlsp));
91  else pair_cuts.push_back(Form("mgluino==%i&&mlsp==%i&&",mglui,mlsp)+cuts);
92  }
93  }
94  }
95 
96 
97  TString folder(infiles);
98  vector<TString > outfiles;
99  folder.Remove(folder.Last('/')+1, folder.Length());
100  gSystem->mkdir(outfolder, kTRUE);
101  // Finding outfile names
102  for(unsigned int ip = 0; ip<pair_cuts.size(); ip++){
103 
104  TString outfile=infiles;
105  outfile.Remove(0, outfile.Last('/')); outfile.ReplaceAll("*","");
106  if(outfile.Contains(".root")) outfile.ReplaceAll(".root","_"+pair_cuts.at(ip)+".root");
107  else outfile += ("_"+pair_cuts.at(ip)+".root");
108  outfile.ReplaceAll(">=","ge"); outfile.ReplaceAll("<=","se"); outfile.ReplaceAll("&&","_");
109  outfile.ReplaceAll(">","g"); outfile.ReplaceAll("<","s"); outfile.ReplaceAll("=","");
110  outfile.ReplaceAll("(",""); outfile.ReplaceAll(")",""); outfile.ReplaceAll("+","");
111  outfile.ReplaceAll("[",""); outfile.ReplaceAll("]",""); outfile.ReplaceAll("|","_");
112  outfile = outfolder+outfile;
113 
114  //cout<<"outfile is"<<outfile<<endl;
115  // Checking if output file exists
116  TString outname(outfile);
117  outname.ReplaceAll(outfolder, ""); outname.ReplaceAll("/", "");
118  vector<TString> existing_outfiles = dirlist(outfolder, outname);
119  if(existing_outfiles.size()>0) {
120  cout<<"File "<<outfile<<" exists. Skipping"<<endl;
121  continue;
122  }
123 
124 
125  // cout<<"creating outfile"<<endl;
126  TFile out_rootfile(outfile, "CREATE");
127  if(out_rootfile.IsZombie() || !out_rootfile.IsOpen()) return;
128  out_rootfile.cd();
129 
130 
131  //cout<<"Skimming the "<<nfiles<<" files in "<<infiles<<endl;
132  long nentries(tree.GetEntries());
133  TTree * ctree = tree.CopyTree(pair_cuts.at(ip));
134  TTree * ctreeglobal = treeglobal.CopyTree("1");
135  if(ctree) ctree->Write();
136  else cout<<"Could not find tree in "<<infiles<<endl;
137  if(ctreeglobal) ctreeglobal->Write();
138  else cout<<"Could not find treeglobal in "<<infiles<<endl;
139  cout<<"Written "<<outfile<<" from "<<nfiles<<" files and "<<nentries<<" entries."<<endl;
140  out_rootfile.Close();
141 
142  }
143 }
144 
tuple infiles
Finding tags for each dataset.
STL namespace.
int main(int argc, char *argv[])
Definition: skim_scan.cxx:28
void onefile_skim(TString infiles, TString outfolder, TString cuts)
Definition: skim_scan.cxx:67
std::vector< TString > dirlist(const TString &folder, const TString &inname="dir", const TString &tag="")
Definition: utilities.cc:287