Belle II Software development
usage_in_python.py
1#!/usr/bin/env python3
2
3
10
11import basf2_mva
12
13if __name__ == "__main__":
14 from basf2 import conditions, find_file
15 # NOTE: do not use testing payloads in production! Any results obtained like this WILL NOT BE PUBLISHED
16 conditions.testing_payloads = [
17 'localdb/database.txt'
18 ]
19
20 train_file = find_file("mva/train_D0toKpipi.root", "examples")
21 test_file = find_file("mva/test_D0toKpipi.root", "examples")
22
23 training_data = basf2_mva.vector(train_file)
24 testing_data = basf2_mva.vector(test_file)
25
26 # Train a MVA method and directly upload it to the database
27 general_options = basf2_mva.GeneralOptions()
28 general_options.m_datafiles = training_data
29 general_options.m_treename = "tree"
30 general_options.m_identifier = "MVADatabaseIdentifier"
31 general_options.m_variables = basf2_mva.vector('M', 'p', 'pz', 'pt', 'phi', 'daughter(0, kaonID)', 'daughter(0, pionID)',
32 'daughter(1, kaonID)', 'daughter(1, pionID)', 'chiProb', 'dr', 'dz', 'dphi')
33 general_options.m_target_variable = "isSignal"
34
35 fastbdt_options = basf2_mva.FastBDTOptions()
36
37 basf2_mva.teacher(general_options, fastbdt_options)
38
39 # Download the weightfile from the database and store it on disk in a root file
40 basf2_mva.download('MVADatabaseIdentifier', 'weightfile.root')
41
42 # Train a MVA method and store the weightfile on disk in a root file
43 general_options.m_identifier = "weightfile2.root"
44 basf2_mva.teacher(general_options, fastbdt_options)
45
46 # Upload the weightfile on disk to the database
47 basf2_mva.upload('weightfile2.root', 'MVADatabaseIdentifier2')
48
49 # Apply the trained methods on data
50 basf2_mva.expert(
51 basf2_mva.vector(
52 'weightfile.root',
53 'weightfile2.root',
54 'MVADatabaseIdentifier',
55 'MVADatabaseIdentifier2'),
56 testing_data,
57 'tree',
58 'expert.root')