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

LQ calibration functions and PSD propfuncs #93

Merged
merged 6 commits into from
Mar 2, 2025
Merged
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
108 changes: 98 additions & 10 deletions src/calibration_functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
end

function _get_e_cal_propsfunc_str(data::LegendData, sel::AnyValiditySelection, detector::DetectorId, e_filter::Symbol; kwargs...)
ecal_props::String = get(get(get(_get_ecal_props(data, sel, detector; kwargs...), e_filter, PropDict()), :cal, PropDict()), :func, "e_max * NaN*keV")
ecal_props::String = get(get(get(_get_ecal_props(data, sel, detector; kwargs...), e_filter, PropDict()), :cal, PropDict()), :func, "$(e_filter) * NaN*keV")

Check warning on line 22 in src/calibration_functions.jl

View check run for this annotation

Codecov / codecov/patch

src/calibration_functions.jl#L22

Added line #L22 was not covered by tests
return ecal_props
end

Expand Down Expand Up @@ -60,6 +60,7 @@


### HPGe PSD calibration functions
# A/E
const _cached_get_aoecal_props = LRU{Tuple{UInt, AnyValiditySelection, Symbol, Symbol}, Union{PropDict,PropDicts.MissingProperty}}(maxsize = 10^3)

function _get_aoecal_props(data::LegendData, sel::AnyValiditySelection; pars_type::Symbol=:ppars, pars_cat::Symbol=:aoe)
Expand All @@ -74,10 +75,29 @@
end

function _get_aoe_cal_propfunc_str(data::LegendData, sel::AnyValiditySelection, detector::DetectorId, aoe_type::Symbol; pars_type::Symbol=:ppars, pars_cat::Symbol=:aoe)
aoecal_props::String = get(_get_aoecal_props(data, sel, detector; pars_type=pars_type, pars_cat=pars_cat)[aoe_type], :func, "a_raw * NaN")
aoecal_props::String = get(_get_aoecal_props(data, sel, detector; pars_type=pars_type, pars_cat=pars_cat)[aoe_type], :func, "$(aoe_type) * NaN")

Check warning on line 78 in src/calibration_functions.jl

View check run for this annotation

Codecov / codecov/patch

src/calibration_functions.jl#L78

Added line #L78 was not covered by tests
return aoecal_props
end

# LQ
const _cached_get_lqcal_props = LRU{Tuple{UInt, AnyValiditySelection, Symbol, Symbol}, Union{PropDict,PropDicts.MissingProperty}}(maxsize = 10^3)

function _get_lqcal_props(data::LegendData, sel::AnyValiditySelection; pars_type::Symbol=:ppars, pars_cat::Symbol=:lq)
key = (objectid(data), sel, pars_type, pars_cat)
get!(_cached_get_lqcal_props, key) do
_get_cal_values(dataprod_parameters(data)[pars_type][pars_cat], sel)

Check warning on line 88 in src/calibration_functions.jl

View check run for this annotation

Codecov / codecov/patch

src/calibration_functions.jl#L85-L88

Added lines #L85 - L88 were not covered by tests
end
end

function _get_lqcal_props(data::LegendData, sel::AnyValiditySelection, detector::DetectorId; pars_type::Symbol=:ppars, pars_cat::Symbol=:lq)
get(_get_lqcal_props(data, sel; pars_type=pars_type, pars_cat=pars_cat), Symbol(detector), PropDict())

Check warning on line 93 in src/calibration_functions.jl

View check run for this annotation

Codecov / codecov/patch

src/calibration_functions.jl#L92-L93

Added lines #L92 - L93 were not covered by tests
end

function _get_lq_cal_propfunc_str(data::LegendData, sel::AnyValiditySelection, detector::DetectorId, lq_type::Symbol; pars_type::Symbol=:ppars, pars_cat::Symbol=:lq)
lqcal_props::String = get(_get_lqcal_props(data, sel, detector; pars_type=pars_type, pars_cat=pars_cat)[lq_type], :func, "$(lq_type) * NaN")
return lqcal_props

Check warning on line 98 in src/calibration_functions.jl

View check run for this annotation

Codecov / codecov/patch

src/calibration_functions.jl#L96-L98

Added lines #L96 - L98 were not covered by tests
end

const _cached_dataprod_psd = LRU{Tuple{UInt, AnyValiditySelection}, Union{PropDict,PropDicts.MissingProperty}}(maxsize = 10^3)

function _dataprod_psd(data::LegendData, sel::AnyValiditySelection)
Expand All @@ -93,6 +113,12 @@
merge(dataprod_aoe_config[ifelse(pars_type == :ppars, :p_default, :default)], get(ifelse(pars_type == :ppars, get(dataprod_aoe_config, :p, PropDict()), dataprod_aoe_config), detector, PropDict()))
end

function _dataprod_lq(data::LegendData, sel::AnyValiditySelection, detector::DetectorId; pars_type::Symbol=:ppars)
dataprod_lq_config = _dataprod_psd(data, sel).lq
@assert pars_type in (:ppars, :rpars) "pars_type must be either :ppars or :rpars"
merge(dataprod_lq_config[ifelse(pars_type == :ppars, :p_default, :default)], get(ifelse(pars_type == :ppars, get(dataprod_lq_config, :p, PropDict()), dataprod_lq_config), detector, PropDict()))

Check warning on line 119 in src/calibration_functions.jl

View check run for this annotation

Codecov / codecov/patch

src/calibration_functions.jl#L116-L119

Added lines #L116 - L119 were not covered by tests
end

"""
get_ged_psd_propfunc(data::LegendData, sel::AnyValiditySelection, detector::DetectorId; pars_type::Symbol=:ppars, pars_cat::Symbol=:aoe)

Expand All @@ -102,12 +128,19 @@
Note: Caches configuration/calibration data internally, use a fresh `data`
object if on-disk configuration/calibration data may have changed.
"""
function get_ged_psd_propfunc(data::LegendData, sel::AnyValiditySelection, detector::DetectorId; pars_type::Symbol=:ppars, pars_cat::Symbol=:aoe)
let aoe_types = collect(keys(_dataprod_aoe(data, sel, detector; pars_type=pars_type).aoe_funcs)), aoe_classifier = Symbol.(string.(keys(_dataprod_aoe(data, sel, detector; pars_type=pars_type).aoe_funcs)) .* "_classifier")
function get_ged_psd_propfunc(data::LegendData, sel::AnyValiditySelection, detector::DetectorId; aoe_pars_type::Symbol=:ppars, aoe_pars_cat::Symbol=:aoe, lq_pars_type::Symbol=:ppars, lq_pars_cat::Symbol=:lq)
let aoe_types = Symbol.(_dataprod_aoe(data, sel, detector; pars_type=aoe_pars_type).aoe_types), aoe_classifier = Symbol.(_dataprod_aoe(data, sel, detector; pars_type=aoe_pars_type).aoe_types .* "_classifier"), lq_types = Symbol.(_dataprod_lq(data, sel, detector; pars_type=lq_pars_type).lq_types), lq_classifier = Symbol.(_dataprod_lq(data, sel, detector; pars_type=lq_pars_type).lq_types .* "_classifier")

Check warning on line 132 in src/calibration_functions.jl

View check run for this annotation

Codecov / codecov/patch

src/calibration_functions.jl#L131-L132

Added lines #L131 - L132 were not covered by tests

ljl_propfunc(Dict{Symbol, String}(
aoe_classifier .=> _get_aoe_cal_propfunc_str.(Ref(data), Ref(sel), Ref(detector), aoe_types; pars_type=pars_type, pars_cat=pars_cat)
))
ljl_propfunc(

Check warning on line 134 in src/calibration_functions.jl

View check run for this annotation

Codecov / codecov/patch

src/calibration_functions.jl#L134

Added line #L134 was not covered by tests
merge(
Dict{Symbol, String}(
aoe_classifier .=> _get_aoe_cal_propfunc_str.(Ref(data), Ref(sel), Ref(detector), aoe_types; pars_type=aoe_pars_type, pars_cat=aoe_pars_cat)
),
Dict{Symbol, String}(
lq_classifier .=> _get_lq_cal_propfunc_str.(Ref(data), Ref(sel), Ref(detector), lq_types; pars_type=lq_pars_type, pars_cat=lq_pars_cat)
)
)
)
end
end
export get_ged_psd_propfunc
Expand Down Expand Up @@ -213,11 +246,11 @@


function _get_ged_aoe_lowcut_propfunc_str(data::LegendData, sel::AnyValiditySelection, detector::DetectorId, aoe_classifier::Symbol; pars_type::Symbol=:ppars, pars_cat::Symbol=:aoe)
"$(aoe_classifier) > $(leftendpoint(dataprod_pars_aoe_window(data, sel, detector, aoe_classifier; pars_type=pars_type, pars_cat=pars_cat)))"
"$(aoe_classifier) < $(leftendpoint(dataprod_pars_aoe_window(data, sel, detector, aoe_classifier; pars_type=pars_type, pars_cat=pars_cat)))"

Check warning on line 249 in src/calibration_functions.jl

View check run for this annotation

Codecov / codecov/patch

src/calibration_functions.jl#L249

Added line #L249 was not covered by tests
end

function _get_ged_aoe_dscut_propfunc_str(data::LegendData, sel::AnyValiditySelection, detector::DetectorId, aoe_classifier::Symbol; pars_type::Symbol=:ppars, pars_cat::Symbol=:aoe)
"$(aoe_classifier) > $(leftendpoint(dataprod_pars_aoe_window(data, sel, detector, aoe_classifier; pars_type=pars_type, pars_cat=pars_cat))) && $(aoe_classifier) < $(rightendpoint(dataprod_pars_aoe_window(data, sel, detector, aoe_classifier; pars_type=pars_type, pars_cat=pars_cat)))"
"$(aoe_classifier) < $(leftendpoint(dataprod_pars_aoe_window(data, sel, detector, aoe_classifier; pars_type=pars_type, pars_cat=pars_cat))) || $(aoe_classifier) > $(rightendpoint(dataprod_pars_aoe_window(data, sel, detector, aoe_classifier; pars_type=pars_type, pars_cat=pars_cat)))"

Check warning on line 253 in src/calibration_functions.jl

View check run for this annotation

Codecov / codecov/patch

src/calibration_functions.jl#L253

Added line #L253 was not covered by tests
end

"""
Expand All @@ -244,6 +277,61 @@
export get_ged_aoe_cut_propfunc


"""
LegendDataManagement.dataprod_pars_lq_window(data::LegendData, sel::AnyValiditySelection, detector::DetectorId, lq_classifier::Symbol; pars_type::Symbol=:ppars, pars_cat::Symbol=:lq)

Get the LQ cut window for the given data, validity selection and detector.
"""
function dataprod_pars_lq_window(data::LegendData, sel::AnyValiditySelection, detector::DetectorId, lq_classifier::Symbol; pars_type::Symbol=:ppars, pars_cat::Symbol=:lq)
lqcut_lo::Float64 = get(_get_lqcal_props(data, sel, detector; pars_type=pars_type, pars_cat=pars_cat)[lq_classifier], :lowcut, -Inf)
lqcut_hi::Float64 = get(_get_lqcal_props(data, sel, detector; pars_type=pars_type, pars_cat=pars_cat)[lq_classifier], :cut, Inf)
ClosedInterval(lqcut_lo, lqcut_hi)

Check warning on line 288 in src/calibration_functions.jl

View check run for this annotation

Codecov / codecov/patch

src/calibration_functions.jl#L285-L288

Added lines #L285 - L288 were not covered by tests
end

function _get_ged_lq_highcut_propfunc_str(data::LegendData, sel::AnyValiditySelection, detector::DetectorId, lq_classifier::Symbol; pars_type::Symbol=:ppars, pars_cat::Symbol=:lq)
"$(lq_classifier) > $(rightendpoint(dataprod_pars_lq_window(data, sel, detector, lq_classifier; pars_type=pars_type, pars_cat=pars_cat)))"

Check warning on line 292 in src/calibration_functions.jl

View check run for this annotation

Codecov / codecov/patch

src/calibration_functions.jl#L291-L292

Added lines #L291 - L292 were not covered by tests
end

function _get_ged_lq_dscut_propfunc_str(data::LegendData, sel::AnyValiditySelection, detector::DetectorId, lq_classifier::Symbol; pars_type::Symbol=:ppars, pars_cat::Symbol=:lq)
"$(lq_classifier) < $(leftendpoint(dataprod_pars_lq_window(data, sel, detector, lq_classifier; pars_type=pars_type, pars_cat=pars_cat))) || $(lq_classifier) > $(rightendpoint(dataprod_pars_lq_window(data, sel, detector, lq_classifier; pars_type=pars_type, pars_cat=pars_cat)))"

Check warning on line 296 in src/calibration_functions.jl

View check run for this annotation

Codecov / codecov/patch

src/calibration_functions.jl#L295-L296

Added lines #L295 - L296 were not covered by tests
end

"""
LegendDataManagement.get_ged_lq_cut_propfunc(data::LegendData, sel::AnyValiditySelection, detector::DetectorId; pars_type::Symbol=:ppars, pars_cat::Symbol=:lq)

Get the LQ cut propfuncs for the given data, validity selection and detector.
"""
function get_ged_lq_cut_propfunc(data::LegendData, sel::AnyValiditySelection, detector::DetectorId; pars_type::Symbol=:ppars, pars_cat::Symbol=:lq)
let lq_classifiers = Symbol.(_dataprod_lq(data, sel, detector; pars_type=pars_type).lq_classifiers), lq_high_cut = Symbol.(_dataprod_lq(data, sel, detector; pars_type=pars_type).lq_classifiers .* "_high_cut"),

Check warning on line 305 in src/calibration_functions.jl

View check run for this annotation

Codecov / codecov/patch

src/calibration_functions.jl#L304-L305

Added lines #L304 - L305 were not covered by tests
lq_ds_cut = Symbol.(_dataprod_lq(data, sel, detector; pars_type=pars_type).lq_classifiers .* "_ds_cut")

ljl_propfunc(

Check warning on line 308 in src/calibration_functions.jl

View check run for this annotation

Codecov / codecov/patch

src/calibration_functions.jl#L308

Added line #L308 was not covered by tests
merge(
Dict{Symbol, String}(
lq_high_cut .=> _get_ged_lq_highcut_propfunc_str.(Ref(data), Ref(sel), Ref(detector), lq_classifiers; pars_type=pars_type, pars_cat=pars_cat)
),
Dict{Symbol, String}(
lq_ds_cut .=> _get_ged_lq_dscut_propfunc_str.(Ref(data), Ref(sel), Ref(detector), lq_classifiers; pars_type=pars_type, pars_cat=pars_cat)
)
)
)
end
end
export get_ged_lq_cut_propfunc


"""
get_ged_psd_classifier_propfunc(data::LegendData, sel::AnyValiditySelection)

Get the PSD cut propfuncs for the given data and validity selection.
"""
function get_ged_psd_classifier_propfunc(data::LegendData, sel::AnyValiditySelection, detector::DetectorId; pars_type::Symbol=:ppars)
dataprod_psd_config = _dataprod_psd(data, sel).psd
@assert pars_type in (:ppars, :rpars) "pars_type must be either :ppars or :rpars"
ljl_propfunc(merge(dataprod_psd_config[ifelse(pars_type == :ppars, :p_default, :default)], get(ifelse(pars_type == :ppars, get(dataprod_psd_config, :p, PropDict()), dataprod_psd_config), detector, PropDict())).psd_classifier)

Check warning on line 331 in src/calibration_functions.jl

View check run for this annotation

Codecov / codecov/patch

src/calibration_functions.jl#L328-L331

Added lines #L328 - L331 were not covered by tests
end
export get_ged_psd_classifier_propfunc


### SiPM LAr cut functions

Expand Down Expand Up @@ -276,7 +364,7 @@
end

function _get_larcal_propfunc_str(data::LegendData, sel::AnyValiditySelection, detector::DetectorId, e_filter::Symbol; kwargs...)
ecal_props::String = get(get(get(_get_larcal_props(data, sel, detector; kwargs...), e_filter, PropDict()), :cal, PropDict()), :func, "trig_max .* (NaN*e)")
ecal_props::String = get(get(get(_get_larcal_props(data, sel, detector; kwargs...), e_filter, PropDict()), :cal, PropDict()), :func, "$(e_filter) .* (NaN*e)")

Check warning on line 367 in src/calibration_functions.jl

View check run for this annotation

Codecov / codecov/patch

src/calibration_functions.jl#L367

Added line #L367 was not covered by tests
return ecal_props
end

Expand Down
Loading