FairRoot/PandaRoot
TMVAApply.C
Go to the documentation of this file.
1 #include "TMVA/Reader.h"
2 #include "TFile.h"
3 #include "TTree.h"
4 #include "TString.h"
5 #include "TRegexp.h"
6 #include "TEventList.h"
7 #include "TLeaf.h"
8 
9 #include <iostream>
10 #include <map>
11 #include <vector>
12 #include <algorithm>
13 #include <utility>
14 
15 #define MAX 1000
16 
17 Float_t fbranch[MAX], freader[MAX];
18 Int_t ibranch[MAX], types[MAX];
20 
21 Int_t ev, run, mode, rec, nbranch;
22 
23 typedef std::map<int, int> CountMap;
24 typedef std::vector<pair<float, int> > ValueMap;
25 
26 // ---------------------------------------------------------------
27 
28 int gettype(TTree *t, TString varname)
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 }
40 
41 // ---------------------------------------------------------------
42 
43 int SplitString(TString s, TString delim, TString *toks, int maxtoks)
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 }
56 
57 // ---------------------------------------------------------------
58 
59 int init(TTree *t, TString vars)
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 }
84 
85 // ---------------------------------------------------------------
86 
87 int uid(int lev, int lrun, int lmode)
88 {
89  return lev+10000*lrun+(((lmode/100)%10)*20+lmode%10)*100000;
90 }
91 
92 // ---------------------------------------------------------------
93 
94 int countEvents(TTree *t, TEventList &el)
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 }
113 
114 // ---------------------------------------------------------------
115 
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 }
138 // ---------------------------------------------------------------
139 
140 int TMVAApply(TString fname, TString vars, TString precut="", TString wfile="", Float_t bglevel=0.0001)
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 }
242 
243 
printf("RealTime=%f seconds, CpuTime=%f seconds\n", rtime, ctime)
Int_t run
Definition: autocutx.C:47
Int_t res
Definition: anadigi.C:166
Int_t i
Definition: run_full.C:25
TTree * b
#define MAX
Definition: TMVAApply.C:15
TLorentzVector s
Definition: Pnd2DStar.C:50
int ev
int n
int TMVAApply(TString fname, TString vars, TString precut="", TString wfile="", Float_t bglevel=0.0001)
Definition: TMVAApply.C:140
TString getFromCut(TString vars)
Definition: TMVAApply.C:116
__m128 v
Definition: P4_F32vec4.h:4
static int init
Definition: ranlxd.cxx:374
double cut[MAX]
Definition: autocutx.C:36
Int_t mode
Definition: autocutx.C:47
int uid(int lev, int lrun, int lmode)
Definition: autocutx.C:122
std::vector< pair< double, int > > ValueMap
Definition: autocutx.C:28
Float_t fbranch[MAX]
Definition: autocutx.C:44
Float_t freader[MAX]
Definition: TMVAApply.C:17
int gettype(TTree *t, TString varname)
Definition: autocutx.C:51
TString vars[MAX]
Definition: autocutx.C:34
TFile * f
Definition: bump_analys.C:12
Bool_t bbranch[MAX]
Definition: autocutx.C:46
int SplitString(TString s, TString delim, StrVec &toks)
Definition: invexp.C:43
Int_t types[MAX]
Definition: TMVAApply.C:18
Int_t nbranch
Definition: autocutx.C:47
int nsig
Definition: toy_core.C:46
std::map< int, int > CountMap
Definition: autocutx.C:29
int countEvents(TTree *t, TEventList &el)
Definition: autocutx.C:129
TTree * t
Definition: bump_analys.C:13
Int_t ibranch[MAX]
Definition: autocutx.C:45
Int_t rec
Definition: autocutx.C:47
CountMap evcnt
Definition: autocutx.C:31