Belle II Software  release-05-01-25
ApplyBelleWeights.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 
14 
15 import basf2 as b2
16 import modularAnalysis as ma
17 import variables as va
18 import os
19 from b2biiConversion import convertBelleMdstToBelleIIMdst
20 
21 # Usual b2bii magic
22 os.environ['PGUSER'] = 'g0db'
23 os.environ['BELLE_POSTGRES_SERVER'] = 'can01'
24 os.environ['USE_GRAND_REPROCESS_DATA'] = '1'
25 
26 my_path = b2.create_path()
27 
28 # Adding database with weight tables
29 b2.use_central_database("BellePID")
30 
31 # Read in and convert Belle mdst
32 inputfile = b2.find_file('b2bii_input_evtgen_exp_07_BptoD0pip-D0toKpipi0-0.mdst', 'examples', False)
33 convertBelleMdstToBelleIIMdst(inputfile, path=my_path)
34 
35 """
36 In this example, we add information from three types of weight tables to the track.
37 Since there are only three types of such tables, the example is quite full.
38 Each weight table contains variables listed in the example.
39 Meaning of the variables can be found in Belle documentation (see links in the body of the example).
40 """
41 
42 ma.fillParticleList(decayString='pi+:all', cut='', path=my_path)
43 
44 """
45 Lepton ID
46 
47 Several weight tables were produced for lepton ID correction at Belle.
48 Naming scheme for payloads is:
49 
50 BelleLID<e|mu>_<LID cut>
51 
52 Study was done for different cut set for electrons and muons.
53 Possible cuts for electron:
54 0.01, 0.10, 0.50, 0.60, 0.80, 0.90
55 
56 Possible cuts for muons:
57 0.10, 0.80, 0.90, 0.95, 0.97
58 
59 Sources of original Belle results are here:
60 https://belle.kek.jp/group/pid_joint/lid/final/
61 
62 Here we add PID correction information that is important for
63 electrons selected with "eIDBelle>0.9" cut.
64 """
65 lid_table = "BelleLIDe_0.90"
66 
67 va.variables.addAlias('LID_ratio', 'extraInfo('+lid_table+'_ratio)')
68 va.variables.addAlias('LID_ratio_stat_err', 'extraInfo('+lid_table+'_ratio_stat_err)')
69 va.variables.addAlias('LID_ratio_syst_err', 'extraInfo('+lid_table+'_ratio_syst_err)')
70 va.variables.addAlias('LID_ratio_syst_err_from_2photon', 'extraInfo('+lid_table+'_ratio_syst_err_from_2photon)')
71 va.variables.addAlias('LID_ratio_syst_err_from_JPsi', 'extraInfo('+lid_table+'_ratio_syst_err_from_JPsi)')
72 va.variables.addAlias('LID_run_bin', 'extraInfo('+lid_table+'_run_bin)')
73 va.variables.addAlias('LID_kinematic_bin', 'extraInfo('+lid_table+'_kinematic_bin)')
74 
75 lid_weights = ['LID_ratio',
76  'LID_ratio_stat_err',
77  'LID_ratio_syst_err',
78  'LID_ratio_syst_err_from_2photon',
79  'LID_ratio_syst_err_from_JPsi',
80  'LID_run_bin',
81  'LID_kinematic_bin']
82 
83 reweighter = b2.register_module('ParticleWeighting')
84 reweighter.param('tableName', lid_table)
85 reweighter.param('particleList', 'pi+:all')
86 my_path.add_module(reweighter)
87 
88 
89 """
90 Kaon ID
91 
92 Several weight tables were produced for kaon/pion ID corrections at Belle.
93 Naming scheme for payloads is:
94 
95 BelleKID<K|Pi><Eff|Fake><Combined|Plus|Minus>_<PID cut:[1..9]>
96 
97 Here,
98  - K or Pi refers to correction information obtained for kaons or pions, accordingly
99  - Eff or Fake refers to efficiency or fake rate corrections
100  - Combined, Plus or Minus refers to the charge of tracks used for the study
101  - PID cut is an integer number from 1 to 9 that correspond to "cut > 0.number"
102 
103 Information is taken from here:
104  https://belle.kek.jp/group/pid_joint/kid/files-2006/
105 
106 Here we add PID correction information for pion efficiency selected as kaons with
107 "kaonIDBelle>0.1" cut.
108 
109 """
110 kid_table = "BelleKIDPiFakePlus_1"
111 
112 va.variables.addAlias('KID_eff_data', 'extraInfo('+kid_table+'_eff_data)')
113 va.variables.addAlias('KID_eff_data_stat_err', 'extraInfo('+kid_table+'_eff_data_stat_err)')
114 va.variables.addAlias('KID_eff_data_syst_err', 'extraInfo('+kid_table+'_eff_data_syst_err)')
115 va.variables.addAlias('KID_eff_mc', 'extraInfo('+kid_table+'_eff_mc)')
116 va.variables.addAlias('KID_eff_mc_stat_err', 'extraInfo('+kid_table+'_eff_mc_stat_err)')
117 va.variables.addAlias('KID_fit_flag', 'extraInfo('+kid_table+'_fit_flag)')
118 va.variables.addAlias('KID_kinematic_bin', 'extraInfo('+kid_table+'_kinematic_bin)')
119 va.variables.addAlias('KID_ratio', 'extraInfo('+kid_table+'_ratio)')
120 va.variables.addAlias('KID_ratio_stat_err', 'extraInfo('+kid_table+'_ratio_stat_err)')
121 va.variables.addAlias('KID_ratio_syst_err', 'extraInfo('+kid_table+'_ratio_syst_err)')
122 va.variables.addAlias('KID_run_bin', 'extraInfo('+kid_table+'_run_bin)')
123 
124 kid_weights = ['KID_eff_data',
125  'KID_eff_data_stat_err',
126  'KID_eff_data_syst_err',
127  'KID_eff_mc',
128  'KID_eff_mc_stat_err',
129  'KID_fit_flag',
130  'KID_ratio',
131  'KID_ratio_stat_err',
132  'KID_ratio_syst_err',
133  'KID_run_bin',
134  'KID_kinematic_bin']
135 
136 reweighter2 = b2.register_module('ParticleWeighting')
137 reweighter2.param('tableName', kid_table)
138 reweighter2.param('particleList', 'pi+:all')
139 my_path.add_module(reweighter2)
140 
141 
142 """
143 Proton ID
144 
145 Several weight tables were produced for proton ID corrections at Belle.
146 Naming scheme for payloads is:
147 
148 BellePID<Plus|Minus>_<PID cut>
149 
150 Here,
151  - Plus or Minus refers to the charge of tracks used for the study
152  - PID cut can be 0.6, 0.7, 0.8 or 0.9
153 
154 Information is taken from here:
155  https://belle.kek.jp/group/pid_joint/protonid/
156 
157 Here we add PID correction information for proton efficiency selected with
158 "protonIDBelle>0.6" cut.
159 """
160 pid_table = "BellePIDPlus_0.6"
161 
162 va.variables.addAlias("PID_eff_data", 'extraInfo('+pid_table+"_eff_data)")
163 va.variables.addAlias("PID_eff_data_stat_err", 'extraInfo('+pid_table+"_eff_data_stat_err)")
164 va.variables.addAlias("PID_eff_mc", 'extraInfo('+pid_table+"_eff_mc)")
165 va.variables.addAlias("PID_eff_mc_stat_err", 'extraInfo('+pid_table+"_eff_mc_stat_err)")
166 va.variables.addAlias("PID_ratio", 'extraInfo('+pid_table+"_ratio)")
167 va.variables.addAlias("PID_ratio_stat_err", 'extraInfo('+pid_table+"_ratio_stat_err)")
168 va.variables.addAlias("PID_ratio_syst_err", 'extraInfo('+pid_table+"_ratio_syst_err)")
169 va.variables.addAlias("PID_kinematic_bin", 'extraInfo('+pid_table+"_kinematic_bin)")
170 va.variables.addAlias("PID_run_bin", 'extraInfo('+pid_table+"_run_bin)")
171 
172 pid_weights = ["PID_eff_data",
173  "PID_eff_data_stat_err",
174  "PID_eff_mc",
175  "PID_eff_mc_stat_err",
176  "PID_ratio",
177  "PID_ratio_stat_err",
178  "PID_ratio_syst_err",
179  "PID_run_bin",
180  "PID_kinematic_bin"]
181 
182 
183 reweighter3 = b2.register_module('ParticleWeighting')
184 reweighter3.param('tableName', pid_table)
185 reweighter3.param('particleList', 'pi+:all')
186 my_path.add_module(reweighter3)
187 
188 """
189 All weighting modules are configured.
190 Let's add some tracks and add the information from weighting tables to them.
191 """
192 pivars = ['p', 'pz']
193 pivars += kid_weights
194 pivars += lid_weights
195 pivars += pid_weights
196 
197 
198 # Saving variables to ntuple
199 output_file = 'ApplyBelleWeightsToTracks.root'
200 ma.variablesToNtuple(decayString='pi+:all',
201  variables=pivars,
202  treename='pion',
203  filename=output_file,
204  path=my_path)
205 
206 # Process the events
207 b2.process(my_path)
208 
209 # print out the summary
210 print(b2.statistics)