Belle II Software development
vertex.py
1#!/usr/bin/env python3
2
3
10
11from basf2 import register_module
12from basf2 import B2WARNING
13
14
15def fitVertex(
16 list_name,
17 conf_level,
18 decay_string='',
19 fitter='KFit',
20 fit_type='vertex',
21 constraint='',
22 massConstraint=[],
23 recoilMass=0,
24 daughtersUpdate=False,
25 smearing=0,
26 path=None,
27):
28 """
29 Perform the specified fit for each Particle in the given ParticleList.
30
31 Info:
32 Direct use of `fitVertex` is not recommended unless you know what you are doing.
33 If you're unsure, you probably want to use `treeFit` or `kFit`.
34 Parameters:
35 list_name (str): name of the input ParticleList
36 conf_level (float): minimum value of the confidence level to accept the fit.
37 Setting this parameter to -1 selects all particle candidates.
38 The value of 0 rejects the particle candidates with failed fit.
39 decay_string (str): select particles used for the vertex fit
40 fitter (str): Rave or KFit
41 fit_type (str): type of the kinematic fit (valid options are vertex/massvertex/mass/fourC/massfourC/recoilmass)
42 constraint (str): add additional constraint to the fit (valid options are empty string/ipprofile/iptube/mother)
43 massConstraint (list(int) or list(str)): list of PDG ids or Names of the particles which are mass-constrained
44 recoilMass (float): invariant mass of recoil in GeV (valid only for recoilmass)
45 daughtersUpdate (bool): make copy of the daughters and update them after the vertex fit
46 smearing (float) : IP tube width is smeared by this value (cm). meaningful only with 'KFit/vertex/iptube' option.
47 path (basf2.Path): modules are added to this path
48 """
49
50 warning = (
51 "Direct use of fitVertex is not recommended unless you know what you are doing.\n"
52 "Please use treeFit or kFit.\n"
53 "See documentation at https://software.belle2.org"
54 )
55
56 B2WARNING(warning)
57
58 _fitVertex(
59 list_name, conf_level, decay_string, fitter, fit_type, constraint,
60 massConstraint, recoilMass, daughtersUpdate, smearing, path)
61
62
63def _fitVertex(
64 list_name,
65 conf_level,
66 decay_string='',
67 fitter='KFit',
68 fit_type='vertex',
69 constraint='',
70 massConstraint=[],
71 recoilMass=0,
72 daughtersUpdate=False,
73 smearing=0,
74 path=None,
75):
76 """
77 An internal function, performs the specified fit for each Particle in the given ParticleList.
78
79 Warning:
80 This is a private function, please use one of the aliases provided.
81
82 Parameters:
83 list_name (str): name of the input ParticleList
84 conf_level (float): minimum value of the confidence level to accept the fit.
85 Setting this parameter to -1 selects all particle candidates.
86 The value of 0 rejects the particle candidates with failed fit.
87 decay_string (str): select particles used for the vertex fit
88 fitter (str): Rave or KFit
89 fit_type (str): type of the kinematic fit (valid options are vertex/massvertex/mass/fourC/massfourC/recoilmass)
90 constraint (str): add additional constraint to the fit (valid options are empty string/ipprofile/iptube/mother)
91 massConstraint (list(int) or list(str)): list of PDG ids or Names of the particles which are mass-constrained
92 recoilMass (float): invariant mass of recoil in GeV (valid only for recoilmass)
93 daughtersUpdate (bool): make copy of the daughters and update them after the vertex fit
94 smearing (float) : IP tube width is smeared by this value (cm). meaningful only with 'KFit/vertex/iptube' option.
95 path (basf2.Path): modules are added to this path
96 """
97
98 from pdg import from_names
99
100 pvfit = register_module('ParticleVertexFitter')
101 pvfit.set_name('ParticleVertexFitter_' + list_name)
102 pvfit.param('listName', list_name)
103 pvfit.param('confidenceLevel', conf_level)
104 pvfit.param('vertexFitter', fitter)
105 pvfit.param('fitType', fit_type)
106 pvfit.param('withConstraint', constraint)
107 pvfit.param('updateDaughters', daughtersUpdate)
108 pvfit.param('decayString', decay_string)
109 pvfit.param('recoilMass', recoilMass)
110 pvfit.param('smearing', smearing)
111 if massConstraint:
112 pvfit.param('massConstraintList', from_names(massConstraint))
113 path.add_module(pvfit)
114
115
116def kFit(list_name,
117 conf_level,
118 fit_type='vertex',
119 constraint='',
120 daughtersUpdate=False,
121 decay_string='',
122 massConstraint=[],
123 recoilMass=0,
124 smearing=0,
125 path=None):
126 """
127 Perform KFit for each Particle in the given ParticleList.
128
129 Parameters:
130 list_name (str): name of the input ParticleList
131 conf_level (float): minimum value of the confidence level to accept the fit
132 Setting this parameter to -1 selects all particle candidates.
133 The value of 0 rejects particle candidates with a failed fit.
134 fit_type (str): type of the kinematic fit. Valid options are
135
136 * ``mass`` for a mass-constrained fit
137 * ``vertex`` for a vertex fit
138 * ``massvertex`` for a vertex fit with a mass constraint on the mother particle
139 * ``fourC`` for a vertex fit in which the mother particle's four-momentum is constrained to the beam four-momentum
140 * ``massfourC`` for a vertex fit with a 4-momentum constraint and mass constraints on the specified daughter particles
141 * ``recoilmass`` for kinematic fit in which the mass of the mother particle's recoil four-momentum with respect
142 to the beam four-momentum is constrained
143
144 constraint (str): add an additional constraint to the fit (valid options are ipprofile or iptube)
145 massConstraint (list(int) or list(str)): list of PDG ids or Names of the particles which are mass-constrained
146 recoilMass (float): invariant mass of recoil in GeV (valid only for recoilmass)
147 daughtersUpdate (bool): make copy of the daughters and update them after the KFit
148 decay_string (str): select particles used for the KFit
149 smearing (float) : IP tube width is smeared by this value (cm). meaningful only with 'iptube' constraint.
150 path (basf2.Path): modules are added to this path
151 """
152
153 _fitVertex(
154 list_name, conf_level, decay_string, 'KFit', fit_type, constraint,
155 massConstraint, recoilMass, daughtersUpdate, smearing, path)
156
157
158def raveFit(
159 list_name,
160 conf_level,
161 fit_type='vertex',
162 decay_string='',
163 constraint='',
164 daughtersUpdate=False,
165 path=None,
166 silence_warning=False,
167):
168 """
169 Performs a vertex fit using `RAVE <https://github.com/rave-package>`_
170 for each Particle in the given ParticleList.
171
172 Warning:
173 `RAVE <https://github.com/rave-package>`_ is deprecated since it is not maintained.
174 Whilst we will not remove RAVE, it is not recommended for analysis use, other than benchmarking or legacy studies.
175 Instead, we recommend :doc:`TreeFitter` (`vertex.treeFit`) or `vertex.kFit`.
176
177 Parameters:
178 list_name (str): name of the input ParticleList
179 conf_level (float): minimum value of the confidence level to accept the fit.
180 Setting this parameter to -1 selects all particle candidates.
181 The value of 0 rejects the particle candidates with failed fit.
182 fit_type (str): type of the RAVE vertex fit.Valid options are
183
184 * ``mass`` for a mass-constrained fit (the 7x7 error matrix of the mother particle has to be defined)
185 * ``vertex`` for a vertex fit without any mass constraint
186 * ``massvertex`` for a mass-constrained vertex fit
187
188 decay_string (str): select particles used for the vertex fit
189 constraint (str): add additional constraint to the fit
190 (valid options are ipprofile or iptube).
191 daughtersUpdate (bool): make copy of the daughters and update them after the Rave vertex fit
192 path (basf2.Path): modules are added to this path
193 silence_warning (bool): silence the warning advertising TreeFitter use
194 """
195
196 # verbose deprecation message
197 message_a = (
198 "RAVE is deprecated since it is not maintained.\n"
199 "Whilst we will not remove RAVE, it is not recommended for analysis use, other than benchmarking or legacy studies.\n"
200 "Instead, we recommend TreeFitter (treeFit) or KFit.\n"
201 "Try: \n treeFit(\'" + list_name + "\'," + str(conf_level) +
202 ", updateAllDaughters=False, path=mypath)\n"
203 )
204 message_b = "To silence this warning, add silence_warning=True when you call this function."
205
206 # if the user wants a constraint, they should check the doc, or send a ticket if it's not implemented
207 if constraint == '':
208 message_if = ""
209 else:
210 message_if = (
211 "Please consult the documentation at https://software.belle2.org \n"
212 "(search for TreeFitter) for special constraints.\n"
213 )
214
215 if not silence_warning:
216 B2WARNING(message_a + message_if + message_b)
217
218 _fitVertex(list_name, conf_level, decay_string, 'Rave', fit_type, constraint, None, 0, daughtersUpdate, 0, path)
219
220
221def treeFit(
222 list_name,
223 conf_level=0.001,
224 massConstraint=[],
225 ipConstraint=False,
226 updateAllDaughters=False,
227 massConstraintDecayString='',
228 massConstraintMassValues=[],
229 customOriginConstraint=False,
230 customOriginVertex=[0.001, 0, 0.0116],
231 customOriginCovariance=[0.0048, 0, 0, 0, 0.003567, 0, 0, 0, 0.0400],
232 originDimension=3,
233 treatAsInvisible='',
234 ignoreFromVertexFit='',
235 path=None,
236):
237 """
238 Perform a :doc:`TreeFitter` fit for each Particle in the given ParticleList.
239
240 :Example:
241 An example of usage for the decay chain :math:`B^0\\to\\pi^+\\pi^-\\pi^0` is the following:
242
243 ::
244
245 reconstructDecay('pi0:A -> gamma:pi0 gamma:pi0', '0.130 < InvM < 0.14', path=mypath)
246 reconstructDecay('B0:treefit -> pi+:my pi-:my pi0:A ', '', path=mypath)
247 treeFit('B0:treefit', ipConstraint=True, path=mypath)
248
249
250 .. note::
251 The Particle object loaded from the KLMCluster does not have proper covariance matrix yet (by release-06 at least).
252 The performance of TreeFit with these particles is not guaranteed.
253 Alternatively, one can perform the TreeFit ignoring these particles from the vertex fit with
254 the option ``ignoreFromVertexFit``.
255
256
257 Parameters:
258 list_name (str): name of the input ParticleList
259 conf_level (float): minimum value of the confidence level to accept the fit.
260 Setting this parameter to -1 selects all particle candidates.
261 The value of 0 rejects the particle candidates with failed fit.
262 massConstraint (list(int, str)): list of PDG ids or names of the particles which are mass-constrained
263 ipConstraint (bool): constrain head production vertex to IP (x-y-z) constraint
264 updateAllDaughters (bool): if true the entire tree will be updated with the fitted values
265 for momenta and vertex position. Otherwise only the momenta of the head of the tree will be updated,
266 however for all daughters we also update the vertex position with the fit results as this would
267 otherwise be set to {0, 0, 0} contact us if this causes any hardship/confusion.
268 massConstraintDecayString (str): Decay string to select which particles' mass should be constrained
269 massConstraintMassValues (list(float, int, str)): list of invariant masses to be used for the mass constraints
270 of the particles selected via the decay string massConstraintDecayString. A floating point value is taken directly
271 while an integer or string is interpreted as defining the PDG id or name of a particle
272 and the corresponding PDG value is taken
273 customOriginConstraint (bool): use a custom origin vertex as the production vertex of your particle.
274 This is useful when fitting D*/D without wanting to fit a B but constraining the process to be B-decay-like.
275 (think of semileptonic modes and stuff with a neutrino in the B decay).
276 customOriginVertex (list(float)): 3d vector of the vertex coordinates you want to use as custom origin.
277 Default numbers are taken for B-mesons
278 customOriginCovariance (list(float)): 3x3 covariance matrix for the custom vertex (type: vector).
279 Default numbers extracted from generator distribution width of B-mesons.
280 originDimension (int): If the origin or IP constraint (``customOriginVertex`` or ``ipConstraint``) are used,
281 this specifies the dimension of the constraint (3D or 2D).
282 treatAsInvisible (str): Decay string to select one particle that will be treated as invisible in the fit.
283 ignoreFromVertexFit (str): Decay string to select particles that will be ignored to determine the vertex position.
284 path (basf2.Path): modules are added to this path
285 """
286
287 import pdg
288
289 treeFitter = register_module("TreeFitter")
290 treeFitter.set_name('TreeFitter_' + list_name)
291 if massConstraint:
292 treeFitter.param('massConstraintList', pdg.from_names(massConstraint))
293 treeFitter.param('particleList', list_name)
294 treeFitter.param('confidenceLevel', conf_level)
295 treeFitter.param('ipConstraint', ipConstraint)
296 treeFitter.param('updateAllDaughters', updateAllDaughters)
297 treeFitter.param('customOriginConstraint', customOriginConstraint)
298 treeFitter.param('customOriginVertex', customOriginVertex)
299 treeFitter.param('customOriginCovariance', customOriginCovariance)
300 treeFitter.param('originDimension', originDimension)
301 treeFitter.param('treatAsInvisible', treatAsInvisible)
302 treeFitter.param('ignoreFromVertexFit', ignoreFromVertexFit)
303 treeFitter.param('massConstraintDecayString', massConstraintDecayString)
304 if massConstraintMassValues:
305 treeFitter.param('massConstraintMassValues', [
306 pdg.get(item).Mass() if isinstance(item, (int, str)) else item for item in massConstraintMassValues])
307 path.add_module(treeFitter)
308
309
310def TagV(
311 list_name,
312 MCassociation='',
313 confidenceLevel=0.,
314 trackFindingType="standard_PXD",
315 constraintType="tube",
316 askMCInfo=False,
317 reqPXDHits=0,
318 maskName='all',
319 fitAlgorithm='KFit',
320 kFitReqReducedChi2=5.0,
321 useTruthInFit=False,
322 useRollBack=False,
323 path=None,
324):
325 """
326 For each Particle in the given Breco ParticleList:
327 perform the fit of tag side using the track list from the RestOfEvent dataobject
328 save the MC Btag in case of signal MC
329
330 Parameters:
331
332 list_name (str): name of the input Breco ParticleList
333 MCassociation (str): use standard MC association or the internal one
334 confidenceLevel (float): minimum value of the ConfidenceLevel to accept the fit. 0 selects CL > 0
335 constraintType (str): choose the constraint used in the fit. Can be set to
336
337 * noConstraint;
338 * IP: **default**, tag B constrained to be on the IP;
339 * tube: tube along the tag B line of flight, only for fully reconstructed signal B;
340 * boost: long tube along the boost direction;
341 * (breco): deprecated, but similar to tube;
342
343 trackFindingType (str): choose how to look for tag tracks. Can be set to
344
345 * standard: all tracks except from Kshorts;
346 * standard_PXD: **default**, same as above but consider only tracks with at least 1 PXD hit.
347 If the fit fails, attempt again with the standard option;
348
349 fitAlgorithm (str): Fitter used for the tag vertex fit: Rave or KFit (default)
350 kFitReqReducedChi2 (float): The required chi2/ndf to accept the kFit result, if it is higher, iteration procedure is applied
351 askMCInfo (bool): True when requesting MC Information from the tracks performing the vertex fit
352 reqPXDHits (int): minimum N PXD hits for a track (default is 0)
353 maskName (str): get particles from a specified ROE mask
354 useTruthInFit (bool): True when the tag vertex fit is performed with the true momentum and
355 position of the tracks (default is false). The variable :b2:var:`TagVFitTruthStatus` is set to 1
356 if the truth-matching succeeds and 2 otherwise.
357 useRollBack (bool): True when the tag vertex fit is performed with position of tracks rolled back to
358 position of the mother B (default is false). The variable :b2:var:`TagVRollBackStatus` is set to 1
359 if the truth-matching succeeds and 2 otherwise.
360 path (basf2.Path): modules are added to this path
361
362 Warning:
363 Note that the useFitAlgorithm (str) parameter is deprecated and replaced by constraintType (str)
364 and trackFindingType (str)
365
366 Warning:
367 The trackFindingType ``singleTrack`` and ``singleTrack_PXD`` are broken and **cannot** be used any more.
368 """
369
370 tvfit = register_module('TagVertex')
371 tvfit.set_name('TagVertex_' + list_name)
372 tvfit.param('listName', list_name)
373 tvfit.param('maskName', maskName)
374 tvfit.param('confidenceLevel', confidenceLevel)
375 tvfit.param('MCAssociation', MCassociation)
376 tvfit.param('trackFindingType', trackFindingType)
377 tvfit.param('constraintType', constraintType)
378 tvfit.param('askMCInformation', askMCInfo)
379 tvfit.param('reqPXDHits', reqPXDHits)
380 tvfit.param('fitAlgorithm', fitAlgorithm)
381 tvfit.param('kFitReqReducedChi2', kFitReqReducedChi2)
382 tvfit.param('useTruthInFit', useTruthInFit)
383 tvfit.param('useRollBack', useRollBack)
384 path.add_module(tvfit)
385
386
387def fitPseudo(
388 list_name,
389 path,
390):
391 """
392 Add a pseudo \"vertex fit\" which adds a covariance matrix from the combination of the four-vectors of the daughters.
393 This is similar to BaBar's "Add4" function.
394 It is commonly used for :math:`\\pi^0\\to\\gamma\\gamma` reconstruction where a vertex fit is not possible.
395
396 Here is the basic usage:
397
398 ::
399
400 from modularAnalysis import fitPseudo
401 from stdPi0s import stdPi0s
402 stdPi0s("eff40_May2020", path=mypath)
403 fitPseudo("pi0:eff40_May2020", path=mypath)
404
405 Parameters:
406 list_name (str): the name of the list to add the covariance matrix to
407 path (basf2.Path): modules are added to this path
408 """
409 pseudofit = register_module('PseudoVertexFitter')
410 pseudofit.set_name('PseudoVertexFitter_' + list_name)
411 pseudofit.param('listName', list_name)
412 path.add_module(pseudofit)
413
414
415if __name__ == '__main__':
416 from basf2.utils import pretty_print_module
417 pretty_print_module(__name__, "vertex")
418
def from_names(names)
Definition: pdg.py:74
def get(name)
Definition: pdg.py:48