FairRoot/PandaRoot
Macros | Typedefs | Functions | Variables
thailand2017/TMVATester.C File Reference
#include "TMVA/Reader.h"
#include "TFile.h"
#include "TTree.h"
#include "TNtuple.h"
#include "TString.h"
#include "TRegexp.h"
#include "TEventList.h"
#include "TLeaf.h"
#include "TCanvas.h"
#include "TH1F.h"
#include "TGraph.h"
#include "TSystem.h"
#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
#include <utility>

Go to the source code of this file.

Macros

#define MAX   1000
 
#define TMVAgettype
 

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)
 
TString getFromCut (TString vars)
 
int init (TTree *t, TString vars)
 
TString getFromWeightFile (TString wfile)
 
void TMVATester (TString fname="", TString treename="", TString sigcut="", TString wfile="", TString precut="")
 

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 20 of file thailand2017/TMVATester.C.

#define TMVAgettype

Definition at line 33 of file thailand2017/TMVATester.C.

Typedef Documentation

typedef std::map<int, int> CountMap

Definition at line 28 of file thailand2017/TMVATester.C.

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

Definition at line 29 of file thailand2017/TMVATester.C.

Function Documentation

TString getFromCut ( TString  vars)

Definition at line 63 of file thailand2017/TMVATester.C.

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

Referenced by TMVATester().

64 {
65  TString toks[50];
66  int n=SplitString(vars, "&&", toks, 50);
67  TRegexp rvar("[_a-zA-Z][_a-zA-Z0-9]*");
68  cout <<"n="<<n<<endl;
69  TString res="";
70 
71  for (int i=0;i<n;++i)
72  {
73  TString v = toks[i](rvar);
74  if (v!="")
75  {
76  if (v=="tag" || res.Contains(" "+v+" ")) continue;
77  res+=v+" ";
78  }
79  }
80 
81  res = res.Strip(TString::kBoth);
82 
83  return res;
84 }
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)
TString getFromWeightFile ( TString  wfile)

Definition at line 111 of file thailand2017/TMVATester.C.

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

Referenced by TMVATester().

112 {
113  TString tmp=gSystem->GetFromPipe("grep Expression "+wfile);
114  TString v[30];
115  int n=SplitString(tmp,"/>",v,30);
116  TString res="";
117  TRegexp r("Expression=\"[_a-zA-Z0-9]+\"");
118 
119  for (int i=0;i<n;++i)
120  {
121  TString exp = (v[i])(r);
122  exp.ReplaceAll("Expression=","");
123  exp.ReplaceAll("\"","");
124  res+=exp+" ";
125  }
126 
127  res = res.Strip(TString::kBoth);
128  return res;
129 }
double r
Definition: RiemannTest.C:14
friend F32vec4 exp(const F32vec4 &a)
Definition: P4_F32vec4.h:109
Int_t res
Definition: anadigi.C:166
Int_t i
Definition: run_full.C:25
int n
__m128 v
Definition: P4_F32vec4.h:4
int SplitString(TString s, TString delim, TString *toks, int maxtoks)
int gettype ( TTree *  t,
TString  varname 
)

Definition at line 34 of file thailand2017/TMVATester.C.

References TString.

Referenced by init().

35 {
36  if (t->GetBranch(varname)==0) return -1;
37 
38  TString leaftype = t->GetLeaf(varname)->GetTypeName();
39 
40  if (leaftype=="Float_t") return 0;
41  else if (leaftype=="Int_t") return 1;
42  else if (leaftype=="Bool_t") return 2;
43 
44  return -1;
45 }
TTree * t
Definition: bump_analys.C:13
int init ( TTree *  t,
TString  vars 
)

Definition at line 89 of file thailand2017/TMVATester.C.

References bbranch, fbranch, gettype(), i, ibranch, SplitString(), TString, types, and v.

Referenced by TMVATester().

90 {
91  TString toks[30];
92  int N = SplitString(vars," ",toks,30);
93 
94  for (int i=0;i<N;++i)
95  {
96  TString v = toks[i];
97  types[i] = gettype(t, v);
98  switch (types[i])
99  {
100  case 0: t->SetBranchAddress(v, &(fbranch[i])); break;
101  case 1: t->SetBranchAddress(v, &(ibranch[i])); break;
102  case 2: t->SetBranchAddress(v, &(bbranch[i])); break;
103  }
104  //cout <<toks[i]<<" of type "<<types[i]<<endl;
105  }
106 
107  return N;
108 }
Int_t i
Definition: run_full.C:25
Int_t types[MAX]
__m128 v
Definition: P4_F32vec4.h:4
Float_t fbranch[MAX]
int gettype(TTree *t, TString varname)
Bool_t bbranch[MAX]
TString vars[MAX]
Definition: autocutx.C:34
int SplitString(TString s, TString delim, TString *toks, int maxtoks)
TTree * t
Definition: bump_analys.C:13
Int_t ibranch[MAX]
int SplitString ( TString  s,
TString  delim,
TString toks,
int  maxtoks 
)

Definition at line 48 of file thailand2017/TMVATester.C.

References i.

Referenced by getFromCut(), getFromWeightFile(), init(), and TMVATester().

49 {
50  TObjArray *tok = s.Tokenize(delim);
51  int N = tok->GetEntries();
52  for (int i=0;i<N;++i)
53  if (i<maxtoks)
54  {
55  toks[i] = ((TObjString*)tok->At(i))->String();
56  toks[i].ReplaceAll("\t","");
57  toks[i] = toks[i].Strip(TString::kBoth);
58  }
59  return N;
60 }
Int_t i
Definition: run_full.C:25
TLorentzVector s
Definition: Pnd2DStar.C:50
void TMVATester ( TString  fname = "",
TString  treename = "",
TString  sigcut = "",
TString  wfile = "",
TString  precut = "" 
)

Definition at line 133 of file thailand2017/TMVATester.C.

References bbranch, c1, elb, els, f, fbranch, freader, getFromCut(), getFromWeightFile(), i, ibranch, init(), leg, max(), Nb, Ns, SplitString(), t, TString, types, and vars.

134 {
135  TString vars="";
136 
137  if ( fname=="" || treename=="" || wfile=="" || sigcut=="" )
138  {
139  cout << "USAGE:\n";
140  cout << "TMVATester.C( <input>, <tree>, <sigcut>, <weights>, [precut] )\n\n";
141  cout << " <input> : input file name containing TTree <tree>\n";
142  cout << " <tree> : name of the TTree containing signal and background\n";
143  cout << " <sigcut> : cut separating signal from background -> bgcut = !(sigcut)\n";
144  cout << " <weights> : file containing the weights from training, usually stored in weights/...\n";
145  cout << " [precut] : optional precut before training; should be the same as for training!'\n\n";
146  cout << "EXAMPLE:\n";
147  cout << "root -l 'TMVATester.C(\"demodata.root\",\"ntp\",\"signal>0\",\"weights/demodata_ntp_MLP.weights.xml\",\"\")'\n\n";
148  return;
149  }
150 
151  if (vars.Contains("&&")) vars = getFromCut(vars);
152  if (vars=="") vars = getFromWeightFile(wfile);
153  cout <<"Vars : "<<vars<<endl;
154 
155  TString bkgcut="!("+sigcut+")";
156 
157  if (precut!="")
158  {
159  cout <<"Precut : "<<precut<<endl;
160  sigcut += "&&" + precut;
161  bkgcut += "&&" + precut;
162  }
163 
164  TFile *f = TFile::Open(fname);
165  TTree *t =(TTree*) f->Get(treename);
166 
167  // prepare variables and branch mapping
168  int Nbr = init(t,vars);
169  TString varname[30];
170  SplitString(vars," ",varname,30);
171 
172  // prepare TMVA reader
173  TMVA::Reader *reader = new TMVA::Reader("Silent");
174  for (int i=0;i<Nbr;++i) reader->AddVariable(varname[i], &freader[i]);
175 
176  TString mvatype="";
177 
178  if (wfile.Contains("BDT")) mvatype = "BDT";
179  else if (wfile.Contains("MLP")) mvatype = "MLP";
180  else if (wfile.Contains("Likelihood")) mvatype = "Likelihood";
181  else {cout <<"Unconfigured method in w-file "<<wfile<<endl; return;}
182 
183  reader->BookMVA(mvatype, wfile);
184 
185 
186  // apply precut cut and create signal and background event lists
187  t->Draw(">>els",sigcut);
188  t->Draw(">>elb",bkgcut);
189  TEventList *els=(TEventList*)gDirectory->Get("els");
190  TEventList *elb=(TEventList*)gDirectory->Get("elb");
191 
192  int Ns = els->GetN();
193  int Nb = elb->GetN();
194 
195  TH1F *hs=new TH1F("hs",Form("%s : output",mvatype.Data()),50,mvatype=="BDT"?-1:0,1);
196  TH1F *hb=new TH1F("hb","output background",50,mvatype=="BDT"?-1:0,1);
197  hb->SetLineColor(kRed+1);
198  hs->SetLineColor(kGreen+2);
199  hs->SetLineWidth(2);
200  hb->SetLineWidth(2);
201 
202  TString friendname = fname;
203  friendname.ReplaceAll(".root","_tmvaout.root");
204  TFile *ftm = new TFile(friendname,"recreate");
205  TNtuple *tf = new TNtuple("tf","tf","tmvaout");
206 
207 
208  for (int i=0;i<t->GetEntriesFast();++i)
209  {
210  t->GetEntry(i);
211  for (int j=0;j<Nbr;++j)
212  {
213  Float_t var=0;
214  switch (types[j]) {case 2: var = (Float_t)bbranch[j]; break; case 1: var = (Float_t) ibranch[j]; break; default: var=fbranch[j]; }
215  freader[j] = var;
216  }
217  float resp=reader->EvaluateMVA(mvatype);
218  tf->Fill(resp);
219  }
220 
221  tf->Write();
222  ftm->Close();
223 
224  // loop through signal event list
225  for (int i=0;i<Ns;++i)
226  {
227  t->GetEntry(els->GetEntry(i));
228 
229  for (int j=0;j<Nbr;++j)
230  {
231  Float_t var=0;
232  switch (types[j]) {case 2: var = (Float_t)bbranch[j]; break; case 1: var = (Float_t) ibranch[j]; break; default: var=fbranch[j]; }
233  freader[j] = var;
234  }
235  double resp=reader->EvaluateMVA(mvatype);
236  hs->Fill(resp);
237  }
238  // loop through backgound event list
239  for (int i=0;i<Nb;++i)
240  {
241  t->GetEntry(elb->GetEntry(i));
242 
243  for (int j=0;j<Nbr;++j)
244  {
245  Float_t var=0;
246  switch (types[j]) {case 2: var = (Float_t)bbranch[j]; break; case 1: var = (Float_t) ibranch[j]; break; default: var=fbranch[j]; }
247  freader[j] = var;
248  }
249  double resp=reader->EvaluateMVA(mvatype);
250  hb->Fill(resp);
251  }
252 
253 
254  // create the plots
255 
256  TCanvas *c1=new TCanvas("c1","c1",10,10,1200,600);
257  c1->Divide(2,1);
258  c1->cd(1);
259  hs->Scale(1./hs->GetEntries());
260  hb->Scale(1./hb->GetEntries());
261  double maxi = max(hs->GetMaximum(), hb->GetMaximum())*1.05;
262 
263  hs->SetMaximum(maxi);
264  hb->SetMaximum(maxi);
265  hs->DrawCopy();
266  hb->DrawCopy("same");
267 
268  TLegend *leg=new TLegend(0.3,0.7,0.6,0.85);
269  leg->SetBorderSize(0);
270  leg->AddEntry(hs, "signal","l");
271  leg->AddEntry(hb, "background","l");
272  leg->Draw();
273 
274  c1->cd(2);
275  double sums=1., sumb=0.;
276 
277  // create the ROC curve graph
278  TGraph *gr=new TGraph();
279  // add points to get a closed curve for Integral
280  gr->SetPoint(0,0,0);
281  gr->SetPoint(1,0,1);
282 
283  for (int i=0;i<=hs->GetNbinsX();++i)
284  {
285  sums-=hs->GetBinContent(i);
286  sumb+=hb->GetBinContent(i);
287  gr->SetPoint(i+2,sumb,sums);
288  }
289  // add point to get a closed curve for Integral
290  gr->SetPoint(hs->GetNbinsX()+3,1,0);
291 
292  // configure graph style
293  gr->Draw("ALP");
294  gr->SetMarkerStyle(20);
295  gr->SetMarkerSize(0.7);
296  gr->GetHistogram()->SetTitle(Form("%s : ROC curve;Background suppression;Signal Efficiency",mvatype.Data()));
297  gr->GetHistogram()->SetMinimum(0);
298  gr->GetHistogram()->SetMaximum(1.05);
299 
300  // print the method and integral on the canvas
301  TLatex lat;
302  lat.SetTextSize(0.045);
303  lat.SetNDC();
304  lat.SetTextColor(4);
305  lat.DrawLatex(0.2,0.2,Form("ROC integral = %.3f",gr->Integral()));
306 
307  cout <<"Integral of ROC curve (0.5 = useless ... 1.0 = perfect separation): "<<gr->Integral()<<endl;
308 }
TEventList * els
Definition: findcuts.C:52
Int_t i
Definition: run_full.C:25
Float_t freader[MAX]
friend F32vec4 max(const F32vec4 &a, const F32vec4 &b)
Definition: P4_F32vec4.h:26
Int_t types[MAX]
Float_t fbranch[MAX]
int Ns
Definition: findcuts.C:56
Bool_t bbranch[MAX]
int Nb
Definition: findcuts.C:56
TString vars[MAX]
Definition: autocutx.C:34
TFile * f
Definition: bump_analys.C:12
c1
Definition: plot_dirc.C:35
int SplitString(TString s, TString delim, TString *toks, int maxtoks)
int init(TTree *t, TString vars)
TEventList * elb
Definition: findcuts.C:52
TTree * t
Definition: bump_analys.C:13
TString getFromCut(TString vars)
Int_t ibranch[MAX]
TString getFromWeightFile(TString wfile)

Variable Documentation

Bool_t bbranch[MAX]

Definition at line 24 of file thailand2017/TMVATester.C.

Referenced by init(), and TMVATester().

Int_t ev

Definition at line 26 of file thailand2017/TMVATester.C.

Float_t fbranch[MAX]

Definition at line 22 of file thailand2017/TMVATester.C.

Referenced by init(), and TMVATester().

Float_t freader[MAX]

Definition at line 22 of file thailand2017/TMVATester.C.

Referenced by TMVATester().

Int_t ibranch[MAX]

Definition at line 23 of file thailand2017/TMVATester.C.

Referenced by init(), and TMVATester().

Int_t mode

Definition at line 26 of file thailand2017/TMVATester.C.

Int_t nbranch

Definition at line 26 of file thailand2017/TMVATester.C.

Int_t rec

Definition at line 26 of file thailand2017/TMVATester.C.

Int_t run

Definition at line 26 of file thailand2017/TMVATester.C.

Int_t types[MAX]

Definition at line 23 of file thailand2017/TMVATester.C.

Referenced by init(), and TMVATester().