FairShip
Loading...
Searching...
No Matches
TrackExtrapolateTool.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
4from typing import Any
5
6import ROOT
7import shipunit as u
8
9minNdf = 20
10parallelToZ = ROOT.TVector3(0.0, 0.0, 1.0)
11top = ROOT.gGeoManager.GetTopVolume()
12# Runge-Kutta extrapolation is done up to the scoring plane (former SplitCal
13# position); beyond it, tracks are extrapolated linearly.
14if top.GetNode("CaloScoringPlane_1"):
15 z_ecal = top.GetNode("CaloScoringPlane_1").GetMatrix().GetTranslation()[2]
16else:
17 print("TrackExtrapolate tool: Error, no scoring plane present")
18 z_ecal = 100 * u.m
19
20
21def extrapolateToPlane(fT, z) -> tuple[bool, Any, Any]:
22 # extrapolate to a plane perpendicular to beam direction (z).
23 # When rc is False, pos and mom are None — callers must check rc first.
24 rc, pos, mom = False, None, None
25 fst = fT.getFitStatus()
26 if fst.isFitConverged() and fst.getNdf() > minNdf:
27 # test for fit status for each point
28 if fT.getPoint(0).getFitterInfo() and fT.getPoint(1).getFitterInfo():
29 fstate0, fstate1 = fT.getFittedState(0), fT.getFittedState(1)
30 fPos0, fPos1 = fstate0.getPos(), fstate1.getPos()
31 if abs(z - fPos0.z()) < abs(z - fPos1.z()):
32 fstate = fstate0
33 else:
34 fstate = fstate1
35 zs = min(z, z_ecal)
36 # muon hypothesis with the fitted charge (mu- is +13); the fitted PDG code keeps the seed's sign
37 rep = ROOT.genfit.RKTrackRep(13 if fstate.getCharge() < 0 else -13)
38 state = ROOT.genfit.StateOnPlane(rep)
39 rep.setPosMom(state, fstate.getPos(), fstate.getMom())
40 plane = ROOT.genfit.DetPlane(ROOT.TVector3(0.0, 0.0, zs), parallelToZ)
41 ROOT.SetOwnership(plane, False) # owned by the SharedPlanePtr
42 try:
43 rep.extrapolateToPlane(state, ROOT.genfit.SharedPlanePtr(plane))
44 except Exception:
45 # Honour the contract: no valid extrapolation -> pos/mom are None.
46 return False, None, None
47 pos, mom = state.getPos(), state.getMom()
48 if z > z_ecal:
49 # RK only reaches z_ecal; continue with linear extrapolation
50 px, py, pz = mom.X(), mom.Y(), mom.Z()
51 if pz == 0:
52 # cannot extrapolate in z with zero longitudinal momentum
53 return False, None, None
54 lam = (z - pos.Z()) / pz
55 pos = ROOT.TVector3(pos.X() + lam * px, pos.Y() + lam * py, z)
56 rc = True
57 return rc, pos, mom
tuple[bool, Any, Any] extrapolateToPlane(fT, z)