18from ROOT
import TMVA, TFile, TString
19from array
import array
20from subprocess
import call
21from os.path
import isfile
31if not isfile(
'tmva_class_example.root'):
32 call([
'curl',
'-L',
'-O',
'http://root.cern.ch/files/tmva_class_example.root'])
35signal = data.Get(
'TreeS')
36background = data.Get(
'TreeB')
39for branch
in signal.GetListOfBranches():
40 branchName = branch.GetName()
41 branches[branchName] = array(
'f', [-999])
42 reader.AddVariable(branchName, branches[branchName])
43 signal.SetBranchAddress(branchName, branches[branchName])
44 background.SetBranchAddress(branchName, branches[branchName])
48def predict(model, test_X, batch_size=32):
52 test_dataset = torch.utils.data.TensorDataset(torch.Tensor(test_X))
53 test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=batch_size, shuffle=
False)
57 for i, data
in enumerate(test_loader):
60 predictions.append(outputs)
61 preds = torch.cat(predictions)
66load_model_custom_objects = {
"optimizer":
None,
"criterion":
None,
"train_func":
None,
"predict_func": predict}
70reader.BookMVA(
'PyTorch',
TString(
'dataset/weights/TMVAClassification_PyTorch.weights.xml'))
74print(
'Some signal example classifications:')
77 print(reader.EvaluateMVA(
'PyTorch'))
80print(
'Some background example classifications:')
82 background.GetEntry(i)
83 print(reader.EvaluateMVA(
'PyTorch'))
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
Create / open a file.
static void PyInitialize()
Initialize Python interpreter.
The Reader class serves to use the MVAs in a specific analysis context.