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

Bug Fix SiPM Simple Calibration + Optimization #112

Merged
merged 13 commits into from
Feb 3, 2025
8 changes: 4 additions & 4 deletions src/sipm_simple_calibration.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,59 +33,59 @@
)

# Initial peak search
cuts_1pe = cut_single_peak(pe_uncal, initial_min_amp, initial_max_amp, relative_cut=relative_cut_noise_cut)

Check warning on line 36 in src/sipm_simple_calibration.jl

View check run for this annotation

Codecov / codecov/patch

src/sipm_simple_calibration.jl#L36

Added line #L36 was not covered by tests

bin_width_cut_min = cuts_1pe.max+n_fwhm_noise_cut*(cuts_1pe.high - cuts_1pe.max)
bin_width_cut = get_friedman_diaconis_bin_width(filter(in(bin_width_cut_min..quantile(pe_uncal, initial_max_bin_width_quantile)), pe_uncal))
peakpos = []
for bin_width_scale in 10 .^ range(0, stop=-3, length=50)
for bin_width_scale in exp10.(range(0, stop=-3, length=50)) # 1e-3 to 1
@debug "Using bin width: $(bin_width_cut)"

Check warning on line 42 in src/sipm_simple_calibration.jl

View check run for this annotation

Codecov / codecov/patch

src/sipm_simple_calibration.jl#L38-L42

Added lines #L38 - L42 were not covered by tests

bin_width_cut = bin_width_cut * bin_width_scale
h_uncal_cut = fit(Histogram, pe_uncal, bin_width_cut_min:bin_width_cut:initial_max_amp)
bin_width_cut_scaled = bin_width_cut * bin_width_scale
h_uncal_cut = fit(Histogram, pe_uncal, bin_width_cut_min:bin_width_cut_scaled:initial_max_amp)
if peakfinder_σ <= 0.0
peakfinder_σ = round(Int, 2*(cuts_1pe.high - cuts_1pe.max) / bin_width_cut / 2.355)
peakfinder_σ = round(Int, 2*(cuts_1pe.high - cuts_1pe.max) / bin_width_cut_scaled / 2.355)

Check warning on line 47 in src/sipm_simple_calibration.jl

View check run for this annotation

Codecov / codecov/patch

src/sipm_simple_calibration.jl#L44-L47

Added lines #L44 - L47 were not covered by tests
end
@debug "Peakfinder σ: $(peakfinder_σ)"
try
c, h_deconv, peakpos, threshold = RadiationSpectra.determine_calibration_constant_through_peak_ratios(h_uncal_cut, collect(range(min_pe_peak, max_pe_peak, step=1)),

Check warning on line 51 in src/sipm_simple_calibration.jl

View check run for this annotation

Codecov / codecov/patch

src/sipm_simple_calibration.jl#L49-L51

Added lines #L49 - L51 were not covered by tests
min_n_peaks = 2, max_n_peaks = max_pe_peak, threshold=peakfinder_threshold, rtol=peakfinder_rtol, α=peakfinder_α, σ=peakfinder_σ)
catch e
@warn "Failed to find peaks with bin width scale $(bin_width_scale): $(e)"
continue

Check warning on line 55 in src/sipm_simple_calibration.jl

View check run for this annotation

Codecov / codecov/patch

src/sipm_simple_calibration.jl#L54-L55

Added lines #L54 - L55 were not covered by tests
else
@debug "Found peaks with bin width scale $(bin_width_scale)"
if !isempty(peakpos)
break

Check warning on line 59 in src/sipm_simple_calibration.jl

View check run for this annotation

Codecov / codecov/patch

src/sipm_simple_calibration.jl#L57-L59

Added lines #L57 - L59 were not covered by tests
end
end
end

Check warning on line 62 in src/sipm_simple_calibration.jl

View check run for this annotation

Codecov / codecov/patch

src/sipm_simple_calibration.jl#L62

Added line #L62 was not covered by tests

if isempty(peakpos) || length(peakpos) < 2
throw(ErrorException("Failed to find peaks"))

Check warning on line 65 in src/sipm_simple_calibration.jl

View check run for this annotation

Codecov / codecov/patch

src/sipm_simple_calibration.jl#L64-L65

Added lines #L64 - L65 were not covered by tests
end

# simple calibration
sort!(peakpos)
@debug "Found $(min_pe_peak) PE Peak positions: $(peakpos[1])"
@debug "Found $(min_pe_peak+1) PE Peak positions: $(peakpos[2])"

Check warning on line 71 in src/sipm_simple_calibration.jl

View check run for this annotation

Codecov / codecov/patch

src/sipm_simple_calibration.jl#L70-L71

Added lines #L70 - L71 were not covered by tests
gain = peakpos[2] - peakpos[1]
@debug "Calculated gain: $(round(gain, digits=2))"

Check warning on line 73 in src/sipm_simple_calibration.jl

View check run for this annotation

Codecov / codecov/patch

src/sipm_simple_calibration.jl#L73

Added line #L73 was not covered by tests
c = 1/gain
offset = - (peakpos[1] * c - min_pe_peak)
@debug "Calculated offset: $(round(offset, digits=2))"

Check warning on line 76 in src/sipm_simple_calibration.jl

View check run for this annotation

Codecov / codecov/patch

src/sipm_simple_calibration.jl#L75-L76

Added lines #L75 - L76 were not covered by tests

f_simple_calib = x -> x .* c .+ offset
f_simple_uncal = x -> (x .- offset) ./ c

pe_simple_cal = f_simple_calib.(pe_uncal)
peakpos_cal = f_simple_calib.(peakpos)

Check warning on line 82 in src/sipm_simple_calibration.jl

View check run for this annotation

Codecov / codecov/patch

src/sipm_simple_calibration.jl#L81-L82

Added lines #L81 - L82 were not covered by tests

bin_width_cal = get_friedman_diaconis_bin_width(filter(in(0.5..min_pe_peak), pe_simple_cal))
bin_width_uncal = f_simple_uncal(bin_width_cal) - f_simple_uncal(0.0)

Check warning on line 85 in src/sipm_simple_calibration.jl

View check run for this annotation

Codecov / codecov/patch

src/sipm_simple_calibration.jl#L84-L85

Added lines #L84 - L85 were not covered by tests

h_calsimple = fit(Histogram, pe_simple_cal, 0.0:bin_width_cal:max_pe_peak + 1)
h_uncal = fit(Histogram, pe_uncal, 0.0:bin_width_uncal:f_simple_uncal(max_pe_peak + 1))

Check warning on line 88 in src/sipm_simple_calibration.jl

View check run for this annotation

Codecov / codecov/patch

src/sipm_simple_calibration.jl#L87-L88

Added lines #L87 - L88 were not covered by tests

result = (
pe_simple_cal = pe_simple_cal,
Expand Down
Loading