17from ROOT
import TMVA, TFile, TString, gROOT
18from array
import array
19from subprocess
import call
20from os.path
import isfile
30fname = str(gROOT.GetTutorialDir()) +
"/machine_learning/data/tmva_class_example.root"
32signal = data.Get(
'TreeS')
33background = data.Get(
'TreeB')
36for branch
in signal.GetListOfBranches():
37 branchName = branch.GetName()
38 branches[branchName] = array(
'f', [-999])
39 reader.AddVariable(branchName, branches[branchName])
40 signal.SetBranchAddress(branchName, branches[branchName])
41 background.SetBranchAddress(branchName, branches[branchName])
45def predict(model, test_X, batch_size=32):
49 test_dataset = torch.utils.data.TensorDataset(torch.Tensor(test_X))
50 test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=batch_size, shuffle=
False)
54 for i, data
in enumerate(test_loader):
57 predictions.append(outputs)
58 preds = torch.cat(predictions)
63load_model_custom_objects = {
"optimizer":
None,
"criterion":
None,
"train_func":
None,
"predict_func": predict}
67reader.BookMVA(
'PyTorch',
TString(
'dataset/weights/TMVAClassification_PyTorch.weights.xml'))
71print(
'Some signal example classifications:')
74 print(reader.EvaluateMVA(
'PyTorch'))
77print(
'Some background example classifications:')
79 background.GetEntry(i)
80 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.