Skip to content

Commit

Permalink
Merge pull request #10 from JuliaHEP/michael-dev
Browse files Browse the repository at this point in the history
Removed dependency on LorentzVectors.jl.
  • Loading branch information
mfarrington1 authored Oct 2, 2024
2 parents 75204b4 + 41727cc commit c05912a
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 5 deletions.
3 changes: 0 additions & 3 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@ uuid = "f612022c-142a-473f-8cfd-a09cf3793c6c"
authors = ["Jerry Ling <proton@jling.dev> and contributors"]
version = "0.1.6"

[deps]
LorentzVectors = "3f54b04b-17fc-5cd4-9758-90c048d965e3"

[compat]
julia = "^1.6"
LorentzVectors = "^0.4"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Expand Down
75 changes: 74 additions & 1 deletion src/LorentzVectorHEP.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module LorentzVectorHEP

using LorentzVectors # provides x, y, z, t
import Base: +, -, *, /, ==, isapprox,

export LorentzVectorCyl, LorentzVector

Expand All @@ -9,6 +9,79 @@ export deltaphi, deltar, deltaeta
export ΔR, Δϕ, Δη
export fromPtEtaPhiE


"""
LorentzVector(t, x, y, z)
Lorentz 4-vector, as used in Special Relativity.
The metric convention is g = diag(+1,-1,-1,-1). No distinction is made between
co- and contravariant vectors.
"""
struct LorentzVector{T <: AbstractFloat}
t :: T
x :: T
y :: T
z :: T
end

"""
LorentzVector(t, x, y, z)
Promoting constructors for LorentzVector{T}.
"""
LorentzVector(t, x, y, z) = LorentzVector(promote(t, x, y, z)...)
LorentzVector(t::T, x::T, y::T, z::T) where {T <: Union{Integer, Rational, Irrational}} =
LorentzVector(float(t), x, y, z)


"""
dot(u, v)
u⋅v
Inner product of 4-vectors, in the Minkowsky metric (+,-,-,-).
"""
function dot(u::LorentzVector, v::LorentzVector)
@fastmath u.t*v.t - u.x*v.x - u.y*v.y - u.z*v.z
end


function +(u::LorentzVector, v::LorentzVector)
@fastmath LorentzVector(u.t + v.t, u.x + v.x, u.y + v.y, u.z + v.z)
end

function -(u::LorentzVector)
@fastmath LorentzVector(-u.t, -u.x, -u.y, -u.z)
end

function -(u::LorentzVector, v::LorentzVector)
@fastmath u + (-v)
end

function *::Number, u::LorentzVector)
@fastmath LorentzVector*u.t, λ*u.x, λ*u.y, λ*u.z)
end

function *(u::LorentzVector, λ::Number)
@fastmath λ * u
end

function /(u::LorentzVector, λ::Number)
@fastmath u * (one(λ) / λ)
end

function ==(u::LorentzVector, v::LorentzVector)
u.t == v.t && u.x == v.x && u.y == v.y && u.z == v.z
end

function isapprox(u::LorentzVector, v::LorentzVector;
atol::Real=0,
rtol::Real=atol>0 ? 0 : min(eps(typeof(u.x)), eps(typeof(v.x))))

err = max(abs(u.t - v.t), sqrt((u.x - v.x)^2 + (u.y - v.y)^2 + (u.z - v.z)^2))
err <= max(atol, rtol*max(abs(u.t), abs(v.t), sqrt(u.x^2 + u.y^2 + u.z^2), sqrt(v.x^2 + v.y^2 + v.z^2)))
end

include("cartesian.jl")
include("cylindrical.jl")

Expand Down
20 changes: 19 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ using Test
v1 = LorentzVectorCyl(43.71242f0, 1.4733887f0, 1.6855469f0, 0.10571289f0)
v2 = LorentzVectorCyl(36.994347f0, 0.38684082f0, -1.3935547f0, 0.10571289f0)
@test (v1+v2).mass == 92.55651f0
@test fast_mass(v1,v2) == 92.55651f0
@test fast_mass(v1,v2) 92.55651f0
@test isapprox(deltar(v1,v2), 3.265188f0, atol=1e-6)
@test isapprox(deltaphi(v1,v2), -3.0791016f0, atol=1e-6)

Expand All @@ -40,6 +40,24 @@ using Test
@test phi(vcart2) -0.9884433806509134 atol=1e-9
@test LorentzVectorHEP.phi02pi(vcart2) 5.294741926528673 atol=1e-9

@test vcart1 + vcart2 LorentzVector(vcart1.t + vcart2.t, vcart1.x + vcart2.x, vcart1.y + vcart2.y, vcart1.z + vcart2.z)
@test vcart1 + vcart2 == vcart2 + vcart1
@test -vcart1 == LorentzVector(-(vcart1.t), -(vcart1.x), -(vcart1.y), -(vcart1.z))
@test vcart1 - vcart2 LorentzVector(vcart1.t - vcart2.t, vcart1.x - vcart2.x, vcart1.y - vcart2.y, vcart1.z - vcart2.z)
@test vcart1 - vcart2 == -(vcart2 - vcart1)

c = rand()
@test c*vcart1 LorentzVector(c*vcart1.t, c*vcart1.x, c*vcart1.y, c*vcart1.z)
@test -c*vcart1 LorentzVector(-c*vcart1.t, -c*vcart1.x, -c*vcart1.y, -c*vcart1.z)
@test vcart1*c == c*vcart1

if c == 0
c += 0.1
end

@test vcart1 / c vcart1 * (1/c)


@test deltaeta(vcart1, vcart2) 0.08825941862546584 atol=1e-9
@test deltaphi(vcart1, vcart2) 3.0317366429628736 atol=1e-9

Expand Down

0 comments on commit c05912a

Please sign in to comment.