From 22e4f77e454cb1af529cdd99b8f0afe6b6eef4da Mon Sep 17 00:00:00 2001 From: x06da Date: Wed, 4 Nov 2026 20:14:21 +0100 Subject: [PATCH] Update PXIII DCCM calibration and improve pitch fitting --- pxiii_bec/macros/mxcomm_analysis.py | 22 +++++- pxiii_bec/macros/pxiii_energy.py | 108 ++++++++++++++------------- pxiii_bec/macros/pxiii_parameters.py | 6 +- 3 files changed, 78 insertions(+), 58 deletions(-) diff --git a/pxiii_bec/macros/mxcomm_analysis.py b/pxiii_bec/macros/mxcomm_analysis.py index 26c183e..27b8608 100644 --- a/pxiii_bec/macros/mxcomm_analysis.py +++ b/pxiii_bec/macros/mxcomm_analysis.py @@ -102,7 +102,19 @@ def fit(data, fit_params): params["base_slope"].set(value=0) # Add peak-specific parameters - params.update(peak_model.guess(processed_data["y_to_fit"], x=processed_data["x_data"])) + x_data = np.asarray(processed_data["x_data"]) + + params.update(peak_model.guess(processed_data["y_to_fit"], x=x_data)) + + # Constrain peak width to something resolvable by the scan + step = np.median(np.diff(np.sort(x_data))) + scan_range = np.ptp(x_data) + + if "peak_sigma" in params: + params["peak_sigma"].set( + min=0.5 * step, + max=0.5 * scan_range, + ) # Perform the fitting lmfit_result = full_model.fit(processed_data["y_to_fit"], params, x=processed_data["x_data"]) @@ -191,8 +203,12 @@ def check_peak_fit(x_data, result, min_fwhm_steps=3): if centre_stderr is None: problems.append("centre uncertainty could not be estimated") - elif centre_stderr > 0.2 * fwhm: - problems.append(f"centre uncertainty is large " f"({centre_stderr:.3g})") + elif centre_stderr > 0.5 * step: + problems.append( + f"centre uncertainty is large " + f"({centre_stderr:.3g}, " + f"{centre_stderr / step:.2f} scan steps)" + ) return {"success": not problems, "problems": problems} diff --git a/pxiii_bec/macros/pxiii_energy.py b/pxiii_bec/macros/pxiii_energy.py index ce0da38..e5f42cc 100644 --- a/pxiii_bec/macros/pxiii_energy.py +++ b/pxiii_bec/macros/pxiii_energy.py @@ -8,15 +8,6 @@ Plotting optional import pandas as pd import numpy as np -# from mx_methods import go_to_peak -# from pxiii_parameters import EnergyDefaults - -# from calculator import ( -# validate_energy, -# convert_from_bragg, -# convert_from_energy, -# ) - def get_current_energy(): """ @@ -140,51 +131,63 @@ def set_mirror_stripe(energy_ev): print(f"Selected mirror stripe: {selected_stripe}") -def mono_pitch_scan( - plot=True, - window_name='Fitting', - plot_widget=None, - ): - """Scan the monochromator pitch and move to the peak.""" +def mono_pitch_scan(plot=True, window_name="Fitting", plot_widget=None): + """Scan DCCM pitch, retrying with a wider range if necessary.""" + + halfwidth = EnergyDefaults.pitch_scan["halfwidth"] + steps = EnergyDefaults.pitch_scan["steps"] + + print("Starting Mono Pitch Scan.") + energy = get_current_energy() + pos = get_dccm_motors_positions(energy) + print(f"Setting DCCM theta2 to default value of {pos['theta2_angle']:.4f}") + scans.umv(EnergyDefaults.mono_pitch, pos['theta2_angle'],relative=False) + + result = go_to_peak( + EnergyDefaults.mono_pitch, + EnergyDefaults.signals["sig2"], + -halfwidth, + halfwidth, + steps=steps, + relative=True, + settle=0.01, + plot=plot, + confirm=False, + min_fwhm_steps=0.8, + window_name=window_name if plot else None, + plot_widget=plot_widget if plot else None, + ) + + extended_scan = False + + if not result["success"]: + extended_scan = True + print("Initial DCCM pitch scan failed. Trying a wider range.") - if plot: - print("Scanning monochromator pitch and moving to peak, with plotting.") result = go_to_peak( EnergyDefaults.mono_pitch, EnergyDefaults.signals["sig2"], - -EnergyDefaults.pitch_scan["halfwidth"], - EnergyDefaults.pitch_scan["halfwidth"], - steps=EnergyDefaults.pitch_scan["steps"], + -2 * halfwidth, + 2 * halfwidth, + steps=2 * steps, relative=True, settle=0.01, - plot=True, + plot=plot, confirm=False, - min_fwhm_steps=1.1, - window_name=window_name, - plot_widget=plot_widget, + min_fwhm_steps=0.8, + window_name=window_name if plot else None, + plot_widget=plot_widget if plot else None, ) - else: - print("Scanning monochromator pitch and moving to peak, without plotting.") - result = go_to_peak( - EnergyDefaults.mono_pitch, - EnergyDefaults.signals["sig2"], - -EnergyDefaults.pitch_scan["halfwidth"], - EnergyDefaults.pitch_scan["halfwidth"], - steps=EnergyDefaults.pitch_scan["steps"], - relative=True, - settle=0.01, - plot=False, - confirm=False, - ) - return result + + result["extended_scan"] = extended_scan + return result def calculate_theta2(theta1): - """Calculate initial DCCM theta2 from theta1""" - correction = 0.0007471 * theta1 + 0.0126144 - theta2 = -theta1 + correction + """Calculate initial DCCM theta2 using the fitted calibration""" + + return float(np.polyval(EnergyDefaults.theta2_calib, theta1)) - return theta2 def get_dccm_motors_positions(energy_ev): """ @@ -200,12 +203,10 @@ def get_dccm_motors_positions(energy_ev): dict: A dictionary containing the calculated DCCM motor positions including values retrieved from the lookup table, if applicable. """ - # dccm_motor_values = get_value_from_lut(energy_ev) + th1_angle = -convert_from_energy(energy_ev, print_result=False)["bragg_angle_deg"] th2_angle = calculate_theta2(th1_angle) - # # add fudge factor - # th2_angle_fudge = th2_angle + 0.006 - # # dccm_motor_values.update({"theta1_angle": th1_angle, "theta2_angle": th2_angle}) + dccm_motor_values = {"theta1_angle": th1_angle, "theta2_angle": th2_angle} return dccm_motor_values @@ -219,7 +220,6 @@ def move_dccm_motors(energy_ev): dccm_pos = get_dccm_motors_positions(energy_ev) print( f"Moving DCCM theta1: {dccm_pos['theta1_angle']: .5g} deg, theta2: {dccm_pos['theta2_angle']: .5g} deg, " - # f"DCM pitch: {dcm_pos['dcm_pitch']: .5g} mrad, " ) umv(EnergyDefaults.energy, dccm_pos["theta1_angle"], EnergyDefaults.mono_pitch, dccm_pos["theta2_angle"]) @@ -245,7 +245,7 @@ def bl_energy( plot: Boolean flag indicating whether to plot the DCCM pitch scan for finding the peak. Returns: - None + Results from mono pitch scan if run """ energy_ev = validate_energy(energy_ev) # Ensure energy is valid. @@ -267,7 +267,11 @@ def bl_energy( set_mirror_stripe(energy_ev) # Step 3: Perform DCCM pitch scan and move to peak. - if plot: - mono_pitch_scan(plot=True) - else: - mono_pitch_scan(plot=False) + if mono_scan: + return mono_pitch_scan( + plot=plot, + window_name=window_name, + plot_widget=plot_widget, + ) + + return None diff --git a/pxiii_bec/macros/pxiii_parameters.py b/pxiii_bec/macros/pxiii_parameters.py index f6ec6f9..a5cc720 100644 --- a/pxiii_bec/macros/pxiii_parameters.py +++ b/pxiii_bec/macros/pxiii_parameters.py @@ -11,7 +11,7 @@ class EnergyDefaults: min_energy_change = 1 min_energy_ev = 4500 - max_energy_ev = 15000 + max_energy_ev = 16500 signals = { "sig1": dev.dccm_di_top, "sig2": dev.dccm_bpmsum, @@ -20,9 +20,9 @@ class EnergyDefaults: } energy = dev.dccm_theta1 mono_pitch = dev.dccm_theta2 - # LUT_table = "luts/energy_lut.csv" stripe_thresholds = {"silicon": 9000, "rhodium": 40000} - pitch_scan = {"halfwidth": 0.01, "steps": 25} + pitch_scan = {"halfwidth": 0.008, "steps": 25} + theta2_calib = [-0.99924628, 0.01270588] default_trans = 1.0