Skip to content
Snippets Groups Projects
Unverified Commit 1c4b1c34 authored by Zhang, Chen's avatar Zhang, Chen Committed by GitHub
Browse files

Merge pull request #30983 from mantidproject/SCD265_DefaultCreatePeaksWorkspaceToPeaksWorkspace

Scd265 default create peaks workspace to peaks workspace
parents fa99886e ba765cb4
No related branches found
No related tags found
No related merge requests found
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#include "MantidDataObjects/LeanElasticPeaksWorkspace.h" #include "MantidDataObjects/LeanElasticPeaksWorkspace.h"
#include "MantidDataObjects/PeaksWorkspace.h" #include "MantidDataObjects/PeaksWorkspace.h"
#include "MantidGeometry/Instrument/Goniometer.h" #include "MantidGeometry/Instrument/Goniometer.h"
#include "MantidKernel/ListValidator.h"
#include "MantidKernel/System.h" #include "MantidKernel/System.h"
namespace Mantid { namespace Mantid {
...@@ -36,12 +37,22 @@ void CreatePeaksWorkspace::init() { ...@@ -36,12 +37,22 @@ void CreatePeaksWorkspace::init() {
declareProperty(std::make_unique<WorkspaceProperty<IPeaksWorkspace>>( declareProperty(std::make_unique<WorkspaceProperty<IPeaksWorkspace>>(
"OutputWorkspace", "", Direction::Output), "OutputWorkspace", "", Direction::Output),
"An output workspace."); "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. /** Execute the algorithm.
*/ */
void CreatePeaksWorkspace::exec() { 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 = MultipleExperimentInfos_sptr instMDWS =
std::dynamic_pointer_cast<MultipleExperimentInfos>(instWS); std::dynamic_pointer_cast<MultipleExperimentInfos>(instWS);
...@@ -49,45 +60,53 @@ void CreatePeaksWorkspace::exec() { ...@@ -49,45 +60,53 @@ void CreatePeaksWorkspace::exec() {
ExperimentInfo_sptr ei; ExperimentInfo_sptr ei;
IPeaksWorkspace_sptr out; 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>(); out = std::make_shared<PeaksWorkspace>();
else setProperty("OutputWorkspace", out);
out = std::make_shared<LeanElasticPeaksWorkspace>();
setProperty("OutputWorkspace", out);
int NumberOfPeaks = getProperty("NumberOfPeaks");
if (instMDWS != nullptr) { if (instMDWS != nullptr) {
if (instMDWS->getNumExperimentInfo() > 0) { if (instMDWS->getNumExperimentInfo() > 0) {
out->setInstrument(instMDWS->getExperimentInfo(0)->getInstrument()); out->setInstrument(instMDWS->getExperimentInfo(0)->getInstrument());
out->mutableRun().setGoniometer( out->mutableRun().setGoniometer(
instMDWS->getExperimentInfo(0)->run().getGoniometer().getR(), false); instMDWS->getExperimentInfo(0)->run().getGoniometer().getR(),
false);
} else {
throw std::invalid_argument(
"InstrumentWorkspace has no ExperimentInfo");
}
} else { } 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 { if (instMDWS || ei) {
ei = std::dynamic_pointer_cast<ExperimentInfo>(instWS); Progress progress(this, 0.0, 1.0, NumberOfPeaks);
if (ei) { // Create some default Peaks
out->setInstrument(ei->getInstrument()); for (int i = 0; i < NumberOfPeaks; i++) {
out->mutableRun().setGoniometer(ei->run().getGoniometer().getR(), false); out->addPeak(Peak(out->getInstrument(),
out->getInstrument()->getDetectorIDs(true)[0], 1.0));
progress.report();
}
} }
} } else if (outputType == "LeanElasticPeak") {
// use LeanElasticPeakWorkspace, which means no instrument related info
Progress progress(this, 0.0, 1.0, NumberOfPeaks); out = std::make_shared<LeanElasticPeaksWorkspace>();
setProperty("OutputWorkspace", out);
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();
}
} else {
// Create some default LeanElasticPeaks
for (int i = 0; i < NumberOfPeaks; i++) { for (int i = 0; i < NumberOfPeaks; i++) {
out->addPeak(LeanElasticPeak()); out->addPeak(LeanElasticPeak());
progress.report(); progress.report();
} }
} else {
throw std::invalid_argument("OutputType MUST be either Full or Lean!");
} }
// ALG END
} }
} // namespace Algorithms } // namespace Algorithms
......
...@@ -63,7 +63,7 @@ public: ...@@ -63,7 +63,7 @@ public:
AnalysisDataService::Instance().remove(outWSName); AnalysisDataService::Instance().remove(outWSName);
} }
void test_exec_no_instr() { void test_exec_leanElasticPeakWorkspace() {
// Name of the output workspace. // Name of the output workspace.
std::string outWSName("CreatePeaksWorkspaceTest_OutputWS2"); std::string outWSName("CreatePeaksWorkspaceTest_OutputWS2");
...@@ -73,6 +73,7 @@ public: ...@@ -73,6 +73,7 @@ public:
TS_ASSERT_THROWS_NOTHING( TS_ASSERT_THROWS_NOTHING(
alg.setPropertyValue("OutputWorkspace", outWSName)); alg.setPropertyValue("OutputWorkspace", outWSName));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("NumberOfPeaks", 13)); TS_ASSERT_THROWS_NOTHING(alg.setProperty("NumberOfPeaks", 13));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("OutputType", "LeanElasticPeak"));
TS_ASSERT_THROWS_NOTHING(alg.execute();) TS_ASSERT_THROWS_NOTHING(alg.execute();)
TS_ASSERT(alg.isExecuted()); TS_ASSERT(alg.isExecuted());
......
...@@ -9,8 +9,12 @@ ...@@ -9,8 +9,12 @@
Description Description
----------- -----------
Create an empty :ref:`PeaksWorkspace <PeaksWorkspace>`. Use This algorithm can be used to create a:
:ref:`algm-LoadIsawPeaks` or :ref:`algm-FindPeaksMD` to
- :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. create a peaks workspace with peaks.
This workspace can serve as a starting point for modifying the This workspace can serve as a starting point for modifying the
...@@ -20,23 +24,32 @@ for example. ...@@ -20,23 +24,32 @@ for example.
If the input workspace is a MDWorkspace then the instrument from the If the input workspace is a MDWorkspace then the instrument from the
first experiment info is used. 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 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** **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())) print("Created a {} with {} rows".format(ws.id(), ws.rowCount()))
Output: Output:
.. testoutput:: ExEmptyTable .. testoutput:: ExEmptyLeanElasticPeaksworkspaceTable
Created a LeanElasticPeaksWorkspace with 0 rows Created a LeanElasticPeaksWorkspace with 0 rows
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment