13from ROOT
import TMVA, TFile, TString
14from array
import array
15from subprocess
import call
16from os.path
import isfile
26if not isfile(
'tmva_class_example.root'):
27 call([
'curl',
'-L',
'-O',
'http://root.cern.ch/files/tmva_class_example.root'])
30signal = data.Get(
'TreeS')
31background = data.Get(
'TreeB')
34for branch
in signal.GetListOfBranches():
35 branchName = branch.GetName()
36 branches[branchName] = array(
'f', [-999])
37 reader.AddVariable(branchName, branches[branchName])
38 signal.SetBranchAddress(branchName, branches[branchName])
39 background.SetBranchAddress(branchName, branches[branchName])
43def predict(model, test_X, batch_size=32):
47 test_dataset = torch.utils.data.TensorDataset(torch.Tensor(test_X))
48 test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=batch_size, shuffle=
False)
52 for i, data
in enumerate(test_loader):
55 predictions.append(outputs)
56 preds = torch.cat(predictions)
61load_model_custom_objects = {
"optimizer":
None,
"criterion":
None,
"train_func":
None,
"predict_func": predict}
65reader.BookMVA(
'PyTorch',
TString(
'dataset/weights/TMVAClassification_PyTorch.weights.xml'))
69print(
'Some signal example classifications:')
72 print(reader.EvaluateMVA(
'PyTorch'))
75print(
'Some background example classifications:')
77 background.GetEntry(i)
78 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.