FairRoot/PandaRoot
examples/analysistools/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;
38  }
39 
40  if (bnames=="*" && precut=="")
41  {
42  cout <<"Nothing to compress. Exiting."<<endl;
43  return;
44  }
45 
46  // do preselection
47  TFile *fi = new TFile(fnamein,"READ");
48  TTree *ti = (TTree*) fi->Get(ntp);
49  ti->Draw(">>el",precut);
50 
51  // Separate names of branches to be considered
52  StrVec bnam;
53  COMSplitString(bnames," ", bnam);
54 
55  // Enable/disable branches
56  ti->SetBranchStatus("*",0);
57  for (uint i=0;i<bnam.size();++i)
58  if (bnam[i].BeginsWith("!")) ti->SetBranchStatus(TString(bnam[i](1,1000)),0);
59  else ti->SetBranchStatus(bnam[i],1);
60 
61  bnam.clear();
62  TObjArray* blist = ti->GetListOfBranches();
63  for(int i=0; i<=blist->GetLast(); ++i)
64  {
65  TString name = ((TBranch*)blist->UncheckedAt(i))->GetName();
66  if (ti->GetBranchStatus(name)>0) bnam.push_back(name);
67  }
68 
69  // print selected branches
70  cout<<"**** Branch pattern: \""<<bnames<<"\""<<endl;
71  cout<<"**** "<<bnam.size()<<" branches found: \n";
72  for (uint i=0;i<bnam.size();++i) printf("%s ",bnam[i].Data());
73  cout <<endl<<endl;
74 
75  cout <<"Converting tree '"<<ntp<<"': "<<fnamein<<" ("<<ti->GetNbranches()<<" br, "<<ti->GetEntriesFast()<<" ev) ... "<<flush;
76 
77  TEventList *el = (TEventList*)gDirectory->Get("el");
78  ti->SetEventList(el);
79 
80  TFile *fo = new TFile(fnameout, "RECREATE");
81  TTree *to = ti->CopyTree("");
82 
83  cout <<"to "<<fnameout<<" ("<<to->GetNbranches()<<" br, "<<to->GetEntriesFast()<<" ev) "<<endl;
84 
85  fo->Write();
86  fo->Close();
87  fi->Close();
88  return 0;
89 }
printf("RealTime=%f seconds, CpuTime=%f seconds\n", rtime, ctime)
Int_t i
Definition: run_full.C:25
int compress(TString ntp="", TString fnamein="", TString fnameout="", TString bnames="", TString precut="")
TLorentzVector s
Definition: Pnd2DStar.C:50
TFile * fi
std::vector< TString > StrVec
TString name
StrVec bnam
Definition: findcuts.C:62
int COMSplitString(TString s, TString delim, StrVec &toks)