FairShip
Loading...
Searching...
No Matches
decorators.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 ctypes import c_double
5
6import ROOT
7import shipunit as u
8
9
10def MCPointPrintOut(self) -> str:
11 p = ROOT.TDatabasePDG.Instance().GetParticle(self.PdgCode())
12 n = ""
13 if p:
14 n = p.GetName()
15 return (
16 f'("{self.Class_Name()}") X:{self.GetX():6.3F}cm Y:{self.GetY():6.3F}cm '
17 f"Z:{self.GetZ():6.3F}cm dE/dx:{self.GetEnergyLoss() / u.MeV:6.2F}MeV {n}"
18 )
19
20
21def MCTrackPrintOut(self) -> str:
22 c = self.GetPdgCode()
23 p = ROOT.TDatabasePDG.Instance().GetParticle(c)
24 n = ""
25 if p:
26 n = p.GetName()
27 m = self.GetMotherId()
28 return '("ShipMCTrack") pdgCode: %7i(%10s) Z=%6.1F m P=%6.3F GeV/c mother=%i %s' % (
29 c,
30 n,
31 self.GetStartZ() / u.m,
32 self.GetP(),
33 m,
34 self.GetProcName(),
35 )
36
37
38def vetoHitPrintOut(self) -> str:
39 return '("vetoHit") detID:%7i ADC:%5.2F TDC:%5.2F' % (self.GetDetectorID(), self.GetADC(), self.GetTDC())
40
41
42def TimeDetHitPrintOut(self) -> str:
43 t = self.GetMeasurements()
44 return '("TimeDetHit") detID:%7i TDC1:%5.2F TDC2:%5.2F isValid:%r' % (
45 self.GetDetectorID(),
46 t[0],
47 t[1],
48 self.isValid(),
49 )
50
51
52def FitTrackPrintOut(self) -> str:
53 st = self.getFitStatus()
54 if st.isFitConverged():
55 chi2DoF = st.getChi2() / st.getNdf()
56 sta = self.getFittedState()
57 P = sta.getMomMag()
58 return '("FitTrack") chi2/dof:%3.1F P:%5.2FGeV/c pdg:%i' % (chi2DoF, P, sta.getPDG())
59 return '("FitTrack") fit not converged'
60
61
62def TParticlePrintOut(self) -> str:
63 return f'("TParticle") {self.GetName()} P:{self.P():5.2F}GeV/c VxZ:{self.Vz() / u.m:5.2F}m'
64
65
66def ShipParticlePrintOut(self) -> str:
67 return (
68 f'("ShipParticle") {self.GetName()} M:{self.GetMass():5.2F}GeV/c2 '
69 f"P:{self.P():5.2F}GeV/c VxZ:{self.Vz() / u.m:5.2F}m"
70 )
71
72
73def Dump(self) -> None:
74 for k, obj in enumerate(self):
75 print(k, obj.__repr__())
76
77
78def TVector3PrintOut(self) -> str:
79 return f"{self.X():9.5F},{self.Y():9.5F},{self.Z():9.5F}"
80
81
82def TLorentzVectorPrintOut(self) -> str:
83 return f"{self.Px():9.5F},{self.Py():9.5F},{self.Pz():9.5F},{self.E():9.5F},{self.Mag():9.5F}"
84
85
86def TEvePointSetPrintOut(self) -> str:
87 x, y, z = c_double(), c_double(), c_double()
88 txt = ""
89 if self.GetN() == 0:
90 txt = "<ROOT.TEvePointSet object>"
91 for n in range(self.GetN()):
92 self.GetPoint(n, x, y, z)
93 txt += f"{n:6d} {x.value:7.1f}, {y.value:7.1f}, {z.value:9.1f} x, y, z cm\n"
94
95 return txt
96
97
98def apply_decorators() -> None:
99 """Apply custom __repr__ methods to ROOT classes.
100
101 Call this function after ROOT libraries are fully loaded to enable
102 enhanced string representations for ROOT objects.
103 """
104 # Method-replacement is the whole point of this function; mypy flags
105 # each line with [method-assign] regardless.
106 ROOT.FairMCPoint.__repr__ = MCPointPrintOut # type: ignore[method-assign]
107 ROOT.ShipMCTrack.__repr__ = MCTrackPrintOut # type: ignore[method-assign]
108 ROOT.genfit.Track.__repr__ = FitTrackPrintOut # type: ignore[method-assign]
109 ROOT.TClonesArray.Dump = Dump # type: ignore[method-assign]
110 ROOT.TVector3.__repr__ = TVector3PrintOut # type: ignore[method-assign]
111 ROOT.TParticle.__repr__ = TParticlePrintOut # type: ignore[method-assign]
112 ROOT.ShipParticle.__repr__ = ShipParticlePrintOut # type: ignore[method-assign]
113 ROOT.TEvePointSet.__repr__ = TEvePointSetPrintOut # type: ignore[method-assign]
114 ROOT.vetoHit.__repr__ = vetoHitPrintOut # type: ignore[method-assign]
115 ROOT.TimeDetHit.__repr__ = TimeDetHitPrintOut # type: ignore[method-assign]
116 ROOT.TLorentzVector.__repr__ = TLorentzVectorPrintOut # type: ignore[method-assign]
str TVector3PrintOut(self)
Definition decorators.py:78
str ShipParticlePrintOut(self)
Definition decorators.py:66
str TLorentzVectorPrintOut(self)
Definition decorators.py:82
str TEvePointSetPrintOut(self)
Definition decorators.py:86
str TimeDetHitPrintOut(self)
Definition decorators.py:42
None Dump(self)
Definition decorators.py:73
str TParticlePrintOut(self)
Definition decorators.py:62
None apply_decorators()
Definition decorators.py:98
str vetoHitPrintOut(self)
Definition decorators.py:38
str MCTrackPrintOut(self)
Definition decorators.py:21
str MCPointPrintOut(self)
Definition decorators.py:10
str FitTrackPrintOut(self)
Definition decorators.py:52