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