Skip to content

Commit

Permalink
Support unpacking .xz data files
Browse files Browse the repository at this point in the history
Automate the unpacking of LZMA compressed data files
for tests (this needs "xzcat" to be present on the system).

Ensure files are closed on reading events.
  • Loading branch information
graeme-a-stewart committed Apr 19, 2024
1 parent 0c4b2e0 commit 8526406
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/Utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ function read_final_state_particles(fname; maxevents = -1, skipevents = 0)
push!(events, input_particles)
ipart += 1
end
close(f)

@info "Total Events: $(length(events))"
@debug events
Expand All @@ -46,6 +47,7 @@ function read_final_state_particles_lv(fname; maxevents = -1, skipevents = 0)
push!(events, input_particles)
ipart += 1
end
close(f)

@info "Total Events: $(length(events))"
@debug events
Expand Down
Binary file removed test/data/events.hepmc3.gz
Binary file not shown.
Binary file added test/data/events.hepmc3.xz
Binary file not shown.
27 changes: 24 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,24 @@ using JSON
using LorentzVectorHEP
using Logging

const events_file = joinpath(@__DIR__, "data", "events.hepmc3")

"""Decompress data file, if necessary"""
function unpack_events(file)
# File already there?
if isfile(file)
return true
end
# LZMA source?
file_compressed = joinpath(dirname(file), basename(file)*".xz")
if isfile(file_compressed)
@debug "Unpacking $(file_compressed)"
run(pipeline(`xzcat $file_compressed`, stdout=file))
return true
end
false
end

"""Read JSON file with fastjet jets in it"""
function read_fastjet_outputs(fname)
f = open(fname)
Expand All @@ -31,6 +49,9 @@ function sort_jets!(jet_array::Vector{LorentzVectorCyl})
end

function main()
# If necessary, unzip the events data file
unpack_events(events_file)

# Read our fastjet outputs (we read for anti-kt, cambridge/achen, inclusive-kt)
algorithms = Dict(-1 => "Anti-kt",
0 => "Cambridge/Achen",
Expand Down Expand Up @@ -76,7 +97,7 @@ function do_test_compare_to_fastjet(strategy::JetRecoStrategy.Strategy, fastjet_

# Now run our jet reconstruction...
# From PseudoJets
events::Vector{Vector{PseudoJet}} = read_final_state_particles(joinpath(@__DIR__, "data", "events.hepmc3"))
events::Vector{Vector{PseudoJet}} = read_final_state_particles(events_file)
jet_collection = FinalJets[]
for (ievt, event) in enumerate(events)
finaljets = final_jets(inclusive_jets(jet_reconstruction(event, R = distance, p = power), ptmin))
Expand Down Expand Up @@ -127,7 +148,7 @@ function do_test_compare_types(strategy::JetRecoStrategy.Strategy;

# Now run our jet reconstruction...
# From PseudoJets
events::Vector{Vector{PseudoJet}} = read_final_state_particles(joinpath(@__DIR__, "data", "events.hepmc3"))
events::Vector{Vector{PseudoJet}} = read_final_state_particles(events_file)
jet_collection = FinalJets[]
for (ievt, event) in enumerate(events)
finaljets = final_jets(inclusive_jets(jet_reconstruction(event, R = distance, p = power), ptmin))
Expand All @@ -136,7 +157,7 @@ function do_test_compare_types(strategy::JetRecoStrategy.Strategy;
end

# From LorentzVector
events_lv::Vector{Vector{LorentzVector}} = read_final_state_particles_lv(joinpath(@__DIR__, "data", "events.hepmc3"))
events_lv::Vector{Vector{LorentzVector}} = read_final_state_particles_lv(events_file)
jet_collection_lv = FinalJets[]
for (ievt, event) in enumerate(events_lv)
finaljets = final_jets(inclusive_jets(jet_reconstruction(event, R = distance, p = power), ptmin))
Expand Down

0 comments on commit 8526406

Please sign in to comment.