{ "cells": [ { "cell_type": "markdown", "id": "cdf89542-fd5b-4657-8a55-0315ba4e6263", "metadata": {}, "source": [ "# Data reduction for Amor\n", "\n", "In this notebook, we will look at the reduction workflow for reflectometry data collected from the PSI\n", "[Amor](https://www.psi.ch/en/sinq/amor) instrument.\n", "This is a living document and there are plans to update this as necessary with changes in the data reduction methodology and code.\n", "\n", "We will begin by importing the modules that are necessary for this notebook and loading the data.\n", "The `sample.nxs` file is the experimental data file of interest,\n", "while `reference.nxs` is the reference measurement of the neutron supermirror." ] }, { "cell_type": "code", "execution_count": null, "id": "455b7a61-5a5d-4d94-993c-02fe41c84e50", "metadata": {}, "outputs": [], "source": [ "import scipp as sc\n", "from ess import amor\n", "import ess" ] }, { "cell_type": "code", "execution_count": null, "id": "1c89f088-0663-49ce-a999-8be271f2e24a", "metadata": {}, "outputs": [], "source": [ "logger = ess.logging.configure_workflow('amor_reduction',\n", " filename='amor.log')" ] }, { "cell_type": "markdown", "id": "dde6c61a-39ed-4e85-af6e-0915baf3ad1b", "metadata": {}, "source": [ "## The Amor beamline\n", "\n", "Before we can load the data, we need to define the parameters of the beamline.\n", "We begin by defining the convention for naming angles in our set-up.\n", "We use the Fig. 5 from the paper by [Stahn & Glavic (2016)](#stahn2016),\n", "which is reproduced below (along with its caption).\n", "\n", "![Figure5](https://ars.els-cdn.com/content/image/1-s2.0-S0168900216300250-gr5.jpg)\n", "\n", "The yellow area shows the incoming and reflected beam, both with the divergence $\\Delta \\theta$.\n", "The inclination of the sample relative to the centre of the incoming beam\n", "(here identical to the instrument horizon) is called $\\omega$,\n", "and the respective angle of the reflected beam relative to the same axis is $\\gamma$.\n", "\n", "In general the detector centre is located at $\\gamma_{\\rm D} = 2\\omega$.\n", "These are instrument coordinates and should not be confused with the situation on the sample,\n", "where the take-off angle of an individual neutron trajectory is called $\\theta$.\n", "\n", "The `amor` module provides a helper function that generates the default beamline parameters.\n", "This function requires the sample rotation angle ($\\omega$) as an input to fully define the beamline.\n", "In the future, all these beamline parameters (including the sample rotation) will be included in the file meta data.\n", "For now, we must define this manually." ] }, { "cell_type": "code", "execution_count": null, "id": "f7f0a7b0-39b5-43d4-bf36-a2dada783eed", "metadata": {}, "outputs": [], "source": [ "sample_rotation = sc.scalar(0.7989, unit='deg')\n", "amor_beamline = amor.make_beamline(sample_rotation=sample_rotation)" ] }, { "cell_type": "markdown", "id": "4d50fbee-cc27-4347-b5bc-48850d954041", "metadata": {}, "source": [ "## Loading the data\n", "\n", "Using the `amor.load` function, we load the `sample.nxs` file and perform some early preprocessing:\n", "\n", "- The `tof` values are converted from nanoseconds to microseconds.\n", "- The raw data contains events coming from two pulses, and these get folded into a single `tof` range" ] }, { "cell_type": "code", "execution_count": null, "id": "ff2c2c9a-ac1f-404a-bef7-559ae85b91af", "metadata": {}, "outputs": [], "source": [ "sample = amor.load(amor.data.get_path(\"sample.nxs\"),\n", " beamline=amor_beamline)\n", "sample" ] }, { "cell_type": "markdown", "id": "7d30344d-fc38-483b-a2ff-872c4727dfa2", "metadata": {}, "source": [ "By simply plotting the data, we get a first glimpse into the data contents." ] }, { "cell_type": "code", "execution_count": null, "id": "4d58db96-96bb-4f02-9ed4-c227b778eb8f", "metadata": {}, "outputs": [], "source": [ "sc.plot(sample)" ] }, { "cell_type": "markdown", "id": "9a43dee7-fea3-4de9-9426-d3fe9741249d", "metadata": {}, "source": [ "### Correcting the position of the detector pixels\n", "\n", "**Note:** once new Nexus files are produced, this step should go away. \n", "\n", "The pixel positions are wrong in the `sample.nxs` file, and require an ad-hoc correction.\n", "We apply an arbitrary shift in the vertical (`y`) direction.\n", "We first move the pixels down by 0.955 degrees,\n", "so that the centre of the beam goes through the centre of the top half of the detector blades\n", "(the bottom half of the detectors was turned off).\n", "Next, we move all the pixels so that the centre of the top half of the detector pixels lies at an angle of $2 \\omega$,\n", "as described in the beamline diagram." ] }, { "cell_type": "code", "execution_count": null, "id": "77bc292a-6cd9-4f55-9479-1b5b2c6b57ac", "metadata": {}, "outputs": [], "source": [ "logger.info(\"Correcting pixel positions in 'sample.nxs'\")\n", "def pixel_position_correction(data: sc.DataArray):\n", " return data.coords['position'].fields.z * sc.tan(2.0 *\n", " data.coords['sample_rotation'] -\n", " (0.955 * sc.units.deg))\n", "sample.coords['position'].fields.y += pixel_position_correction(sample)" ] }, { "cell_type": "markdown", "id": "5746a0e8-6e90-459b-a8cf-37af826686f9", "metadata": {}, "source": [ "We now check that the detector pixels are in the correct position by showing the instrument view" ] }, { "cell_type": "code", "execution_count": null, "id": "77c70704-b45f-4131-aa84-63765a99668a", "metadata": {}, "outputs": [], "source": [ "amor.instrument_view(sample)" ] }, { "cell_type": "markdown", "id": "5844965c-2d8a-4dd8-a080-855945e47ad0", "metadata": {}, "source": [ "## Coordinate transformation graph\n", "\n", "To compute the wavelength $\\lambda$, the scattering angle $\\theta$, and the $Q$ vector for our data,\n", "we construct a coordinate transformation graph.\n", "\n", "It is based on classical conversions from `tof` and pixel `position` to $\\lambda$ (`wavelength`),\n", "$\\theta$ (`theta`) and $Q$ (`Q`),\n", "but comprises a number of modifications.\n", "\n", "The computation of the scattering angle $\\theta$ includes a correction for the Earth's gravitational field which bends the flight path of the neutrons.\n", "The angle can be found using the following expression\n", "\n", "$$\\theta = \\sin^{-1}\\left(\\frac{\\left\\lvert y + \\frac{g m_{\\rm n}}{2 h^{2}} \\lambda^{2} L_{2}^{2} \\right\\rvert }{L_{2}}\\right) - \\omega$$\n", "\n", "where $m_{\\rm n}$ is the neutron mass,\n", "$g$ is the acceleration due to gravity,\n", "and $h$ is Planck's constant.\n", "\n", "For a graphical representation of the above expression,\n", "we consider once again the situation with a convergent beam onto an inclined sample.\n", "\n", "![specular_reflection](amor_specular_reflection.png)\n", "\n", "The detector (in green), whose center is located at an angle $\\gamma_{\\rm D}$ from the horizontal plane,\n", "has a physical extent and is measuring counts at multiple scattering angles at the same time.\n", "We consider two possible paths for neutrons.\n", "The first path (cyan) is travelling horizontally from the source to the sample and subsequently,\n", "following specular reflection, hits the detector at $\\gamma_{\\rm D}$ from the horizontal plane.\n", "From the symmetry of Bragg's law, the scattering angle for this path is $\\theta_{1} = \\gamma_{\\rm D} - \\omega$.\n", "\n", "The second path (red) is hitting the bottom edge of the detector.\n", "Assuming that all reflections are specular,\n", "the only way the detector can record neutron events at this location is if the neutron originated from the bottom part of the convergent beam.\n", "Using the same symmetry argument as above, the scattering angle is $\\theta_{2} = \\gamma_{2} - \\omega$. \n", "\n", "This expression differs slightly from the equation found in the computation of the $\\theta$ angle in other techniques such as\n", "[SANS](https://docs.mantidproject.org/nightly/algorithms/Q1D-v2.html#q-unit-conversion),\n", "in that the horizontal $x$ term is absent,\n", "because we assume a planar symmetry and only consider the vertical $y$ component of the displacement.\n", "\n", "The conversion graph is defined in the reflectometry module,\n", "and can be obtained via" ] }, { "cell_type": "code", "execution_count": null, "id": "71b31e8c-7138-45a5-a9c1-8eb4e9727b09", "metadata": {}, "outputs": [], "source": [ "graph = amor.conversions.specular_reflection()\n", "sc.show_graph(graph, simplified=True)" ] }, { "cell_type": "markdown", "id": "e1163c27-a355-432f-b060-f45f8b8bf215", "metadata": {}, "source": [ "## Computing the wavelength\n", "\n", "To compute the wavelength of the neutrons,\n", "we request the `wavelength` coordinate from the `transform_coords` method by supplying our graph defined above\n", "(see [here](https://scipp.github.io/scippneutron/user-guide/coordinate-transformations.html)\n", "for more information about using `transform_coords`).\n", "\n", "We also exclude all neutrons with a wavelength lower than 2.4 Å." ] }, { "cell_type": "code", "execution_count": null, "id": "9a8dade8-11c0-4b43-a8a4-ff431801f7b9", "metadata": {}, "outputs": [], "source": [ "sample_wav = sample.transform_coords([\"wavelength\"], graph=graph)\n", "wavelength_edges = sc.array(dims=['wavelength'], values=[2.4, 16.0], unit='angstrom')\n", "sample_wav = sc.bin(sample_wav, edges=[wavelength_edges])\n", "sample_wav" ] }, { "cell_type": "code", "execution_count": null, "id": "c7e4ecca-bb23-4b6c-a056-ebb9dcfa579d", "metadata": {}, "outputs": [], "source": [ "sample_wav.bins.concatenate('detector_id').plot()" ] }, { "cell_type": "markdown", "id": "64a6b838-c594-48bc-a85f-3ba9a40088a6", "metadata": {}, "source": [ "## Compute the Q vector\n", "\n", "Using the same method, we can compute the $Q$ vector,\n", "which now depends on both detector position (id) and wavelength" ] }, { "cell_type": "code", "execution_count": null, "id": "8bc2a9e3-ca17-4724-a47e-9bcd22c6919a", "metadata": {}, "outputs": [], "source": [ "sample_q = sample_wav.transform_coords([\"Q\"], graph=graph)\n", "sample_q" ] }, { "cell_type": "code", "execution_count": null, "id": "c793a61d-b525-4d8f-88fd-297aead68daf", "metadata": {}, "outputs": [], "source": [ "q_edges = sc.geomspace(dim='Q', start=0.008, stop=0.08, num=201, unit='1/angstrom')\n", "sample_q_binned = sc.bin(sample_q, edges=[q_edges])\n", "sample_q_summed = sample_q_binned.sum('detector_id')\n", "sc.plot(sample_q_summed, norm=\"log\")" ] }, { "cell_type": "markdown", "id": "cc8f2bdc-c47f-4c74-80b2-0689ddbda590", "metadata": {}, "source": [ "## Normalize by the super-mirror\n", "\n", "To perform the normalization, we load the super-mirror `reference.nxs` file." ] }, { "cell_type": "code", "execution_count": null, "id": "7d88e47d-c0bc-4878-ad95-488cf286d666", "metadata": {}, "outputs": [], "source": [ "reference = amor.load(amor.data.get_path(\"reference.nxs\"),\n", " beamline=amor_beamline)\n", "reference.coords['position'].fields.y += pixel_position_correction(reference)\n", "reference" ] }, { "cell_type": "markdown", "id": "0cc91176-8022-4248-b6d9-504b7865e2bf", "metadata": {}, "source": [ "We convert the reference to wavelength using the same graph" ] }, { "cell_type": "code", "execution_count": null, "id": "a085f3e6-7351-4779-96ce-60600a988cf0", "metadata": {}, "outputs": [], "source": [ "reference_wav = reference.transform_coords([\"wavelength\"], graph=graph)\n", "reference_wav = sc.bin(reference_wav, edges=[wavelength_edges])\n", "reference_wav.bins.concatenate('detector_id').plot()" ] }, { "cell_type": "markdown", "id": "fa8fb76d-3cd8-4adb-89eb-fc00e47ea6b5", "metadata": {}, "source": [ "And we then convert to $Q$ as well" ] }, { "cell_type": "code", "execution_count": null, "id": "7fd3e32e-cfdf-47fd-a575-87707b55db4c", "metadata": {}, "outputs": [], "source": [ "reference_q = reference_wav.transform_coords([\"Q\"], graph=graph)\n", "reference_q_binned = sc.bin(reference_q, edges=[q_edges])\n", "reference_q_summed = reference_q_binned.sum('detector_id')\n", "sc.plot(reference_q_summed, norm=\"log\")" ] }, { "cell_type": "markdown", "id": "a3af2ec5-f4ec-400d-ab89-a483262daf34", "metadata": {}, "source": [ "Finally, we divide the sample by the reference to obtain" ] }, { "cell_type": "code", "execution_count": null, "id": "784c11db-f6d8-4959-8777-3be98aa8ba16", "metadata": {}, "outputs": [], "source": [ "normalized = sample_q_summed / reference_q_summed\n", "sc.plot(normalized, norm=\"log\")" ] }, { "cell_type": "markdown", "id": "0fc6d74e-e97e-415d-8d1f-6a6396c0e8eb", "metadata": {}, "source": [ "## Make a $(\\lambda, \\theta)$ map\n", "\n", "A good sanity check is to create a two-dimensional map of the counts in $\\lambda$ and $\\theta$ bins.\n", "To achieve this, we request two output coordinates from the `transform_coords` method." ] }, { "cell_type": "code", "execution_count": null, "id": "30d0d9dd-b4ec-4d57-8314-2c81c9e727d9", "metadata": {}, "outputs": [], "source": [ "sample_theta = sample.transform_coords([\"theta\", \"wavelength\"], graph=graph)\n", "sample_theta" ] }, { "cell_type": "markdown", "id": "d6e51845-09ef-4596-b0b8-593eafddf5f2", "metadata": {}, "source": [ "Then, we concatenate all the events in the `detector_id` dimension" ] }, { "cell_type": "code", "execution_count": null, "id": "69e2de29-2f6c-459e-9521-177a3c0887f5", "metadata": {}, "outputs": [], "source": [ "sample_theta = sample_theta.bins.concatenate('detector_id')\n", "sample_theta" ] }, { "cell_type": "markdown", "id": "417b0c29-6f96-4a19-9828-5dbce9df6124", "metadata": {}, "source": [ "Finally, we bin into the existing `theta` dimension, and into a new `wavelength` dimension,\n", "to create a 2D output" ] }, { "cell_type": "code", "execution_count": null, "id": "466687c7-7603-40a5-ab8d-f3e23ecdab0a", "metadata": {}, "outputs": [], "source": [ "nbins = 165\n", "theta_edges = sc.linspace(dim='theta', start=0.4, stop=1.2, num=nbins, unit='deg')\n", "wavelength_edges = sc.linspace(dim='wavelength', start=1.0, stop=15.0, num=nbins, unit='angstrom')\n", "binned = sc.bin(sample_theta, edges=[sc.to_unit(theta_edges, 'rad'), wavelength_edges])\n", "binned" ] }, { "cell_type": "code", "execution_count": null, "id": "87ba276f-d15f-4f51-aafe-748544d5c65a", "metadata": {}, "outputs": [], "source": [ "binned.bins.sum().plot()" ] }, { "cell_type": "markdown", "id": "b4ccc717-7b2d-491e-b202-3f16f0a825af", "metadata": {}, "source": [ "This plot can be used to check if the value of the sample rotation angle $\\omega$ is correct.\n", "The bright triangles should be pointing back to the origin $\\lambda = \\theta = 0$." ] }, { "cell_type": "markdown", "id": "db1a9cf1-dc47-4b1f-b459-db5b701b558d", "metadata": {}, "source": [ "## References" ] }, { "cell_type": "markdown", "id": "ac7196da-a78d-4f50-b4f5-62e9119bc2b9", "metadata": {}, "source": [ "\n", "Stahn J., Glavic A., **2016**,\n", "*Focusing neutron reflectometry: Implementation and experience on the TOF-reflectometer Amor*,\n", "[Nuclear Instruments and Methods in Physics Research Section A: Accelerators, Spectrometers, Detectors and Associated Equipment, 821, 44-54](https://doi.org/10.1016/j.nima.2016.03.007)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3" } }, "nbformat": 4, "nbformat_minor": 5 }