Belle II Software  release-06-00-14
B2A202-LoadReconstructedParticles.py
1 #!/usr/bin/env python3
2 
3 
10 
11 
25 
26 import basf2 as b2
27 import modularAnalysis as ma
28 import variables.collections as vc
29 import variables.utils as vu
30 from stdV0s import stdKshorts
31 from stdPi0s import stdPi0s
32 from stdKlongs import stdKlongs
33 
34 # create path
35 my_path = b2.create_path()
36 
37 # load input ROOT file
38 ma.inputMdst(environmentType='default',
39  filename=b2.find_file('B2pi0D_D2hh_D2hhh_B2munu.root', 'examples', False),
40  path=my_path)
41 
42 # print contents of the DataStore before loading Particles
43 ma.printDataStore(path=my_path)
44 
45 # create and fill gamma/e/mu/pi/K/p/n ParticleLists
46 # second argument are the selection criteria: '' means no cut, take all
47 #
48 # note that you can give any name to your lists e.g. 'gamma:mycandidates',
49 # except for the name 'all' which is the only name that is reserved for lists
50 # with no cuts
51 ma.fillParticleList(decayString='gamma:all', cut='', path=my_path)
52 ma.fillParticleList(decayString='e-:all', cut='', path=my_path)
53 ma.fillParticleList(decayString='mu-:all', cut='', path=my_path)
54 ma.fillParticleList(decayString='pi-:all', cut='', path=my_path)
55 ma.fillParticleList(decayString='K-:all', cut='', path=my_path)
56 ma.fillParticleList(decayString='anti-p-:all', cut='', path=my_path)
57 ma.fillParticleList(decayString='anti-n0:all', cut='', path=my_path)
58 
59 # alternatively, we can create and fill final state Particle lists only
60 # with candidates that pass certain PID requirements
61 ma.fillParticleList(decayString='gamma:highE', cut='E > 1.0', path=my_path)
62 ma.fillParticleList(decayString='e+:good', cut='electronID > 0.1', path=my_path)
63 ma.fillParticleList(decayString='mu+:good', cut='muonID > 0.1', path=my_path)
64 ma.fillParticleList(decayString='pi+:good', cut='protonID > 0.1', path=my_path)
65 ma.fillParticleList(decayString='K+:good', cut='kaonID > 0.1', path=my_path)
66 ma.fillParticleList(decayString='p+:good', cut='protonID > 0.1', path=my_path)
67 
68 # another possibility is to use default functions
69 # for example stdKshorts() from stdV0s.py that:
70 # - takes all V0 candidates, performs vertex fit, and fills 'K_S0:merged' ParticleList
71 # (-> for more details about V0s have a look at B2A203-LoadV0s.py)
72 # or for example stdPi0s() from stdPi0s.py:
73 stdKshorts(prioritiseV0=True, path=my_path)
74 stdPi0s(listtype='eff10_May2020Fit', path=my_path)
75 stdPi0s(listtype='eff20_May2020Fit', path=my_path)
76 stdPi0s(listtype='eff30_May2020Fit', path=my_path)
77 stdPi0s(listtype='eff40_May2020Fit', path=my_path)
78 stdPi0s(listtype='eff50_May2020Fit', path=my_path)
79 stdPi0s(listtype='eff60_May2020Fit', path=my_path)
80 stdKlongs(listtype='allklm', path=my_path) # only 'allklm' is recommended at the moment
81 
82 # print contents of the DataStore after loading Particles
83 ma.printDataStore(path=my_path)
84 
85 # print out the contents of each ParticleList
86 ma.printList('gamma:all', False, path=my_path)
87 ma.printList('gamma:highE', False, path=my_path)
88 ma.printList('e-:all', False, path=my_path)
89 ma.printList('e-:good', False, path=my_path)
90 ma.printList('mu-:all', False, path=my_path)
91 ma.printList('mu-:good', False, path=my_path)
92 ma.printList('pi-:all', False, path=my_path)
93 ma.printList('pi-:good', False, path=my_path)
94 ma.printList('K-:all', False, path=my_path)
95 ma.printList('K-:good', False, path=my_path)
96 ma.printList('anti-p-:all', False, path=my_path)
97 ma.printList('anti-p-:good', False, path=my_path)
98 ma.printList('K_S0:merged', False, path=my_path)
99 ma.printList('pi0:eff40_May2020Fit', False, path=my_path)
100 ma.printList('K_L0:allklm', False, path=my_path)
101 ma.printList('n0:all', False, path=my_path)
102 
103 
104 # Select variables that we want to store to ntuple
105 # You can either use preselected variable groups from variableCollections
106 # or use your own lists. Both options are shown here.
107 # For more information on the VariableManager, VariableCollections, etc.,
108 # please refer to the dedicated VariableManager examples.
109 
110 # Note: vc.<collection> is a list (of variables); multiple lists are
111 # concatenated with the + operator.
112 
113 charged_particle_variables = vc.reco_stats + \
114  vc.kinematics + \
115  vc.track + \
116  vc.track_hits + \
117  vc.pid + \
118  vc.mc_truth + \
119  vc.mc_kinematics
120 
121 gamma_variables = vc.kinematics + \
122  vc.mc_kinematics + \
123  vc.cluster
124 
125 K0s_variables = vc.kinematics + \
126  vc.inv_mass + \
127  vc.vertex + \
128  vc.mc_vertex + \
129  vc.pid + \
130  vc.mc_truth + \
131  ['dr', 'dz', 'isSignal', 'chiProb']
132 
133 pi0_variables = vc.mc_truth + \
134  vc.kinematics + \
135  ['extraInfo(BDT)', 'decayAngle(0)']
136 
137 K0l_variables = vc.kinematics + \
138  vc.mc_kinematics + \
139  vc.klm_cluster
140 
141 n0_variables = K0l_variables + \
142  ['isFromECL', 'isFromKLM']
143 
144 # Saving variables to ntuple
145 output_file = 'B2A202-LoadReconstructedParticles.root'
146 ma.variablesToNtuple(decayString='pi+:all',
147  variables=charged_particle_variables,
148  treename='pion',
149  filename=output_file,
150  path=my_path)
151 ma.variablesToNtuple(decayString='K+:all',
152  variables=charged_particle_variables,
153  treename='kaon',
154  filename=output_file,
155  path=my_path)
156 ma.variablesToNtuple(decayString='e+:all',
157  variables=charged_particle_variables,
158  treename='elec',
159  filename=output_file,
160  path=my_path)
161 ma.variablesToNtuple(decayString='mu+:all',
162  variables=charged_particle_variables,
163  treename='muon',
164  filename=output_file,
165  path=my_path)
166 ma.variablesToNtuple(decayString='gamma:all',
167  variables=gamma_variables,
168  treename='phot',
169  filename=output_file,
170  path=my_path)
171 ma.variablesToNtuple(decayString='K_L0:allklm',
172  variables=K0l_variables,
173  treename='klong',
174  filename=output_file,
175  path=my_path)
176 ma.variablesToNtuple(decayString='n0:all',
177  variables=n0_variables,
178  treename='neutron',
179  filename=output_file,
180  path=my_path)
181 
182 # Note here, that since we want to get info about gammas from pi0,
183 # we convert names of the variables from the gamma list in the way that they will
184 # correspond to given gammas.
185 ma.variablesToNtuple(decayString='pi0:eff40_May2020Fit',
186  variables=pi0_variables + vu.create_aliases_for_selected(gamma_variables, 'pi0 -> ^gamma ^gamma'),
187  filename=output_file,
188  treename='pi0',
189  path=my_path)
190 
191 # Here for pions from K0s we do the same thing, but here we add custom aliases
192 # (see ntuples to see the difference)
193 ma.variablesToNtuple(decayString='K_S0:merged',
194  variables=K0s_variables +
195  vu.create_aliases_for_selected(charged_particle_variables, 'K_S0 -> ^pi+ pi-', 'pip') +
196  vu.create_aliases_for_selected(charged_particle_variables, 'K_S0 -> pi+ ^pi-', 'pim'),
197  filename=output_file,
198  treename='kshort',
199  path=my_path)
200 
201 # Process the events
202 b2.process(my_path)
203 
204 # print out the summary
205 print(b2.statistics)