Skip to content

Commit

Permalink
Adapt tests to allow variable float types
Browse files Browse the repository at this point in the history
Numerical type can be passed as a parameter to the test struct
This is passed down to read the events in the required precision.
Add some tests for e+e- and pp in Float32
  • Loading branch information
graeme-a-stewart committed Dec 16, 2024
1 parent faf67cf commit 9f96393
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/Utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ the particles of a particular event. In particular T can be `PseudoJet` or
a `LorentzVector` type. Note, if T is not `PseudoJet`, the order of the
arguments in the constructor must be `(t, x, y, z)`.
"""
function read_final_state_particles(fname; maxevents = -1, skipevents = 0, T = PseudoJet)
function read_final_state_particles(fname; maxevents = -1, skipevents = 0,
T = PseudoJet{Float64})
f = open(fname)
if endswith(fname, ".gz")
@debug "Reading gzipped file $fname"
Expand Down
40 changes: 33 additions & 7 deletions test/_common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ a vector of `FinalJet` objects, e.g.,
selector = (cs) -> exclusive_jets(cs; njets = 2)
```
"""
struct ComparisonTest
struct ComparisonTest{T <: Real}
events_file::AbstractString
fastjet_outputs::AbstractString
algorithm::JetAlgorithm.Algorithm
Expand All @@ -54,14 +54,24 @@ struct ComparisonTest
R::Real
selector::Any
selector_name::AbstractString
numtype::Type{T}
end

"Define the element type based off the type parameter"
Base.eltype(::Type{ComparisonTest{T}}) where {T} = T

"""Constructor where there is no type given"""
function ComparisonTest(events_file, fastjet_outputs, algorithm, strategy, power, R,
selector, selector_name)
ComparisonTest(events_file, fastjet_outputs, algorithm, strategy, power, R, selector,
selector_name, Float64)
end

"""Constructor where there is no selector_name given"""
function ComparisonTest(events_file, fastjet_outputs, algorithm, strategy, power, R,
selector)
selector_name = ""
ComparisonTest(events_file, fastjet_outputs, algorithm, strategy, power, R, selector,
selector_name)
"", Float64)
end

"""Read JSON file with fastjet jets in it"""
Expand Down Expand Up @@ -94,7 +104,13 @@ end

function run_reco_test(test::ComparisonTest; testname = nothing)
# Read the input events
events = JetReconstruction.read_final_state_particles(test.events_file)
if JetReconstruction.is_pp(test.algorithm)
events = JetReconstruction.read_final_state_particles(test.events_file;
T = PseudoJet{test.numtype})
else
events = JetReconstruction.read_final_state_particles(test.events_file;
T = EEjet{test.numtype})
end
# Read the fastjet results
fastjet_jets = read_fastjet_outputs(test.fastjet_outputs)
sort_jets!(fastjet_jets)
Expand All @@ -117,6 +133,16 @@ function run_reco_test(test::ComparisonTest; testname = nothing)
end
end

# For Float64 we use a tolerance of 1e-6
# For Float32 we use a tolerance of 1e-2, which is very low
# but the problem is that we are comparing with FastJet in double
# precision and in an iterative algorithm, this can lead to accumulating
# differences that end up quite large
tol = 1e-6
if test.numtype == Float32
tol = 1e-2
end

@testset "$testname" begin
# Test each event in turn...
for (ievt, event) in enumerate(jet_collection)
Expand All @@ -131,9 +157,9 @@ function run_reco_test(test::ComparisonTest; testname = nothing)
# the momentum
# Sometimes phi could be in the range [-π, π], but FastJet always is [0, 2π]
normalised_phi = jet.phi < 0.0 ? jet.phi + 2π : jet.phi
@test jet.rapfastjet_jets[ievt]["jets"][ijet]["rap"] atol=1e-7
@test normalised_phifastjet_jets[ievt]["jets"][ijet]["phi"] atol=1e-7
@test jet.ptfastjet_jets[ievt]["jets"][ijet]["pt"] rtol=1e-6
@test jet.rapfastjet_jets[ievt]["jets"][ijet]["rap"] rtol=tol
@test normalised_phifastjet_jets[ievt]["jets"][ijet]["phi"] rtol=tol
@test jet.ptfastjet_jets[ievt]["jets"][ijet]["pt"] rtol=tol
end
end
end
Expand Down
3 changes: 3 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ function main()
include("test-pp-reconstruction.jl")
include("test-ee-reconstruction.jl")

# Also test reconstruction with Float32
include("test-f32-reconstruction.jl")

# Compare inputting data in PseudoJet with using a LorentzVector
do_test_compare_types(RecoStrategy.N2Plain, algname = pp_algorithms[-1], power = -1)
do_test_compare_types(RecoStrategy.N2Tiled, algname = pp_algorithms[-1], power = -1)
Expand Down
20 changes: 20 additions & 0 deletions test/test-f32-reconstruction.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Tests of reconstruction algorithms in Float32

include("common.jl")

durham_njets3_f32 = ComparisonTest(events_file_ee,
joinpath(@__DIR__, "data",
"jet-collections-fastjet-njets3-Durham-eeH.json.gz"),
JetAlgorithm.Durham, RecoStrategy.N2Plain, 1, 4.0,
(cs) -> exclusive_jets(cs; njets = 3),
"exclusive njets Float32", Float32)

run_reco_test(durham_njets3_f32)

antikt_pcut_f32 = ComparisonTest(events_file_pp,
joinpath(@__DIR__, "data",
"jet-collections-fastjet-inclusive-AntiKt.json.gz"),
JetAlgorithm.AntiKt, RecoStrategy.N2Tiled, -1, 0.4,
(cs) -> inclusive_jets(cs; ptmin = 5.0),
"inclusive-float32", Float32)
run_reco_test(antikt_pcut_f32)

0 comments on commit 9f96393

Please sign in to comment.