diff --git a/Code/Mantid/Framework/Algorithms/src/AppendSpectra.cpp b/Code/Mantid/Framework/Algorithms/src/AppendSpectra.cpp index 7c37d7c6e2cb40b7cba7b9cf5a7e3cdd90bcd845..4efa279245915d7c63bcb81534a1c786ab5824bd 100644 --- a/Code/Mantid/Framework/Algorithms/src/AppendSpectra.cpp +++ b/Code/Mantid/Framework/Algorithms/src/AppendSpectra.cpp @@ -3,6 +3,7 @@ #include "MantidAPI/WorkspaceValidators.h" #include "MantidAPI/WorkspaceOpOverloads.h" #include "MantidDataObjects/EventWorkspace.h" +#include "MantidKernel/BoundedValidator.h" #include "MantidKernel/SingletonHolder.h" using namespace Mantid::Kernel; @@ -44,6 +45,11 @@ void AppendSpectra::init() { "ValidateInputs", true, "Perform a set of checks that the two input workspaces are compatible."); + declareProperty("Number", 1, + boost::make_shared<BoundedValidator<int>>(1, EMPTY_INT()), + "Append the spectra from InputWorkspace2 multiple times (for " + "MatrixWorkspaces only)"); + declareProperty( new WorkspaceProperty<>("OutputWorkspace", "", Direction::Output), "The name of the output workspace"); @@ -79,10 +85,13 @@ void AppendSpectra::exec() { } const bool mergeLogs = getProperty("MergeLogs"); + const int number = getProperty("Number"); if (event_ws1 && event_ws2) { // Both are event workspaces. Use the special method MatrixWorkspace_sptr output = this->execEvent(); + if (number > 1) + g_log.warning("Number property is ignored for event workspaces"); if (mergeLogs) combineLogs(ws1->run(), ws2->run(), output->mutableRun()); // Set the output workspace @@ -97,6 +106,8 @@ void AppendSpectra::exec() { "Workspace2D's must have the same number of bins."); MatrixWorkspace_sptr output = execWS2D(ws1, ws2); + for (int i = 1; i < number; i++) + output = execWS2D(output, ws2); if (mergeLogs) combineLogs(ws1->run(), ws2->run(), output->mutableRun()); diff --git a/Code/Mantid/Framework/Algorithms/test/AppendSpectraTest.h b/Code/Mantid/Framework/Algorithms/test/AppendSpectraTest.h index e514fc5ee49542ff8577a0ee8c214835b049d1c6..9205239267112cf0e8e673ac8dcb54cb91d3752e 100644 --- a/Code/Mantid/Framework/Algorithms/test/AppendSpectraTest.h +++ b/Code/Mantid/Framework/Algorithms/test/AppendSpectraTest.h @@ -94,6 +94,46 @@ public: TS_ASSERT_EQUALS(output->getDetector(10 + maskBottom)->isMasked(), true); } + //---------------------------------------------------------------------------------------------- + void testExecNumber() + { + setupWS(); + + AppendSpectra alg; + if ( !alg.isInitialized() ) alg.initialize(); + + // Get the two input workspaces for later + MatrixWorkspace_sptr in1 = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>("top"); + MatrixWorkspace_sptr in2 = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>("bottom"); + + // Now it should succeed + TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("InputWorkspace1","top") ); + TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("InputWorkspace2","bottom") ); + TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("OutputWorkspace","top") ); + TS_ASSERT_THROWS_NOTHING( alg.setProperty("Number",2) ); + TS_ASSERT_THROWS_NOTHING( alg.execute() ); + TS_ASSERT( alg.isExecuted() ); + + MatrixWorkspace_const_sptr output; + TS_ASSERT_THROWS_NOTHING( output = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>("top") ); + TS_ASSERT_EQUALS( output->getNumberHistograms(), 40 ); + // Check a few values + TS_ASSERT_EQUALS( output->readX(0)[0], in1->readX(0)[0] ); + TS_ASSERT_EQUALS( output->readX(15)[444], in2->readX(5)[444] ); + TS_ASSERT_EQUALS( output->readX(30)[444], in2->readX(5)[444] ); + TS_ASSERT_EQUALS( output->readY(3)[99], in1->readY(3)[99] ); + TS_ASSERT_EQUALS( output->readE(7)[700], in1->readE(7)[700] ); + TS_ASSERT_EQUALS( output->readY(19)[55], in2->readY(9)[55] ); + TS_ASSERT_EQUALS( output->readE(10)[321], in2->readE(0)[321] ); + TS_ASSERT_EQUALS( output->readY(34)[55], in2->readY(9)[55] ); + TS_ASSERT_EQUALS( output->readE(25)[321], in2->readE(0)[321] ); + // There will be a spectra number clash here so all spectra numbers should + // be reset + TS_ASSERT_EQUALS( output->getAxis(1)->spectraNo(5), 5 ); + TS_ASSERT_EQUALS( output->getAxis(1)->spectraNo(12), 12 ); + TS_ASSERT_EQUALS( output->getAxis(1)->spectraNo(27), 27 ); + } + //---------------------------------------------------------------------------------------------- void testExecMismatchedWorkspaces() { diff --git a/Code/Mantid/docs/source/algorithms/AppendSpectra-v1.rst b/Code/Mantid/docs/source/algorithms/AppendSpectra-v1.rst index ab75a35b7b2ca91aed8384523da651adcd86a077..1b99faa99dfd2c5820c15dda43c6cc6190e94ca5 100644 --- a/Code/Mantid/docs/source/algorithms/AppendSpectra-v1.rst +++ b/Code/Mantid/docs/source/algorithms/AppendSpectra-v1.rst @@ -58,9 +58,9 @@ Usage ws = CreateSampleWorkspace(BankPixelWidth=1) ws2 = CreateSampleWorkspace(BankPixelWidth=2) for wsLoop in [ws,ws2]: - print "Workspace '%s' has %i spectra beforehand" % (wsLoop,wsLoop.getNumberHistograms()) - wsOut = AppendSpectra(ws,ws2) - print "Workspace '%s' has %i spectra after AppendSpectra" % (wsOut,wsOut.getNumberHistograms()) + print "Workspace '%s' has %i spectra beforehand" % (wsLoop, wsLoop.getNumberHistograms()) + wsOut = AppendSpectra(ws, ws2) + print "Workspace '%s' has %i spectra after AppendSpectra" % (wsOut, wsOut.getNumberHistograms()) Output: @@ -71,6 +71,26 @@ Output: Workspace 'ws2' has 8 spectra beforehand Workspace 'wsOut' has 10 spectra after AppendSpectra +**Example: Appending two workspaces** + +.. testcode:: ExAppendSpectra + + ws = CreateSampleWorkspace(BankPixelWidth=1) + ws2 = CreateSampleWorkspace(BankPixelWidth=1) + for wsLoop in [ws,ws2]: + print "Workspace '%s' has %i spectra beforehand" % (wsLoop, wsLoop.getNumberHistograms()) + wsOut = AppendSpectra(ws, ws2, Number=4) + print "Workspace '%s' has %i spectra after AppendSpectra" % (wsOut, wsOut.getNumberHistograms()) + + +Output: + +.. testoutput:: ExAppendSpectra + + Workspace 'ws' has 2 spectra beforehand + Workspace 'ws2' has 2 spectra beforehand + Workspace 'wsOut' has 10 spectra after AppendSpectra + .. categories:: .. sourcelink::