diff --git a/Framework/Algorithms/src/CreatePeaksWorkspace.cpp b/Framework/Algorithms/src/CreatePeaksWorkspace.cpp index e549c6607f9bb0d1a1d545614dd7a3c1a381ca45..fd7683648780dc87654d894bf17313c30adec003 100644 --- a/Framework/Algorithms/src/CreatePeaksWorkspace.cpp +++ b/Framework/Algorithms/src/CreatePeaksWorkspace.cpp @@ -11,6 +11,7 @@ #include "MantidDataObjects/LeanElasticPeaksWorkspace.h" #include "MantidDataObjects/PeaksWorkspace.h" #include "MantidGeometry/Instrument/Goniometer.h" +#include "MantidKernel/ListValidator.h" #include "MantidKernel/System.h" namespace Mantid { @@ -36,12 +37,22 @@ void CreatePeaksWorkspace::init() { declareProperty(std::make_unique<WorkspaceProperty<IPeaksWorkspace>>( "OutputWorkspace", "", Direction::Output), "An output workspace."); + // explicit control of output peak workspace tyep + // Full: standar peak workspace + // Lean: LeanElasticPeakWorkspace + const std::vector<std::string> peakworkspaceTypes{"Peak", "LeanElasticPeak"}; + declareProperty( + "OutputType", "Peak", + std::make_shared<StringListValidator>(peakworkspaceTypes), + "Output peak workspace type, default to full peak workspace."); } /** Execute the algorithm. */ void CreatePeaksWorkspace::exec() { - Workspace_sptr instWS = this->getProperty("InstrumentWorkspace"); + Workspace_sptr instWS = getProperty("InstrumentWorkspace"); + const std::string outputType = getProperty("OutputType"); + int NumberOfPeaks = getProperty("NumberOfPeaks"); MultipleExperimentInfos_sptr instMDWS = std::dynamic_pointer_cast<MultipleExperimentInfos>(instWS); @@ -49,45 +60,53 @@ void CreatePeaksWorkspace::exec() { ExperimentInfo_sptr ei; IPeaksWorkspace_sptr out; - if (instWS) + // By default, we generate a PeakWorkspace unless user explicitly + // requires a LeanElasticPeakWorkspace + if (outputType == "Peak") { out = std::make_shared<PeaksWorkspace>(); - else - out = std::make_shared<LeanElasticPeaksWorkspace>(); - setProperty("OutputWorkspace", out); - int NumberOfPeaks = getProperty("NumberOfPeaks"); + setProperty("OutputWorkspace", out); - if (instMDWS != nullptr) { - if (instMDWS->getNumExperimentInfo() > 0) { - out->setInstrument(instMDWS->getExperimentInfo(0)->getInstrument()); - out->mutableRun().setGoniometer( - instMDWS->getExperimentInfo(0)->run().getGoniometer().getR(), false); + if (instMDWS != nullptr) { + if (instMDWS->getNumExperimentInfo() > 0) { + out->setInstrument(instMDWS->getExperimentInfo(0)->getInstrument()); + out->mutableRun().setGoniometer( + instMDWS->getExperimentInfo(0)->run().getGoniometer().getR(), + false); + } else { + throw std::invalid_argument( + "InstrumentWorkspace has no ExperimentInfo"); + } } else { - throw std::invalid_argument("InstrumentWorkspace has no ExperimentInfo"); + ei = std::dynamic_pointer_cast<ExperimentInfo>(instWS); + if (ei) { + out->setInstrument(ei->getInstrument()); + out->mutableRun().setGoniometer(ei->run().getGoniometer().getR(), + false); + } } - } else { - ei = std::dynamic_pointer_cast<ExperimentInfo>(instWS); - if (ei) { - out->setInstrument(ei->getInstrument()); - out->mutableRun().setGoniometer(ei->run().getGoniometer().getR(), false); + if (instMDWS || ei) { + Progress progress(this, 0.0, 1.0, NumberOfPeaks); + // Create some default Peaks + for (int i = 0; i < NumberOfPeaks; i++) { + out->addPeak(Peak(out->getInstrument(), + out->getInstrument()->getDetectorIDs(true)[0], 1.0)); + progress.report(); + } } - } - - Progress progress(this, 0.0, 1.0, NumberOfPeaks); + } else if (outputType == "LeanElasticPeak") { + // use LeanElasticPeakWorkspace, which means no instrument related info + out = std::make_shared<LeanElasticPeaksWorkspace>(); + setProperty("OutputWorkspace", out); - if (instMDWS || ei) { - // Create some default Peaks - for (int i = 0; i < NumberOfPeaks; i++) { - out->addPeak(Peak(out->getInstrument(), - out->getInstrument()->getDetectorIDs(true)[0], 1.0)); - progress.report(); - } - } else { - // Create some default LeanElasticPeaks + Progress progress(this, 0.0, 1.0, NumberOfPeaks); for (int i = 0; i < NumberOfPeaks; i++) { out->addPeak(LeanElasticPeak()); progress.report(); } + } else { + throw std::invalid_argument("OutputType MUST be either Full or Lean!"); } + // ALG END } } // namespace Algorithms diff --git a/Framework/Algorithms/test/CreatePeaksWorkspaceTest.h b/Framework/Algorithms/test/CreatePeaksWorkspaceTest.h index 81235387015ad4bed229229bc140e3cec5c3f90a..8603a66f27581fbb935a60ac925166a802cd40c5 100644 --- a/Framework/Algorithms/test/CreatePeaksWorkspaceTest.h +++ b/Framework/Algorithms/test/CreatePeaksWorkspaceTest.h @@ -63,7 +63,7 @@ public: AnalysisDataService::Instance().remove(outWSName); } - void test_exec_no_instr() { + void test_exec_leanElasticPeakWorkspace() { // Name of the output workspace. std::string outWSName("CreatePeaksWorkspaceTest_OutputWS2"); @@ -73,6 +73,7 @@ public: TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("OutputWorkspace", outWSName)); TS_ASSERT_THROWS_NOTHING(alg.setProperty("NumberOfPeaks", 13)); + TS_ASSERT_THROWS_NOTHING(alg.setProperty("OutputType", "LeanElasticPeak")); TS_ASSERT_THROWS_NOTHING(alg.execute();) TS_ASSERT(alg.isExecuted()); diff --git a/docs/source/algorithms/CreatePeaksWorkspace-v1.rst b/docs/source/algorithms/CreatePeaksWorkspace-v1.rst index a78790ddbd5cc5239e44ce62d5d8c0bac88fc554..394437e3b9fad83fe5d1333e3edad26ce01062aa 100644 --- a/docs/source/algorithms/CreatePeaksWorkspace-v1.rst +++ b/docs/source/algorithms/CreatePeaksWorkspace-v1.rst @@ -9,8 +9,12 @@ Description ----------- -Create an empty :ref:`PeaksWorkspace <PeaksWorkspace>`. Use -:ref:`algm-LoadIsawPeaks` or :ref:`algm-FindPeaksMD` to +This algorithm can be used to create a: + +- :ref:`PeaksWorkspace <PeaksWorkspace>` (Default, or when `OutputType` is set to `Peak`.) +- :ref:`LeanElasticPeaksWorkspace <LeanElasticPeaksWorkspace>` (when `OutputType` is set to `LeanElasticPeak`) + +Use :ref:`algm-LoadIsawPeaks` or :ref:`algm-FindPeaksMD` to create a peaks workspace with peaks. This workspace can serve as a starting point for modifying the @@ -20,23 +24,32 @@ for example. If the input workspace is a MDWorkspace then the instrument from the first experiment info is used. -If the `InstrumentWorkspace` is not provided then a -:ref:`LeanElasticPeaksWorkspace <LeanElasticPeaksWorkspace>` is -created instead of a :ref:`PeaksWorkspace <PeaksWorkspace>` - Usage ----- +**Example: An empty table, not tied to an instrument** + +.. testcode:: ExEmptyPeaksworkspaceTable + + ws = CreatePeaksWorkspace() + print("Created a {} with {} rows".format(ws.id(), ws.rowCount())) + +Output: + +.. testoutput:: ExEmptyPeaksworkspaceTable + + Created a PeaksWorkspace with 0 rows + **Example: Create an empty LeanElasticPeaksWorkspace, not tied to an instrument** -.. testcode:: ExEmptyTable +.. testcode:: ExEmptyLeanElasticPeaksworkspaceTable - ws = CreatePeaksWorkspace(NumberOfPeaks=0) + ws = CreatePeaksWorkspace(NumberOfPeaks=0, OutputType="LeanElasticPeak") print("Created a {} with {} rows".format(ws.id(), ws.rowCount())) Output: -.. testoutput:: ExEmptyTable +.. testoutput:: ExEmptyLeanElasticPeaksworkspaceTable Created a LeanElasticPeaksWorkspace with 0 rows