Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code formatting #56

Merged
merged 3 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .JuliaFormatter.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
style = "sciml"
yas_style_nesting = true
13 changes: 13 additions & 0 deletions .github/workflows/Format.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: Format suggestions
on:
pull_request:
# this argument is not required if you don't use the `suggestion-label` input
types: [ opened, reopened, synchronize, labeled, unlabeled ]
jobs:
code-style:
runs-on: ubuntu-latest
steps:
- uses: julia-actions/julia-format@v3
with:
version: '1' # Set `version` to '1.0.54' if you need to use JuliaFormatter.jl v1.0.54 (default: '1')
suggestion-label: 'format-suggest' # leave this unset or empty to show suggestions for all PRs
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
Loading
Loading