1 #include "TMVA/Reader.h"
7 #include "TEventList.h"
29 typedef std::vector<pair<float, int> >
ValueMap;
36 if (t->GetBranch(varname)==0)
return -1;
38 TString leaftype = t->GetLeaf(varname)->GetTypeName();
40 if (leaftype==
"Float_t")
return 0;
41 else if (leaftype==
"Int_t")
return 1;
42 else if (leaftype==
"Bool_t")
return 2;
50 TObjArray *tok = s.Tokenize(delim);
51 int N = tok->GetEntries();
55 toks[
i] = ((TObjString*)tok->At(
i))->String();
56 toks[
i].ReplaceAll(
"\t",
"");
57 toks[
i] = toks[
i].Strip(TString::kBoth);
67 TRegexp rvar(
"[_a-zA-Z][_a-zA-Z0-9]*");
76 if (v==
"tag" || res.Contains(
" "+v+
" "))
continue;
81 res = res.Strip(TString::kBoth);
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;
113 TString tmp=gSystem->GetFromPipe(
"grep Expression "+wfile);
117 TRegexp
r(
"Expression=\"[_a-zA-Z0-9]+\"");
119 for (
int i=0;
i<
n;++
i)
122 exp.ReplaceAll(
"Expression=",
"");
123 exp.ReplaceAll(
"\"",
"");
127 res = res.Strip(TString::kBoth);
137 if ( fname==
"" || treename==
"" || wfile==
"" || sigcut==
"" )
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 -b -q 'TMVATester.C(\"demodata.root\",\"ntp\",\"signal>0\",\"weights/demodata_ntp_MLP.weights.xml\",\"\")'\n\n";
151 if (vars.Contains(
"&&")) vars =
getFromCut(vars);
153 cout <<
"Vars : "<<vars<<endl;
155 TString bkgcut=
"!("+sigcut+
")";
159 cout <<
"Precut : "<<precut<<endl;
160 sigcut +=
"&&" + precut;
161 bkgcut +=
"&&" + precut;
164 TFile *
f = TFile::Open(fname);
165 TTree *
t =(TTree*) f->Get(treename);
168 int Nbr =
init(t,vars);
173 TMVA::Reader *reader =
new TMVA::Reader(
"Silent");
174 for (
int i=0;
i<Nbr;++
i) reader->AddVariable(varname[
i], &
freader[i]);
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;}
183 reader->BookMVA(mvatype, wfile);
187 t->Draw(
">>els",sigcut);
188 t->Draw(
">>elb",bkgcut);
189 TEventList *
els=(TEventList*)gDirectory->Get(
"els");
190 TEventList *
elb=(TEventList*)gDirectory->Get(
"elb");
192 int Ns = els->GetN();
193 int Nb = elb->GetN();
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);
203 friendname.ReplaceAll(
".root",
"_tmvaout.root");
204 TFile *ftm =
new TFile(friendname,
"recreate");
205 TNtuple *tf =
new TNtuple(
"tf",
"tf",
"tmvaout");
208 for (
int i=0;i<t->GetEntriesFast();++
i)
211 for (
int j=0;j<Nbr;++j)
214 switch (
types[j]) {
case 2: var = (Float_t)
bbranch[j];
break;
case 1: var = (Float_t)
ibranch[j];
break;
default: var=
fbranch[j]; }
217 float resp=reader->EvaluateMVA(mvatype);
225 for (
int i=0;i<
Ns;++
i)
227 t->GetEntry(els->GetEntry(i));
229 for (
int j=0;j<Nbr;++j)
232 switch (
types[j]) {
case 2: var = (Float_t)
bbranch[j];
break;
case 1: var = (Float_t)
ibranch[j];
break;
default: var=
fbranch[j]; }
235 double resp=reader->EvaluateMVA(mvatype);
239 for (
int i=0;i<
Nb;++
i)
241 t->GetEntry(elb->GetEntry(i));
243 for (
int j=0;j<Nbr;++j)
246 switch (
types[j]) {
case 2: var = (Float_t)
bbranch[j];
break;
case 1: var = (Float_t)
ibranch[j];
break;
default: var=
fbranch[j]; }
249 double resp=reader->EvaluateMVA(mvatype);
256 TCanvas *
c1=
new TCanvas(
"c1",
"c1",10,10,1200,600);
259 hs->Scale(1./hs->GetEntries());
260 hb->Scale(1./hb->GetEntries());
261 double maxi =
max(hs->GetMaximum(), hb->GetMaximum())*1.05;
263 hs->SetMaximum(maxi);
264 hb->SetMaximum(maxi);
266 hb->DrawCopy(
"same");
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");
275 double sums=1., sumb=0.;
278 TGraph *gr=
new TGraph();
283 for (
int i=0;i<=hs->GetNbinsX();++
i)
285 sums-=hs->GetBinContent(i);
286 sumb+=hb->GetBinContent(i);
287 gr->SetPoint(i+2,sumb,sums);
290 gr->SetPoint(hs->GetNbinsX()+3,1,0);
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);
302 lat.SetTextSize(0.045);
305 lat.DrawLatex(0.2,0.2,Form(
"ROC integral = %.3f",gr->Integral()));
307 cout <<
"Integral of ROC curve (0.5 = useless ... 1.0 = perfect separation): "<<gr->Integral()<<endl;
friend F32vec4 exp(const F32vec4 &a)
void TMVATester(TString fname="", TString treename="", TString sigcut="", TString wfile="", TString precut="")
TString getFromCut(TString vars)
friend F32vec4 max(const F32vec4 &a, const F32vec4 &b)
TString getFromWeightFile(TString wfile)
std::vector< pair< double, int > > ValueMap
int gettype(TTree *t, TString varname)
int SplitString(TString s, TString delim, StrVec &toks)
std::map< int, int > CountMap