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#include "MantidKernel/System.h"
16
17namespace Mantid::Algorithms {
18
19// Register the algorithm into the AlgorithmFactory
20DECLARE_ALGORITHM(CreatePeaksWorkspace)
21
22using namespace Mantid::Kernel;
23using namespace Mantid::API;
24using namespace Mantid::DataObjects;
25
29 declareProperty(std::make_unique<WorkspaceProperty<Workspace>>("InstrumentWorkspace", "", Direction::Input,
31 "An optional input workspace containing the default instrument for peaks "
32 "in this workspace.");
33 declareProperty("NumberOfPeaks", 1, "Number of dummy peaks to initially create.");
34 declareProperty(std::make_unique<WorkspaceProperty<IPeaksWorkspace>>("OutputWorkspace", "", Direction::Output),
35 "An output workspace.");
36 // explicit control of output peak workspace tyep
37 // Full: standar peak workspace
38 // Lean: LeanElasticPeakWorkspace
39 const std::vector<std::string> peakworkspaceTypes{"Peak", "LeanElasticPeak"};
40 declareProperty("OutputType", "Peak", std::make_shared<StringListValidator>(peakworkspaceTypes),
41 "Output peak workspace type, default to full peak workspace.");
42}
43
47 Workspace_sptr instWS = getProperty("InstrumentWorkspace");
48 const std::string outputType = getProperty("OutputType");
49 int NumberOfPeaks = getProperty("NumberOfPeaks");
50
51 MultipleExperimentInfos_sptr instMDWS = std::dynamic_pointer_cast<MultipleExperimentInfos>(instWS);
52
54
56 // By default, we generate a PeakWorkspace unless user explicitly
57 // requires a LeanElasticPeakWorkspace
58 if (outputType == "Peak") {
59 out = std::make_shared<PeaksWorkspace>();
60 setProperty("OutputWorkspace", out);
61
62 if (instMDWS != nullptr) {
63 if (instMDWS->getNumExperimentInfo() > 0) {
64 out->setInstrument(instMDWS->getExperimentInfo(0)->getInstrument());
65 out->mutableRun().setGoniometer(instMDWS->getExperimentInfo(0)->run().getGoniometer().getR(), false);
66 } else {
67 throw std::invalid_argument("InstrumentWorkspace has no ExperimentInfo");
68 }
69 } else {
70 ei = std::dynamic_pointer_cast<ExperimentInfo>(instWS);
71 if (ei) {
72 out->setInstrument(ei->getInstrument());
73 out->mutableRun().setGoniometer(ei->run().getGoniometer().getR(), false);
74 }
75 }
76 if (instMDWS || ei) {
77 Progress progress(this, 0.0, 1.0, NumberOfPeaks);
78 // Create some default Peaks
79 for (int i = 0; i < NumberOfPeaks; i++) {
80 out->addPeak(Peak(out->getInstrument(), out->getInstrument()->getDetectorIDs(true)[0], 1.0));
81 progress.report();
82 }
83 }
84 } else if (outputType == "LeanElasticPeak") {
85 // use LeanElasticPeakWorkspace, which means no instrument related info
86 out = std::make_shared<LeanElasticPeaksWorkspace>();
87 setProperty("OutputWorkspace", out);
88
89 if (instMDWS) {
90 if (instMDWS->getNumExperimentInfo() > 0) {
91 ei = std::dynamic_pointer_cast<ExperimentInfo>(instMDWS->getExperimentInfo(0));
92 }
93 } else {
94 ei = std::dynamic_pointer_cast<ExperimentInfo>(instWS);
95 }
96
97 if (ei)
98 out->copyExperimentInfoFrom(ei.get());
99
100 Progress progress(this, 0.0, 1.0, NumberOfPeaks);
101 for (int i = 0; i < NumberOfPeaks; i++) {
102 out->addPeak(LeanElasticPeak());
103 progress.report();
104 }
105 } else {
106 throw std::invalid_argument("OutputType MUST be either Peak or LeanElasticPeak!");
107 }
108 // ALG END
109}
110
111} // namespace Mantid::Algorithms
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
Definition: Algorithm.cpp:1913
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
Definition: Algorithm.cpp:2076
void progress(double p, const std::string &msg="", double estimatedTime=0.0, int progressPrecision=0)
Sends ProgressNotification.
Definition: Algorithm.cpp:231
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
Definition: Workspace_fwd.h:20
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