18from ROOT
import TMVA, TFile, TString, gROOT
19from array
import array
20from subprocess
import call
21from os.path
import isfile
31fname = str(gROOT.GetTutorialDir()) +
"/tmva/data/tmva_class_example.root"
33signal = data.Get(
'TreeS')
34background = data.Get(
'TreeB')
37for branch
in signal.GetListOfBranches():
38 branchName = branch.GetName()
39 branches[branchName] = array(
'f', [-999])
40 reader.AddVariable(branchName, branches[branchName])
41 signal.SetBranchAddress(branchName, branches[branchName])
42 background.SetBranchAddress(branchName, branches[branchName])
46def predict(model, test_X, batch_size=32):
50 test_dataset = torch.utils.data.TensorDataset(torch.Tensor(test_X))
51 test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=batch_size, shuffle=
False)
55 for i, data
in enumerate(test_loader):
58 predictions.append(outputs)
59 preds = torch.cat(predictions)
64load_model_custom_objects = {
"optimizer":
None,
"criterion":
None,
"train_func":
None,
"predict_func": predict}
68reader.BookMVA(
'PyTorch',
TString(
'dataset/weights/TMVAClassification_PyTorch.weights.xml'))
72print(
'Some signal example classifications:')
75 print(reader.EvaluateMVA(
'PyTorch'))
78print(
'Some background example classifications:')
80 background.GetEntry(i)
81 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.