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