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