Skip to content

Commit

Permalink
added formatting and respective workflow (#18)
Browse files Browse the repository at this point in the history
* added formatting and respective workflow

* added info in readme

* updated .JuliaFormatter.toml

---------

Co-authored-by: Uwe Hernandez Acosta <u.hernandez@hzdr.de>
  • Loading branch information
szabo137 and Uwe Hernandez Acosta authored Oct 4, 2024
1 parent 4fa96cf commit 971358a
Show file tree
Hide file tree
Showing 12 changed files with 320 additions and 278 deletions.
2 changes: 1 addition & 1 deletion .JuliaFormatter.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# See https://domluna.github.io/JuliaFormatter.jl/stable/ for a list of options
style = "blue"
indent = 4
indent = 2
margin = 92
normalize_line_endings = "unix"
2 changes: 2 additions & 0 deletions .formatting/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[deps]
JuliaFormatter = "98e50ef6-434e-11e9-1051-2b60c6c9e899"
12 changes: 12 additions & 0 deletions .formatting/format_all.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using JuliaFormatter

# we asume the format_all.jl script is located in QEDbase.jl/.formatting
project_path = Base.Filesystem.joinpath(Base.Filesystem.dirname(Base.source_path()), "..")

not_formatted = format(project_path; verbose=true)
if not_formatted
@info "Formatting verified."
else
@warn "Formatting verification failed: Some files are not properly formatted!"
end
exit(not_formatted ? 0 : 1)
16 changes: 16 additions & 0 deletions .github/workflows/Formatting.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: formatter
on: [pull_request]
jobs:
formatter:
runs-on: ubuntu-latest
steps:
- name: checkout repo
uses: actions/checkout@v4
- name: install Julia
uses: julia-actions/setup-julia@v1
with:
version: "1.10"
- name: Install Julia requirements
run: julia --project=${GITHUB_WORKSPACE}/.formatting -e 'import Pkg; Pkg.instantiate()'
- name: Check code style
run: julia --project=${GITHUB_WORKSPACE}/.formatting ${GITHUB_WORKSPACE}/.formatting/format_all.jl
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,18 @@ Base interfaces for four-momenta in high-energy physics.
[![Test workflow status](https://github.com/JuliaHEP/LorentzVectorBase.jl/actions/workflows/Test.yml/badge.svg?branch=main)](https://github.com/JuliaHEP/LorentzVectorBase.jl/actions/workflows/Test.yml?query=branch%3Amain)
[![Docs workflow Status](https://github.com/JuliaHEP/LorentzVectorBase.jl/actions/workflows/Docs.yml/badge.svg?branch=main)](https://github.com/JuliaHEP/LorentzVectorBase.jl/actions/workflows/Docs.yml?query=branch%3Amain)
[![Coverage](https://codecov.io/gh/JuliaHEP/LorentzVectorBase.jl/branch/main/graph/badge.svg)](https://codecov.io/gh/JuliaHEP/LorentzVectorBase.jl)

# Code Formatting

This package follows a standardized formatting configuration to ensure consistency across
the codebase. We enforce these formatting guidelines by running checks on all pull requests
through continuous integration (CI).

To format your code locally and ensure it meets the required standards, you can run the
following command in your terminal:

```bash
julia --project=.formatting -e 'using Pkg; Pkg.instantiate(); include(".formatting/format_all.jl")'
```

This will apply the necessary formatting rules to your code before submission.
28 changes: 14 additions & 14 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@ using LorentzVectorBase
using Documenter

DocMeta.setdocmeta!(
LorentzVectorBase, :DocTestSetup, :(using LorentzVectorBase); recursive=true
LorentzVectorBase, :DocTestSetup, :(using LorentzVectorBase); recursive=true
)

const numbered_pages = [
file for file in readdir(joinpath(@__DIR__, "src")) if
file != "index.md" && splitext(file)[2] == ".md"
file for file in readdir(joinpath(@__DIR__, "src")) if
file != "index.md" && splitext(file)[2] == ".md"
]

makedocs(;
modules=[LorentzVectorBase],
authors="Uwe Hernandez Acosta <u.hernandez@hzdr.de>",
repo="https://github.com/JuliaHEP/LorentzVectorBase.jl/blob/{commit}{path}#{line}",
sitename="LorentzVectorBase.jl",
format=Documenter.HTML(;
canonical="https://JuliaHEP.github.io/LorentzVectorBase.jl",
repolink="https://github.com/JuliaHEP/LorentzVectorBase.jl",
edit_link="main",
assets=String[],
),
pages=["index.md"; numbered_pages],
modules=[LorentzVectorBase],
authors="Uwe Hernandez Acosta <u.hernandez@hzdr.de>",
repo="https://github.com/JuliaHEP/LorentzVectorBase.jl/blob/{commit}{path}#{line}",
sitename="LorentzVectorBase.jl",
format=Documenter.HTML(;
canonical="https://JuliaHEP.github.io/LorentzVectorBase.jl",
repolink="https://github.com/JuliaHEP/LorentzVectorBase.jl",
edit_link="main",
assets=String[],
),
pages=["index.md"; numbered_pages],
)

deploydocs(; repo="github.com/JuliaHEP/LorentzVectorBase.jl")
92 changes: 46 additions & 46 deletions src/coordinate_systems/PtEtaPhiM.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,107 +24,107 @@ py(::PtEtaPhiM, v) = pt(v) * sin(phi(v))
pz(::PtEtaPhiM, v) = pt(v) * sinh(eta(v))

function energy(::PtEtaPhiM, mom)
return sqrt(px(mom)^2 + py(mom)^2 + pz(mom)^2 + invariant_mass2(mom))
return sqrt(px(mom)^2 + py(mom)^2 + pz(mom)^2 + invariant_mass2(mom))
end

@inline function spatial_magnitude2(::PtEtaPhiM, mom)
return pt(mom)^2 * (1 + sinh(eta(mom))^2)
return pt(mom)^2 * (1 + sinh(eta(mom))^2)
end

@inline function spatial_magnitude(::PtEtaPhiM, mom)
return sqrt(spatial_magnitude2(mom))
return sqrt(spatial_magnitude2(mom))
end

@inline function mass2(::PtEtaPhiM, mom)
return mass(mom)^2
return mass(mom)^2
end

@inline function boost_beta(::PtEtaPhiM, mom)
en = energy(mom)
rho = spatial_magnitude(mom)
if !iszero(en)
rho / en
elseif iszero(rho)
return zero(rho)
else
throw(
ArgumentError(
"There is no beta for a four-momentum with vanishing time/energy component"
),
)
end
en = energy(mom)
rho = spatial_magnitude(mom)
if !iszero(en)
rho / en
elseif iszero(rho)
return zero(rho)
else
throw(
ArgumentError(
"There is no beta for a four-momentum with vanishing time/energy component"
),
)
end
end

@inline function boost_gamma(::PtEtaPhiM, mom)
return inv(sqrt(one(energy(mom)) - boost_beta(mom)^2))
return inv(sqrt(one(energy(mom)) - boost_beta(mom)^2))
end

########################
# transverse coordinates
########################

@inline function pt2(::PtEtaPhiM, mom)
return pt(mom)^2
return pt(mom)^2
end

@inline function mt2(::PtEtaPhiM, mom)
return energy(mom)^2 - pz(mom)^2
return energy(mom)^2 - pz(mom)^2
end

function mt(::PtEtaPhiM, mom)
mT2 = mt2(mom)
if mT2 < zero(mT2)
# add optional waring: negative transverse mass -> -sqrt(-mT2) is returned.
-sqrt(-mT2)
else
sqrt(mT2)
end
mT2 = mt2(mom)
if mT2 < zero(mT2)
# add optional waring: negative transverse mass -> -sqrt(-mT2) is returned.
-sqrt(-mT2)
else
sqrt(mT2)
end
end

@inline function rapidity(::PtEtaPhiM, mom)
E = energy(mom)
pz1 = pz(mom)
return 0.5 * log((E + pz1) / (E - pz1))
E = energy(mom)
pz1 = pz(mom)
return 0.5 * log((E + pz1) / (E - pz1))
end

#######################
# spherical coordinates
#######################

@inline function polar_angle(::PtEtaPhiM, mom)
xcomp = px(mom)
ycomp = py(mom)
zcomp = pz(mom)
xcomp = px(mom)
ycomp = py(mom)
zcomp = pz(mom)

return if iszero(xcomp) && iszero(ycomp) && iszero(zcomp)
zero(xcomp)
else
atan(transverse_momentum(mom), zcomp)
end
return if iszero(xcomp) && iszero(ycomp) && iszero(zcomp)
zero(xcomp)
else
atan(transverse_momentum(mom), zcomp)
end
end

@inline function cos_theta(::PtEtaPhiM, mom)
r = spatial_magnitude(mom)
return iszero(r) ? one(px(mom)) : pz(mom) / r
r = spatial_magnitude(mom)
return iszero(r) ? one(px(mom)) : pz(mom) / r
end

function cos_phi(::PtEtaPhiM, mom)
pT = pt(mom)
return iszero(pT) ? one(pT) : px(mom) / pT
pT = pt(mom)
return iszero(pT) ? one(pT) : px(mom) / pT
end

function sin_phi(::PtEtaPhiM, mom)
pT = pt(mom)
return iszero(pT) ? zero(pT) : py(mom) / pT
pT = pt(mom)
return iszero(pT) ? zero(pT) : py(mom) / pT
end

########################
# light cone coordinates
########################
@inline function plus_component(::PtEtaPhiM, mom)
return 0.5 * (energy(mom) + pz(mom))
return 0.5 * (energy(mom) + pz(mom))
end

@inline function minus_component(::PtEtaPhiM, mom)
return 0.5 * (energy(mom) - pz(mom))
return 0.5 * (energy(mom) - pz(mom))
end
Loading

0 comments on commit 971358a

Please sign in to comment.