FairShip
Loading...
Searching...
No Matches
shipTrackAccess.py
Go to the documentation of this file.
1# SPDX-License-Identifier: LGPL-3.0-or-later
2# SPDX-FileCopyrightText: Copyright CERN for the benefit of the SHiP Collaboration
3
4"""Unified access to GenFit and ACTS fitted-track branches.
5
6Reconstruction writes either GenFit tracks (branch ``FitTracks``, default)
7or ACTS tracks (branch ``RecoTracks``, with ``--trackFitter acts``).
8Analysis code can use these helpers to support both output formats without
9caring which fitter produced the file.
10"""
11
12from __future__ import annotations
13
14import math
15from dataclasses import dataclass
16
17import ROOT
18
19
20@dataclass
22 """Fitter-agnostic summary of one fitted track.
23
24 ``mom``, ``dir``, ``pos`` and the momentum uncertainties are only
25 available when ``converged`` is true; they are ``None`` otherwise.
26 """
27
28 converged: bool
29 ndf: float
30 chi2: float # total chi2, not chi2/ndf
31 n_points: int
32 mom: ROOT.TVector3 | None = None
33 dir: ROOT.TVector3 | None = None
34 pos: ROOT.TVector3 | None = None
35 sigma_px: float | None = None
36 sigma_py: float | None = None
37 sigma_pz: float | None = None
38
39
40def uses_acts(tree) -> bool:
41 """Return True if the tree holds ACTS tracks (RecoTracks branch)."""
42 return bool(tree.GetBranch("RecoTracks"))
43
44
45def get_tracks(tree):
46 """Return the fitted-track branch, whichever fitter produced it."""
47 return tree.RecoTracks if uses_acts(tree) else tree.FitTracks
48
49
50def track_info(track, acts: bool) -> TrackInfo:
51 """Summarize a fitted track from either fitter into a TrackInfo."""
52 if acts:
53 mom = ROOT.TVector3(track.px(), track.py(), track.pz())
54 cov = track.GetCovarianceElements()
55 return TrackInfo(
56 converged=True, # only successful ACTS fits are persisted
57 ndf=track.nDoF(),
58 chi2=track.chi2(),
59 n_points=track.nMeasurements(),
60 mom=mom,
61 dir=mom.Unit(),
62 pos=ROOT.TVector3(track.x(), track.y(), track.z()),
63 sigma_px=math.sqrt(cov[15]),
64 sigma_py=math.sqrt(cov[18]),
65 sigma_pz=math.sqrt(cov[20]),
66 )
67 fit_status = track.getFitStatus()
68 info = TrackInfo(
69 converged=fit_status.isFitConverged(),
70 ndf=fit_status.getNdf(),
71 chi2=fit_status.getChi2(),
72 n_points=track.getNumPoints(),
73 )
74 try:
75 fitted_state = track.getFittedState()
76 except Exception:
77 return info
78 cov = fitted_state.get6DCov()
79 info.mom = ROOT.TVector3(fitted_state.getMom())
80 info.dir = ROOT.TVector3(fitted_state.getDir())
81 info.pos = ROOT.TVector3(fitted_state.getPos())
82 info.sigma_px = math.sqrt(cov[3][3])
83 info.sigma_py = math.sqrt(cov[4][4])
84 info.sigma_pz = math.sqrt(cov[5][5])
85 return info
bool uses_acts(tree)
TrackInfo track_info(track, bool acts)