Skip to content

Commit

Permalink
changed lq cut calculation to overloaded function
Browse files Browse the repository at this point in the history
  • Loading branch information
DaGeibl committed Jan 27, 2025
1 parent 38585b2 commit 537e584
Showing 1 changed file with 81 additions and 54 deletions.
135 changes: 81 additions & 54 deletions src/aoe_cut.jl
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,9 @@ Get the survival fraction of a peak after a AoE cut value `aoe_cut` for a given
- `result`: Dict of results for each peak
- `report`: Dict of reports for each peak
"""
function get_peaks_survival_fractions(aoe::Vector{<:Unitful.RealOrRealQuantity}, e::Vector{<:T}, peaks::Vector{<:T}, peak_names::Vector{Symbol}, windows::Vector{<:Tuple{T, T}}, aoe_cut::Unitful.RealOrRealQuantity,;
uncertainty::Bool=true, inverted_mode::Bool=false, aoe_lowcut_vec::BitVector=falses(length(aoe)), bin_width_window::T=2.0u"keV", sigma_high_sided::Unitful.RealOrRealQuantity=Inf*unit(first(aoe)), fit_funcs::Vector{Symbol}=fill(:gamma_def, length(peaks))) where T<:Unitful.Energy{<:Real}
#version for lq with cut_vector
function get_peaks_survival_fractions(aoe::Vector{<:Unitful.RealOrRealQuantity}, e::Vector{<:T}, peaks::Vector{<:T}, peak_names::Vector{Symbol}, windows::Vector{<:Tuple{T, T}}, aoe_cut::Unitful.RealOrRealQuantity, cut_vector::BitVector,;

Check warning on line 99 in src/aoe_cut.jl

View check run for this annotation

Codecov / codecov/patch

src/aoe_cut.jl#L99

Added line #L99 was not covered by tests
uncertainty::Bool=true, inverted_mode::Bool=false, bin_width_window::T=2.0u"keV", sigma_high_sided::Unitful.RealOrRealQuantity=Inf*unit(first(aoe)), fit_funcs::Vector{Symbol}=fill(:gamma_def, length(peaks))) where T<:Unitful.Energy{<:Real}
@assert length(peaks) == length(peak_names) == length(windows) "Length of peaks, peak_names and windows must be equal"
# create return and result vectors
v_result = Vector{NamedTuple}(undef, length(peak_names))
Expand All @@ -107,7 +108,7 @@ function get_peaks_survival_fractions(aoe::Vector{<:Unitful.RealOrRealQuantity},
# extract peak, name and window
peak, name, window, fit_func = peaks[i], peak_names[i], windows[i], fit_funcs[i]
# fit peak
result_peak, report_peak = get_peak_survival_fraction(aoe, e, peak, collect(window), aoe_cut; uncertainty=uncertainty, inverted_mode=inverted_mode, aoe_lowcut_vec=aoe_lowcut_vec, bin_width_window=bin_width_window, sigma_high_sided=sigma_high_sided, fit_func=fit_func)
result_peak, report_peak = get_peak_survival_fraction(aoe, e, peak, collect(window), aoe_cut, cut_vector; uncertainty=uncertainty, inverted_mode=inverted_mode, bin_width_window=bin_width_window, sigma_high_sided=sigma_high_sided, fit_func=fit_func)

Check warning on line 111 in src/aoe_cut.jl

View check run for this annotation

Codecov / codecov/patch

src/aoe_cut.jl#L111

Added line #L111 was not covered by tests
# save results
v_result[i] = result_peak
v_report[i] = report_peak
Expand All @@ -119,6 +120,13 @@ function get_peaks_survival_fractions(aoe::Vector{<:Unitful.RealOrRealQuantity},

return result, report
end
get_peaks_survival_fractions(aoe, e, peaks, peak_names, left_window_sizes::Vector{<:Unitful.Energy{<:Real}}, right_window_sizes::Vector{<:Unitful.Energy{<:Real}}, aoe_cut, cut_vector; kwargs...) = get_peaks_survival_fractions(aoe, e, peaks, peak_names, [(l,r) for (l,r) in zip(left_window_sizes, right_window_sizes)], aoe_cut, cut_vector; kwargs...)

Check warning on line 123 in src/aoe_cut.jl

View check run for this annotation

Codecov / codecov/patch

src/aoe_cut.jl#L123

Added line #L123 was not covered by tests

function get_peaks_survival_fractions(aoe::Vector{<:Unitful.RealOrRealQuantity}, e::Vector{<:T}, peaks::Vector{<:T}, peak_names::Vector{Symbol}, windows::Vector{<:Tuple{T, T}}, aoe_cut::Unitful.RealOrRealQuantity,;

Check warning on line 125 in src/aoe_cut.jl

View check run for this annotation

Codecov / codecov/patch

src/aoe_cut.jl#L125

Added line #L125 was not covered by tests
uncertainty::Bool=true, inverted_mode::Bool=false, bin_width_window::T=2.0u"keV", sigma_high_sided::Unitful.RealOrRealQuantity=Inf*unit(first(aoe)), fit_funcs::Vector{Symbol}=fill(:gamma_def, length(peaks))) where T<:Unitful.Energy{<:Real}

get_peaks_survival_fractions(aoe, e, peaks, peak_names, windows, aoe_cut, trues(length(aoe)); uncertainty=uncertainty, inverted_mode=inverted_mode, bin_width_window=bin_width_window, sigma_high_sided=sigma_high_sided, fit_funcs=fit_funcs)

Check warning on line 128 in src/aoe_cut.jl

View check run for this annotation

Codecov / codecov/patch

src/aoe_cut.jl#L128

Added line #L128 was not covered by tests
end
get_peaks_survival_fractions(aoe, e, peaks, peak_names, left_window_sizes::Vector{<:Unitful.Energy{<:Real}}, right_window_sizes::Vector{<:Unitful.Energy{<:Real}}, aoe_cut; kwargs...) = get_peaks_survival_fractions(aoe, e, peaks, peak_names, [(l,r) for (l,r) in zip(left_window_sizes, right_window_sizes)], aoe_cut; kwargs...)

Base.@deprecate get_peaks_surrival_fractions(args...; kwargs...) get_peaks_survival_fractions(args...; kwargs...)
Expand All @@ -137,52 +145,62 @@ Get the survival fraction of a peak after a AoE cut value `aoe_cut` for a given
- `sf`: Survival fraction
- `err`: Uncertainties
"""
function get_peak_survival_fraction(aoe::Vector{<:Unitful.RealOrRealQuantity}, e::Vector{<:T}, peak::T, window::Vector{T}, aoe_cut::Unitful.RealOrRealQuantity,;
uncertainty::Bool=true, inverted_mode::Bool=false, aoe_lowcut_vec::BitVector=falses(length(aoe)), bin_width_window::T=2.0u"keV", sigma_high_sided::Unitful.RealOrRealQuantity=Inf*unit(first(aoe)), fit_func::Symbol=:gamma_def) where T<:Unitful.Energy{<:Real}
# estimate bin width
bin_width = get_friedman_diaconis_bin_width(e[e .> peak - bin_width_window .&& e .< peak + bin_width_window])
# get energy before cut and create histogram
peakhist = fit(Histogram, ustrip.(e), ustrip(peak-first(window):bin_width:peak+last(window)))
# estimate peak stats
peakstats = estimate_single_peak_stats(peakhist)
# fit peak and return number of signal counts
result_before, report_before = fit_single_peak_th228(peakhist, peakstats,; uncertainty=uncertainty, fit_func=fit_func)
function get_peak_survival_fraction(aoe::Vector{<:Unitful.RealOrRealQuantity}, e::Vector{<:T}, peak::T, window::Vector{T}, aoe_cut::Unitful.RealOrRealQuantity;

Check warning on line 148 in src/aoe_cut.jl

View check run for this annotation

Codecov / codecov/patch

src/aoe_cut.jl#L148

Added line #L148 was not covered by tests
uncertainty::Bool=true, inverted_mode::Bool=false, bin_width_window::T=2.0u"keV", sigma_high_sided::Unitful.RealOrRealQuantity=Inf*unit(first(aoe)), fit_func::Symbol=:gamma_def) where T<:Unitful.Energy{<:Real}

# get energy after cuts
e_survived, e_cut = if !inverted_mode
#normal aoe version
e[aoe_cut .< aoe .< sigma_high_sided], e[aoe .<= aoe_cut .|| aoe .>= sigma_high_sided]
else
#lq version
e[aoe .< aoe_cut .&& .!aoe_lowcut_vec], e[aoe_cut .<= aoe .&& .!aoe_lowcut_vec]
end

# estimate bin width
bin_width = get_friedman_diaconis_bin_width(e[e .> peak - bin_width_window .&& e .< peak + bin_width_window])
# get energy after cut and create histogram
survived = fit(Histogram, ustrip(e_survived), ustrip(peak-first(window):bin_width:peak+last(window)))
cut = fit(Histogram, ustrip(e_cut), ustrip(peak-first(window):bin_width:peak+last(window)))
# fit peak and return number of signal counts
result_after, report_after = fit_subpeaks_th228(survived, cut, result_before; uncertainty=uncertainty, fit_func=fit_func)
# calculate survival fraction
sf = result_after.sf * 100u"percent"
result = (
peak = peak,
fit_func=fit_func,
n_before = result_before.n,
n_after = result_before.n * result_after.sf,
sf = sf,
gof = (after = result_after.gof, before = result_before.gof),
)
report = (
peak = result.peak,
n_before = result.n_before,
n_after = result.n_before * result_after.sf,
sf = result.sf,
before = report_before,
after = report_after,
)
return result, report
get_peak_survival_fractions(aoe, e, peak, window, aoe_cut, trues(length(aoe)); uncertainty=uncertainty, inverted_mode=inverted_mode, bin_width_window=bin_width_window, sigma_high_sided=sigma_high_sided, fit_func=fit_func)

Check warning on line 151 in src/aoe_cut.jl

View check run for this annotation

Codecov / codecov/patch

src/aoe_cut.jl#L151

Added line #L151 was not covered by tests
end


"""
#Overloaded method with an additional cut_vector parameter.
"""
function get_peak_survival_fraction(aoe::Vector{<:Unitful.RealOrRealQuantity}, e::Vector{<:T}, peak::T, window::Vector{T}, aoe_cut::Unitful.RealOrRealQuantity, cut_vector::BitVector;

Check warning on line 158 in src/aoe_cut.jl

View check run for this annotation

Codecov / codecov/patch

src/aoe_cut.jl#L158

Added line #L158 was not covered by tests
uncertainty::Bool=true, inverted_mode::Bool=false, bin_width_window::T=2.0u"keV", sigma_high_sided::Unitful.RealOrRealQuantity=Inf*unit(first(aoe)), fit_func::Symbol=:gamma_def) where T<:Unitful.Energy{<:Real}
# estimate bin width
bin_width = get_friedman_diaconis_bin_width(e[e .> peak - bin_width_window .&& e .< peak + bin_width_window])

Check warning on line 161 in src/aoe_cut.jl

View check run for this annotation

Codecov / codecov/patch

src/aoe_cut.jl#L161

Added line #L161 was not covered by tests
# get energy before cut and create histogram
peakhist = fit(Histogram, ustrip.(e), ustrip(peak-first(window):bin_width:peak+last(window)))

Check warning on line 163 in src/aoe_cut.jl

View check run for this annotation

Codecov / codecov/patch

src/aoe_cut.jl#L163

Added line #L163 was not covered by tests
# estimate peak stats
peakstats = estimate_single_peak_stats(peakhist)

Check warning on line 165 in src/aoe_cut.jl

View check run for this annotation

Codecov / codecov/patch

src/aoe_cut.jl#L165

Added line #L165 was not covered by tests
# fit peak and return number of signal counts
result_before, report_before = fit_single_peak_th228(peakhist, peakstats; uncertainty=uncertainty, fit_func=fit_func)

Check warning on line 167 in src/aoe_cut.jl

View check run for this annotation

Codecov / codecov/patch

src/aoe_cut.jl#L167

Added line #L167 was not covered by tests

# get energy after cuts
e_survived, e_cut = if !inverted_mode

Check warning on line 170 in src/aoe_cut.jl

View check run for this annotation

Codecov / codecov/patch

src/aoe_cut.jl#L170

Added line #L170 was not covered by tests
#normal aoe version
e[aoe_cut .< aoe .< sigma_high_sided], e[aoe .<= aoe_cut .|| aoe .>= sigma_high_sided]

Check warning on line 172 in src/aoe_cut.jl

View check run for this annotation

Codecov / codecov/patch

src/aoe_cut.jl#L172

Added line #L172 was not covered by tests
else
#lq version
e[aoe .< aoe_cut .&& cut_vector], e[aoe_cut .<= aoe .&& cut_vector]

Check warning on line 175 in src/aoe_cut.jl

View check run for this annotation

Codecov / codecov/patch

src/aoe_cut.jl#L175

Added line #L175 was not covered by tests
end

# estimate bin width
bin_width = get_friedman_diaconis_bin_width(e[e .> peak - bin_width_window .&& e .< peak + bin_width_window])

Check warning on line 179 in src/aoe_cut.jl

View check run for this annotation

Codecov / codecov/patch

src/aoe_cut.jl#L179

Added line #L179 was not covered by tests
# get energy after cut and create histogram
survived = fit(Histogram, ustrip(e_survived), ustrip(peak-first(window):bin_width:peak+last(window)))
cut = fit(Histogram, ustrip(e_cut), ustrip(peak-first(window):bin_width:peak+last(window)))

Check warning on line 182 in src/aoe_cut.jl

View check run for this annotation

Codecov / codecov/patch

src/aoe_cut.jl#L181-L182

Added lines #L181 - L182 were not covered by tests
# fit peak and return number of signal counts
result_after, report_after = fit_subpeaks_th228(survived, cut, result_before; uncertainty=uncertainty, fit_func=fit_func)

Check warning on line 184 in src/aoe_cut.jl

View check run for this annotation

Codecov / codecov/patch

src/aoe_cut.jl#L184

Added line #L184 was not covered by tests
# calculate survival fraction
sf = result_after.sf * 100u"percent"
result = (

Check warning on line 187 in src/aoe_cut.jl

View check run for this annotation

Codecov / codecov/patch

src/aoe_cut.jl#L186-L187

Added lines #L186 - L187 were not covered by tests
peak = peak,
fit_func=fit_func,
n_before = result_before.n,
n_after = result_before.n * result_after.sf,
sf = sf,
gof = (after = result_after.gof, before = result_before.gof),
)
report = (

Check warning on line 195 in src/aoe_cut.jl

View check run for this annotation

Codecov / codecov/patch

src/aoe_cut.jl#L195

Added line #L195 was not covered by tests
peak = result.peak,
n_before = result.n_before,
n_after = result.n_before * result_after.sf,
sf = result.sf,
before = report_before,
after = report_after,
)
return result, report

Check warning on line 203 in src/aoe_cut.jl

View check run for this annotation

Codecov / codecov/patch

src/aoe_cut.jl#L203

Added line #L203 was not covered by tests
end

Base.@deprecate get_peak_surrival_fraction(args...; kwargs...) get_peak_survival_fraction(args...; kwargs...)
Expand All @@ -200,24 +218,33 @@ Get the survival fraction of a continuum after a AoE cut value `aoe_cut` for a g
- `n_after`: Number of counts after the cut
- `sf`: Survival fraction
"""
function get_continuum_survival_fraction(aoe::Vector{<:Unitful.RealOrRealQuantity}, e::Vector{<:T}, center::T, window::T, aoe_cut::Unitful.RealOrRealQuantity,; inverted_mode::Bool=false, aoe_lowcut_vec::BitVector=falses(length(aoe)), sigma_high_sided::Unitful.RealOrRealQuantity=Inf*unit(first(aoe))) where T<:Unitful.Energy{<:Real}
function get_continuum_survival_fraction(aoe::Vector{<:Unitful.RealOrRealQuantity}, e::Vector{<:T}, center::T, window::T, aoe_cut::Unitful.RealOrRealQuantity;

Check warning on line 221 in src/aoe_cut.jl

View check run for this annotation

Codecov / codecov/patch

src/aoe_cut.jl#L221

Added line #L221 was not covered by tests
inverted_mode::Bool=false, sigma_high_sided::Unitful.RealOrRealQuantity=Inf*unit(first(aoe))) where T<:Unitful.Energy{<:Real}

get_continuum_survival_fraction(aoe, e, center, window, aoe_cut, trues(length(aoe)); inverted_mode=inverted_mode, sigma_high_sided=sigma_high_sided)

Check warning on line 224 in src/aoe_cut.jl

View check run for this annotation

Codecov / codecov/patch

src/aoe_cut.jl#L224

Added line #L224 was not covered by tests
end

function get_continuum_survival_fraction(aoe::Vector{<:Unitful.RealOrRealQuantity}, e::Vector{<:T}, center::T, window::T, aoe_cut::Unitful.RealOrRealQuantity, cut_vector::BitVector,; inverted_mode::Bool=false, sigma_high_sided::Unitful.RealOrRealQuantity=Inf*unit(first(aoe))) where T<:Unitful.Energy{<:Real}

Check warning on line 227 in src/aoe_cut.jl

View check run for this annotation

Codecov / codecov/patch

src/aoe_cut.jl#L227

Added line #L227 was not covered by tests
# scale unit
e_unit = u"keV"
# get energy around center
e_wdw = e[center - window .< e .< center + window]
cut_vector = cut_vector[center - window .< e .< center + window]

Check warning on line 231 in src/aoe_cut.jl

View check run for this annotation

Codecov / codecov/patch

src/aoe_cut.jl#L231

Added line #L231 was not covered by tests
aoe = aoe[center - window .< e .< center + window]
e = e[center - window .< e .< center + window]

# get bin width
bin_width = get_friedman_diaconis_bin_width(e_wdw)
bin_width = get_friedman_diaconis_bin_width(e)
# get number of events in window before cut
n_before = length(e_wdw)
n_before = length(e)
# get energy after cuts
e_survived, e_cut = if !inverted_mode
#normal aoe version
e[aoe_cut .< aoe .< sigma_high_sided], e[aoe .<= aoe_cut .|| aoe .>= sigma_high_sided]
else
#lq version
e[aoe .< aoe_cut .&& .!aoe_lowcut_vec], e[aoe_cut .<= aoe .&& .!aoe_lowcut_vec]
e[aoe .< aoe_cut .&& .!cut_vector], e[aoe_cut .<= aoe .&& .!cut_vector]

Check warning on line 245 in src/aoe_cut.jl

View check run for this annotation

Codecov / codecov/patch

src/aoe_cut.jl#L245

Added line #L245 was not covered by tests
end
n_after = length(e_survived[center - window .< e_survived .< center + window])
n_after = length(e_survived)

# calculate survival fraction
sf = n_after / n_before
Expand Down

0 comments on commit 537e584

Please sign in to comment.