From 4bc261a4cd3f37f54917b2be81e8094966d74670 Mon Sep 17 00:00:00 2001 From: Andreas Gieb Date: Thu, 27 Feb 2025 14:23:08 +0100 Subject: [PATCH 1/2] updated sideband selection logic for detectors with bad energy resoltion; added plot recipe to monitor sidebandregions --- ext/LegendSpecFitsRecipesBaseExt.jl | 38 ++++++++++++++++++++++++++--- src/lqcut.jl | 30 +++++++++++++++++++---- 2 files changed, 59 insertions(+), 9 deletions(-) diff --git a/ext/LegendSpecFitsRecipesBaseExt.jl b/ext/LegendSpecFitsRecipesBaseExt.jl index e753fee6..b2bee21a 100644 --- a/ext/LegendSpecFitsRecipesBaseExt.jl +++ b/ext/LegendSpecFitsRecipesBaseExt.jl @@ -1476,7 +1476,7 @@ end # recipe for the lq_cut report -@recipe function f(report::NamedTuple{(:cut, :fit_result, :temp_hists, :fit_report)}, lq_class::Vector{Float64}, e_cal, plot_type::Symbol) +@recipe function f(report::NamedTuple{(:cut, :fit_result, :temp_hists, :fit_report, :dep_σ, :edges)}, lq_class::Vector{Float64}, e_cal, plot_type::Symbol) # Extract cutvalue from the report cut_value = Measurements.value.(report.cut) @@ -1662,10 +1662,40 @@ end report.temp_hists.hist_sb2 end - + elseif plot_type == :energy_spectrum + # Plot energy spectrum with DEP and sideband regions + left_margin := -2Plots.mm + bottom_margin := -4Plots.mm + top_margin := -3Plots.mm + thickness_scaling := 1.6 + size := (1200, 900) + framestyle := :box + formatter := :plain + xlabel := "Energy" + ylabel := "Counts" + + @series begin + seriestype := :stephist + label := "Energy Spectrum (σ: $(report.dep_σ))" + bins := 1000 + e_cal[1500u"keV" .< e_cal .< 1660u"keV"] + end + + @series begin + seriestype := :vline + label := "DEP Region" + fillcolor := :red + [report.edges.DEP_edge_left, report.edges.DEP_edge_right] + end + + @series begin + seriestype := :vline + label := "Sideband Edges" + fillcolor := :green + legend := :bottomright + [report.edges.sb1_edge, report.edges.sb2_edge] + end end end - - end # module LegendSpecFitsRecipesBaseExt diff --git a/src/lqcut.jl b/src/lqcut.jl index 52c16d2f..249dbc1d 100644 --- a/src/lqcut.jl +++ b/src/lqcut.jl @@ -159,11 +159,29 @@ function lq_cut( dep_µ::Unitful.Energy, dep_σ::Unitful.Energy, e_cal::Vector{<:Unitful.Energy}, lq_classifier::Vector{<:AbstractFloat}; cut_sigma::Float64=3.0, dep_sideband_sigma::Float64=4.5, cut_truncation_sigma::Float64=3.5, uncertainty::Bool=true ) - # define sidebands - lq_dep = lq_classifier[dep_µ - dep_sideband_sigma * dep_σ .< e_cal .< dep_µ + dep_sideband_sigma * dep_σ] - lq_sb1 = lq_classifier[dep_µ - 2 * dep_sideband_sigma * dep_σ .< e_cal .< dep_µ - dep_sideband_sigma * dep_σ] - lq_sb2 = lq_classifier[dep_µ + dep_sideband_sigma * dep_σ .< e_cal .< dep_µ + 2 * dep_sideband_sigma * dep_σ] - + # define sidebands; different for low and high energy resolution detectors to avoid sb reaching into 212-Bi FEP + DEP_edge_left = dep_µ - dep_sideband_sigma * dep_σ + DEP_edge_right = dep_µ + dep_sideband_sigma * dep_σ + + if dep_σ < 2.0u"keV" + sb1_edge = dep_µ - 2 * dep_sideband_sigma * dep_σ + sb2_edge = dep_µ + 2 * dep_sideband_sigma * dep_σ + + lq_dep = lq_classifier[DEP_edge_left .< e_cal .< DEP_edge_right] + lq_sb1 = lq_classifier[sb1_edge .< e_cal .< DEP_edge_left] + lq_sb2 = lq_classifier[DEP_edge_right .< e_cal .< sb2_edge] + else + sb1_edge = dep_µ - 2 * dep_sideband_sigma * dep_σ + sb2_edge = dep_µ - 3 * dep_sideband_sigma * dep_σ + + lq_dep = lq_classifier[DEP_edge_left .< e_cal .< DEP_edge_right] + lq_sb1 = lq_classifier[sb1_edge .< e_cal .< DEP_edge_left] + lq_sb2 = lq_classifier[sb2_edge .< e_cal .< sb1_edge] + end + + # save edges for crosschecks + edges_for_crosschecks = (;DEP_edge_left, DEP_edge_right, sb1_edge, sb2_edge) + # generate values for histogram edges combined = filter(isfinite,[lq_dep; lq_sb1; lq_sb2]) ideal_bin_width = get_friedman_diaconis_bin_width(combined) @@ -206,6 +224,8 @@ function lq_cut( fit_result = fit_result, temp_hists = temp_hists, fit_report = fit_report, + dep_σ = dep_σ, + edges = edges_for_crosschecks, ) return result, report From 3632ef8b7729ddc429e91e94771aea79bb2fd5d4 Mon Sep 17 00:00:00 2001 From: Andreas Gieb Date: Fri, 28 Feb 2025 16:51:19 +0100 Subject: [PATCH 2/2] minor tweaks to plotting recipes --- ext/LegendSpecFitsRecipesBaseExt.jl | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/ext/LegendSpecFitsRecipesBaseExt.jl b/ext/LegendSpecFitsRecipesBaseExt.jl index b2bee21a..19797451 100644 --- a/ext/LegendSpecFitsRecipesBaseExt.jl +++ b/ext/LegendSpecFitsRecipesBaseExt.jl @@ -1390,9 +1390,9 @@ end xlabel := "Drift Time" ylabel := "LQ (A.U.)" framestyle := :box - left_margin := -2Plots.mm - bottom_margin := -4Plots.mm - top_margin := -3Plots.mm + left_margin := (-2, :mm) + bottom_margin := (-4, :mm) + top_margin := (-3, :mm) color := :viridis formatter := :plain thickness_scaling := 1.6 @@ -1482,9 +1482,9 @@ end cut_value = Measurements.value.(report.cut) # Plot configuration for all types - left_margin := -2Plots.mm - bottom_margin := -4Plots.mm - top_margin := -3Plots.mm + left_margin := (-2, :mm) + bottom_margin := (-4, :mm) + top_margin := (-3, :mm) thickness_scaling := 1.6 size := (1200, 900) framestyle := :box @@ -1664,9 +1664,9 @@ end elseif plot_type == :energy_spectrum # Plot energy spectrum with DEP and sideband regions - left_margin := -2Plots.mm - bottom_margin := -4Plots.mm - top_margin := -3Plots.mm + left_margin := (-2, :mm) + bottom_margin := (-4, :mm) + top_margin := (-3, :mm) thickness_scaling := 1.6 size := (1200, 900) framestyle := :box @@ -1676,7 +1676,7 @@ end @series begin seriestype := :stephist - label := "Energy Spectrum (σ: $(report.dep_σ))" + label := "Energy Spectrum (σ: $(round(u"keV", report.dep_σ, sigdigits=3)))" bins := 1000 e_cal[1500u"keV" .< e_cal .< 1660u"keV"] end @@ -1692,7 +1692,7 @@ end seriestype := :vline label := "Sideband Edges" fillcolor := :green - legend := :bottomright + legend := :topleft [report.edges.sb1_edge, report.edges.sb2_edge] end end