Belle II Software  release-05-01-25
B2A712-DeepContinuumSuppression_MVATrain.py
1 #!/usr/bin/env python3
2 
3 
21 
22 import basf2_mva
23 import subprocess
24 import json
25 
26 
27 def choose_input_features(use_vertex_features=True, use_charge_and_ROE_features=False, use_continuum_features=1):
28  """
29  Function to return all names of input features.
30  :param use_vertex_features: If Vertex info should be included.
31  :param use_charge_and_ROE_features: If charge and ROE should be included as extra features(information already
32  included in group structure). This option is only nevessary when using Relation Layers.
33  :param use_continuum_features: Use old Continuum Features (0: No, 1: Yes, 2: Use only the old features)
34  :return: Array of feature names
35  """
36  contVar = [
37  'R2',
38  'thrustBm',
39  'thrustOm',
40  'cosTBTO',
41  'cosTBz',
42  'KSFWVariables(et)',
43  'KSFWVariables(mm2)',
44  'KSFWVariables(hso00)',
45  'KSFWVariables(hso02)',
46  'KSFWVariables(hso04)',
47  'KSFWVariables(hso10)',
48  'KSFWVariables(hso12)',
49  'KSFWVariables(hso14)',
50  'KSFWVariables(hso20)',
51  'KSFWVariables(hso22)',
52  'KSFWVariables(hso24)',
53  'KSFWVariables(hoo0)',
54  'KSFWVariables(hoo1)',
55  'KSFWVariables(hoo2)',
56  'KSFWVariables(hoo3)',
57  'KSFWVariables(hoo4)',
58  'CleoCone(1)',
59  'CleoCone(2)',
60  'CleoCone(3)',
61  'CleoCone(4)',
62  'CleoCone(5)',
63  'CleoCone(6)',
64  'CleoCone(7)',
65  'CleoCone(8)',
66  'CleoCone(9)']
67 
68  if use_continuum_features == 2:
69  return contVar
70 
71  basic_variables = ['p', 'phi', 'cosTheta', 'pErr', 'phiErr', 'cosThetaErr']
72  vertex_variables = ['distance', 'dphi', 'dcosTheta']
73 
74  cluster_specific_variables = ['clusterNHits', 'clusterTiming', 'clusterE9E25', 'clusterReg']
75  track_specific_variables = ['kaonID', 'electronID', 'muonID', 'protonID', 'pValue', 'nCDCHits']
76 
77  if use_charge_and_ROE_features:
78  cluster_specific_variables += ['isInRestOfEvent']
79  track_specific_variables += ['isInRestOfEvent', 'charge']
80 
81  cluster_specific_variables += ['thrustsig' + var for var in basic_variables]
82  track_specific_variables += ['thrustsig' + var for var in basic_variables]
83 
84  if use_vertex_features:
85  track_specific_variables += ['thrustsig' + var for var in vertex_variables]
86 
87  cluster_lists = ['Csig', 'Croe']
88  track_lists = ['TPsig', 'TMsig', 'TProe', 'TMroe']
89 
90  variables = []
91  for plist in track_lists:
92  for rank in range(5):
93  for var in track_specific_variables:
94  variables.append('{}_{}{}'.format(var, plist, rank))
95 
96  for plist in cluster_lists:
97  for rank in range(10):
98  for var in cluster_specific_variables:
99  variables.append('{}_{}{}'.format(var, plist, rank))
100 
101  if use_continuum_features:
102  variables += contVar
103 
104  return variables
105 
106 
107 if __name__ == "__main__":
108 
109  # In this path there are already several trained weightfiles. Look at README for a short explanation
110  path = '/group/belle2/tutorial/release_01-00-00/inputForDCSTutorial/'
111 
112  train_data = path + 'train.root'
113  test_data = path + 'test.root'
114 
115  general_options = basf2_mva.GeneralOptions()
116  general_options.m_datafiles = basf2_mva.vector(train_data)
117  general_options.m_treename = "tree"
118  general_options.m_identifier = "Deep_Feed_Forward.xml"
119  general_options.m_variables = basf2_mva.vector(*choose_input_features(True, False, 1))
120  general_options.m_spectators = basf2_mva.vector('Mbc', 'DeltaZ')
121  general_options.m_target_variable = "isNotContinuumEvent"
122 
123  specific_options = basf2_mva.PythonOptions()
124  specific_options.m_framework = "contrib_keras"
125  specific_options.m_steering_file = 'analysis/examples/tutorials/B2A714-DeepContinuumSuppression_MVAModel.py'
126  specific_options.m_training_fraction = 0.9
127 
128  # These options are custom made in B2A714. You can also add your own parameters.
129  # Try different options and compare them by handing multiple weightfiles in basf2_mva_evaluation.py
130  keras_dic = {
131  # If Relation layer should be used instead of Feed Forward.
132  # Only works with choose_input_features(True, True, 1)
133  'use_relation_layers': False,
134  # The following options are for using Adversaries. To disable them leave lambda to zero.
135  # See mva/examples/keras/adversary_network.py for details
136  'lambda': 0, # Use 500 as starting point to try the Adversaries out
137  'number_bins': 10,
138  'adversary_steps': 5}
139  specific_options.m_config = json.dumps(keras_dic)
140 
141  # Train a MVA method and store the weightfile (MVAFastBDT.root) locally.
142  basf2_mva.teacher(general_options, specific_options)
143 
144  # Evaluate training.
145  subprocess.call('basf2_mva_evaluate.py '
146  ' -train ' + train_data +
147  ' -data ' + test_data +
148  ' -id ' + 'Deep_Feed_Forward.xml' +
149  ' --output qqbarSuppressionEvaluation.pdf',
150  shell=True
151  )
152 
153  # If you're only interested in the network output distribution, then
154  # comment these in to apply the trained methods on train and test sample
155  #
156  # basf2_mva.expert(basf2_mva.vector('Deep_Feed_Forward.xml'), basf2_mva.vector(train_data), 'tree', 'MVAExpert_train.root')
157  # basf2_mva.expert(basf2_mva.vector('Deep_Feed_Forward.xml'), basf2_mva.vector(test_data), 'tree', 'MVAExpert_test.root')