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