Belle II Software development
B2A702-ContinuumSuppression_MVATrain.py
1#!/usr/bin/env python3
2
3
10
11
25
26import basf2 as b2
27import basf2_mva
28import subprocess
29
30
31if __name__ == "__main__":
32
33 # Note that the target variable 'isNotContinuum' needs to be
34 # saved in your train.root and test.root files, along with the
35 # trainingVars, listed again here (see B2A701).
36 import os
37 if not os.getenv('BELLE2_EXAMPLES_DATA_DIR'):
38 b2.B2FATAL("You need the example data installed. Run `b2install-data example` in terminal for it.")
39
40 # Use this path to run over Bd_KsPi0 reconstructed signal and qqbar skims.
41 path = os.getenv('BELLE2_EXAMPLES_DATA_DIR')+'/mva/B2A701_output_'
42
43 train_data = path + 'train.root'
44 test_data = path + 'test.root'
45 apply_signal_data = path + 'apply_signal.root'
46 apply_qqbar_data = path + 'apply_qqbar.root'
47
48 # Define the variables for training.
49 # For details, please see the Continuum suppression section at https://software.belle2.org
50 # Note that KSFWVariables takes the optional additional argument FS1, to return the variables calculated from the
51 # signal-B final state particles.
52 # CleoCone also takes the optional additional argument ROE, to return the cones calculated from ROE particles only.
53 trainVars = [
54 'R2',
55 'thrustBm',
56 'thrustOm',
57 'cosTBTO',
58 'cosTBz',
59 'KSFWVariables(pt_sum)',
60 'KSFWVariables(mm2)',
61 'KSFWVariables(hso00)',
62 'KSFWVariables(hso01)',
63 'KSFWVariables(hso02)',
64 'KSFWVariables(hso03)',
65 'KSFWVariables(hso04)',
66 'KSFWVariables(hso10)',
67 'KSFWVariables(hso12)',
68 'KSFWVariables(hso14)',
69 'KSFWVariables(hso20)',
70 'KSFWVariables(hso22)',
71 'KSFWVariables(hso24)',
72 'KSFWVariables(hoo0)',
73 'KSFWVariables(hoo1)',
74 'KSFWVariables(hoo2)',
75 'KSFWVariables(hoo3)',
76 'KSFWVariables(hoo4)',
77 'CleoConeCS(1)',
78 'CleoConeCS(2)',
79 'CleoConeCS(3)',
80 'CleoConeCS(4)',
81 'CleoConeCS(5)',
82 'CleoConeCS(6)',
83 'CleoConeCS(7)',
84 'CleoConeCS(8)',
85 'CleoConeCS(9)'
86 ]
87
88 general_options = basf2_mva.GeneralOptions()
89 general_options.m_datafiles = basf2_mva.vector(train_data)
90 general_options.m_treename = "tree"
91 general_options.m_identifier = "MVAFastBDT.root"
92 general_options.m_variables = basf2_mva.vector(*trainVars)
93 general_options.m_target_variable = "isNotContinuumEvent"
94 fastbdt_options = basf2_mva.FastBDTOptions()
95
96 # Train a MVA method and store the weightfile (MVAFastBDT.root) locally.
97 basf2_mva.teacher(general_options, fastbdt_options)
98
99 # Evaluate training.
100 subprocess.call('basf2_mva_evaluate.py '
101 ' -train ' + train_data +
102 ' -data ' + test_data +
103 ' -id ' + 'MVAFastBDT.root' +
104 ' --output qqbarSuppressionEvaluation.pdf',
105 shell=True
106 )
107
108 # If you're only interested in the network output distribution, then
109 # comment these in to apply the trained methods on an independent sample
110 # (produced in B2A701 if you ran with the `apply_signal` and `apply_qqbar` options).
111 #
112 basf2_mva.expert(
113 basf2_mva.vector('MVAFastBDT.root'),
114 basf2_mva.vector(apply_signal_data),
115 'tree',
116 'MVAExpert_signal.root')
117 basf2_mva.expert(
118 basf2_mva.vector('MVAFastBDT.root'),
119 basf2_mva.vector(apply_qqbar_data),
120 'tree',
121 'MVAExpert_qqbar.root')