FairRoot/PandaRoot
production/scripts/compress.C
Go to the documentation of this file.
1 #include "TFile.h"
2 #include "TTree.h"
3 #include "TString.h"
4 #include "TEventList.h"
5 #include "TDirectory.h"
6 #include <iostream>
7 #include "TObjArray.h"
8 #include "TRegexp.h"
9 #include <vector>
10 
11 typedef std::vector<TString> StrVec;
12 
14 {
15  toks.clear();
16  TObjArray *tok = s.Tokenize(delim);
17  int N = tok->GetEntries();
18  for (int i=0;i<N;++i)
19  {
20  TString token = (((TObjString*)tok->At(i))->String()).Strip(TString::kBoth);
21  toks.push_back(token);
22  }
23  return toks.size();
24 }
25 
26 int compress(TString ntp="", TString fnamein="", TString fnameout="", TString bnames="", TString precut="" )
27 {
28  if (ntp=="" || fnamein=="" || fnameout=="")
29  {
30  cout <<"USAGE:\ncompress(TString tree, TString infile, TString outfile, TString bnames, TString precut)\n\n";
31  cout <<" tree : name of the TTree to be compressed.\n";
32  cout <<" infile : input file name.\n";
33  cout <<" outfile : output file name.\n";
34  cout <<" bnames : blank separated list of branch variable names to be copied; can make use of name* / *name /*name*; !(*)name(*) excludes variables (default = \"*\").\n";
35  cout <<" precut : cut to be applied before compressing branches (default = \"\"). Either bnames!=\"*\" or precut!=\"\".\n\n";
36 
37  return 0;
38  }
39 
40  if (bnames=="*" && precut=="")
41  {
42  cout <<"Nothing to compress. Exiting."<<endl;
43  return 0;
44  }
45 
46  TFile *fi = new TFile(fnamein,"READ");
47  TTree *ti = (TTree*) fi->Get(ntp);
48 
49  // do preselection
50  ti->Draw(">>el",precut);
51  TEventList *el = (TEventList*)gDirectory->Get("el");
52 
53  int N0 = ti->GetEntries();
54  int Ns = el->GetN();
55 
56  // Separate names of branches to be considered
57  StrVec bnam;
58  COMSplitString(bnames," ", bnam);
59 
60  // Enable/disable branches
61  ti->SetBranchStatus("*",0);
62  for (unsigned int i=0;i<bnam.size();++i)
63  if (bnam[i].BeginsWith("!")) {TString tmp = bnam[i](1,1000); ti->SetBranchStatus(tmp,0);}
64  else ti->SetBranchStatus(bnam[i],1);
65 
66  bnam.clear();
67  TObjArray* blist = ti->GetListOfBranches();
68  for(unsigned int i=0; i<=blist->GetLast(); ++i)
69  {
70  TString name = ((TBranch*)blist->UncheckedAt(i))->GetName();
71  if (ti->GetBranchStatus(name)>0) bnam.push_back(name);
72  }
73 
74  // print selected branches
75  cout<<"**** Pre cut : \"" << precut << "\"" << endl;
76  cout<<"**** Selected : " << Ns << " / "<< N0 << " entries"<< endl;
77  cout<<"**** Branch pattern: \"" << bnames << "\"" << endl;
78  cout<<"**** "<<bnam.size()<<" branches found: \n";
79  for (unsigned int i=0;i<bnam.size();++i) printf("%s ",bnam[i].Data());
80  cout <<endl<<endl;
81 
82  cout <<"Converting tree '"<<ntp<<"': "<<fnamein<<" ("<<ti->GetNbranches()<<" br, "<<ti->GetEntriesFast()<<" ev) ... "<<flush;
83 
84  ti->SetEventList(el);
85 
86  TFile *fo = new TFile(fnameout, "RECREATE");
87  TTree *to = ti->CopyTree("");
88 
89  cout <<"to "<<fnameout<<" ("<<to->GetNbranches()<<" br, "<<to->GetEntriesFast()<<" ev) "<<endl;
90 
91  fo->Write();
92  fo->Close();
93  fi->Close();
94 
95  return 0;
96 }
printf("RealTime=%f seconds, CpuTime=%f seconds\n", rtime, ctime)
std::vector< TString > StrVec
Int_t i
Definition: run_full.C:25
TLorentzVector s
Definition: Pnd2DStar.C:50
int Ns
Definition: findcuts.C:56
TFile * fi
std::vector< TString > StrVec
TString name
StrVec bnam
Definition: findcuts.C:62
int compress(TString ntp="", TString fnamein="", TString fnameout="", TString bnames="", TString precut="")
int COMSplitString(TString s, TString delim, StrVec &toks)