Skip to content

Commit ba1d784

Browse files
Reformat test codes
1 parent 7630c1c commit ba1d784

File tree

2 files changed

+74
-58
lines changed

2 files changed

+74
-58
lines changed

test/data/hepmc32summary.jl

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,21 @@ struct Particle{T}
1313
vertex::Integer
1414
end
1515

16-
Particle{T}() where T = Particle(LorentzVector{T}(0., 0., 0., 0.), 0, 0, 0, 0)
16+
Particle{T}() where {T} = Particle(LorentzVector{T}(0.0, 0.0, 0.0, 0.0), 0, 0, 0, 0)
1717

1818
""" Read a [HepMC3](https://doi.org/10.1016/j.cpc.2020.107310) ascii file.
1919
2020
Each event is passed to the provided function f as a vector of Particles. A
2121
maximum number of events to read (value -1 to read all availble events) and
2222
a number of events to skip at the beginning of the file can be provided.
2323
"""
24-
function read_events(f, fin; maxevents=-1, skipevents=0)
24+
function read_events(f, fin; maxevents = -1, skipevents = 0)
2525
T = Float64
2626
particles = Particle{T}[]
2727
ievent = 0
2828
ipart = 0
2929
toskip = skipevents
30-
30+
3131
for (il, l) in enumerate(eachline(fin))
3232
if occursin(r"HepMC::.*-END_EVENT_LISTING", l)
3333
break
@@ -57,22 +57,22 @@ function read_events(f, fin; maxevents=-1, skipevents=0)
5757
px = parse(T, tok[5])
5858
py = parse(T, tok[6])
5959
pz = parse(T, tok[7])
60-
e = parse(T, tok[8])
60+
e = parse(T, tok[8])
6161
status = parse(Int, tok[10])
62-
push!(particles, Particle{T}(LorentzVector(e,px,py,pz), status, pdgid, barcode, vertex))
62+
push!(particles,
63+
Particle{T}(LorentzVector(e, px, py, pz), status, pdgid, barcode, vertex))
6364
end
6465
end
6566
#processing the last event:
6667
ievent > 0 && f(particles)
6768
end
6869

69-
70-
read_events(fname, maxevents=-1, skipevents=0) = begin
70+
function read_events(fname, maxevents = -1, skipevents = 0)
7171
f = open(fname)
7272

7373
events = Vector{LorentzVector}[]
7474

75-
read_events(f, maxevents=maxevents, skipevents=skipevents) do parts
75+
read_events(f, maxevents = maxevents, skipevents = skipevents) do parts
7676
input_particles = LorentzVector[]
7777
for p in parts
7878
if p.status == 1
@@ -85,35 +85,34 @@ read_events(fname, maxevents=-1, skipevents=0) = begin
8585
events
8686
end
8787

88-
parse_command_line(args) = begin
89-
s = ArgParseSettings(autofix_names = true)
90-
@add_arg_table! s begin
88+
function parse_command_line(args)
89+
s = ArgParseSettings(autofix_names = true)
90+
@add_arg_table! s begin
9191
"--summary"
9292
help = "Print only summary information, filename and average density"
9393
action = :store_true
9494

95-
"--maxevents", "-n"
96-
help = "Maximum number of events to read. -1 to read all events from the file."
97-
arg_type = Int
98-
default = -1
95+
"--maxevents", "-n"
96+
help = "Maximum number of events to read. -1 to read all events from the file."
97+
arg_type = Int
98+
default = -1
9999

100-
"--skip", "-s"
101-
help = "Number of events to skip at beginning of the file."
102-
arg_type = Int
103-
default = 0
100+
"--skip", "-s"
101+
help = "Number of events to skip at beginning of the file."
102+
arg_type = Int
103+
default = 0
104104

105-
"files"
106-
help = "The HepMC3 event files to read."
107-
required = true
105+
"files"
106+
help = "The HepMC3 event files to read."
107+
required = true
108108
nargs = '+'
109-
end
110-
return parse_args(args, s; as_symbols = true)
109+
end
110+
return parse_args(args, s; as_symbols = true)
111111
end
112112

113-
114113
function main()
115114
args = parse_command_line(ARGS)
116-
115+
117116
for file in args[:files]
118117
events = read_events(file, args[:maxevents], args[:skip])
119118
n_events = length(events)

test/runtests.jl

Lines changed: 49 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ using Logging
99
const events_file = joinpath(@__DIR__, "data", "events.hepmc3")
1010

1111
const algorithms = Dict(-1 => "Anti-kt",
12-
0 => "Cambridge/Achen",
13-
1 => "Inclusive-kt")
12+
0 => "Cambridge/Achen",
13+
1 => "Inclusive-kt")
1414

1515
"""Simple structure with necessary parameters for an exclusive selection test"""
1616
struct InclusiveTest
@@ -70,9 +70,12 @@ function main()
7070
unpack_events(events_file)
7171

7272
# Read our fastjet inclusive outputs (we read for anti-kt, cambridge/achen, inclusive-kt)
73-
fastjet_alg_files_inclusive = Dict(-1 => joinpath(@__DIR__, "data", "jet-collections-fastjet-inclusive-akt.json"),
74-
0 => joinpath(@__DIR__, "data", "jet-collections-fastjet-inclusive-ca.json"),
75-
1 => joinpath(@__DIR__, "data", "jet-collections-fastjet-inclusive-ikt.json"))
73+
fastjet_alg_files_inclusive = Dict(-1 => joinpath(@__DIR__, "data",
74+
"jet-collections-fastjet-inclusive-akt.json"),
75+
0 => joinpath(@__DIR__, "data",
76+
"jet-collections-fastjet-inclusive-ca.json"),
77+
1 => joinpath(@__DIR__, "data",
78+
"jet-collections-fastjet-inclusive-ikt.json"))
7679

7780
fastjet_data = Dict{Int, Vector{Any}}()
7881
for (k, v) in pairs(fastjet_alg_files_inclusive)
@@ -83,8 +86,10 @@ function main()
8386

8487
# Test each stratgy for inclusive jet selection
8588
for power in keys(algorithms)
86-
do_test_compare_to_fastjet(RecoStrategy.N2Plain, fastjet_data[power], algname = algorithms[power], power = power)
87-
do_test_compare_to_fastjet(RecoStrategy.N2Tiled, fastjet_data[power], algname = algorithms[power], power = power)
89+
do_test_compare_to_fastjet(RecoStrategy.N2Plain, fastjet_data[power],
90+
algname = algorithms[power], power = power)
91+
do_test_compare_to_fastjet(RecoStrategy.N2Tiled, fastjet_data[power],
92+
algname = algorithms[power], power = power)
8893
end
8994

9095
# Compare inputing data in PseudoJet with using a LorentzVector
@@ -93,17 +98,27 @@ function main()
9398

9499
# Now test exclusive selections
95100
inclusive_tests = InclusiveTest[]
96-
push!(inclusive_tests, InclusiveTest("exclusive njets=4", 1, "jet-collections-fastjet-njets4-ikt.json", nothing, 4))
97-
push!(inclusive_tests, InclusiveTest("exclusive dijmax=20", 1, "jet-collections-fastjet-dij20-ikt.json", 20.0, nothing))
98-
push!(inclusive_tests, InclusiveTest("exclusive njets=4", 0, "jet-collections-fastjet-njets4-ca.json", nothing, 4))
99-
push!(inclusive_tests, InclusiveTest("exclusive dijmax=0.99", 0, "jet-collections-fastjet-dij099-ca.json", 0.99, nothing))
101+
push!(inclusive_tests,
102+
InclusiveTest("exclusive njets=4", 1, "jet-collections-fastjet-njets4-ikt.json",
103+
nothing, 4))
104+
push!(inclusive_tests,
105+
InclusiveTest("exclusive dijmax=20", 1, "jet-collections-fastjet-dij20-ikt.json",
106+
20.0, nothing))
107+
push!(inclusive_tests,
108+
InclusiveTest("exclusive njets=4", 0, "jet-collections-fastjet-njets4-ca.json",
109+
nothing, 4))
110+
push!(inclusive_tests,
111+
InclusiveTest("exclusive dijmax=0.99", 0,
112+
"jet-collections-fastjet-dij099-ca.json", 0.99, nothing))
100113

101114
for test in inclusive_tests
102115
input_file = joinpath(@__DIR__, "data", test.fastjet_file)
103116
fastjet_jets = read_fastjet_outputs(input_file)
104117
sort_jets!(fastjet_jets)
105-
do_test_compare_to_fastjet(RecoStrategy.N2Tiled, fastjet_jets; algname = test.algname,
106-
power = test.power, selection = test.selction, njets = test.njets, dijmax = test.dijmax)
118+
do_test_compare_to_fastjet(RecoStrategy.N2Tiled, fastjet_jets;
119+
algname = test.algname,
120+
power = test.power, selection = test.selction,
121+
njets = test.njets, dijmax = test.dijmax)
107122
end
108123
end
109124

@@ -116,13 +131,13 @@ njets -> apply inclusive njets cut
116131
If dijmax and njets are nothing, test inclusive jets with pt >= ptmin
117132
"""
118133
function do_test_compare_to_fastjet(strategy::RecoStrategy.Strategy, fastjet_jets;
119-
algname = "Unknown",
120-
selection = "Inclusive",
121-
ptmin::Float64 = 5.0,
122-
distance::Float64 = 0.4,
123-
power::Integer = -1,
124-
dijmax = nothing,
125-
njets = nothing)
134+
algname = "Unknown",
135+
selection = "Inclusive",
136+
ptmin::Float64 = 5.0,
137+
distance::Float64 = 0.4,
138+
power::Integer = -1,
139+
dijmax = nothing,
140+
njets = nothing)
126141

127142
# Strategy
128143
if (strategy == RecoStrategy.N2Plain)
@@ -169,9 +184,9 @@ function do_test_compare_to_fastjet(strategy::RecoStrategy.Strategy, fastjet_jet
169184
# the momentum
170185
# Sometimes phi could be in the range [-π, π], but FastJet always is [0, 2π]
171186
normalised_phi = jet.phi < 0.0 ? jet.phi + 2π : jet.phi
172-
@test jet.rap fastjet_jets[ievt]["jets"][ijet]["rap"] atol = 1e-7
173-
@test normalised_phi fastjet_jets[ievt]["jets"][ijet]["phi"] atol = 1e-7
174-
@test jet.pt fastjet_jets[ievt]["jets"][ijet]["pt"] rtol = 1e-6
187+
@test jet.rapfastjet_jets[ievt]["jets"][ijet]["rap"] atol=1e-7
188+
@test normalised_phifastjet_jets[ievt]["jets"][ijet]["phi"] atol=1e-7
189+
@test jet.ptfastjet_jets[ievt]["jets"][ijet]["pt"] rtol=1e-6
175190
end
176191
end
177192
end
@@ -180,10 +195,10 @@ function do_test_compare_to_fastjet(strategy::RecoStrategy.Strategy, fastjet_jet
180195
end
181196

182197
function do_test_compare_types(strategy::RecoStrategy.Strategy;
183-
algname = "Unknown",
184-
ptmin::Float64 = 5.0,
185-
distance::Float64 = 0.4,
186-
power::Integer = -1)
198+
algname = "Unknown",
199+
ptmin::Float64 = 5.0,
200+
distance::Float64 = 0.4,
201+
power::Integer = -1)
187202

188203
# Strategy
189204
if (strategy == RecoStrategy.N2Plain)
@@ -201,7 +216,8 @@ function do_test_compare_types(strategy::RecoStrategy.Strategy;
201216
events::Vector{Vector{PseudoJet}} = read_final_state_particles(events_file)
202217
jet_collection = FinalJets[]
203218
for (ievt, event) in enumerate(events)
204-
finaljets = final_jets(inclusive_jets(jet_reconstruction(event, R = distance, p = power), ptmin))
219+
finaljets = final_jets(inclusive_jets(jet_reconstruction(event, R = distance,
220+
p = power), ptmin))
205221
sort_jets!(finaljets)
206222
push!(jet_collection, FinalJets(ievt, finaljets))
207223
end
@@ -210,7 +226,8 @@ function do_test_compare_types(strategy::RecoStrategy.Strategy;
210226
events_lv::Vector{Vector{LorentzVector}} = read_final_state_particles_lv(events_file)
211227
jet_collection_lv = FinalJets[]
212228
for (ievt, event) in enumerate(events_lv)
213-
finaljets = final_jets(inclusive_jets(jet_reconstruction(event, R = distance, p = power), ptmin))
229+
finaljets = final_jets(inclusive_jets(jet_reconstruction(event, R = distance,
230+
p = power), ptmin))
214231
sort_jets!(finaljets)
215232
push!(jet_collection_lv, FinalJets(ievt, finaljets))
216233
end
@@ -222,9 +239,9 @@ function do_test_compare_types(strategy::RecoStrategy.Strategy;
222239
@test size(event.jets) == size(event_lv.jets)
223240
# Test each jet in turn
224241
for (jet, jet_lv) in zip(event.jets, event_lv.jets)
225-
@test jet.rap jet_lv.rap atol = 1e-7
226-
@test jet.phi jet_lv.phi atol = 1e-7
227-
@test jet.pt jet_lv.pt rtol = 1e-6
242+
@test jet.rapjet_lv.rap atol=1e-7
243+
@test jet.phijet_lv.phi atol=1e-7
244+
@test jet.ptjet_lv.pt rtol=1e-6
228245
end
229246
end
230247
end

0 commit comments

Comments
 (0)