babymaker  e95a6a9342d4604277fe7cc6149b6b5b24447d89
skim_scan_onefile.cxx
Go to the documentation of this file.
1 // skim_scan_onefile.cxx: Separate signal scan by mass point
2 #include "utilities.hh"
3 
4 #include <fstream>
5 #include <iostream>
6 #include <cmath>
7 #include <string>
8 #include <sstream>
9 #include <vector>
10 #include <stdlib.h> /* atoi */
11 #include "TH2.h"
12 #include "TChain.h"
13 #include "TFile.h"
14 #include "TString.h"
15 #include "TMath.h"
16 #include "TSystem.h"
17 #include "TDirectory.h"
18 #include <ctime>
19 
20 using namespace std;
21 
22 int main(int argc, char *argv[]){
23  time_t startTime;
24  time(&startTime);
25 
26  if(argc < 1) {
27  cout<<endl<<"Requires 1 argument: "<<"./run/skim_scan_onefile.exe infile";
28  return 1;
29  }
30 
31  TString infiles(argv[1]);
32  infiles += "/*.root";
33  TString outpath = "";
34  if (argc>1) outpath = argv[2];
35  TChain tree("tree");
36  tree.Add(infiles);
37  TChain treeglobal("treeglobal");
38  treeglobal.Add(infiles);
39  //long nentries(tree.GetEntries());
40 
41  TString outfolder = outpath;
42  outfolder.Remove(outfolder.Last('/')+1, outfolder.Length());
43  if(outfolder!="") gSystem->mkdir(outfolder, kTRUE);
44 
45  //Project tree into mass plane to find points for skim
46  TH2F * mass_plane = new TH2F("mglu_vs_mlsp","mglu_vs_mlsp",3000,-0.5,2999.5,3000,-0.5,2999.5);
47  tree.Project("mglu_vs_mlsp","mgluino:mlsp","","colz");
48 
49  //find relevant range to loop over
50  int ini_x = mass_plane->FindFirstBinAbove(0,1);
51  int last_x = mass_plane->FindLastBinAbove(0,1);
52  int ini_y = mass_plane->FindFirstBinAbove(0,2);
53  int last_y = mass_plane->FindLastBinAbove(0,2);
54 
55  //load all pairs as cuts into vector
56  vector<TString> pair_cuts;
57  vector<TString> mass_tag;
58  int Npoints=0;
59  for(int iy=ini_y; iy<=last_y; iy++){
60  for(int ix=ini_x; ix<=last_x; ix++){
61  if(mass_plane->GetBinContent(ix,iy) > 0){
62  int mglui = static_cast<int>(mass_plane->GetYaxis()->GetBinCenter(iy));
63  int mlsp = static_cast<int>(mass_plane->GetXaxis()->GetBinCenter(ix));
64  pair_cuts.push_back(Form("mgluino==%i&&mlsp==%i",mglui,mlsp));
65  mass_tag.push_back(Form("mGluino-%i_mLSP-%i",mglui,mlsp));
66  cout<<"Found mass point "<<mass_tag.back()<<endl;
67  Npoints++;
68  }
69  }
70  }
71 
72  time_t curTime;
73  time(&curTime);
74  // char time_c[100];
75  //struct tm * timeinfo = localtime(&curTime);
76  //strftime(time_c,100,"%Y-%m-%d %H:%M:%S",timeinfo);
77  int seconds(floor(difftime(curTime,startTime)+0.5));
78  cout<<endl<<"Took "<<seconds<<" seconds to fill chain, project and find the "<<Npoints<<" mass pairs"<<endl<<endl;
79 
80  // Finding outfile names
81  for(unsigned int ip = 0; ip<pair_cuts.size(); ip++){
82  time_t startloop;
83  time(&startloop);
84  TString outfile = "";
85  if (outpath==""){
86  outfile=infiles;
87  //outfile.Remove(0, outfile.Last('/')); outfile.ReplaceAll("*","");
88  if(outfile.Contains(".root")) outfile.ReplaceAll(".root","_"+pair_cuts.at(ip)+".root");
89  else outfile += ("_"+pair_cuts.at(ip)+".root");
90  outfile.ReplaceAll(">=","ge"); outfile.ReplaceAll("<=","se"); outfile.ReplaceAll("&&","_");
91  outfile.ReplaceAll(">","g"); outfile.ReplaceAll("<","s"); outfile.ReplaceAll("=","");
92  outfile.ReplaceAll("(",""); outfile.ReplaceAll(")",""); outfile.ReplaceAll("+","");
93  outfile.ReplaceAll("[",""); outfile.ReplaceAll("]",""); outfile.ReplaceAll("|","_");
94  outfile.ReplaceAll("/","");
95  //outfile = outfolder+outfile;
96  } else {
97  outfile = outpath;
98  outfile.ReplaceAll("MASS_TAG",mass_tag[ip]);
99  }
100 
101  TFile out_rootfile(outfile.Data(), "RECREATE");
102  if(out_rootfile.IsZombie() || !out_rootfile.IsOpen()) return 1;
103  out_rootfile.cd();
104 
105  //cout<<"Skimming the "<<nfiles<<" files in "<<infiles<<endl;
106  TTree * ctree = tree.CopyTree(pair_cuts.at(ip));
107  TTree * ctreeglobal = treeglobal.CopyTree("1");
108  if(ctree) ctree->Write();
109  else cout<<"Could not find tree in "<<infiles<<endl;
110  if(ctreeglobal) ctreeglobal->Write();
111  else cout<<"Could not find treeglobal in "<<infiles<<endl;
112 
113  time_t endloop;
114  time(&endloop);
115  int secs(floor(difftime(endloop,startloop)+0.5));
116  cout<<"Written "<<outfile<<" with "<<ctree->GetEntries()<<" entries in "<<secs<<" seconds"<<endl;
117  out_rootfile.Close();
118  }
119  time(&curTime);
120  cout<<"Took "<< difftime(curTime,startTime)<<" seconds to skim "<< pair_cuts.size()<<" files."<<endl;
121 }
122 
tuple infiles
Finding tags for each dataset.
int main(int argc, char *argv[])
STL namespace.
tuple outpath
Definition: sub_cond.py:250