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