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