#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; // --------------------------------------------------------------- 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; } // --------------------------------------------------------------- int init(TTree *t, TString vars) { TString toks[30]; int N = SplitString(vars," ",toks,30); for (int i=0;iSetBranchAddress(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"; 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]); reader->BookMVA("BDT",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","network output: signal(blue), bkg(red)",50,-1,1); TH1F *hb=new TH1F("hb","network output background",50,-1,1); hb->SetLineColor(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("BDT"); 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("BDT"); hs->Fill(resp); } // loop through backgound event list for (int i=0;iGetEntry(elb->GetEntry(i)); for (int j=0;jEvaluateMVA("BDT"); 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"); c1->cd(2); double sums=1., sumb=0.; TGraph *gr=new TGraph(); for (int i=0;i<=hs->GetNbinsX();++i) { sums-=hs->GetBinContent(i); sumb+=hb->GetBinContent(i); gr->SetPoint(i,sumb,sums); } gr->Draw("ALP"); gr->SetMarkerStyle(20); gr->SetMarkerSize(0.7); gr->GetHistogram()->SetTitle("ROC curve"); gr->GetHistogram()->SetXTitle("Background suppression"); gr->GetHistogram()->SetYTitle("Signal Efficiency"); }