Belle II Software  release-05-01-25
B2A703-ContinuumSuppression_MVAExpert.py
1 #!/usr/bin/env python3
2 
3 
21 
22 import basf2 as b2
23 import modularAnalysis as ma
24 import sys
25 
26 
27 # --I/O----------------------------------------------------------------------------------------
28 if (len(sys.argv) < 2 or sys.argv[1] not in ['signal', 'qqbar']):
29  sys.exit("usage:\n\tbasf2 B2A703-ContinuumSuppression_MVAExpert.py <signal,qqbar>")
30 
31 import os
32 if not os.getenv('BELLE2_EXAMPLES_DATA_DIR'):
33  b2.B2FATAL("You need the example data installed. Run `b2install-data example` in terminal for it.")
34 
35 step = str(sys.argv[1])
36 
37 path = os.getenv('BELLE2_EXAMPLES_DATA_DIR')+'/mva/'
38 input_file = ''
39 
40 if step == 'signal':
41  input_file = [path + 'Bd_KsPi0_expert/*']
42 elif step == 'qqbar':
43  input_file = [path + '*bar_expert/*']
44 else:
45  sys.exit('Step does not match any of the available samples: `signal` or `qqbar`')
46 
47 outfile = 'MVAExpert_fullNTuple_' + step + '.root'
48 # ---------------------------------------------------------------------------------------------
49 
50 # Perform analysis.
51 main = b2.create_path()
52 
53 ma.inputMdst(environmentType='default',
54  filename=input_file,
55  path=main)
56 
57 ma.fillParticleList(decayString='gamma:all',
58  cut='', path=main)
59 ma.fillParticleList(decayString='pi+:good',
60  cut='chiProb > 0.001 and pionID > 0.5',
61  path=main)
62 ma.fillParticleList(decayString='pi-:good',
63  cut='chiProb > 0.001 and pionID > 0.5',
64  path=main)
65 
66 ma.reconstructDecay(decayString='K_S0 -> pi+:good pi-:good',
67  cut='0.480<=M<=0.516',
68  dmID=1,
69  path=main)
70 ma.reconstructDecay(decayString='pi0 -> gamma:all gamma:all',
71  cut='0.115<=M<=0.152',
72  dmID=1,
73  path=main)
74 ma.reconstructDecay(decayString='B0 -> K_S0 pi0',
75  cut='5.2 < Mbc < 5.3 and -0.3 < deltaE < 0.2',
76  path=main)
77 
78 ma.matchMCTruth(list_name='B0', path=main)
79 ma.buildRestOfEvent(list_name='B0', path=main)
80 
81 # The momentum cuts used to be hard-coded in the continuum suppression module. They can now be applied
82 # via this mask. The nCDCHits requirement is new, and is recommended to remove VXD-only fake tracks.
83 cleanMask = ('cleanMask', 'nCDCHits > 0 and useCMSFrame(p)<=3.2', 'p >= 0.05 and useCMSFrame(p)<=3.2')
84 ma.appendROEMasks(list_name='B0',
85  mask_tuples=[cleanMask],
86  path=main)
87 
88 ma.buildContinuumSuppression(list_name='B0',
89  roe_mask='cleanMask',
90  path=main)
91 
92 # Define the variables for training.
93 # For details, please see the Continuum suppression section at https://software.belle2.org
94 # Note that KSFWVariables takes the optional additional argument FS1, to return the variables calculated from the
95 # signal-B final state particles.
96 # CleoCone also takes the optional additional argument ROE, to return the cones calculated from ROE particles only.
97 trainVars = [
98  'R2',
99  'thrustBm',
100  'thrustOm',
101  'cosTBTO',
102  'cosTBz',
103  'KSFWVariables(et)',
104  'KSFWVariables(mm2)',
105  'KSFWVariables(hso00)',
106  'KSFWVariables(hso02)',
107  'KSFWVariables(hso04)',
108  'KSFWVariables(hso10)',
109  'KSFWVariables(hso12)',
110  'KSFWVariables(hso14)',
111  'KSFWVariables(hso20)',
112  'KSFWVariables(hso22)',
113  'KSFWVariables(hso24)',
114  'KSFWVariables(hoo0)',
115  'KSFWVariables(hoo1)',
116  'KSFWVariables(hoo2)',
117  'KSFWVariables(hoo3)',
118  'KSFWVariables(hoo4)',
119  'CleoCone(1)',
120  'CleoCone(2)',
121  'CleoCone(3)',
122  'CleoCone(4)',
123  'CleoCone(5)',
124  'CleoCone(6)',
125  'CleoCone(7)',
126  'CleoCone(8)',
127  'CleoCone(9)'
128 ]
129 
130 # Target variable used in training.
131 targetVar = ['isNotContinuumEvent']
132 
133 # MVAExpert
134 main.add_module('MVAExpert', listNames=['B0'], extraInfoName='FastBDT', identifier='MVAFastBDT.root')
135 
136 # Variables from MVAExpert.
137 expertVars = ['extraInfo(FastBDT)', 'transformedNetworkOutput(FastBDT,0.1,1.0)']
138 
139 # Create output file with all sets of variables.
140 ma.variablesToNtuple(decayString='B0',
141  variables=trainVars + targetVar + expertVars,
142  treename='tree',
143  filename=outfile,
144  path=main)
145 
146 b2.process(main)
147 print(b2.statistics)
B2A714-DeepContinuumSuppression_MVAModel.batch_generator.len
len
Number of events.
Definition: B2A714-DeepContinuumSuppression_MVAModel.py:70