Skip to content

Commit

Permalink
corr bkg test function
Browse files Browse the repository at this point in the history
  • Loading branch information
Sofia Calgaro committed Dec 31, 2024
1 parent daf90a3 commit 6e1610d
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 10 deletions.
43 changes: 33 additions & 10 deletions src/fitting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ using Plots
using Cuba
using SpecialFunctions

"""
get_bkg_info(config)
Function that retrieves background information from config in input.
"""
function get_bkg_info(config)
bkg_shape=:uniform
bkg_shape_pars=nothing
Expand All @@ -18,6 +23,33 @@ function get_bkg_info(config)
return bkg_shape,bkg_shape_pars
end


"""
get_corr_info(config)
Function that retrieves information about correlated background from config in input.
"""
function get_corr_info(config)
if !(haskey(config["bkg"], "correlated"))
return false, nothing, nothing
end

if (haskey(config["bkg"],"correlated")) & (config["bkg"]["correlated"]["mode"]!="none")
corr= true
hier_mode =config["bkg"]["correlated"]["mode"]
hier_range =config["bkg"]["correlated"]["range"]
return corr,hier_mode,hier_range
else
return false,nothing,nothing
end
end


"""
get_range(fit_range::Union{Vector{Vector{Int}}, Vector{Vector{Float64}}})
Function that returns lower and upper edges of fit ranges.
"""
function get_range(fit_range::Union{Vector{Vector{Int}}, Vector{Vector{Float64}}})
range_l = [arr[1] for arr in fit_range]
range_h = [arr[2] for arr in fit_range]
Expand Down Expand Up @@ -137,16 +169,7 @@ Function to retrieve useful pieces (prior, likelihood, posterior), also in savin
function get_stat_blocks(partitions,events::Array{Vector{Float64}},part_event_index,fit_ranges;config,bkg_only)
settings=get_settings(config)


if (haskey(config["bkg"],"correlated")) & (config["bkg"]["correlated"]["mode"]!="none")
corr= true
hier_mode =config["bkg"]["correlated"]["mode"]
hier_range =config["bkg"]["correlated"]["range"]
else
corr=false
hier_mode=nothing
hier_range=nothing
end
corr,hier_mode,hier_range = get_corr_info(config)

bkg_shape,bkg_shape_pars = get_bkg_info(config)

Expand Down
1 change: 1 addition & 0 deletions test/io/test_all.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ Test.@testset "likelihood" begin
include("test_get_events.jl")
include("test_get_partition_event_index.jl")
include("test_get_partitions_events.jl")
include("test_get_corr_info.jl")
end
73 changes: 73 additions & 0 deletions test/io/test_get_corr_info.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using Pkg
Pkg.activate(".")
Pkg.instantiate()
using Random
include("../../src/ZeroNuFit.jl")
using .ZeroNuFit
include("../../main.jl")

@testset "test_get_corr_info" begin

@info "Testing function to retrieve bkg correlation info (function 'get_corr_info' in src/fitting.jl)"

# no entry for correlated bkg
config = Dict("bkg" => Dict())

corr = false
hier_mode=nothing
hier_range=nothing
try
corr,hier_mode,hier_range = ZeroNuFit.get_corr_info(config)
catch e
@error "Error in 'get_corr_info' evaluation: $e"
throw(e)
end

@test corr == false
@test hier_mode == nothing
@test hier_range == nothing

# not-correlated bkg
config = Dict("bkg" => Dict("correlated" => Dict("mode" => "none", "range" => "none")))

corr = false
hier_mode=nothing
hier_range=nothing
try
corr,hier_mode,hier_range = ZeroNuFit.get_corr_info(config)
catch e
@error "Error in 'get_corr_info' evaluation: $e"
throw(e)
end

@test corr == false
@test hier_mode == nothing
@test hier_range == nothing

# correlated bkg
config = Dict("bkg" => Dict("correlated" => Dict("mode" => "lognormal", "range" => [0,0.1])))

corr = false
hier_mode=nothing
hier_range=nothing
try
corr,hier_mode,hier_range = ZeroNuFit.get_corr_info(config)
catch e
@error "Error in 'get_corr_info' evaluation: $e"
throw(e)
end

expected_value = true
@testset "Check corr accuracy" begin
@test corr == expected_value
end
expected_value = config["bkg"]["correlated"]["mode"]
@testset "Check hier_mode accuracy" begin
@test hier_mode == expected_value
end
expected_value = config["bkg"]["correlated"]["range"]
@testset "Check hier_range accuracy" begin
@test hier_range == expected_value
end

end

0 comments on commit 6e1610d

Please sign in to comment.