Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 33 additions & 5 deletions src/ess/powder/calibration.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@
import scipp as sc
import scipp.constants

from .types import CorrectedDetector, SampleRun
from .types import (
DetectorLtotal,
DetectorTwoTheta,
ElasticCoordTransformGraph,
EmptyDetector,
RunType,
SampleRun,
)


class OutputCalibrationData(Mapping[int, sc.Variable]):
Expand Down Expand Up @@ -93,13 +100,34 @@ def to_cif_format(self) -> sc.DataArray:
)


def detector_two_theta(
detector: EmptyDetector[RunType],
graph: ElasticCoordTransformGraph[RunType],
) -> DetectorTwoTheta[RunType]:
"""Compute the scattering angle (two-theta) for each detector pixel.

Parameters
----------
detector:
Data array with detector positions.
graph:
Coordinate transformation graph for elastic scattering.
"""
return DetectorTwoTheta[RunType](
detector.transform_coords(
"two_theta", graph=graph, keep_intermediate=False
).coords["two_theta"]
)


def assemble_output_calibration(
data: CorrectedDetector[SampleRun],
ltotal: DetectorLtotal[SampleRun],
two_theta: DetectorTwoTheta[SampleRun],
) -> OutputCalibrationData:
"""Construct output calibration data from average pixel positions."""
# Use nanmean because pixels without events have position=NaN.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please restore the comment.

average_l = sc.nanmean(data.coords["Ltotal"])
average_two_theta = sc.nanmean(data.coords["two_theta"])
average_l = sc.nanmean(ltotal)
average_two_theta = sc.nanmean(two_theta)
difc = sc.to_unit(
2
* sc.constants.m_n
Expand All @@ -111,4 +139,4 @@ def assemble_output_calibration(
return OutputCalibrationData({1: difc})


providers = (assemble_output_calibration,)
providers = (detector_two_theta, assemble_output_calibration)
5 changes: 5 additions & 0 deletions src/ess/powder/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

DetectorBankSizes = reduce_t.DetectorBankSizes

DetectorLtotal = tof_t.DetectorLtotal
TofDetector = tof_t.TofDetector
TofMonitor = tof_t.TofMonitor
PulseStrideOffset = tof_t.PulseStrideOffset
Expand Down Expand Up @@ -103,6 +104,10 @@ class WavelengthDetector(sciline.Scope[RunType, sc.DataArray], sc.DataArray):
"""Histogrammed intensity vs d-spacing."""


class DetectorTwoTheta(sciline.Scope[RunType, sc.Variable], sc.Variable):
"""Scattering angle (two-theta) for each detector pixel."""


class ElasticCoordTransformGraph(sciline.Scope[RunType, dict], dict):
"""Graph for transforming coordinates in elastic scattering."""

Expand Down
Loading