20 #include "TDirectory.h" 28 int main(
int argc,
char *argv[]){
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;;
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);
40 njobs = atoi(argv[4]);
44 unsigned nfiles(files.size()), ini(0), end(nfiles);
46 if(ijob<1 || ijob>njobs){
47 cout<<endl<<
"You need to set the 5th argument between 1 and "<<njobs<<endl<<endl;
50 unsigned jobfiles = (nfiles+njobs-1)/njobs;
51 unsigned nbigjobs = (nfiles+njobs-1)%njobs+1;
53 ini = jobfiles*(ijob-1);
56 ini = nbigjobs*jobfiles+(jobfiles-1)*(ijob-1-nbigjobs);
57 end = ini + jobfiles-1;
60 cout<<
"Doing files "<<ini+1<<
" to "<<end<<
" out of "<<nfiles<<endl;
61 for(
unsigned file(ini); file < end; file++){
68 TString folder(infiles), outfile(infiles);
69 folder.Remove(folder.Last(
'/')+1, folder.Length());
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;
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;
90 gSystem->mkdir(outfolder, kTRUE);
91 TFile out_rootfile(outfile,
"CREATE");
92 if(out_rootfile.IsZombie() || !out_rootfile.IsOpen())
return;
95 int nfiles = tree.Add(infiles);
96 TChain treeglobal(
"treeglobal");
97 treeglobal.Add(infiles);
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;
void onefile_skim(TString infiles, TString outfolder, TString cuts)
std::vector< TString > dirlist(const TString &folder, const TString &inname="dir", const TString &tag="")
int main(int argc, char *argv[])