Mantid
Loading...
Searching...
No Matches
CreatePeaksWorkspace.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
10#include "MantidAPI/Run.h"
15
16namespace Mantid::Algorithms {
17
18// Register the algorithm into the AlgorithmFactory
19DECLARE_ALGORITHM(CreatePeaksWorkspace)
20
21using namespace Mantid::Kernel;
22using namespace Mantid::API;
23using namespace Mantid::DataObjects;
24
28 declareProperty(std::make_unique<WorkspaceProperty<Workspace>>("InstrumentWorkspace", "", Direction::Input,
30 "An optional input workspace containing the default instrument for peaks "
31 "in this workspace.");
32 declareProperty("NumberOfPeaks", 1, "Number of dummy peaks to initially create.");
33 declareProperty(std::make_unique<WorkspaceProperty<IPeaksWorkspace>>("OutputWorkspace", "", Direction::Output),
34 "An output workspace.");
35 // explicit control of output peak workspace tyep
36 // Full: standar peak workspace
37 // Lean: LeanElasticPeakWorkspace
38 const std::vector<std::string> peakworkspaceTypes{"Peak", "LeanElasticPeak"};
39 declareProperty("OutputType", "Peak", std::make_shared<StringListValidator>(peakworkspaceTypes),
40 "Output peak workspace type, default to full peak workspace.");
41}
42
46 Workspace_sptr instWS = getProperty("InstrumentWorkspace");
47 const std::string outputType = getProperty("OutputType");
48 int NumberOfPeaks = getProperty("NumberOfPeaks");
49
50 MultipleExperimentInfos_sptr instMDWS = std::dynamic_pointer_cast<MultipleExperimentInfos>(instWS);
51
53
55 // By default, we generate a PeakWorkspace unless user explicitly
56 // requires a LeanElasticPeakWorkspace
57 if (outputType == "Peak") {
58 out = std::make_shared<PeaksWorkspace>();
59 setProperty("OutputWorkspace", out);
60
61 if (instMDWS != nullptr) {
62 if (instMDWS->getNumExperimentInfo() > 0) {
63 out->setInstrument(instMDWS->getExperimentInfo(0)->getInstrument());
64 out->mutableRun().setGoniometer(instMDWS->getExperimentInfo(0)->run().getGoniometer().getR(), false);
65 } else {
66 throw std::invalid_argument("InstrumentWorkspace has no ExperimentInfo");
67 }
68 } else {
69 ei = std::dynamic_pointer_cast<ExperimentInfo>(instWS);
70 if (ei) {
71 out->setInstrument(ei->getInstrument());
72 out->mutableRun().setGoniometer(ei->run().getGoniometer().getR(), false);
73 }
74 }
75 if (instMDWS || ei) {
76 Progress progress(this, 0.0, 1.0, NumberOfPeaks);
77 // Create some default Peaks
78 for (int i = 0; i < NumberOfPeaks; i++) {
79 out->addPeak(Peak(out->getInstrument(), out->getInstrument()->getDetectorIDs(true)[0], 1.0));
80 progress.report();
81 }
82 }
83 } else if (outputType == "LeanElasticPeak") {
84 // use LeanElasticPeakWorkspace, which means no instrument related info
85 out = std::make_shared<LeanElasticPeaksWorkspace>();
86 setProperty("OutputWorkspace", out);
87
88 if (instMDWS) {
89 if (instMDWS->getNumExperimentInfo() > 0) {
90 ei = std::dynamic_pointer_cast<ExperimentInfo>(instMDWS->getExperimentInfo(0));
91 }
92 } else {
93 ei = std::dynamic_pointer_cast<ExperimentInfo>(instWS);
94 }
95
96 if (ei)
97 out->copyExperimentInfoFrom(ei.get());
98
99 Progress progress(this, 0.0, 1.0, NumberOfPeaks);
100 for (int i = 0; i < NumberOfPeaks; i++) {
101 out->addPeak(LeanElasticPeak());
102 progress.report();
103 }
104 } else {
105 throw std::invalid_argument("OutputType MUST be either Peak or LeanElasticPeak!");
106 }
107 // ALG END
108}
109
110} // namespace Mantid::Algorithms
#define DECLARE_ALGORITHM(classname)
Definition Algorithm.h:538
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
void progress(double p, const std::string &msg="", double estimatedTime=0.0, int progressPrecision=0)
Sends ProgressNotification.
Helper class for reporting progress from algorithms.
Definition Progress.h:25
A property class for workspaces.
void exec() override
Run the algorithm.
void init() override
Initialise the properties.
Structure describing a single-crystal peak.
Structure describing a single-crystal peak.
Definition Peak.h:34
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
std::shared_ptr< IPeaksWorkspace > IPeaksWorkspace_sptr
shared pointer to Mantid::API::IPeaksWorkspace
std::shared_ptr< Workspace > Workspace_sptr
shared pointer to Mantid::API::Workspace
std::shared_ptr< ExperimentInfo > ExperimentInfo_sptr
Shared pointer to ExperimentInfo.
std::shared_ptr< MultipleExperimentInfos > MultipleExperimentInfos_sptr
@ Input
An input workspace.
Definition Property.h:53
@ Output
An output workspace.
Definition Property.h:54