Belle II Software  release-08-01-10
B2A701-ContinuumSuppression_Input.py
1 #!/usr/bin/env python3
2 
3 
10 
11 
24 
25 import basf2 as b2
26 import modularAnalysis as ma
27 import sys
28 
29 
30 # --I/O----------------------------------------------------------------------------------------
31 # Please note that the files here are quite small and needed just to test the script.
32 # To obtain meaningful output to be used in B2A702 and B2A703 please use grid.
33 # > gbasf2 B2A701-ContinuumSuppression_Input.py -i <.mdst file addresses> -p <project name> -s <release_number>
34 #
35 # For B2A70{2|3} you need four sets of ntuples:
36 # -Train dataset composed of signal (B->KsPi0 ) and background MC (continuum MC) samples
37 # -Test dataset composed of signal (B->KsPi0 ) and background MC (continuum MC) samples
38 # -Signal dataset only
39 # -Background dataset only
40 # For each of this four sets you need to submit separate grid job.
41 # (Or use files that are already prepared for B2A702 and B2A703).
42 
43 
44 input_file_list = []
45 
46 # Please pick a meaningful output name when running on the grid
47 outfile = "B2A702_output.root"
48 
49 step = 'train'
50 
51 if len(sys.argv) >= 2:
52  if sys.argv[1] not in ['train', 'test', 'apply_signal', 'apply_qqbar']:
53  sys.exit("usage:\n\tbasf2 B2A701-ContinuumSuppression_Input.py <train,test,apply_signal,apply_qqbar>")
54  else:
55  step = str(sys.argv[1])
56 
57 if step == 'train':
58  input_file_list = [b2.find_file('ccbar_sample_to_train.root', 'examples', False),
59  b2.find_file('Bd2K0spi0_to_train.root', 'examples', False)]
60 elif step == 'test':
61  input_file_list = [b2.find_file('ccbar_sample_to_test.root', 'examples', False),
62  b2.find_file('Bd2K0spi0_to_test.root', 'examples', False)]
63 elif step == 'apply_signal':
64  input_file_list = [b2.find_file('Bd2K0spi0_to_test.root', 'examples', False)]
65 elif step == 'apply_qqbar':
66  input_file_list = [b2.find_file('ccbar_sample_to_test.root', 'examples', False)]
67 else:
68  sys.exit('Step does not match any of the available samples: `train`, `test`, `apply_signal`or `apply_qqbar`')
69 outfile = step + '.root'
70 
71 # ---------------------------------------------------------------------------------------------
72 
73 # Perform analysis.
74 my_path = b2.create_path()
75 
76 ma.inputMdstList(filelist=input_file_list,
77  path=my_path)
78 
79 ma.fillParticleList(decayString='gamma:all',
80  cut='',
81  path=my_path)
82 ma.fillParticleList(decayString='pi+:good',
83  cut='chiProb > 0.001 and pionID > 0.5',
84  path=my_path)
85 ma.fillParticleList(decayString='pi-:good',
86  cut='chiProb > 0.001 and pionID > 0.5',
87  path=my_path)
88 
89 ma.reconstructDecay(decayString='K_S0 -> pi+:good pi-:good',
90  cut='0.480<=M<=0.516',
91  dmID=1,
92  path=my_path)
93 ma.reconstructDecay(decayString='pi0 -> gamma:all gamma:all',
94  cut='0.115<=M<=0.152',
95  dmID=1,
96  path=my_path)
97 ma.reconstructDecay(decayString='B0 -> K_S0 pi0',
98  cut='5.2 < Mbc < 5.3 and -0.3 < deltaE < 0.2',
99  path=my_path)
100 
101 ma.matchMCTruth(list_name='B0', path=my_path)
102 ma.buildRestOfEvent(target_list_name='B0', path=my_path)
103 
104 # The momentum cuts used to be hard-coded in the continuum suppression module. They can now be applied
105 # via this mask. The nCDCHits requirement is new, and is recommended to remove VXD-only fake tracks.
106 cleanMask = ('cleanMask', 'nCDCHits > 0 and useCMSFrame(p)<=3.2', 'p >= 0.05 and useCMSFrame(p)<=3.2')
107 ma.appendROEMasks(list_name='B0',
108  mask_tuples=[cleanMask],
109  path=my_path)
110 
111 ma.buildContinuumSuppression(list_name='B0',
112  roe_mask='cleanMask',
113  path=my_path)
114 
115 # Define the variables for training.
116 # For details, please see the Continuum suppression section at https://software.belle2.org
117 # Note that KSFWVariables takes the optional additional argument FS1, to return the variables calculated from the
118 # signal-B final state particles.
119 trainVars = [
120  'R2',
121  'thrustBm',
122  'thrustOm',
123  'cosTBTO',
124  'cosTBz',
125  'KSFWVariables(et)',
126  'KSFWVariables(mm2)',
127  'KSFWVariables(hso00)',
128  'KSFWVariables(hso02)',
129  'KSFWVariables(hso04)',
130  'KSFWVariables(hso10)',
131  'KSFWVariables(hso12)',
132  'KSFWVariables(hso14)',
133  'KSFWVariables(hso20)',
134  'KSFWVariables(hso22)',
135  'KSFWVariables(hso24)',
136  'KSFWVariables(hoo0)',
137  'KSFWVariables(hoo1)',
138  'KSFWVariables(hoo2)',
139  'KSFWVariables(hoo3)',
140  'KSFWVariables(hoo4)',
141  'CleoConeCS(1)',
142  'CleoConeCS(2)',
143  'CleoConeCS(3)',
144  'CleoConeCS(4)',
145  'CleoConeCS(5)',
146  'CleoConeCS(6)',
147  'CleoConeCS(7)',
148  'CleoConeCS(8)',
149  'CleoConeCS(9)'
150 ]
151 
152 # Save target variable necessary for training.
153 targetVar = ['isNotContinuumEvent']
154 
155 # Create output file.
156 ma.variablesToNtuple(decayString='B0',
157  variables=trainVars + targetVar,
158  treename='tree',
159  filename=outfile,
160  path=my_path)
161 
162 b2.process(my_path)
163 print(b2.statistics)