FairRoot/PandaRoot
Macros | Typedefs | Functions | Variables
TMVAApply.C File Reference
#include "TMVA/Reader.h"
#include "TFile.h"
#include "TTree.h"
#include "TString.h"
#include "TRegexp.h"
#include "TEventList.h"
#include "TLeaf.h"
#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
#include <utility>

Go to the source code of this file.

Macros

#define MAX   1000
 

Typedefs

typedef std::map< int, int > CountMap
 
typedef std::vector< pair
< float, int > > 
ValueMap
 

Functions

int gettype (TTree *t, TString varname)
 
int SplitString (TString s, TString delim, TString *toks, int maxtoks)
 
int init (TTree *t, TString vars)
 
int uid (int lev, int lrun, int lmode)
 
int countEvents (TTree *t, TEventList &el)
 
TString getFromCut (TString vars)
 
int TMVAApply (TString fname, TString vars, TString precut="", TString wfile="", Float_t bglevel=0.0001)
 

Variables

Float_t fbranch [MAX]
 
Float_t freader [MAX]
 
Int_t ibranch [MAX]
 
Int_t types [MAX]
 
Bool_t bbranch [MAX]
 
Int_t ev
 
Int_t run
 
Int_t mode
 
Int_t rec
 
Int_t nbranch
 

Macro Definition Documentation

#define MAX   1000

Definition at line 15 of file TMVAApply.C.

Typedef Documentation

typedef std::map<int, int> CountMap

Definition at line 23 of file TMVAApply.C.

typedef std::vector<pair<float, int> > ValueMap

Definition at line 24 of file TMVAApply.C.

Function Documentation

int countEvents ( TTree *  t,
TEventList &  el 
)

Definition at line 94 of file TMVAApply.C.

References ev, evcnt, i, mode, run, and uid().

Referenced by TMVAApply().

95 {
96  t->SetBranchStatus("*",0);
97  t->SetBranchStatus("ev",1);
98  t->SetBranchStatus("run",1);
99  t->SetBranchStatus("mode",1);
100  t->SetBranchStatus("recmode",1);
101 
102  CountMap evcnt;
103 
104  for (int i=0;i<el.GetN();++i)
105  {
106  t->GetEntry(el.GetEntry(i));
107  evcnt[uid(ev,run,mode)]+=1;
108  }
109  t->SetBranchStatus("*",1);
110 
111  return evcnt.size();
112 }
Int_t i
Definition: run_full.C:25
int uid(int lev, int lrun, int lmode)
Definition: TMVAApply.C:87
Int_t run
Definition: TMVAApply.C:21
Int_t ev
Definition: TMVAApply.C:21
std::map< int, int > CountMap
Definition: autocutx.C:29
TTree * t
Definition: bump_analys.C:13
CountMap evcnt
Definition: autocutx.C:31
Int_t mode
Definition: TMVAApply.C:21
TString getFromCut ( TString  vars)

Definition at line 116 of file TMVAApply.C.

References i, n, res, SplitString(), TString, and v.

Referenced by TMVAApply().

117 {
118  TString toks[50];
119  int n=SplitString(vars, "&&", toks, 50);
120  TRegexp rvar("[_a-zA-Z][_a-zA-Z0-9]*");
121 
122  TString res=" ";
123 
124  for (int i=0;i<n;++i)
125  {
126  TString v = toks[i](rvar);
127  if (v!="")
128  {
129  if (v=="tag" || res.Contains(" "+v+" ")) continue;
130  res+=v+" ";
131  }
132  }
133 
134  res = res.Strip(TString::kBoth);
135 
136  return res;
137 }
Int_t res
Definition: anadigi.C:166
Int_t i
Definition: run_full.C:25
int n
__m128 v
Definition: P4_F32vec4.h:4
TString vars[MAX]
Definition: autocutx.C:34
int SplitString(TString s, TString delim, TString *toks, int maxtoks)
Definition: TMVAApply.C:43
int gettype ( TTree *  t,
TString  varname 
)

Definition at line 28 of file TMVAApply.C.

References TString.

Referenced by init().

29 {
30  if (t->GetBranch(varname)==0) return -1;
31 
32  TString leaftype = t->GetLeaf(varname)->GetTypeName();
33 
34  if (leaftype=="Float_t") return 0;
35  else if (leaftype=="Int_t") return 1;
36  else if (leaftype=="Bool_t") return 2;
37 
38  return -1;
39 }
TTree * t
Definition: bump_analys.C:13
int init ( TTree *  t,
TString  vars 
)

Definition at line 59 of file TMVAApply.C.

References bbranch, ev, fbranch, gettype(), i, ibranch, mode, rec, run, SplitString(), TString, types, and v.

Referenced by TMVAApply().

60 {
61  t->SetBranchAddress("ev",&ev);
62  t->SetBranchAddress("run",&run);
63  t->SetBranchAddress("mode",&mode);
64  t->SetBranchAddress("recmode",&rec);
65 
66  TString toks[30];
67  int N = SplitString(vars," ",toks,30);
68 
69  for (int i=0;i<N;++i)
70  {
71  TString v = toks[i];
72  types[i] = gettype(t, v);
73  switch (types[i])
74  {
75  case 0: t->SetBranchAddress(v, &(fbranch[i])); break;
76  case 1: t->SetBranchAddress(v, &(ibranch[i])); break;
77  case 2: t->SetBranchAddress(v, &(bbranch[i])); break;
78  }
79  //cout <<toks[i]<<" of type "<<types[i]<<endl;
80  }
81 
82  return N;
83 }
Int_t i
Definition: run_full.C:25
Bool_t bbranch[MAX]
Definition: TMVAApply.C:19
Float_t fbranch[MAX]
Definition: TMVAApply.C:17
__m128 v
Definition: P4_F32vec4.h:4
Int_t ibranch[MAX]
Definition: TMVAApply.C:18
int gettype(TTree *t, TString varname)
Definition: TMVAApply.C:28
Int_t run
Definition: TMVAApply.C:21
Int_t rec
Definition: TMVAApply.C:21
TString vars[MAX]
Definition: autocutx.C:34
Int_t ev
Definition: TMVAApply.C:21
Int_t types[MAX]
Definition: TMVAApply.C:18
TTree * t
Definition: bump_analys.C:13
int SplitString(TString s, TString delim, TString *toks, int maxtoks)
Definition: TMVAApply.C:43
Int_t mode
Definition: TMVAApply.C:21
int SplitString ( TString  s,
TString  delim,
TString toks,
int  maxtoks 
)

Definition at line 43 of file TMVAApply.C.

References i.

Referenced by getFromCut(), init(), and TMVAApply().

44 {
45  TObjArray *tok = s.Tokenize(delim);
46  int N = tok->GetEntries();
47  for (int i=0;i<N;++i)
48  if (i<maxtoks)
49  {
50  toks[i] = ((TObjString*)tok->At(i))->String();
51  toks[i].ReplaceAll("\t","");
52  toks[i] = toks[i].Strip(TString::kBoth);
53  }
54  return N;
55 }
Int_t i
Definition: run_full.C:25
TLorentzVector s
Definition: Pnd2DStar.C:50
int TMVAApply ( TString  fname,
TString  vars,
TString  precut = "",
TString  wfile = "",
Float_t  bglevel = 0.0001 
)

Definition at line 140 of file TMVAApply.C.

References b, bbranch, countEvents(), cut, ev, f, fbranch, freader, getFromCut(), i, ibranch, init(), mode, nsig, printf(), run, s, SplitString(), t, TString, types, and uid().

141 {
142  if (vars.Contains("&&")) vars = getFromCut(vars);
143  cout <<"Vars : "<<vars<<endl;
144 
145  TString sigcut = "tag&&mode%1000!=900";
146  TString bkgcut = "tag&&mode%1000==900";
147 
148  if (precut!="")
149  {
150  cout <<"Precut : "<<precut<<endl;
151  sigcut += "&&" + precut;
152  bkgcut += "&&" + precut;
153  }
154 
155  // determine ntp name and S0 and B0
156  TRegexp rmode("M[0-9][0-9][0-9]");
157  TRegexp rntp("n[0-9][0-9][0-9]");
158  TRegexp regS("[0-9]+S");
159  TRegexp regB("[0-9]+B");
160 
161  TString s = fname(regS); s = s(0,s.Length()-1);
162  TString b = fname(regB); b = b(0,b.Length()-1);
163  int n0s = s.Atoi()*10000;
164  int n0b = b.Atoi()*10000;
165  TString treename = fname(rntp);
166  TString smode = fname(rmode);
167 
168  if (wfile=="") wfile="weights/M"+smode(1,3)+treename(1,3)+"_BDT.weights.xml";
169 
170  TFile *f = TFile::Open(fname);
171  TTree *t =(TTree*) f->Get(treename);
172 
173  // prepare variables and branch mapping
174  int Nbr = init(t,vars);
175  TString varname[30];
176  SplitString(vars," ",varname,30);
177 
178  // prepare TMVA reader
179  TMVA::Reader *reader = new TMVA::Reader("Silent");
180  for (int i=0;i<Nbr;++i) reader->AddVariable(varname[i], &freader[i]);
181  reader->BookMVA("BDT",wfile);
182 
183  // initial number of S and B events
184  TEventList elsall("elsall"), elball("elball");
185  t->Draw(">>elsall",sigcut);
186  t->Draw(">>elball",bkgcut);
187  float nsig = countEvents(t,elsall);
188  float nbkg = countEvents(t,elball);
189 
190  // apply tag cut
191  TString tagcut="tag";
192  if (precut!="") tagcut+="&&"+precut;
193  t->Draw(">>el",tagcut);
194  TEventList *el=(TEventList*)gDirectory->Get("el");
195 
196  int N = el->GetN();
197 
198  CountMap sigcnt, bkgcnt;
199  ValueMap sigvals, bkgvals;
200 
201  // loop through TTree
202  for (int i=0;i<N;++i)
203  {
204  t->GetEntry(el->GetEntry(i));
205 
206  if (i%10000==0) cout <<"#"<<flush;
207 
208  for (int j=0;j<Nbr;++j)
209  {
210  Float_t var=0;
211  switch (types[j]) {case 2: var = (Float_t)bbranch[j]; break; case 1: var = (Float_t) ibranch[j]; break; default: var=fbranch[j]; }
212  freader[j] = var;
213  }
214 
215  float mvares = reader->EvaluateMVA("BDT");
216 
217  if (mode%1000==900)
218  bkgvals.push_back( std::make_pair(mvares, uid(ev,run,mode) ));
219  else
220  sigvals.push_back( std::make_pair(mvares, uid(ev,run,mode) ));
221  }
222 
223  sort(bkgvals.begin(), bkgvals.end());
224 
225  int nbgmax = bglevel*n0b, i = bkgvals.size()-1;
226  while ((int)bkgcnt.size()<nbgmax && i>=0) bkgcnt[bkgvals[i--].second]+=1;
227  double cut = bkgvals[i].first;
228 
229  for (i=0;i<(int)sigvals.size();++i)
230  if (sigvals[i].first>cut) sigcnt[sigvals[i].second]+=1;
231  cout <<endl;
232  printf("S : eff = %0.3f N = %5d / %5d N0 = %d\n", (double)sigcnt.size()/n0s, (int)sigcnt.size(), (int)nsig, n0s);
233  printf("B : eff = %0.5f N = %5d / %5d N0 = %d\n", (double)bkgcnt.size()/n0b, (int)bkgcnt.size(), (int)nbkg, n0b);
234  cout <<"\nCUT = "<<cut<<endl;
235 
236  if (precut=="") precut="tag";
237  else precut="tag&&"+precut;
238  TString cfgline = smode(1,3)+treename(1,3)+" : "+precut+" : M"+smode(1,3)+treename(1,3)+"_BDT "+vars+" "+TString::Format("%f",cut);
239  cout <<"cfgline -> "<<cfgline<<endl;
240  return 0;
241 }
printf("RealTime=%f seconds, CpuTime=%f seconds\n", rtime, ctime)
Int_t i
Definition: run_full.C:25
TTree * b
TLorentzVector s
Definition: Pnd2DStar.C:50
Bool_t bbranch[MAX]
Definition: TMVAApply.C:19
TString getFromCut(TString vars)
Definition: TMVAApply.C:116
int uid(int lev, int lrun, int lmode)
Definition: TMVAApply.C:87
Float_t fbranch[MAX]
Definition: TMVAApply.C:17
Int_t ibranch[MAX]
Definition: TMVAApply.C:18
double cut[MAX]
Definition: autocutx.C:36
std::vector< pair< double, int > > ValueMap
Definition: autocutx.C:28
Int_t run
Definition: TMVAApply.C:21
Float_t freader[MAX]
Definition: TMVAApply.C:17
TString vars[MAX]
Definition: autocutx.C:34
int countEvents(TTree *t, TEventList &el)
Definition: TMVAApply.C:94
Int_t ev
Definition: TMVAApply.C:21
TFile * f
Definition: bump_analys.C:12
Int_t types[MAX]
Definition: TMVAApply.C:18
int nsig
Definition: toy_core.C:46
std::map< int, int > CountMap
Definition: autocutx.C:29
int init(TTree *t, TString vars)
Definition: TMVAApply.C:59
TTree * t
Definition: bump_analys.C:13
int SplitString(TString s, TString delim, TString *toks, int maxtoks)
Definition: TMVAApply.C:43
Int_t mode
Definition: TMVAApply.C:21
int uid ( int  lev,
int  lrun,
int  lmode 
)

Definition at line 87 of file TMVAApply.C.

Referenced by countEvents(), and TMVAApply().

88 {
89  return lev+10000*lrun+(((lmode/100)%10)*20+lmode%10)*100000;
90 }

Variable Documentation

Bool_t bbranch[MAX]

Definition at line 19 of file TMVAApply.C.

Referenced by init(), and TMVAApply().

Int_t ev

Definition at line 21 of file TMVAApply.C.

Referenced by countEvents(), init(), and TMVAApply().

Float_t fbranch[MAX]

Definition at line 17 of file TMVAApply.C.

Referenced by init(), and TMVAApply().

Float_t freader[MAX]

Definition at line 17 of file TMVAApply.C.

Referenced by TMVAApply().

Int_t ibranch[MAX]

Definition at line 18 of file TMVAApply.C.

Referenced by init(), and TMVAApply().

Int_t mode

Definition at line 21 of file TMVAApply.C.

Referenced by countEvents(), init(), and TMVAApply().

Int_t nbranch

Definition at line 21 of file TMVAApply.C.

Int_t rec

Definition at line 21 of file TMVAApply.C.

Referenced by init().

Int_t run

Definition at line 21 of file TMVAApply.C.

Referenced by countEvents(), init(), and TMVAApply().

Int_t types[MAX]

Definition at line 18 of file TMVAApply.C.

Referenced by init(), and TMVAApply().