Skip to content

Commit 46b3485

Browse files
authored
Fixing gravity correction sign handling (#85)
1 parent 5998acc commit 46b3485

19 files changed

+472
-165
lines changed

pixi.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ docs = {features = ["installer", "docs"], solve-group = "default"} # readthedoc
146146

147147
[tool.pixi.tasks]
148148
# Documentation
149-
docs-build = { cmd = 'sphinx-build -W --keep-going -n -T -b html docs docs/build/html && echo -e "##########\n DOCS point your browser to file://$(pwd)/build/html/index.html\n##########"', description = "Build the documentation" }
149+
docs-build = { cmd = 'sphinx-build --keep-going -n -T -b html docs docs/build/html && echo -e "##########\n DOCS point your browser to file://$(pwd)/build/html/index.html\n##########"', description = "Build the documentation" }
150150
docs-clean = { cmd = "rm -rf docs/build", description = "Clean the documentation build artifacts" }
151151
# Packaging
152152
conda-builder = { cmd = "pixi build", description = "Command that creates the conda package" }

reduction/data/reference_fbck.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
# 198415 198403 4.68567 2.5026 5.84094 0.0879 0.2052 332.49 0.0087
1818
# 198416 198403 9.27811 2.50312 5.84062 0.1740 0.4060 1251.1 0.0344
1919
# Q [1/Angstrom] R dR dQ [FWHM]
20-
0.0079633412841666 1.0320123125424443 0.0919633911247547 0.0001811746549253
2120
0.0081226081098499 0.9553312739097752 0.0683833529488042 0.0001847981480238
2221
0.0082850602720469 0.8799194292970859 0.0610211493810700 0.0001884941109843
2322
0.0084507614774878 0.9876096489604828 0.0608483374129951 0.0001922639932040
@@ -40,7 +39,6 @@
4039
0.0111506003012550 0.9971824028768898 0.0523298230913529 0.0002540463155815
4140
0.0113736123072801 0.9573338425426422 0.0488851111827781 0.0002591272418932
4241
0.0116010845534257 1.0560559370122706 0.0476703162388120 0.0002643097867310
43-
0.0116010845534257 0.9628679287316180 0.1359697151147919 0.0002629805247960
4442
0.0118331062444942 1.0625193915580156 0.0455091504622335 0.0002695959824656
4543
0.0118331062444942 1.1022599911552518 0.1011574603124846 0.0002682401352919
4644
0.0120697683693841 1.0555508442225094 0.0428327366205141 0.0002749879021150
@@ -80,7 +78,6 @@
8078
0.0210137588940102 0.0229392489239258 0.0020410333961966 0.0004757383546659
8179
0.0214340340718904 0.0218465458871662 0.0019340236638051 0.0004852531217592
8280
0.0218627147533282 0.0176634733592326 0.0016114280920659 0.0004949581841944
83-
0.0222999690483947 0.0207222286589651 0.0017673103323342 0.0005048573478783
8481
0.0222999690483947 0.0140754009641088 0.0027430460859610 0.0005049939725205
8582
0.0227459684293626 0.0162785590534892 0.0015020511349239 0.0005149544948358
8683
0.0227459684293626 0.0146482030025221 0.0009196854005510 0.0005150938519709

reduction/data/reference_r201284_quick.txt

Lines changed: 0 additions & 20 deletions
This file was deleted.

reduction/data/reference_rq.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
# 198415 198403 4.68567 2.5026 5.84094 0.0879 0.2052 332.49 0.0087
1818
# 198416 198403 9.27811 2.50312 5.84062 0.1740 0.4060 1251.1 0.0344
1919
# Q [1/Angstrom] R dR dQ [FWHM]
20-
0.0079633412841666 1.0310348944619467 0.0919496966514896 0.0001811746549253
2120
0.0081226081098499 0.9548169246185914 0.0683403898049301 0.0001847981480238
2221
0.0082850602720469 0.8799194292970859 0.0610211493810700 0.0001884941109843
2322
0.0084507614774878 0.9880664878997731 0.0608469953695779 0.0001922639932040
@@ -40,7 +39,6 @@
4039
0.0111506003012550 0.9968007019677384 0.0523444935643575 0.0002540463155815
4140
0.0113736123072801 0.9570270080507320 0.0488826413618744 0.0002591272418932
4241
0.0116010845534257 1.0560559370122706 0.0476703162388120 0.0002643097867310
43-
0.0116010845534257 0.9628679287316180 0.1359697151147919 0.0002629805247960
4442
0.0118331062444942 1.0625193915580156 0.0455091504622335 0.0002695959824656
4543
0.0118331062444942 1.1027667248231421 0.1012010545517131 0.0002682401352919
4644
0.0120697683693841 1.0557357632228019 0.0428247213544392 0.0002749879021150
@@ -81,7 +79,6 @@
8179
0.0214340340718904 0.0218640068269973 0.0019353744401254 0.0004852531217592
8280
0.0218627147533282 0.0176634733592326 0.0016114280920659 0.0004949581841944
8381
0.0222999690483947 0.0207487733091790 0.0017792554073740 0.0005048573478783
84-
0.0222999690483947 0.0140707365027053 0.0027424659594895 0.0005049939725205
8582
0.0227459684293626 0.0162785590534871 0.0015019389013074 0.0005149544948358
8683
0.0227459684293626 0.0145665428188618 0.0009188736722958 0.0005150938519709
8784
0.0232008877979499 0.0160840931910092 0.0014101231831638 0.0005252535847326

reduction/data/reference_rq_201282.txt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
# Run title: Expt 8 Cu-B BF4 noEtOH Full OCV 1-201282-1.
44
# Run start time: 2023-01-16T10:47:39.627455667
55
# Reduction time: Fri Jul 11 13:38:18 2025
6-
# Q summing: False
76
# TOF weighted: False
87
# Bck in Q: False
98
# Theta offset: 0
@@ -16,8 +15,6 @@
1615
# 201287 201051 2.36511 2.50266 5.84109 0.0444 0.1036 108.65 0.0028
1716
# 201288 201051 4.68567 2.50266 5.84109 0.0879 0.2052 374.58 0.0141
1817
# Q [1/Angstrom] R dR dQ [FWHM]
19-
0.0078740166107227 1.4298817937686725 0.4021546705243180 0.0001785155408216
20-
0.0079921268598835 1.0464165998640591 0.0877222071798383 0.0001811932739339
2118
0.0081120087627818 1.0201670196357679 0.0804624967445296 0.0001839111730429
2219
0.0082336888942235 1.0079439046970262 0.0773456137720529 0.0001866698406386
2320
0.0083571942276368 0.8580655386536505 0.0696273751192951 0.0001894698882482
@@ -29,7 +26,6 @@
2926
0.0091381177336698 1.0501522989795551 0.0559336105271315 0.0002071745730249
3027
0.0092751894996749 0.9919913449117463 0.0524082655606986 0.0002102821916203
3128
0.0094143173421700 1.1142711141102681 0.1738139233867726 0.0002134364244946
32-
0.0094143173421700 1.0651018230117180 0.0518198764793556 0.0002134364244946
3329
0.0095555321023025 1.0942823641920671 0.1005475067241905 0.0002166379708620
3430
0.0095555321023025 1.0431946505595266 0.0477022753297744 0.0002166379708620
3531
0.0096988650838371 1.0445143851077090 0.0459832496736763 0.0002198875404249
@@ -48,7 +44,6 @@
4844
0.0112559288862039 1.0462039128375169 0.0596315292988098 0.0002551884675775
4945
0.0114247678194969 1.0736829257476801 0.0578997949983643 0.0002590162945912
5046
0.0115961393367894 1.0252111735643916 0.0544691966671061 0.0002629015390101
51-
0.0115961393367894 1.2948067743081022 0.2744253042966722 0.0002629015390101
5247
0.0117700814268412 1.0125517015257290 0.0526812652951897 0.0002668450620952
5348
0.0117700814268412 0.9807653502792314 0.1172718340021845 0.0002668450620952
5449
0.0119466326482438 1.0136793752175577 0.0499103239044689 0.0002708477380266

reduction/data/reference_rq_avg.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
# 198415 198403 4.68567 2.5026 5.84094 0.0879 0.2052 332.49 0.0087
1818
# 198416 198403 9.27811 2.50312 5.84062 0.1740 0.4060 1251.1 0.0344
1919
# Q [1/Angstrom] R dR dQ [FWHM]
20-
0.0079633412841666 1.0310348944619467 0.0919496966514896 0.0001811746549253
2120
0.0081226081098499 0.9548169246185914 0.0683403898049301 0.0001847981480238
2221
0.0082850602720469 0.8799194292970859 0.0610211493810700 0.0001884941109843
2322
0.0084507614774878 0.9880664878997731 0.0608469953695779 0.0001922639932040

reduction/data/reference_short_nobck.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Reduction unknown
33
# Run title: sample1_pcps_d2obuffer-198382-7.
44
# Run start time: 2022-12-07T05:50:40.745408667
5-
# Reduction time: Fri Jul 11 13:43:31 2025
5+
# Reduction time: Wed Sep 10 11:16:46 2025
66
# Q summing: False
77
# TOF weighted: False
88
# Bck in Q: False

reduction/lr_reduction/event_reduction.py

Lines changed: 17 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import numpy as np
1313
from scipy.optimize import brentq
1414

15+
from lr_reduction.gravity_correction import GravityDirection, gravity_correction
1516
from lr_reduction.instrument_settings import InstrumentSettings
1617
from lr_reduction.utils import mantid_algorithm_exec
1718

@@ -326,6 +327,8 @@ class EventReflectivity:
326327
If ``UseDeadTimeThreshold`` is True, this is the upper limit for dead-time correction ratios
327328
use_emmission_time : bool
328329
If True, the emission time delay will be computed
330+
gravity_direction : GravityDirection
331+
Direction of gravity correction to apply
329332
"""
330333

331334
QX_VS_QZ = 0
@@ -363,6 +366,7 @@ def __init__(
363366
dead_time_threshold: Optional[float] = 1.5,
364367
instrument_settings: InstrumentSettings = None,
365368
use_emission_time=True,
369+
gravity_direction=None # undetermined
366370
):
367371
if instrument in [self.INSTRUMENT_4A, self.INSTRUMENT_4B]:
368372
self.instrument = instrument
@@ -393,6 +397,7 @@ def __init__(
393397
self.dead_time_threshold = dead_time_threshold
394398
self.instrument_settings = instrument_settings
395399
self.use_emission_time = use_emission_time
400+
self.grav_direction = gravity_direction
396401

397402
# Turn on functional background estimation
398403
self.use_functional_bck = functional_background
@@ -941,9 +946,17 @@ def _reflectivity(
941946
# convert tof values to wavelength.
942947
wl_list = tofs / self.constant
943948

944-
# Gravity correction
945-
d_theta = self.gravity_correction(ws, wl_list)
946-
# collect weighted events
949+
# Gravity correction (check on value in template for gravity correction):
950+
if self.grav_direction is not None:
951+
d_theta = gravity_correction(ws, wl_list, gravity_direction=self.grav_direction)
952+
else:
953+
if peak_position == 0: # is direct beam
954+
# for direct beam, find the direction using the `ths` value of the associated reflectivity run
955+
d_theta = gravity_correction(
956+
ws, wl_list, gravity_direction=GravityDirection.find_direction(self._ws_sc))
957+
else: # is reflectivity run
958+
d_theta = gravity_correction(ws, wl_list, gravity_direction=None)
959+
947960
event_weights = evt_list.getWeights()
948961

949962
# Calculate per-spectum offset in theta for q-summing.
@@ -955,6 +968,7 @@ def _reflectivity(
955968
ths_value = ws.getRun()["ths"].value[-1]
956969
delta_theta_f *= np.sign(ths_value)
957970

971+
# TODO: Check in code for any other calls of gravity_correction.
958972
# convert wavelengths into qz. This could be separated to enable output in lam and q.
959973
qz = 4.0 * np.pi / wl_list * np.sin(theta + delta_theta_f - d_theta)
960974
# Remove unfeasible negative values:
@@ -1224,75 +1238,6 @@ def emission_time_correction(self, ws, tofs):
12241238
tofs -= t_off + t_mult * tofs / self.constant
12251239
return tofs
12261240

1227-
def gravity_correction(self, ws, wl_list):
1228-
"""
1229-
Gravity correction for each event
1230-
Think this works on an array of wavelengths so could work for non-event list too.
1231-
1232-
Parameters
1233-
----------
1234-
ws : mantid.api.Workspace
1235-
Mantid workspace to extract correction meta-data from.
1236-
wl_list : numpy.ndarray
1237-
Array of wavelengths for each event.
1238-
1239-
Returns
1240-
-------
1241-
numpy.ndarray
1242-
Array of gravity-corrected theta values for each event, in radians.
1243-
"""
1244-
# Xi reference would be the position of xi if the si slit were to be positioned
1245-
# at the sample. The distance from the sample to si is then xi_reference - xi.
1246-
xi_reference = 445
1247-
if ws.getInstrument().hasParameter("xi-reference"):
1248-
xi_reference = ws.getInstrument().getNumberParameter("xi-reference")[0]
1249-
1250-
# Distance between the s1 and the sample
1251-
s1_sample_distance = 1485
1252-
if ws.getInstrument().hasParameter("s1-sample-distance"):
1253-
s1_sample_distance = ws.getInstrument().getNumberParameter("s1-sample-distance")[0] * 1000
1254-
1255-
xi = 310
1256-
if ws.getInstrument().hasParameter("BL4B:Mot:xi.RBV"):
1257-
xi = abs(ws.getRun().getProperty("BL4B:Mot:xi.RBV").value[0])
1258-
1259-
sample_si_distance = xi_reference - xi
1260-
slit_distance = s1_sample_distance - sample_si_distance
1261-
1262-
# Angle of the incident beam on a horizontal sample
1263-
# TODO: this will need to be updated with logged value.
1264-
theta_in = -4.0
1265-
1266-
# Calculation from the ILL paper. This works for inclined beams.
1267-
# Calculated theta is the angle on the sample
1268-
1269-
g = 9.8067 # m/s^2
1270-
h = 6.6260715e-34 # Js=kg m^2/s
1271-
mn = 1.67492749804e-27 # kg
1272-
1273-
v = h / (mn * wl_list * 1e-10)
1274-
k = g / (2 * v**2)
1275-
1276-
# Define the sample position as x=0, y=0. increasing x is towards moderator
1277-
xs = 0
1278-
1279-
# positions of slits
1280-
x1 = sample_si_distance / 1000
1281-
x2 = (sample_si_distance + slit_distance) / 1000
1282-
1283-
# height of slits determined by incident theta, y=0 is the sample height
1284-
y1 = x1 * np.tan(theta_in * np.pi / 180)
1285-
y2 = x2 * np.tan(theta_in * np.pi / 180)
1286-
1287-
# This is the location of the top of the parabola
1288-
x0 = (y1 - y2 + k * (x1**2 - x2**2)) / (2 * k * (x1 - x2))
1289-
1290-
# Angle is arctan(dy/dx) at sample
1291-
theta_sample = np.arctan(2 * k * (x0 - xs)) * 180 / np.pi
1292-
1293-
return (theta_sample - theta_in) * np.pi / 180.0
1294-
1295-
12961241
def compute_resolution(ws, default_dq=0.027, theta=None, q_summing=False):
12971242
"""
12981243
Compute the Q resolution from the meta data.

0 commit comments

Comments
 (0)