Belle II Software  release-06-01-15
B2A713-DeepContinuumSuppression_MVAExpert.py
1 #!/usr/bin/env python3
2 
3 
10 
11 
31 
32 import basf2
33 import modularAnalysis as ma
34 from stdV0s import stdKshorts
35 from stdPi0s import stdPi0s
36 from vertex import TagV
37 import variables as v
38 import glob
39 import sys
40 import numpy as np
41 
42 basf2.set_log_level(basf2.LogLevel.ERROR)
43 
44 # --I/O----------------------------------------------------------------------------------------
45 step = 'signal'
46 
47 if len(sys.argv) >= 2:
48  if sys.argv[1] not in ['signal', 'qqbar']:
49  sys.exit("usage:\n\tbasf2 B2A713-DeepContinuumSuppression_MVAExpert.py <signal,qqbar>")
50  else:
51  step = str(sys.argv[1])
52 
53 if step == 'signal':
54  input_file_list = [basf2.find_file('Bd2K0spi0_to_test.root', 'examples', False)]
55 elif step == 'qqbar':
56  input_file_list = [basf2.find_file('ccbar_sample_to_test.root', 'examples', False)]
57 else:
58  sys.exit('Step does not match any of the available samples: `signal` or `qqbar`')
59 
60 outfile = 'MVAExpert_fullNTuple_' + step + '.root'
61 # ---------------------------------------------------------------------------------------------
62 
63 main = basf2.create_path()
64 
65 # Perform analysis.
66 ma.inputMdstList('default', input_file_list, path=main)
67 
68 main.add_module('ProgressBar')
69 
70 # Build B candidate like in B2A701-ContinuumSuppression_Input.py
71 stdKshorts(path=main)
72 stdPi0s('eff40_May2020', path=main)
73 ma.reconstructDecay('B0 -> K_S0:merged pi0:eff40_May2020', '5.2 < Mbc < 5.3 and -0.3 < deltaE < 0.2', path=main)
74 
75 ma.matchMCTruth('B0', path=main)
76 ma.buildRestOfEvent('B0', path=main)
77 
78 cleanMask = ('cleanMask', 'nCDCHits > 0 and useCMSFrame(p)<=3.2', 'p >= 0.05 and useCMSFrame(p)<=3.2')
79 ma.appendROEMasks('B0', [cleanMask], path=main)
80 
81 ma.buildContinuumSuppression('B0', 'cleanMask', path=main)
82 
83 # Accept only correctly reconstructed B candidates as signal
84 ma.applyCuts('B0', 'isSignal or isContinuumEvent', path=main)
85 
86 # Tag B candidate for Vertex information
87 TagV('B0', path=main)
88 
89 # Loop over each possible ROE (1 for every B candidate) in every event
90 roe_path = basf2.create_path()
91 
92 deadEndPath = basf2.create_path()
93 
94 ma.signalSideParticleFilter('B0', '', roe_path, deadEndPath)
95 
96 # Build particle lists for low level variables
97 ma.fillParticleList('gamma:roe', 'isInRestOfEvent == 1 and goodBelleGamma == 1', path=roe_path)
98 ma.fillParticleList('gamma:signal', 'isInRestOfEvent == 0 and goodBelleGamma == 1', path=roe_path)
99 ma.fillParticleList('pi+:chargedProe', 'isInRestOfEvent == 1', path=roe_path)
100 ma.fillParticleList('pi+:chargedPsignal', 'isInRestOfEvent == 0', path=roe_path)
101 ma.fillParticleList('pi-:chargedMroe', 'isInRestOfEvent == 1', path=roe_path)
102 ma.fillParticleList('pi-:chargedMsignal', 'isInRestOfEvent == 0', path=roe_path)
103 
104 v.variables.addAlias('cmsp', 'useCMSFrame(p)')
105 
106 ma.rankByHighest('gamma:roe', 'cmsp', path=roe_path)
107 ma.rankByHighest('gamma:signal', 'cmsp', path=roe_path)
108 ma.rankByHighest('pi+:chargedProe', 'cmsp', path=roe_path)
109 ma.rankByHighest('pi+:chargedPsignal', 'cmsp', path=roe_path)
110 ma.rankByHighest('pi-:chargedMroe', 'cmsp', path=roe_path)
111 ma.rankByHighest('pi-:chargedMsignal', 'cmsp', path=roe_path)
112 
113 # Define traditional Continuum Suppression Variables
114 contVars = [
115  'R2',
116  'thrustBm',
117  'thrustOm',
118  'cosTBTO',
119  'cosTBz',
120  'KSFWVariables(et)',
121  'KSFWVariables(mm2)',
122  'KSFWVariables(hso00)',
123  'KSFWVariables(hso02)',
124  'KSFWVariables(hso04)',
125  'KSFWVariables(hso10)',
126  'KSFWVariables(hso12)',
127  'KSFWVariables(hso14)',
128  'KSFWVariables(hso20)',
129  'KSFWVariables(hso22)',
130  'KSFWVariables(hso24)',
131  'KSFWVariables(hoo0)',
132  'KSFWVariables(hoo1)',
133  'KSFWVariables(hoo2)',
134  'KSFWVariables(hoo3)',
135  'KSFWVariables(hoo4)',
136  'CleoConeCS(1)',
137  'CleoConeCS(2)',
138  'CleoConeCS(3)',
139  'CleoConeCS(4)',
140  'CleoConeCS(5)',
141  'CleoConeCS(6)',
142  'CleoConeCS(7)',
143  'CleoConeCS(8)',
144  'CleoConeCS(9)'
145 ]
146 
147 # Define additional low level variables
148 basic_variables = ['p', 'phi', 'cosTheta', 'pErr', 'phiErr', 'cosThetaErr']
149 vertex_variables = ['distance', 'dphi', 'dcosTheta']
150 cluster_specific_variables = ['clusterNHits', 'clusterTiming', 'clusterE9E25', 'clusterReg', 'isInRestOfEvent']
151 track_specific_variables = ['kaonID', 'electronID', 'muonID', 'protonID', 'pValue', 'nCDCHits', 'isInRestOfEvent', 'charge']
152 
153 for variablename in basic_variables + vertex_variables:
154  v.variables.addAlias('thrustsig' + variablename, 'useBThrustFrame(' + variablename + ',Signal)')
155 
156 cluster_variables = cluster_specific_variables[:]
157 for variablename in basic_variables:
158  cluster_variables.append('thrustsig' + variablename)
159 
160 track_variables = track_specific_variables
161 for variablename in basic_variables + vertex_variables:
162  track_variables.append('thrustsig' + variablename)
163 
164 variables = ['isContinuumEvent', 'isNotContinuumEvent', 'isSignal', 'M', 'p', 'Mbc', 'DeltaZ',
165  'deltaE', 'daughter(0, M)', 'daughter(0, p)', 'daughter(1, M)', 'daughter(1, p)']
166 for rank in range(10):
167  for shortcut, particlelist in [('Croe', 'gamma:roe'), ('Csig', 'gamma:signal')]:
168  for variable in cluster_variables:
169  v.variables.addAlias(
170  f'{variable}_{shortcut}{rank}', f'getVariableByRank({particlelist}, cmsp, {variable}, {rank + 1})')
171  variables.append(f'{variable}_{shortcut}{rank}')
172 
173 for rank in range(5):
174  for shortcut, particlelist in [('TProe', 'pi+:chargedProe'), ('TPsig', 'pi+:chargedPsignal'),
175  ('TMroe', 'pi+:chargedMroe'), ('TMsig', 'pi+:chargedMsignal')]:
176  for variable in track_variables:
177  v.variables.addAlias(
178  f'{variable}_{shortcut}{rank}', f'getVariableByRank({particlelist}, cmsp, {variable}, {rank + 1})')
179  variables.append(f'{variable}_{shortcut}{rank}')
180 
181 # MVAExpert
182 roe_path.add_module('MVAExpert', listNames=['B0'], extraInfoName='Deep_CS', identifier='Deep_Feed_Forward.xml')
183 
184 # Variables from MVAExpert.
185 expertVars = ['extraInfo(Deep_CS)', 'transformedNetworkOutput(Deep_CS,0.1,1.0)']
186 
187 # Create output file with all sets of variables.
188 ma.variablesToNtuple('B0', variables + expertVars, treename='tree', filename=outfile, path=roe_path)
189 
190 main.for_each('RestOfEvent', 'RestOfEvents', roe_path)
191 
192 basf2.process(main)
193 print(basf2.statistics)