Mantid
Loading...
Searching...
No Matches
ConvertPeaksWorkspace.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2021 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 +
7
10#include "MantidAPI/Run.h"
11#include "MantidAPI/Sample.h"
17#include "MantidKernel/Logger.h"
18
19namespace Mantid::Crystal {
20
21using namespace Mantid::API;
22using namespace Mantid::DataObjects;
23using namespace Mantid::Geometry;
24using namespace Mantid::Kernel;
25
27namespace {
28Logger logger("ConvertPeaksWorkspace");
29}
30
31DECLARE_ALGORITHM(ConvertPeaksWorkspace)
32
33
38 // Input peakworkspace
39 declareProperty(std::make_unique<WorkspaceProperty<IPeaksWorkspace>>("PeakWorkspace", "", Direction::Input),
40 "Workspace of Indexed Peaks");
41
42 // donor workspace if going from lean to regular
43 declareProperty(std::make_unique<WorkspaceProperty<Workspace>>("InstrumentWorkspace", "", Direction::Input,
45 "Donor Workspace with instrument for conversion");
46
47 // output
48 declareProperty(std::make_unique<WorkspaceProperty<IPeaksWorkspace>>("OutputWorkspace", "", Direction::Output),
49 "Converted Workspaces");
50}
51
57std::map<std::string, std::string> ConvertPeaksWorkspace::validateInputs() {
58 std::map<std::string, std::string> issues;
59
60 IPeaksWorkspace_sptr ipws = getProperty("PeakWorkspace");
61 PeaksWorkspace_sptr pws = std::dynamic_pointer_cast<PeaksWorkspace>(ipws);
62 LeanElasticPeaksWorkspace_sptr lpws = std::dynamic_pointer_cast<LeanElasticPeaksWorkspace>(ipws);
63
64 if (lpws && !pws) {
65 // case I: missing instrument when converting to PeaksWorkspace
66 if (getPointerToProperty("InstrumentWorkspace")->isDefault()) {
67 issues["InstrumentWorkspace"] = "Need a PeaksWorkspace with proper instrument attached to assist conversion.";
68 }
69 // case II: instrument cannot be found within donor workspace
70 else {
71 Workspace_sptr ws = getProperty("InstrumentWorkspace");
72 ExperimentInfo_sptr inputExperimentInfo = std::dynamic_pointer_cast<ExperimentInfo>(ws);
73 if (!inputExperimentInfo) {
74 issues["InstrumentWorkspace"] = "Invalid instrument found in donor workspace.";
75 }
76 }
77 }
78
79 return issues;
80}
81
87 // parsing input
88 IPeaksWorkspace_sptr ipws = getProperty("PeakWorkspace");
89 PeaksWorkspace_sptr pws = std::dynamic_pointer_cast<PeaksWorkspace>(ipws);
90 LeanElasticPeaksWorkspace_sptr lpws = std::dynamic_pointer_cast<LeanElasticPeaksWorkspace>(ipws);
91
92 // decide which route to take
93 if (pws && !lpws) {
94 g_log.notice() << "PeaksWorkspace -> LeanElasticPeaksWorkspace\n";
96 setProperty("OutputWorkspace", outpws);
97 } else {
98 g_log.notice() << "LeanElasticPeaksWorkspace -> PeaksWorkspace\n";
99 Workspace_sptr ws = getProperty("InstrumentWorkspace");
100 IPeaksWorkspace_sptr outpws = makePeaksWorkspace(ipws, ws);
101 setProperty("OutputWorkspace", outpws);
102 }
103
104 // cleanup
105}
106
114 // prep
115 PeaksWorkspace_sptr pws = std::dynamic_pointer_cast<PeaksWorkspace>(ipws);
116 LeanElasticPeaksWorkspace_sptr lpws = std::make_shared<LeanElasticPeaksWorkspace>();
117
118 ExperimentInfo_sptr inputExperimentInfo = std::dynamic_pointer_cast<ExperimentInfo>(ipws);
119 lpws->copyExperimentInfoFrom(inputExperimentInfo.get());
120
121 // down casting Peaks to LeanElasticPeaks
122 for (int i = 0; i < pws->getNumberPeaks(); ++i) {
123 LeanElasticPeak lpk(pws->getPeak(i));
124 lpws->addPeak(lpk);
125 }
126
127 //
128 IPeaksWorkspace_sptr outpws = std::dynamic_pointer_cast<IPeaksWorkspace>(lpws);
129 return outpws;
130}
131
140 const Workspace_sptr &ws) {
141 // prep
142 LeanElasticPeaksWorkspace_sptr lpws = std::dynamic_pointer_cast<LeanElasticPeaksWorkspace>(ipws);
143 PeaksWorkspace_sptr pws = std::make_shared<PeaksWorkspace>();
144 // Instrument_const_sptr inst = ws->getInstrument();
145
146 ExperimentInfo_sptr inputExperimentInfo = std::dynamic_pointer_cast<ExperimentInfo>(ws);
147 pws->copyExperimentInfoFrom(inputExperimentInfo.get());
148 Instrument_const_sptr inst = inputExperimentInfo->getInstrument();
149
150 // up casting LeanElasticPeaks to Peaks
151 for (int i = 0; i < lpws->getNumberPeaks(); ++i) {
152 // NOTE:
153 // This try-catch block here
154 // - For cases where incorrect goniometer settings leads to a negative wavelength.
155 // - Have a slightly negative impact on runtime. For example, with a testing pws with 7k peaks
156 // - with try-catch: runtime ~ 50s
157 // - without try-catch: runtime ~ 38s
158 try {
159 Peak pk(lpws->getPeak(i), inst);
160 pws->addPeak(pk);
161 } catch (const std::invalid_argument &errmsg) {
162 g_log.warning() << errmsg.what() << "\n";
163 }
164 }
165
166 //
167 IPeaksWorkspace_sptr outpws = std::dynamic_pointer_cast<IPeaksWorkspace>(pws);
168 return outpws;
169}
170
171} // namespace Mantid::Crystal
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
Kernel::Property * getPointerToProperty(const std::string &name) const override
Get a property by name.
Definition: Algorithm.cpp:2033
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
Definition: Algorithm.cpp:2076
Kernel::Logger & g_log
Definition: Algorithm.h:451
bool isDefault(const std::string &name) const
Definition: Algorithm.cpp:2084
A property class for workspaces.
ConvertPeaksWorkspace : Bi-directional conversion between a regular PeaksWorkspace (with instrument) ...
Mantid::API::IPeaksWorkspace_sptr makePeaksWorkspace(const Mantid::API::IPeaksWorkspace_sptr &ipws, const Mantid::API::Workspace_sptr &ws)
LeanElasticPeaksWorkspace -> PeaksWorkspace.
void exec() override
Run the algorithm.
Mantid::API::IPeaksWorkspace_sptr makeLeanElasticPeaksWorkspace(const Mantid::API::IPeaksWorkspace_sptr &ipws)
PeaksWorkspace -> LeanElasticPeaksWorkspace.
std::map< std::string, std::string > validateInputs() override
Private validator for inputs.
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.
The Logger class is in charge of the publishing messages from the framework through various channels.
Definition: Logger.h:52
void notice(const std::string &msg)
Logs at notice level.
Definition: Logger.cpp:95
void warning(const std::string &msg)
Logs at warning level.
Definition: Logger.cpp:86
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< LeanElasticPeaksWorkspace > LeanElasticPeaksWorkspace_sptr
Typedef for a shared pointer to a peaks workspace.
std::shared_ptr< PeaksWorkspace > PeaksWorkspace_sptr
Typedef for a shared pointer to a peaks workspace.
std::shared_ptr< const Instrument > Instrument_const_sptr
Shared pointer to an const instrument object.
@ Input
An input workspace.
Definition: Property.h:53
@ Output
An output workspace.
Definition: Property.h:54