Skip to content

Commit

Permalink
Reformatted Julia sources
Browse files Browse the repository at this point in the history
From a `format(".")` in the package root
  • Loading branch information
graeme-a-stewart committed Jun 7, 2024
1 parent 4905f6a commit 14beebf
Show file tree
Hide file tree
Showing 18 changed files with 580 additions and 555 deletions.
114 changes: 59 additions & 55 deletions examples/instrumented-jetreco.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,44 +22,39 @@ using JetReconstruction
# Parsing for algorithm and strategy enums
include(joinpath(@__DIR__, "parse-options.jl"))

function profile_code(profile, jet_reconstruction, events, niters; R = 0.4, p = -1, strategy = RecoStrategy.N2Tiled)
function profile_code(profile, jet_reconstruction, events, niters; R = 0.4, p = -1,
strategy = RecoStrategy.N2Tiled)
Profile.init(n = 5 * 10^6, delay = 0.00001)
profile_events(events) = begin
for evt in events
jet_reconstruction(evt, R = R, p = p, strategy = strategy)
end
end
profile_events(events[1:1])
@profile for i 1:niters
@profile for i in 1:niters
profile_events(events)
end
profile_path = joinpath("profile", profile, "profsvg.svg")
mkpath(dirname(profile_path))
statprofilehtml(path = dirname(profile_path))
fcolor = FlameGraphs.FlameColors(
reverse(colormap("Blues", 15))[1:5],
colorant"slategray4",
colorant"gray95",
reverse(colormap("Reds", 15))[1:5],
reverse(sequential_palette(39, 10; s = 38, b = 2))[1:5],#brownish pallette
)
ProfileSVG.save(
fcolor,
profile_path,
combine = true,
timeunit = :ms,
font = "Arial, Helvetica, sans-serif",
)
println(
"Flame graph from ProfileSVG.jl at file://",
abspath(profile_path),
"\n",
"""
\tRed tint: Runtime dispatch
\tBrown/yellow tint: Garbage collection
\tBlue tint: OK
""",
)
fcolor = FlameGraphs.FlameColors(reverse(colormap("Blues", 15))[1:5],
colorant"slategray4",
colorant"gray95",
reverse(colormap("Reds", 15))[1:5],
reverse(sequential_palette(39, 10; s = 38, b = 2))[1:5])
ProfileSVG.save(fcolor,
profile_path,
combine = true,
timeunit = :ms,
font = "Arial, Helvetica, sans-serif")
println("Flame graph from ProfileSVG.jl at file://",
abspath(profile_path),
"\n",
"""
\tRed tint: Runtime dispatch
\tBrown/yellow tint: Garbage collection
\tBlue tint: OK
""")
end
"""
Top level call funtion for demonstrating the use of jet reconstruction
Expand All @@ -70,20 +65,18 @@ happens inside the JetReconstruction package itself.
Some other ustilities are also supported here, such as profiling and
serialising the reconstructed jet outputs.
"""
function jet_process(
events::Vector{Vector{PseudoJet}};
distance::Real = 0.4,
algorithm::JetAlgorithm.Algorithm = JetAlgorithm.AntiKt,
ptmin::Real = 5.0,
dcut = nothing,
njets = nothing,
strategy::RecoStrategy.Strategy,
nsamples::Integer = 1,
gcoff::Bool = false,
profile = nothing,
alloc::Bool = false,
dump::Union{String, Nothing} = nothing,
)
function jet_process(events::Vector{Vector{PseudoJet}};
distance::Real = 0.4,
algorithm::JetAlgorithm.Algorithm = JetAlgorithm.AntiKt,
ptmin::Real = 5.0,
dcut = nothing,
njets = nothing,
strategy::RecoStrategy.Strategy,
nsamples::Integer = 1,
gcoff::Bool = false,
profile = nothing,
alloc::Bool = false,
dump::Union{String, Nothing} = nothing,)
@info "Will process $(size(events)[1]) events"

# Map algorithm to power
Expand All @@ -98,19 +91,22 @@ function jet_process(
if nsamples > 1 || !isnothing(profile)
@info "Doing initial warm-up run"
for event in events
_ = inclusive_jets(jet_reconstruct(event, R = distance, p = power, strategy = strategy), ptmin)
_ = inclusive_jets(jet_reconstruct(event, R = distance, p = power,
strategy = strategy), ptmin)
end
end

if !isnothing(profile)
profile_code(profile, jet_reconstruct, events, nsamples, R = distance, p = power, strategy = strategy)
profile_code(profile, jet_reconstruct, events, nsamples, R = distance, p = power,
strategy = strategy)
return nothing
end

if alloc
println("Memory allocation statistics:")
@timev for event in events
_ = inclusive_jets(jet_reconstruct(event, R = distance, p = power, strategy = strategy), ptmin)
_ = inclusive_jets(jet_reconstruct(event, R = distance, p = power,
strategy = strategy), ptmin)
end
return nothing
end
Expand All @@ -120,16 +116,21 @@ function jet_process(
cummulative_time = 0.0
cummulative_time2 = 0.0
lowest_time = typemax(Float64)
for irun 1:nsamples
for irun in 1:nsamples
gcoff && GC.enable(false)
t_start = time_ns()
for (ievt, event) in enumerate(events)
if !isnothing(njets)
finaljets = exclusive_jets(jet_reconstruct(event, R = distance, p = power, strategy = strategy), njets = njets)
finaljets = exclusive_jets(jet_reconstruct(event, R = distance, p = power,
strategy = strategy),
njets = njets)
elseif !isnothing(dcut)
finaljets = exclusive_jets(jet_reconstruct(event, R = distance, p = power, strategy = strategy), dcut = dcut)
finaljets = exclusive_jets(jet_reconstruct(event, R = distance, p = power,
strategy = strategy),
dcut = dcut)
else
finaljets = inclusive_jets(jet_reconstruct(event, R = distance, p = power, strategy = strategy), ptmin)
finaljets = inclusive_jets(jet_reconstruct(event, R = distance, p = power,
strategy = strategy), ptmin)
end
# Only print the jet content once
if irun == 1
Expand Down Expand Up @@ -185,7 +186,7 @@ function jet_process(
end
end

parse_command_line(args) = begin
function parse_command_line(args)
s = ArgParseSettings(autofix_names = true)
@add_arg_table! s begin
"--maxevents", "-n"
Expand Down Expand Up @@ -262,7 +263,7 @@ parse_command_line(args) = begin
return parse_args(args, s; as_symbols = true)
end

main() = begin
function main()
args = parse_command_line(ARGS)
if args[:debug]
logger = ConsoleLogger(stdout, Logging.Debug)
Expand All @@ -272,12 +273,15 @@ main() = begin
logger = ConsoleLogger(stdout, Logging.Warn)
end
global_logger(logger)
events::Vector{Vector{PseudoJet}} =
read_final_state_particles(args[:file], maxevents = args[:maxevents], skipevents = args[:skip])
jet_process(events, distance = args[:distance], algorithm = args[:algorithm], strategy = args[:strategy],
ptmin = args[:ptmin], dcut = args[:exclusive_dcut], njets = args[:exclusive_njets],
nsamples = args[:nsamples], gcoff = args[:gcoff], profile = args[:profile],
alloc = args[:alloc], dump = args[:dump])
events::Vector{Vector{PseudoJet}} = read_final_state_particles(args[:file],
maxevents = args[:maxevents],
skipevents = args[:skip])
jet_process(events, distance = args[:distance], algorithm = args[:algorithm],
strategy = args[:strategy],
ptmin = args[:ptmin], dcut = args[:exclusive_dcut],
njets = args[:exclusive_njets],
nsamples = args[:nsamples], gcoff = args[:gcoff], profile = args[:profile],
alloc = args[:alloc], dump = args[:dump])
nothing
end

Expand Down
45 changes: 24 additions & 21 deletions examples/jetreco.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,14 @@ happens inside the JetReconstruction package itself.
Final jets can be serialised if the "dump" option is given
"""
function jet_process(
events::Vector{Vector{PseudoJet}};
distance::Real = 0.4,
algorithm::JetAlgorithm.Algorithm = JetAlgorithm.AntiKt,
ptmin::Real = 5.0,
dcut = nothing,
njets = nothing,
strategy::RecoStrategy.Strategy,
dump::Union{String, Nothing} = nothing,
)
function jet_process(events::Vector{Vector{PseudoJet}};
distance::Real = 0.4,
algorithm::JetAlgorithm.Algorithm = JetAlgorithm.AntiKt,
ptmin::Real = 5.0,
dcut = nothing,
njets = nothing,
strategy::RecoStrategy.Strategy,
dump::Union{String, Nothing} = nothing,)
@info "Will process $(size(events)[1]) events"

# If we are dumping the results, setup the JSON structure
Expand All @@ -57,11 +55,14 @@ function jet_process(
# Now run over each event
for (ievt, event) in enumerate(events)
if !isnothing(njets)
finaljets = exclusive_jets(jet_reconstruct(event, R = distance, p = power, strategy = strategy), njets = njets)
finaljets = exclusive_jets(jet_reconstruct(event, R = distance, p = power,
strategy = strategy), njets = njets)
elseif !isnothing(dcut)
finaljets = exclusive_jets(jet_reconstruct(event, R = distance, p = power, strategy = strategy), dcut = dcut)
finaljets = exclusive_jets(jet_reconstruct(event, R = distance, p = power,
strategy = strategy), dcut = dcut)
else
finaljets = inclusive_jets(jet_reconstruct(event, R = distance, p = power, strategy = strategy), ptmin)
finaljets = inclusive_jets(jet_reconstruct(event, R = distance, p = power,
strategy = strategy), ptmin)
end
@info begin
jet_output = "Event $(ievt)\n"
Expand All @@ -83,7 +84,7 @@ function jet_process(
end
end

parse_command_line(args) = begin
function parse_command_line(args)
s = ArgParseSettings(autofix_names = true)
@add_arg_table! s begin
"--maxevents", "-n"
Expand Down Expand Up @@ -134,16 +135,18 @@ parse_command_line(args) = begin
return parse_args(args, s; as_symbols = true)
end


main() = begin
function main()
args = parse_command_line(ARGS)
logger = ConsoleLogger(stdout, Logging.Info)
global_logger(logger)
events::Vector{Vector{PseudoJet}} =
read_final_state_particles(args[:file], maxevents = args[:maxevents], skipevents = args[:skip])
jet_process(events, distance = args[:distance], algorithm = args[:algorithm], strategy = args[:strategy],
ptmin = args[:ptmin], dcut = args[:exclusive_dcut], njets = args[:exclusive_njets],
dump = args[:dump])
events::Vector{Vector{PseudoJet}} = read_final_state_particles(args[:file],
maxevents = args[:maxevents],
skipevents = args[:skip])
jet_process(events, distance = args[:distance], algorithm = args[:algorithm],
strategy = args[:strategy],
ptmin = args[:ptmin], dcut = args[:exclusive_dcut],
njets = args[:exclusive_njets],
dump = args[:dump])
nothing
end

Expand Down
22 changes: 11 additions & 11 deletions examples/parse-options.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ from the command line.
"""

function ArgParse.parse_item(::Type{RecoStrategy.Strategy}, x::AbstractString)
s = tryparse(RecoStrategy.Strategy, x)
if s === nothing
throw(ErrorException("Invalid value for strategy: $(x)"))
end
s
s = tryparse(RecoStrategy.Strategy, x)
if s === nothing
throw(ErrorException("Invalid value for strategy: $(x)"))
end
s
end

function ArgParse.parse_item(::Type{JetAlgorithm.Algorithm}, x::AbstractString)
s = tryparse(JetAlgorithm.Algorithm, x)
if s === nothing
throw(ErrorException("Invalid value for algorithm: $(x)"))
end
s
end
s = tryparse(JetAlgorithm.Algorithm, x)
if s === nothing
throw(ErrorException("Invalid value for algorithm: $(x)"))
end
s
end
10 changes: 5 additions & 5 deletions examples/visualise-jets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ using JetReconstruction
include(joinpath(@__DIR__, "parse-options.jl"))

function main()

s = ArgParseSettings(autofix_names = true)
@add_arg_table s begin
"--event", "-e"
Expand Down Expand Up @@ -57,15 +56,16 @@ function main()
end
args = parse_args(ARGS, s; as_symbols = true)


logger = ConsoleLogger(stdout, Logging.Info)
global_logger(logger)

events::Vector{Vector{PseudoJet}} =
read_final_state_particles(args[:file], maxevents = args[:event], skipevents = args[:event])
events::Vector{Vector{PseudoJet}} = read_final_state_particles(args[:file],
maxevents = args[:event],
skipevents = args[:event])

power = JetReconstruction.algorithm2power[args[:algorithm]]
cs = jet_reconstruct(events[1], R = args[:distance], p = power, strategy = args[:strategy])
cs = jet_reconstruct(events[1], R = args[:distance], p = power,
strategy = args[:strategy])

plt = jetsplot(events[1], cs; Module = CairoMakie)
save(args[:output], plt)
Expand Down
35 changes: 19 additions & 16 deletions ext/JetVisualisation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ using WGLMakie
jetsplot(my_objects, cs, Module=Main) #default
```
"""
function JetReconstruction.jetsplot(objects, cs::ClusterSequence; barsize_phi = 0.1, barsize_eta = 0.1, colormap = :glasbey_hv_n256, Module = CairoMakie)
function JetReconstruction.jetsplot(objects, cs::ClusterSequence; barsize_phi = 0.1,
barsize_eta = 0.1, colormap = :glasbey_hv_n256,
Module = CairoMakie)
idx_arrays = Vector{Int}[]
for elt in cs.history
elt.parent2 == JetReconstruction.BeamJet || continue
Expand Down Expand Up @@ -84,28 +86,29 @@ using WGLMakie
jetsplot(my_objects, my_colour_arrays, Module=Main) #default
```
"""
function JetReconstruction.jetsplot(objects, idx_arrays; barsize_phi = 0.1, barsize_eta = 0.1, colormap = :glasbey_hv_n256, Module = Main)
function JetReconstruction.jetsplot(objects, idx_arrays; barsize_phi = 0.1,
barsize_eta = 0.1, colormap = :glasbey_hv_n256,
Module = Main)
cs = fill(0, length(objects)) # colours
for i in 1:length(idx_arrays), j in idx_arrays[i]
cs[j] = i
end

pts = sqrt.(JetReconstruction.pt2.(objects))

Module.meshscatter(
Module.Point3f.(JetReconstruction.phi.(objects), JetReconstruction.rapidity.(objects), 0pts);
color = cs,
markersize = Module.Vec3f.(barsize_phi, barsize_eta, pts),
colormap = colormap,
marker = Module.Rect3f(Module.Vec3f(0), Module.Vec3f(1)),
figure = (size = (700, 600),),
axis = (
type = Module.Axis3, perspectiveness = 0.5, azimuth = 2.6, elevation = 0.5,
xlabel = "ϕ", ylabel = "η", zlabel = "kt",
limits = (nothing, nothing, nothing, nothing, 0, findmax(pts)[1] + 10),
),
shading = NoShading,
)
Module.meshscatter(Module.Point3f.(JetReconstruction.phi.(objects),
JetReconstruction.rapidity.(objects), 0pts);
color = cs,
markersize = Module.Vec3f.(barsize_phi, barsize_eta, pts),
colormap = colormap,
marker = Module.Rect3f(Module.Vec3f(0), Module.Vec3f(1)),
figure = (size = (700, 600),),
axis = (type = Module.Axis3, perspectiveness = 0.5, azimuth = 2.6,
elevation = 0.5,
xlabel = "ϕ", ylabel = "η", zlabel = "kt",
limits = (nothing, nothing, nothing, nothing, 0,
findmax(pts)[1] + 10)),
shading = NoShading,)
end

end
Loading

0 comments on commit 14beebf

Please sign in to comment.