Skip to content

Commit

Permalink
Merge pull request #117 from legend-exp/aoe_tests
Browse files Browse the repository at this point in the history
Add tests for A/E correction fits (individual and combined)
  • Loading branch information
fhagemann authored Feb 20, 2025
2 parents 72432a2 + a9df3bb commit 113fc5f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ LsqFit = "0.14, 0.15"
Measurements = "2.5"
Measures = "0.3"
Optim = "1.7.5"
Optimization = "3.25, 4"
Optimization = "3.26.0, 4"
OptimizationBBO = "0.3, 0.4"
OptimizationNLopt = "0.2, 0.3"
OptimizationOptimJL = "0.3, 0.4"
Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ Test.@testset "Package LegendSpecFits" begin
include("test_singlefit.jl")
include("test_docs.jl")
include("test_lq.jl")
include("test_aoe.jl")
isempty(Test.detect_ambiguities(LegendSpecFits))
end # testset
46 changes: 46 additions & 0 deletions test/test_aoe.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# This file is a part of LegendSpecFits.jl, licensed under the MIT License (MIT).

using LegendSpecFits
using Test
using Measurements: value as mvalue
using Distributions
using Unitful

include("test_utils.jl")


@testset "A/E energy correction" begin

# generate example A/E distribution with E-dependence and low A/E tail
e_cal = rand(Distributions.Exponential(300), 5_000_000) .+ 300
μA, μB, σA, σB = 1.01, -4e-6, 5e-3, 12.0
myμ(E) = μA + μB * E
myσ(E) = sqrt(σA^2 + σB^2/E^2)
aoe = [let= myμ(E), _σ = myσ(E); (rand() < 0.2 ? -rand(Distributions.Exponential(5*_σ)) : 0) +*randn() + _μ; end for E in e_cal]


# fit the A/E vs. E distribution
compton_bands = collect((550:50:2350)u"keV")
compton_window = 20u"keV"
compton_band_peakhists = LegendSpecFits.generate_aoe_compton_bands(aoe, e_cal*u"keV", compton_bands, compton_window)
result_fit, report_fit = LegendSpecFits.fit_aoe_compton(compton_band_peakhists.peakhists, compton_band_peakhists.peakstats, compton_bands, uncertainty=true)
μs = [result_fit[band].μ for band in compton_bands]
σs = [result_fit[band].σ for band in compton_bands]
result_fit_single, report_fit_single = LegendSpecFits.fit_aoe_corrections(compton_bands, μs, σs)
result_fit_combined, report_fit_combined = LegendSpecFits.fit_aoe_compton_combined(compton_band_peakhists.peakhists, compton_band_peakhists.peakstats, compton_bands, result_fit_single, uncertainty=true)

# check that the measured results agree within 5% with the original values
@testset "Individual A/E fits" begin
@test isapprox(mvalue(result_fit_single.µ_compton.par[1]), μA, rtol = 0.1)
@test isapprox(mvalue(result_fit_single.µ_compton.par[2]), μB * u"keV^-1", rtol = 0.1)
@test isapprox(abs(mvalue(result_fit_single.σ_compton.par[1])), σA, rtol = 0.1)
@test isapprox(abs(mvalue(result_fit_single.σ_compton.par[2])), σB * u"keV^2", rtol = 0.1)
end

@testset "Combined A/E fits" begin
@test isapprox(mvalue(result_fit_combined.µA), μA, rtol = 0.1)
@test isapprox(mvalue(result_fit_combined.μB), μB, rtol = 0.1)
@test isapprox(abs(mvalue(result_fit_combined.σA)), σA, rtol = 0.1)
@test isapprox(abs(mvalue(result_fit_combined.σB)), σB, rtol = 0.1)
end
end

0 comments on commit 113fc5f

Please sign in to comment.