Skip to content

Commit c16a41c

Browse files
authored
add PtEtaPhiM coordinates used by LorentzVectorHEP.jl (#4)
* add PtEtaPhiM coordinates used by LorentzVectorHEP.jl
1 parent adf7189 commit c16a41c

File tree

5 files changed

+196
-78
lines changed

5 files changed

+196
-78
lines changed

src/LorentzVectorBase.jl

+1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ export coordinate_system, coordinate_names
44

55
include("interface.jl")
66
include("coordinate_systems/exyz.jl")
7+
include("coordinate_systems/PtEtaPhiM.jl")
78

89
end

src/coordinate_systems/PtEtaPhiM.jl

+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
"""
2+
3+
PtEtaPhiM <: AbstractCoordinateSystem
4+
5+
Cylindrical coordinate system for four-momenta. Using this requires the implementation of the following interface functions:
6+
7+
```julia
8+
pt(::CustomFourMomentum)
9+
eta(::CustomFourMomentum)
10+
phi(::CustomFourMomentum)
11+
mass(::CustomFourMomentum)
12+
```
13+
14+
"""
15+
struct PtEtaPhiM <: AbstractCoordinateSystem end
16+
coordinate_names(::PtEtaPhiM) = (:pt, :eta, :phi, :energy)
17+
18+
19+
####
20+
# derived components
21+
####
22+
23+
px(::PtEtaPhiM, v) = pt(v) * cos(phi(v))
24+
py(::PtEtaPhiM, v) = pt(v) * sin(phi(v))
25+
pz(::PtEtaPhiM, v) = pt(v) * sinh(eta(v))
26+
27+
function energy(::PtEtaPhiM, mom)
28+
sqrt(px(mom)^2 + py(mom)^2 + pz(mom)^2 + invariant_mass2(mom))
29+
end
30+
31+
@inline function spatial_magnitude2(::PtEtaPhiM, mom)
32+
return pt(mom)^2 * (1+sinh(eta(mom))^2)
33+
end
34+
35+
@inline function spatial_magnitude(::PtEtaPhiM, mom)
36+
return sqrt(spatial_magnitude2(mom))
37+
end
38+
39+
@inline function mass2(::PtEtaPhiM, mom)
40+
mass(mom)^2
41+
end
42+
43+
@inline function boost_beta(::PtEtaPhiM, mom)
44+
en = energy(mom)
45+
rho = spatial_magnitude(mom)
46+
if !iszero(en)
47+
rho / en
48+
elseif iszero(rho)
49+
return zero(rho)
50+
else
51+
throw(
52+
ArgumentError(
53+
"There is no beta for a four-momentum with vanishing time/energy component"
54+
),
55+
)
56+
end
57+
end
58+
59+
@inline function boost_gamma(::PtEtaPhiM, mom)
60+
return inv(sqrt(one(energy(mom)) - boost_beta(mom)^2))
61+
end
62+
63+
########################
64+
# transverse coordinates
65+
########################
66+
67+
@inline function pt2(::PtEtaPhiM, mom)
68+
return pt(mom)^2
69+
end
70+
71+
@inline function mt2(::PtEtaPhiM, mom)
72+
return energy(mom)^2 - pz(mom)^2
73+
end
74+
75+
function mt(::PtEtaPhiM, mom)
76+
mT2 = mt2(mom)
77+
if mT2 < zero(mT2)
78+
# add optional waring: negative transverse mass -> -sqrt(-mT2) is returned.
79+
-sqrt(-mT2)
80+
else
81+
sqrt(mT2)
82+
end
83+
end
84+
85+
@inline function rapidity(::PtEtaPhiM, mom)
86+
E = energy(mom)
87+
pz1 = pz(mom)
88+
return 0.5*log((E+pz1)/(E-pz1))
89+
end
90+
91+
#######################
92+
# spherical coordinates
93+
#######################
94+
95+
@inline function polar_angle(::PtEtaPhiM, mom)
96+
xcomp = px(mom)
97+
ycomp = py(mom)
98+
zcomp = pz(mom)
99+
100+
return if iszero(xcomp) && iszero(ycomp) && iszero(zcomp)
101+
zero(xcomp)
102+
else
103+
atan(transverse_momentum(mom), zcomp)
104+
end
105+
end
106+
107+
@inline function cos_theta(::PtEtaPhiM, mom)
108+
r = spatial_magnitude(mom)
109+
return iszero(r) ? one(px(mom)) : pz(mom) / r
110+
end
111+
112+
function cos_phi(::PtEtaPhiM, mom)
113+
pT = pt(mom)
114+
return iszero(pT) ? one(pT) : px(mom) / pT
115+
end
116+
117+
function sin_phi(::PtEtaPhiM, mom)
118+
pT = pt(mom)
119+
return iszero(pT) ? zero(pT) : py(mom) / pT
120+
end
121+
122+
########################
123+
# light cone coordinates
124+
########################
125+
@inline function plus_component(::PtEtaPhiM, mom)
126+
return 0.5 * (energy(mom) + pz(mom))
127+
end
128+
129+
@inline function minus_component(::PtEtaPhiM, mom)
130+
return 0.5 * (energy(mom) - pz(mom))
131+
end

src/coordinate_systems/exyz.jl

+21-21
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ energy(::CustomFourMomentum)
1313
1414
"""
1515
struct EXYZ <: AbstractCoordinateSystem end
16-
coordinate_names(::EXYZ) = (:energy, :px, :py, :pz)
16+
coordinate_names(::EXYZ) = (:px, :py, :pz, :energy)
1717

1818
####
1919
# Interface functions
@@ -93,7 +93,7 @@ end
9393

9494
"""
9595
96-
invariant_mass2(::EXYZ,mom)
96+
mass2(::EXYZ,mom)
9797
9898
Return the squared invariant mass of a given four-momentum, i.e. the minkowski dot with itself.
9999
@@ -103,13 +103,13 @@ Return the squared invariant mass of a given four-momentum, i.e. the minkowski d
103103
104104
105105
"""
106-
@inline function invariant_mass2(::EXYZ, mom)
106+
@inline function mass2(::EXYZ, mom)
107107
return energy(mom)^2 - px(mom)^2 - py(mom)^2 - pz(mom)^2
108108
end
109109

110110
"""
111111
112-
invariant_mass(::EXYZ,mom)
112+
mass(::EXYZ,mom)
113113
114114
Return the the invariant mass of a given four-momentum, i.e. the square root of the minkowski dot with itself.
115115
@@ -123,8 +123,8 @@ Return the the invariant mass of a given four-momentum, i.e. the square root of
123123
If the squared invariant mass `m2` is negative, `-sqrt(-m2)` is returned.
124124
125125
"""
126-
@inline function invariant_mass(cs::EXYZ, mom)
127-
m2 = invariant_mass2(mom)
126+
@inline function mass(::EXYZ, mom)
127+
m2 = mass2(mom)
128128
if m2 < zero(m2)
129129
# Think about including this waring, maybe optional with a global PRINT_WARINGS switch.
130130
#@warn("The square of the invariant mass (m2=P*P) is negative. The value -sqrt(-m2) is returned.")
@@ -181,7 +181,7 @@ end
181181
########################
182182
"""
183183
184-
transverse_momentum2(::EXYZ,mom)
184+
pt2(::EXYZ,mom)
185185
186186
Return the squared transverse momentum for a given four-momentum, i.e. the sum of its squared 1- and 2-component.
187187
@@ -194,13 +194,13 @@ Return the squared transverse momentum for a given four-momentum, i.e. the sum o
194194
The transverse components are defined w.r.t. to the 3-axis.
195195
196196
"""
197-
@inline function transverse_momentum2(::EXYZ, mom)
197+
@inline function pt2(::EXYZ, mom)
198198
return px(mom)^2 + py(mom)^2
199199
end
200200

201201
"""
202202
203-
transverse_momentum(::EXYZ, mom)
203+
pt(::EXYZ, mom)
204204
205205
Return the transverse momentum for a given four-momentum, i.e. the spatial_magnitude of its transverse components.
206206
@@ -214,13 +214,13 @@ Return the transverse momentum for a given four-momentum, i.e. the spatial_magni
214214
The transverse components are defined w.r.t. to the 3-axis.
215215
216216
"""
217-
@inline function transverse_momentum(::EXYZ, mom)
218-
return sqrt(transverse_momentum2(mom))
217+
@inline function pt(::EXYZ, mom)
218+
return sqrt(pt2(mom))
219219
end
220220

221221
"""
222222
223-
transverse_mass2(::EXYZ, mom)
223+
mt2(::EXYZ, mom)
224224
225225
Return the squared transverse mass for a given four-momentum, i.e. the difference of its squared 0- and 3-component.
226226
@@ -235,13 +235,13 @@ Return the squared transverse mass for a given four-momentum, i.e. the differenc
235235
236236
237237
"""
238-
@inline function transverse_mass2(::EXYZ, mom)
238+
@inline function mt2(::EXYZ, mom)
239239
return energy(mom)^2 - pz(mom)^2
240240
end
241241

242242
"""
243243
244-
transverse_mass(::EXYZ,mom)
244+
mt(::EXYZ,mom)
245245
246246
Return the transverse momentum for a given four-momentum, i.e. the square root of its squared transverse mass.
247247
@@ -259,8 +259,8 @@ Return the transverse momentum for a given four-momentum, i.e. the square root o
259259
If the squared transverse mass `mt2` is negative, `-sqrt(-mt2)` is returned.
260260
261261
"""
262-
function transverse_mass(::EXYZ, mom)
263-
mT2 = transverse_mass2(mom)
262+
function mt(::EXYZ, mom)
263+
mT2 = mt2(mom)
264264
if mT2 < zero(mT2)
265265
# add optional waring: negative transverse mass -> -sqrt(-mT2) is returned.
266266
-sqrt(-mT2)
@@ -292,11 +292,11 @@ Return the [rapidity](https://en.wikipedia.org/wiki/Rapidity) for a given four-m
292292
end
293293

294294
"""
295-
pseudorapidity(::EXYZ, mom)
295+
eta(::EXYZ, mom)
296296
297297
TBW
298298
"""
299-
function pseudorapidity(::EXYZ, mom)
299+
function eta(::EXYZ, mom)
300300
cth = cos_theta(mom)
301301

302302
if cth^2 < one(cth)
@@ -308,7 +308,7 @@ function pseudorapidity(::EXYZ, mom)
308308
return zero(z)
309309
end
310310

311-
@warn "Pseudorapidity: transverse momentum is zero! return +/- 10e10"
311+
@warn "Pseudorapidity (η): transverse momentum is zero! return +/- 10e10"
312312
if z > zero(z)
313313
return 10e10
314314
end
@@ -364,7 +364,7 @@ end
364364

365365
"""
366366
367-
azimuthal_angle(::EXYZ, mom)
367+
phi(::EXYZ, mom)
368368
369369
Return the phi angle for a given four-momentum, i.e. the azimuthal angle of its spatial components in [spherical coordinates](https://en.wikipedia.org/wiki/Spherical_coordinate_system).
370370
@@ -377,7 +377,7 @@ Return the phi angle for a given four-momentum, i.e. the azimuthal angle of its
377377
The [spherical coordinates](https://en.wikipedia.org/wiki/Spherical_coordinate_system) are defined w.r.t. to the 3-axis.
378378
379379
"""
380-
function azimuthal_angle(::EXYZ, mom)
380+
function phi(::EXYZ, mom)
381381
xcomp = px(mom)
382382
ycomp = py(mom)
383383
return iszero(xcomp) && iszero(ycomp) ? zero(xcomp) : atan(ycomp, xcomp)

src/interface.jl

+20-20
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,25 @@ function coordinate_names end
1515
function coordinate_system end
1616

1717
const FOURMOMENTUM_GETTER_FUNCTIONS = (
18-
:energy,
1918
:px,
2019
:py,
2120
:pz,
22-
:spatial_magnitude2,
21+
:energy,
22+
:pt,
23+
:pt2,
24+
:eta,
25+
:phi,
2326
:spatial_magnitude,
24-
:invariant_mass2,
25-
:invariant_mass,
27+
:spatial_magnitude2,
28+
:mass,
29+
:mass2,
2630
:boost_beta,
2731
:boost_gamma,
28-
:transverse_momentum2,
29-
:transverse_momentum,
30-
:transverse_mass2,
31-
:transverse_mass,
32+
:mt2,
33+
:mt,
3234
:rapidity,
33-
:pseudorapidity,
3435
:polar_angle,
3536
:cos_theta,
36-
:azimuthal_angle,
3737
:cos_phi,
3838
:sin_phi,
3939
:plus_component,
@@ -49,16 +49,16 @@ for func in FOURMOMENTUM_GETTER_FUNCTIONS
4949
end
5050

5151
const FOURMOMENTUM_GETTER_ALIASSES = Dict(
52-
:mag2 => :spatial_magnitude2,
53-
:mag => :spatial_magnitude,
54-
:mass2 => :invariant_mass2,
55-
:mass => :invariant_mass,
56-
:pt2 => :transverse_momentum2,
57-
:perp2 => :transverse_momentum2,
58-
:pt => :transverse_momentum,
59-
:perp => :transverse_momentum,
60-
:mt2 => :transverse_mass2,
61-
:mt => :transverse_mass,
52+
:invariant_mass => :mass,
53+
:invariant_mass2 => :mass2,
54+
:transverse_momentum => :pt,
55+
:transverse_momentum2 => :pt2,
56+
:perp => :pt,
57+
:perp2 => :pt2,
58+
:transverse_mass => :mt,
59+
:transverse_mass2 => :mt2,
60+
:azimuthal_angle => :phi,
61+
:pseudorapidity => :eta,
6262
)
6363

6464
for (alias, func) in FOURMOMENTUM_GETTER_ALIASSES

0 commit comments

Comments
 (0)