Skip to content

Commit 9f96393

Browse files
Adapt tests to allow variable float types
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
1 parent faf67cf commit 9f96393

File tree

4 files changed

+58
-8
lines changed

4 files changed

+58
-8
lines changed

src/Utils.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ the particles of a particular event. In particular T can be `PseudoJet` or
2121
a `LorentzVector` type. Note, if T is not `PseudoJet`, the order of the
2222
arguments in the constructor must be `(t, x, y, z)`.
2323
"""
24-
function read_final_state_particles(fname; maxevents = -1, skipevents = 0, T = PseudoJet)
24+
function read_final_state_particles(fname; maxevents = -1, skipevents = 0,
25+
T = PseudoJet{Float64})
2526
f = open(fname)
2627
if endswith(fname, ".gz")
2728
@debug "Reading gzipped file $fname"

test/_common.jl

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ a vector of `FinalJet` objects, e.g.,
4545
selector = (cs) -> exclusive_jets(cs; njets = 2)
4646
```
4747
"""
48-
struct ComparisonTest
48+
struct ComparisonTest{T <: Real}
4949
events_file::AbstractString
5050
fastjet_outputs::AbstractString
5151
algorithm::JetAlgorithm.Algorithm
@@ -54,14 +54,24 @@ struct ComparisonTest
5454
R::Real
5555
selector::Any
5656
selector_name::AbstractString
57+
numtype::Type{T}
58+
end
59+
60+
"Define the element type based off the type parameter"
61+
Base.eltype(::Type{ComparisonTest{T}}) where {T} = T
62+
63+
"""Constructor where there is no type given"""
64+
function ComparisonTest(events_file, fastjet_outputs, algorithm, strategy, power, R,
65+
selector, selector_name)
66+
ComparisonTest(events_file, fastjet_outputs, algorithm, strategy, power, R, selector,
67+
selector_name, Float64)
5768
end
5869

5970
"""Constructor where there is no selector_name given"""
6071
function ComparisonTest(events_file, fastjet_outputs, algorithm, strategy, power, R,
6172
selector)
62-
selector_name = ""
6373
ComparisonTest(events_file, fastjet_outputs, algorithm, strategy, power, R, selector,
64-
selector_name)
74+
"", Float64)
6575
end
6676

6777
"""Read JSON file with fastjet jets in it"""
@@ -94,7 +104,13 @@ end
94104

95105
function run_reco_test(test::ComparisonTest; testname = nothing)
96106
# Read the input events
97-
events = JetReconstruction.read_final_state_particles(test.events_file)
107+
if JetReconstruction.is_pp(test.algorithm)
108+
events = JetReconstruction.read_final_state_particles(test.events_file;
109+
T = PseudoJet{test.numtype})
110+
else
111+
events = JetReconstruction.read_final_state_particles(test.events_file;
112+
T = EEjet{test.numtype})
113+
end
98114
# Read the fastjet results
99115
fastjet_jets = read_fastjet_outputs(test.fastjet_outputs)
100116
sort_jets!(fastjet_jets)
@@ -117,6 +133,16 @@ function run_reco_test(test::ComparisonTest; testname = nothing)
117133
end
118134
end
119135

136+
# For Float64 we use a tolerance of 1e-6
137+
# For Float32 we use a tolerance of 1e-2, which is very low
138+
# but the problem is that we are comparing with FastJet in double
139+
# precision and in an iterative algorithm, this can lead to accumulating
140+
# differences that end up quite large
141+
tol = 1e-6
142+
if test.numtype == Float32
143+
tol = 1e-2
144+
end
145+
120146
@testset "$testname" begin
121147
# Test each event in turn...
122148
for (ievt, event) in enumerate(jet_collection)
@@ -131,9 +157,9 @@ function run_reco_test(test::ComparisonTest; testname = nothing)
131157
# the momentum
132158
# Sometimes phi could be in the range [-π, π], but FastJet always is [0, 2π]
133159
normalised_phi = jet.phi < 0.0 ? jet.phi + 2π : jet.phi
134-
@test jet.rapfastjet_jets[ievt]["jets"][ijet]["rap"] atol=1e-7
135-
@test normalised_phifastjet_jets[ievt]["jets"][ijet]["phi"] atol=1e-7
136-
@test jet.ptfastjet_jets[ievt]["jets"][ijet]["pt"] rtol=1e-6
160+
@test jet.rapfastjet_jets[ievt]["jets"][ijet]["rap"] rtol=tol
161+
@test normalised_phifastjet_jets[ievt]["jets"][ijet]["phi"] rtol=tol
162+
@test jet.ptfastjet_jets[ievt]["jets"][ijet]["pt"] rtol=tol
137163
end
138164
end
139165
end

test/runtests.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ function main()
4141
include("test-pp-reconstruction.jl")
4242
include("test-ee-reconstruction.jl")
4343

44+
# Also test reconstruction with Float32
45+
include("test-f32-reconstruction.jl")
46+
4447
# Compare inputting data in PseudoJet with using a LorentzVector
4548
do_test_compare_types(RecoStrategy.N2Plain, algname = pp_algorithms[-1], power = -1)
4649
do_test_compare_types(RecoStrategy.N2Tiled, algname = pp_algorithms[-1], power = -1)

test/test-f32-reconstruction.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Tests of reconstruction algorithms in Float32
2+
3+
include("common.jl")
4+
5+
durham_njets3_f32 = ComparisonTest(events_file_ee,
6+
joinpath(@__DIR__, "data",
7+
"jet-collections-fastjet-njets3-Durham-eeH.json.gz"),
8+
JetAlgorithm.Durham, RecoStrategy.N2Plain, 1, 4.0,
9+
(cs) -> exclusive_jets(cs; njets = 3),
10+
"exclusive njets Float32", Float32)
11+
12+
run_reco_test(durham_njets3_f32)
13+
14+
antikt_pcut_f32 = ComparisonTest(events_file_pp,
15+
joinpath(@__DIR__, "data",
16+
"jet-collections-fastjet-inclusive-AntiKt.json.gz"),
17+
JetAlgorithm.AntiKt, RecoStrategy.N2Tiled, -1, 0.4,
18+
(cs) -> inclusive_jets(cs; ptmin = 5.0),
19+
"inclusive-float32", Float32)
20+
run_reco_test(antikt_pcut_f32)

0 commit comments

Comments
 (0)