Commit 29e686cb authored by David Fairbrother's avatar David Fairbrother Committed by Gemma Guest
Browse files

Add monitor handling in PreProcess

Return monitors when loading in the preprocess algorithm.
Re #32125
parent ec80631a
...@@ -5,7 +5,8 @@ ...@@ -5,7 +5,8 @@
# Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS # Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
# SPDX - License - Identifier: GPL - 3.0 + # SPDX - License - Identifier: GPL - 3.0 +
from mantid.api import (AlgorithmFactory, DataProcessorAlgorithm, MatrixWorkspaceProperty, MatrixWorkspace, WorkspaceGroup) from mantid.api import (AlgorithmFactory, DataProcessorAlgorithm, MatrixWorkspaceProperty, MatrixWorkspace,
PropertyMode, WorkspaceGroup)
from mantid.kernel import (CompositeValidator, StringArrayLengthValidator, StringArrayMandatoryValidator, from mantid.kernel import (CompositeValidator, StringArrayLengthValidator, StringArrayMandatoryValidator,
StringArrayProperty, Direction) StringArrayProperty, Direction)
...@@ -14,6 +15,7 @@ class ReflectometryISISPreprocess(DataProcessorAlgorithm): ...@@ -14,6 +15,7 @@ class ReflectometryISISPreprocess(DataProcessorAlgorithm):
_RUNS = 'InputRunList' _RUNS = 'InputRunList'
_GROUP_TOF = 'GroupTOFWorkspaces' _GROUP_TOF = 'GroupTOFWorkspaces'
_OUTPUT_WS = 'OutputWorkspace' _OUTPUT_WS = 'OutputWorkspace'
_MONITOR_WS = 'MonitorWorkspace'
_EVENT_MODE = "EventMode" _EVENT_MODE = "EventMode"
def __init__(self): def __init__(self):
...@@ -46,10 +48,15 @@ class ReflectometryISISPreprocess(DataProcessorAlgorithm): ...@@ -46,10 +48,15 @@ class ReflectometryISISPreprocess(DataProcessorAlgorithm):
MatrixWorkspaceProperty(self._OUTPUT_WS, '', direction=Direction.Output), MatrixWorkspaceProperty(self._OUTPUT_WS, '', direction=Direction.Output),
doc='The preprocessed output workspace. If multiple input runs are specified ' doc='The preprocessed output workspace. If multiple input runs are specified '
'they will be summed into a single output workspace.') 'they will be summed into a single output workspace.')
self.declareProperty(
MatrixWorkspaceProperty(self._MONITOR_WS, '', direction=Direction.Output, optional=PropertyMode.Optional),
doc='The loaded monitors workspace. This is only output in event mode.')
def PyExec(self): def PyExec(self):
workspace = self._loadRun(self.getPropertyValue(self._RUNS)) workspace, monitor_ws = self._loadRun(self.getPropertyValue(self._RUNS))
self.setProperty(self._OUTPUT_WS, workspace) self.setProperty(self._OUTPUT_WS, workspace)
if monitor_ws:
self.setProperty(self._MONITOR_WS, monitor_ws)
@staticmethod @staticmethod
def _get_input_runs_validator(): def _get_input_runs_validator():
...@@ -64,12 +71,12 @@ class ReflectometryISISPreprocess(DataProcessorAlgorithm): ...@@ -64,12 +71,12 @@ class ReflectometryISISPreprocess(DataProcessorAlgorithm):
"""Load a run as an event workspace if slicing is requested, or a histogram """Load a run as an event workspace if slicing is requested, or a histogram
workspace otherwise. Transmission runs are always loaded as histogram workspaces.""" workspace otherwise. Transmission runs are always loaded as histogram workspaces."""
event_mode = self.getProperty(self._EVENT_MODE).value event_mode = self.getProperty(self._EVENT_MODE).value
monitor_ws = None
if event_mode: if event_mode:
alg = self.createChildAlgorithm('LoadEventNexus', Filename=run, LoadMonitors=True) alg = self.createChildAlgorithm('LoadEventNexus', Filename=run, LoadMonitors=True)
alg.execute() alg.execute()
ws = alg.getProperty('OutputWorkspace').value ws = alg.getProperty('OutputWorkspace').value
# TODO monitor_ws = alg.getProperty('MonitorWorkspace').value
# monitors = alg.MonitorWorkspace
self._validate_event_ws(ws) self._validate_event_ws(ws)
self.log().information('Loaded event workspace') self.log().information('Loaded event workspace')
else: else:
...@@ -77,7 +84,7 @@ class ReflectometryISISPreprocess(DataProcessorAlgorithm): ...@@ -77,7 +84,7 @@ class ReflectometryISISPreprocess(DataProcessorAlgorithm):
alg.execute() alg.execute()
ws = alg.getProperty('OutputWorkspace').value ws = alg.getProperty('OutputWorkspace').value
self.log().information('Loaded workspace ') self.log().information('Loaded workspace ')
return ws return ws, monitor_ws
@staticmethod @staticmethod
def _validate_event_ws(workspace): def _validate_event_ws(workspace):
......
...@@ -47,12 +47,33 @@ class ReflectometryISISPreprocessTest(unittest.TestCase): ...@@ -47,12 +47,33 @@ class ReflectometryISISPreprocessTest(unittest.TestCase):
with self.assertRaisesRegex(RuntimeError, "Workspace Groups"): with self.assertRaisesRegex(RuntimeError, "Workspace Groups"):
ReflectometryISISPreprocess._validate_event_ws(ws) ReflectometryISISPreprocess._validate_event_ws(ws)
def _run_test(self, args): def test_monitors_are_not_loaded_in_histo_mode(self):
args = {'InputRunList': '13460',
"EventMode": False,
"OutputWorkspace": "ws"}
output_ws, monitor_ws = self._run_test_with_monitors(args)
self.assertIsInstance(output_ws, MatrixWorkspace)
self.assertIsNone(monitor_ws)
def test_monitors_are_loaded_in_event_mode(self):
args = {'InputRunList': '13460',
"EventMode": True,
"OutputWorkspace": "ws"}
output_ws, monitor_ws = self._run_test_with_monitors(args)
self.assertIsInstance(output_ws, IEventWorkspace)
self.assertIsInstance(monitor_ws, MatrixWorkspace)
def _run_test_with_monitors(self, args):
alg = create_algorithm('ReflectometryISISPreprocess', **args) alg = create_algorithm('ReflectometryISISPreprocess', **args)
alg.setChild(True) alg.setChild(True)
alg.setRethrows(True) alg.setRethrows(True)
alg.execute() alg.execute()
output_ws = alg.getProperty('OutputWorkspace').value output_ws = alg.getProperty('OutputWorkspace').value
monitor_ws = alg.getProperty('MonitorWorkspace').value
return output_ws, monitor_ws
def _run_test(self, args):
output_ws, _ = self._run_test_with_monitors(args)
return output_ws return output_ws
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment