From 3880c4d29810824988810275a3ba6e5b115297fd Mon Sep 17 00:00:00 2001 From: Andrei Savici Date: Thu, 26 Dec 2013 16:37:45 -0500 Subject: [PATCH] Finalizing autoreduction algorithms. Refs #8626 --- .../plugins/algorithms/GetEiT0atSNS.py | 86 +++++++++++++++++++ .../python/plugins/algorithms/CMakeLists.txt | 1 + .../plugins/algorithms/CorrectLogTimesTest.py | 9 +- .../plugins/algorithms/GetEiT0atSNSTest.py | 23 +++++ 4 files changed, 116 insertions(+), 3 deletions(-) create mode 100644 Code/Mantid/Framework/PythonInterface/plugins/algorithms/GetEiT0atSNS.py create mode 100644 Code/Mantid/Framework/PythonInterface/test/python/plugins/algorithms/GetEiT0atSNSTest.py diff --git a/Code/Mantid/Framework/PythonInterface/plugins/algorithms/GetEiT0atSNS.py b/Code/Mantid/Framework/PythonInterface/plugins/algorithms/GetEiT0atSNS.py new file mode 100644 index 000000000000..86676aa78157 --- /dev/null +++ b/Code/Mantid/Framework/PythonInterface/plugins/algorithms/GetEiT0atSNS.py @@ -0,0 +1,86 @@ +"""*WIKI* +Get Ei and T0 on ARCS and SEQUOIA instruments. It accounts for the following: +* in the ADARA framework, the monitors are in the first frame. +* SEQUOIA has event based monitors. +* some data aquisition errors will create unphysical monitor IDs. This will be ignored +* when vChTrans is 2, on ARCS and SEQUOIA there is no chopper in the beam (white beam). Will return not a number for both Ei and T0 +*WIKI*""" + +import mantid +import numpy + + +class GetEiT0atSNS(mantid.api.PythonAlgorithm): + def category(self): + """ Return category + """ + return "PythonAlgorithms;Inelastic" + + def name(self): + """ Return name + """ + return "GetEiT0atSNS" + + def PyInit(self): + """ Declare properties + """ + self.declareProperty(mantid.api.WorkspaceProperty("MonitorWorkspace", "",direction=mantid.kernel.Direction.InOut), "Monitor workspace") + self.declareProperty("IncidentEnergyGuess",-1.,doc="Incident energy guess") + self.declareProperty("Ei",0.0,mantid.kernel.Direction.Output) + self.declareProperty("T0",0.0,mantid.kernel.Direction.Output) + return + + def PyExec(self): + """ Main execution body + """ + wm=self.getProperty("MonitorWorkspace").value + i=wm.getInstrument() + + if numpy.mean(wm.getRun()['vChTrans'].value) == 2: + Ei=numpy.nan + Tzero=numpy.nan + else: + EGuess=self.getProperty("IncidentEnergyGuess").value + if EGuess<0: + try: + EGuess=wm.getRun()['EnergyRequest'].getStatistics().mean + except: + raise RuntimeError("No energy guess was given or could be found in sample logs") + try: + #fix more than 2 monitors + sp1=-1 + sp2=-1 + nsp=wm.getNumberHistograms() + if nsp < 2: + raise ValueError("There are less than 2 monitors") + for sp in range(nsp): + if wm.getSpectrum(sp).getDetectorIDs()[0]==-int(i.getNumberParameter('ei-mon1-spec')[0]): + sp1=sp + if wm.getSpectrum(sp).getDetectorIDs()[0]==-int(i.getNumberParameter('ei-mon2-spec')[0]): + sp2=sp + if sp1==-1: + raise RuntimeError("Could not find spectrum for the first monitor") + if sp2==-1: + raise RuntimeError("Could not find spectrum for the second monitor") + #change frame for monitors. ARCS monitors would be in the first frame for Ei>10meV + so=i.getSource().getPos() + m1=wm.getDetector(sp1).getPos() + m2=wm.getDetector(sp2).getPos() + v=437.4*numpy.sqrt(wm.getRun()['EnergyRequest'].getStatistics().mean) + t1=m1.distance(so)*1e6/v + t2=m2.distance(so)*1e6/v + t1f=int(t1*60e-6) #frame number for monitor 1 + t2f=int(t2*60e-6) #frame number for monitor 2 + wtemp=mantid.simpleapi.ChangeBinOffset(wm,t1f*16667,0,0) + wtemp=mantid.simpleapi.ChangeBinOffset(wtemp,t2f*16667,1,1) + wtemp=mantid.simpleapi.Rebin(InputWorkspace=wtemp,Params="1",PreserveEvents=True) + alg=mantid.simpleapi.GetEi(InputWorkspace=wtemp,Monitor1Spec=sp1+1,Monitor2Spec=sp2+1,EnergyEstimate=EGuess) #Run GetEi algorithm + Ei=alg[0] + Tzero=alg[3] #Extract incident energy and T0 + mantid.simpleapi.DeleteWorkspace(wtemp) + except Exception as e: + raise RuntimeError("Could not get Ei, and this is not a white beam run\n"+e.message) + self.setProperty("Ei",Ei) + self.setProperty("T0",Tzero) + +mantid.api.AlgorithmFactory.subscribe(GetEiT0atSNS) diff --git a/Code/Mantid/Framework/PythonInterface/test/python/plugins/algorithms/CMakeLists.txt b/Code/Mantid/Framework/PythonInterface/test/python/plugins/algorithms/CMakeLists.txt index a8a3f3299356..2d563d44bb32 100644 --- a/Code/Mantid/Framework/PythonInterface/test/python/plugins/algorithms/CMakeLists.txt +++ b/Code/Mantid/Framework/PythonInterface/test/python/plugins/algorithms/CMakeLists.txt @@ -12,6 +12,7 @@ set ( TEST_PY_FILES DakotaChiSquaredTest.py FilterLogByTimeTest.py FindReflectometryLinesTest.py + GetEiT0atSNSTest.py LoadFullprofFileTest.py LoadLiveDataTest.py LoadLogPropertyTableTest.py diff --git a/Code/Mantid/Framework/PythonInterface/test/python/plugins/algorithms/CorrectLogTimesTest.py b/Code/Mantid/Framework/PythonInterface/test/python/plugins/algorithms/CorrectLogTimesTest.py index bfef172f442f..1a33b125b19f 100644 --- a/Code/Mantid/Framework/PythonInterface/test/python/plugins/algorithms/CorrectLogTimesTest.py +++ b/Code/Mantid/Framework/PythonInterface/test/python/plugins/algorithms/CorrectLogTimesTest.py @@ -7,7 +7,8 @@ class CorrectLogTimesTest(unittest.TestCase): def testCLTWrongLog(self): - w=Load('CNCS_7860_event.nxs') + w=CreateSingleValuedWorkspace(DataValue='1',ErrorValue='1') + LoadNexusLogs(Workspace=w,Filename='CNCS_7860_event.nxs') try: CorrectLogTimes(Workspace=w,LogNames="s1") @@ -18,7 +19,8 @@ def testCLTWrongLog(self): DeleteWorkspace(w) def testCLTsingle(self): - w=Load('CNCS_7860_event.nxs') + w=CreateSingleValuedWorkspace(DataValue='1',ErrorValue='1') + LoadNexusLogs(Workspace=w,Filename='CNCS_7860_event.nxs') self.assertFalse(w.getRun()['proton_charge'].firstTime()==w.getRun()['Speed4'].firstTime()) CorrectLogTimes(Workspace=w,LogNames="Speed4") self.assertTrue(w.getRun()['proton_charge'].firstTime()==w.getRun()['Speed4'].firstTime()) @@ -26,7 +28,8 @@ def testCLTsingle(self): DeleteWorkspace(w) def testCLTall(self): - w=Load('CNCS_7860_event.nxs') + w=CreateSingleValuedWorkspace(DataValue='1',ErrorValue='1') + LoadNexusLogs(Workspace=w,Filename='CNCS_7860_event.nxs') self.assertFalse(w.getRun()['proton_charge'].firstTime()==w.getRun()['Speed4'].firstTime()) CorrectLogTimes(Workspace=w,LogNames="") self.assertTrue(w.getRun()['proton_charge'].firstTime()==w.getRun()['Speed4'].firstTime()) diff --git a/Code/Mantid/Framework/PythonInterface/test/python/plugins/algorithms/GetEiT0atSNSTest.py b/Code/Mantid/Framework/PythonInterface/test/python/plugins/algorithms/GetEiT0atSNSTest.py new file mode 100644 index 000000000000..4d1680c778eb --- /dev/null +++ b/Code/Mantid/Framework/PythonInterface/test/python/plugins/algorithms/GetEiT0atSNSTest.py @@ -0,0 +1,23 @@ +import unittest +from mantid.simpleapi import * +from mantid.api import * +from testhelpers import * +from numpy import * +from string import * + +class GetEiT0atSNSTest(unittest.TestCase): + + def testGETS(self): + w=Load('ADARAMonitors.nxs') + res=GetEiT0atSNS(w) + self.assertAlmostEqual(res[0],20.118,delta=0.01) + self.assertAlmostEqual(res[1],36.056,delta=0.01) + try: + res=GetEiT0atSNS(w,0.1) + except Exception as e: + s="Could not get Ei, and this is not a white beam run\nNo peak found for the monitor1" + self.assertEquals(find(e.message,s),0) + DeleteWorkspace(w) + +if __name__ == '__main__': + unittest.main()