diff --git a/libra_toolbox/neutron_detection/activation_foils/compass.py b/libra_toolbox/neutron_detection/activation_foils/compass.py index af8b05a..d64e68b 100644 --- a/libra_toolbox/neutron_detection/activation_foils/compass.py +++ b/libra_toolbox/neutron_detection/activation_foils/compass.py @@ -662,7 +662,70 @@ def get_gamma_emitted( channel_nb: int, search_width: float = 800, summing_method: str = 'sum_gaussian', + efficiency_function = None, + efficiency_function_args: list = None ): + """ + Estimate the emitted gamma-ray counts for the foil product nuclide. + + This method performs background-subtracted peak area extraction in the + specified detector channel, converts the measured energy scale using the + supplied calibration polynomial, and divides the measured peak counts by + the detector efficiency to estimate the total emitted gamma rays. + + Parameters + ---------- + background_measurement : Measurement + The background measurement used to subtract the continuum and + environmental counts from the sample spectrum. + efficiency_coeffs : array-like + Polynomial coefficients defining the detector efficiency as a + function of energy, used with ``np.polyval(efficiency_coeffs, energy)`` + when no custom efficiency function is provided. + calibration_coeffs : array-like + Polynomial coefficients used to convert raw detector channels to + calibrated energies before peak integration. + channel_nb : int + Detector channel number to analyze. + search_width : float, optional + Width, in energy units, used to search for and fit the product peak. + Default is 800. + summing_method : str, optional + Peak-summing strategy passed to ``get_multipeak_area``. Supported + values include ``'sum_gaussian'`` and ``'sum_histogram'``. Default is + ``'sum_gaussian'``. + efficiency_function : callable, optional + Custom function that evaluates detector efficiency as a function of + energy. If provided, it is used instead of the polynomial + efficiency coefficients provided in ``efficiency_coeffs``. + This is useful when efficiency is known from + calibration data or a fitted model instead of a simple polynomial. + efficiency_function_args : list, optional + Additional keyword arguments to pass to ``efficiency_function`` if + it requires extra parameters. + + Returns + ------- + tuple[np.ndarray, np.ndarray] + A pair ``(gamma_emmitted, gamma_emmitted_err)`` containing the + estimated emitted gamma-ray counts and their propagated uncertainty. + + Notes + ----- + The effective detection efficiency is determined as follows: + + - If ``efficiency_function`` is not ``None``, then + ``detection_efficiency = efficiency_function(energy, **efficiency_function_kwargs)``. + - Otherwise, ``detection_efficiency = np.polyval(efficiency_coeffs, energy)``. + + The quantity returned is therefore: + + gamma_emmitted = nb_counts_measured / detection_efficiency + + where ``nb_counts_measured`` is the background-subtracted counts under + the expected product gamma peak(s). + """ + # find right background detector background_detector = background_measurement.get_detector(channel_nb) @@ -687,7 +750,12 @@ def get_gamma_emitted( nb_counts_measured = np.array(nb_counts_measured) nb_counts_measured_err = np.sqrt(nb_counts_measured) - detection_efficiency = np.polyval(efficiency_coeffs, energy) + # check if there is an efficiency function provided, if so use it to get the detection efficiency + if efficiency_function is not None: + detection_efficiency = efficiency_function(energy, *efficiency_function_args) + else: + # otherwise, just use the polynomial coefficients to get the detection efficiency + detection_efficiency = np.polyval(efficiency_coeffs, energy) gamma_emmitted = nb_counts_measured / detection_efficiency gamma_emmitted_err = nb_counts_measured_err / detection_efficiency diff --git a/test/neutron_detection/test_compass.py b/test/neutron_detection/test_compass.py index c564ed9..9e5cd25 100644 --- a/test/neutron_detection/test_compass.py +++ b/test/neutron_detection/test_compass.py @@ -834,6 +834,75 @@ def test_get_multipeak_area_two_close_peaks(): assert np.isclose(areas[i], expected_area, rtol=1e-2) +def eff_curve_func(E, *a): + eff = 0 + E = np.array(E) + for i,a_i in enumerate(a): + eff += a_i * E**i + return eff + + +@pytest.mark.parametrize( + "efficiency_coeffs, efficiency_function, efficiency_function_args, expected_efficiency", + [ + (np.array([0.0, 0.25]), None, None, 0.25), + (np.array([0.0, 0.5]), eff_curve_func, [-0.80, 1.0], 0.20), + ], +) +def test_get_gamma_emitted_uses_custom_or_polynomial_efficiency( + monkeypatch, efficiency_coeffs, efficiency_function, efficiency_function_args, expected_efficiency +): + # BUILD + nuclide_reactant = Nuclide(name="TestNuclide", atomic_mass=200) + activated_nuclide = Nuclide( + name="ActivatedNuclide", + energy=[1.0], + intensity=[1.0], + half_life=10 * 24 * 3600, + ) + + reaction = Reaction( + reactant=nuclide_reactant, + product=activated_nuclide, + cross_section=20.0, + ) + + foil = ActivationFoil(reaction=reaction, mass=0.1, name="TestFoil") + measurement = compass.SampleMeasurement("sample") + measurement.foil = foil + measurement.start_time = datetime.datetime(2024, 11, 7) + measurement.stop_time = datetime.datetime(2024, 11, 7, 1) + measurement.detectors = [compass.Detector(channel_nb=3)] + measurement.get_detector(3).real_count_time = 3600 + measurement.get_detector(3).live_count_time = 3600 + measurement.get_detector(3).events = np.array([(0, 1.0), (1, 2.0), (2, 3.0)]) + + background_measurement = compass.Measurement("background") + background_measurement.detectors = [compass.Detector(channel_nb=3)] + background_measurement.get_detector(3).real_count_time = 3600 + background_measurement.get_detector(3).live_count_time = 3600 + background_measurement.get_detector(3).events = np.array([(0, 0.0), (1, 0.0)]) + + monkeypatch.setattr(compass, "get_multipeak_area", lambda *args, **kwargs: np.array([100.0])) + + # RUN + gammas_emitted = measurement.get_gamma_emitted( + background_measurement=background_measurement, + efficiency_coeffs=efficiency_coeffs, + calibration_coeffs=np.array([1.0, 0.0]), + channel_nb=3, + search_width=300, + summing_method="sum_gaussian", + efficiency_function=efficiency_function, + efficiency_function_args=efficiency_function_args, + ) + + print(f"Computed gammas emitted: {gammas_emitted[0]}, Expected: {100.0 / expected_efficiency}") + # TEST + expected = 100.0 / expected_efficiency + assert np.isclose(gammas_emitted[0], expected) + + @pytest.mark.parametrize( "efficiency, summing_method", [