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