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