Skip to content

SXD: Add providers for computing Q histograms#70

Merged
jl-wynen merged 2 commits intomainfrom
bifrost-live-sxd
Oct 9, 2025
Merged

SXD: Add providers for computing Q histograms#70
jl-wynen merged 2 commits intomainfrom
bifrost-live-sxd

Conversation

@jl-wynen
Copy link
Copy Markdown
Member

@jl-wynen jl-wynen commented Oct 8, 2025

This should make the SXD workflow ready for live processing. Simply request the new Q2dHistogram and QHistogram. (See below.)

Once we can select ROI's in the dashboard, we should see if we can adapt this workflow for it.

You can use the workflow by adapting the bragg peak monitor docs notebook:

import scippnexus as snx

from ess.bifrost.data import (
    simulated_elastic_incoherent_with_phonon,
    tof_lookup_table_simulation,
)
from ess.bifrost.single_crystal import BifrostBraggPeakMonitorWorkflow
from ess.bifrost.single_crystal.types import *
from ess.bifrost.types import McStasDetectorData
from ess.reduce.time_of_flight import DetectorLtotal
from ess.spectroscopy.types import *

workflow = BifrostBraggPeakMonitorWorkflow()
workflow[Filename[SampleRun]] = simulated_elastic_incoherent_with_phonon()
workflow[TimeOfFlightLookupTableFilename] = tof_lookup_table_simulation()

# --- New params ---
workflow[QRange] = (sc.scalar(2.7, unit='1/Å'), sc.scalar(2.84, unit='1/Å'))
workflow[SampleRotationBins] = sc.scalar(1, unit='deg')
workflow[QParallelBins] = sc.linspace('Q_parallel', -3, 3, 100, unit='1/Å')
workflow[QPerpendicularBins] = sc.linspace('Q_perpendicular', -3, 3, 100, unit='1/Å')
# ---

# hack to work with existing files:
workflow[NeXusDetectorName] = "309_channel_9_5_triplet"


def assemble_detector_data_flatten(
    detector: CalibratedBeamline[RunType],
    data: NeXusData[snx.NXdetector, RunType],
) -> McStasDetectorData[RunType]:
    from ess.reduce.nexus.workflow import assemble_detector_data as assemble
    base = assemble(detector, data)

    # The monitor only has one pixel, so combine all pixels from the selected bank:
    monitor = base.bins.concat()
    monitor.coords['detector_number'] = sc.index(0)
    # Halfway between the bank and the sample:
    monitor.coords['position'] = base.coords['position'].mean() / 2
    # Zero out the y-component because the monitor is in the sample plane:
    monitor.coords['position'].fields.y = sc.scalar(0.0, unit=monitor.coords['position'].unit)
    return McStasDetectorData[RunType](monitor)


def detector_ltotal_from_straight_line_approximation(
    detector_beamline: DetectorData[RunType],
) -> DetectorLtotal[RunType]:
    from ess.reduce.time_of_flight import eto_to_tof
    return eto_to_tof.detector_ltotal_from_straight_line_approximation(
        detector_beamline  # type: ignore[arg-type]
    )


workflow.insert(assemble_detector_data_flatten)
workflow.insert(detector_ltotal_from_straight_line_approximation)

results = workflow.compute([Q2dHistogram[SampleRun], QHistogram[SampleRun]])

a, b = results.values()

a.plot()  # 2d Histogram
b.plot()  # 1d histogram

@jl-wynen jl-wynen requested a review from SimonHeybrock October 8, 2025 14:38
Comment thread src/ess/bifrost/single_crystal/q_map.py Outdated
Comment on lines +85 to +86
) -> QHistogram[RunType]:
"""Integrate the data over |Q| and histogram in the sample rotation."""
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This seems to be something quite specific, does it deserve a better name? QHistogram does not tell me much about what it is.

Comment thread src/ess/bifrost/single_crystal/q_map.py Outdated
Comment on lines +74 to +76
) -> Q2dHistogram[RunType]:
"""Histogram the data in Q_parallel and Q_perpendicular"""
return Q2dHistogram[RunType](
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Not sure about the naming, is there a better option? If not, feel free to ignore this comment.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I changed the names. What do you think?
Note that the data does not yet represent intensities. But that is what it will ultimately be once we implement normalisation.

Comment thread src/ess/bifrost/single_crystal/types.py Outdated
Comment on lines +30 to +33
class Q2dHistogram(sciline.Scope[RunType, sc.DataArray], sc.DataArray): ...


class QHistogram(sciline.Scope[RunType, sc.DataArray], sc.DataArray): ...
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Docstrings?

Comment thread src/ess/bifrost/single_crystal/q_map.py Outdated
CountsWithQMapCoords,
Q2dHistogram,
QHistogram,
IntensityQparQPerp,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Inconsistent camel-casing.

@jl-wynen jl-wynen merged commit 3e1a906 into main Oct 9, 2025
4 checks passed
@jl-wynen jl-wynen deleted the bifrost-live-sxd branch October 9, 2025 12:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants