Skip to content

Commit

Permalink
fix q summing
Browse files Browse the repository at this point in the history
  • Loading branch information
mdoucet committed Dec 3, 2024
1 parent aaae8a5 commit 1d7b8da
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
16 changes: 14 additions & 2 deletions reduction/lr_reduction/event_reduction.py
Original file line number Diff line number Diff line change
Expand Up @@ -608,9 +608,13 @@ def _reflectivity(self, ws, peak_position, peak, low_res, theta, q_bins=None, q_
d_theta = self.gravity_correction(ws, wl_list)
event_weights = evt_list.getWeights()

# Sign will depend on reflect up or down
x_distance = _pixel_width * (j - peak_position)
delta_theta_f = np.arctan(x_distance / self.det_distance) / 2.0

# Sign will depend on reflect up or down
ths_value = ws.getRun()['ths'].value[-1]
delta_theta_f *= ths_value / np.fabs(ths_value)

qz=4.0*np.pi/wl_list * np.sin(theta + delta_theta_f - d_theta)
qz = np.fabs(qz)

Expand Down Expand Up @@ -649,6 +653,10 @@ def _reflectivity(self, ws, peak_position, peak, low_res, theta, q_bins=None, q_
if wl_dist is not None and wl_bins is not None:
bin_size = _q_bins[1:] - _q_bins[:-1]
non_zero = counts > 0
# Deal with the case where we don't sum all the bins
if not sum_pixels:
bin_size = np.tile(bin_size, [counts.shape[0], 1])

d_refl_sq[non_zero] = refl[non_zero] / np.sqrt(counts[non_zero]) / charge / bin_size[non_zero]
refl[non_zero] = refl[non_zero] / charge / bin_size[non_zero]
else:
Expand All @@ -670,7 +678,11 @@ def _get_events(self, ws, peak, low_res):
else:
pixel = i * self.n_y + j
evt_list = ws.getSpectrum(pixel)
wl_list = evt_list.getTofs() / self.constant
tofs = evt_list.getTofs()
# Correct for emission time as needed
if self.use_emission_time:
tofs = self.emission_time_correction(ws, tofs)
wl_list = tofs / self.constant
wl_events = np.concatenate((wl_events, wl_list))

return wl_events
Expand Down
13 changes: 11 additions & 2 deletions reduction/lr_reduction/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from mantid.api import *
from mantid.kernel import *

from . import event_reduction, reduction_template_reader
from . import event_reduction, peak_finding, reduction_template_reader

TOLERANCE = 0.07
OUTPUT_NORM_DATA = False
Expand Down Expand Up @@ -207,9 +207,18 @@ def process_from_template_ws(ws_sc, template_data, q_summing=False,
else:
peak_bck = None

#TODO: Fit this peak
peak_center = (peak[0]+peak[1])/2.0

# Fit the reflected beam position, which may not be in the middle and is
# used in the q-summing calculation
if q_summing:
x_min=template_data.data_peak_range[0]
x_max=template_data.data_peak_range[1]
_, _x, _y = peak_finding.process_data(ws_sc, summed=True, tof_step=200)
peak_center = np.argmax(_y[x_min:x_max]) + x_min
peak_center, sc_width, _ = peak_finding.fit_signal_flat_bck(_x, _y, x_min=x_min, x_max=x_max, center=peak_center, sigma=3.)
print("Peak center: %g" % peak_center)

if template_data.data_x_range_flag:
low_res = template_data.data_x_range
else:
Expand Down
2 changes: 1 addition & 1 deletion reduction/lr_reduction/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def reduce(ws, template_file, output_dir, average_overlap=False,
# Call the reduction using the template
qz_mid, refl, d_refl, meta_data = template.process_from_template_ws(ws, template_data,
q_summing=q_summing,
tof_weighted=q_summing,
tof_weighted=False,
clean=q_summing,
bck_in_q=bck_in_q,
info=True)
Expand Down

0 comments on commit 1d7b8da

Please sign in to comment.