#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 #include #include #include #include #define MAX 1000 Float_t fbranch[MAX], freader[MAX]; Int_t ibranch[MAX], types[MAX]; Bool_t bbranch[MAX]; Int_t ev, run, mode, rec, nbranch; typedef std::map CountMap; typedef std::vector > ValueMap; // --------------------------------------------------------------- #ifndef TMVAgettype #define TMVAgettype int gettype(TTree *t, TString varname) { if (t->GetBranch(varname)==0) return -1; TString leaftype = t->GetLeaf(varname)->GetTypeName(); if (leaftype=="Float_t") return 0; else if (leaftype=="Int_t") return 1; else if (leaftype=="Bool_t") return 2; return -1; } // --------------------------------------------------------------- int SplitString(TString s, TString delim, TString *toks, int maxtoks) { TObjArray *tok = s.Tokenize(delim); int N = tok->GetEntries(); for (int i=0;iAt(i))->String(); toks[i].ReplaceAll("\t",""); toks[i] = toks[i].Strip(TString::kBoth); } return N; } // --------------------------------------------------------------- TString getFromCut(TString vars) { TString toks[50]; int n=SplitString(vars, "&&", toks, 50); TRegexp rvar("[_a-zA-Z][_a-zA-Z0-9]*"); cout <<"n="<SetBranchAddress(v, &(fbranch[i])); break; case 1: t->SetBranchAddress(v, &(ibranch[i])); break; case 2: t->SetBranchAddress(v, &(bbranch[i])); break; } //cout <GetFromPipe("grep Expression "+wfile); TString v[30]; int n=SplitString(tmp,"/>",v,30); TString res=""; TRegexp r("Expression=\"[_a-zA-Z0-9]+\""); for (int i=0;i, , , , [precut] )\n\n"; cout << " : input file name containing TTree \n"; cout << " : name of the TTree containing signal and background\n"; cout << " : cut separating signal from background -> bgcut = !(sigcut)\n"; cout << " : file containing the weights from training, usually stored in weights/...\n"; cout << " [precut] : optional precut before training; should be the same as for training!'\n\n"; cout << "EXAMPLE:\n"; cout << "root -l -b -q 'TMVATester.C(\"demodata.root\",\"ntp\",\"signal>0\",\"weights/demodata_ntp_MLP.weights.xml\",\"\")'\n\n"; return; } if (vars.Contains("&&")) vars = getFromCut(vars); if (vars=="") vars = getFromWeightFile(wfile); cout <<"Vars : "<Get(treename); // prepare variables and branch mapping int Nbr = init(t,vars); TString varname[30]; SplitString(vars," ",varname,30); // prepare TMVA reader TMVA::Reader *reader = new TMVA::Reader("Silent"); for (int i=0;iAddVariable(varname[i], &freader[i]); TString mvatype=""; if (wfile.Contains("BDT")) mvatype = "BDT"; else if (wfile.Contains("MLP")) mvatype = "MLP"; else if (wfile.Contains("Likelihood")) mvatype = "Likelihood"; else {cout <<"Unconfigured method in w-file "<BookMVA(mvatype, wfile); // apply precut cut and create signal and background event lists t->Draw(">>els",sigcut); t->Draw(">>elb",bkgcut); TEventList *els=(TEventList*)gDirectory->Get("els"); TEventList *elb=(TEventList*)gDirectory->Get("elb"); int Ns = els->GetN(); int Nb = elb->GetN(); TH1F *hs=new TH1F("hs",Form("%s : output",mvatype.Data()),50,mvatype=="BDT"?-1:0,1); TH1F *hb=new TH1F("hb","output background",50,mvatype=="BDT"?-1:0,1); hb->SetLineColor(kRed+1); hs->SetLineColor(kGreen+2); hs->SetLineWidth(2); hb->SetLineWidth(2); TString friendname = fname; friendname.ReplaceAll(".root","_tmvaout.root"); TFile *ftm = new TFile(friendname,"recreate"); TNtuple *tf = new TNtuple("tf","tf","tmvaout"); for (int i=0;iGetEntriesFast();++i) { t->GetEntry(i); for (int j=0;jEvaluateMVA(mvatype); tf->Fill(resp); } tf->Write(); ftm->Close(); // loop through signal event list for (int i=0;iGetEntry(els->GetEntry(i)); for (int j=0;jEvaluateMVA(mvatype); hs->Fill(resp); } // loop through backgound event list for (int i=0;iGetEntry(elb->GetEntry(i)); for (int j=0;jEvaluateMVA(mvatype); hb->Fill(resp); } // create the plots TCanvas *c1=new TCanvas("c1","c1",10,10,1200,600); c1->Divide(2,1); c1->cd(1); hs->Scale(1./hs->GetEntries()); hb->Scale(1./hb->GetEntries()); double maxi = max(hs->GetMaximum(), hb->GetMaximum())*1.05; hs->SetMaximum(maxi); hb->SetMaximum(maxi); hs->DrawCopy(); hb->DrawCopy("same"); TLegend *leg=new TLegend(0.3,0.7,0.6,0.85); leg->SetBorderSize(0); leg->AddEntry(hs, "signal","l"); leg->AddEntry(hb, "background","l"); leg->Draw(); c1->cd(2); double sums=1., sumb=0.; // create the ROC curve graph TGraph *gr=new TGraph(); // add points to get a closed curve for Integral gr->SetPoint(0,0,0); gr->SetPoint(1,0,1); for (int i=0;i<=hs->GetNbinsX();++i) { sums-=hs->GetBinContent(i); sumb+=hb->GetBinContent(i); gr->SetPoint(i+2,sumb,sums); } // add point to get a closed curve for Integral gr->SetPoint(hs->GetNbinsX()+3,1,0); // configure graph style gr->Draw("ALP"); gr->SetMarkerStyle(20); gr->SetMarkerSize(0.7); gr->GetHistogram()->SetTitle(Form("%s : ROC curve;Background suppression;Signal Efficiency",mvatype.Data())); gr->GetHistogram()->SetMinimum(0); gr->GetHistogram()->SetMaximum(1.05); // print the method and integral on the canvas TLatex lat; lat.SetTextSize(0.045); lat.SetNDC(); lat.SetTextColor(4); lat.DrawLatex(0.2,0.2,Form("ROC integral = %.3f",gr->Integral())); cout <<"Integral of ROC curve (0.5 = useless ... 1.0 = perfect separation): "<Integral()<