Logo ROOT   6.16/01
Reference Guide
ApplicationClassificationKeras.py
Go to the documentation of this file.
1#!/usr/bin/env python
2
3from ROOT import TMVA, TFile, TString
4from array import array
5from subprocess import call
6from os.path import isfile
7
8# Setup TMVA
11reader = TMVA.Reader("Color:!Silent")
12
13# Load data
14if not isfile('tmva_class_example.root'):
15 call(['curl', '-O', 'http://root.cern.ch/files/tmva_class_example.root'])
16
17data = TFile.Open('tmva_class_example.root')
18signal = data.Get('TreeS')
19background = data.Get('TreeB')
20
21branches = {}
22for branch in signal.GetListOfBranches():
23 branchName = branch.GetName()
24 branches[branchName] = array('f', [-999])
25 reader.AddVariable(branchName, branches[branchName])
26 signal.SetBranchAddress(branchName, branches[branchName])
27 background.SetBranchAddress(branchName, branches[branchName])
28
29# Book methods
30reader.BookMVA('PyKeras', TString('dataset/weights/TMVAClassification_PyKeras.weights.xml'))
31
32# Print some example classifications
33print('Some signal example classifications:')
34for i in range(20):
35 signal.GetEntry(i)
36 print(reader.EvaluateMVA('PyKeras'))
37print('')
38
39print('Some background example classifications:')
40for i in range(20):
41 background.GetEntry(i)
42 print(reader.EvaluateMVA('PyKeras'))
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseGeneralPurpose, Int_t netopt=0)
Create / open a file.
Definition: TFile.cxx:3975
static void PyInitialize()
Initialize Python interpreter.
The Reader class serves to use the MVAs in a specific analysis context.
Definition: Reader.h:63
static Tools & Instance()
Definition: Tools.cxx:75