15#include <boost/lexical_cast.hpp>
26template <
class Type> Type
lexCast(std::string input,
const std::string &errorMessage) {
28 return boost::lexical_cast<Type>(input);
29 }
catch (boost::bad_lexical_cast &) {
30 throw std::runtime_error(errorMessage + input);
35 if (
index.size() > 1) {
38 if (range.
count() < 1) {
40 }
else if (range.
count() == 1) {
42 start = boost::lexical_cast<double>(range[0]);
43 }
catch (boost::bad_lexical_cast &) {
44 throw std::runtime_error(std::string(
"Provided incorrect range values. Range is "
45 "specfifed by start_value:stop_value, but "
54 std::string errorMessage = std::string(
"Provided incorrect range values. Range is "
55 "specfifed by start_value:stop_value, but "
57 range[0] + std::string(
" and ") + range[1];
58 start = lexCast<double>(range[0], errorMessage);
59 end = lexCast<double>(range[1], errorMessage);
62 std::swap(start, end);
71void addGroupWorkspace(std::vector<InputSpectraToFit> &nameList,
double start,
double end,
int wi,
int spec,
int period,
72 const std::shared_ptr<API::WorkspaceGroup> &wsg);
73void addMatrixworkspace(std::vector<InputSpectraToFit> &nameList,
double start,
double end,
const std::string &
name,
74 int wi,
int spec,
int period,
const std::optional<API::Workspace_sptr> &workspaceOptional,
75 const std::shared_ptr<API::MatrixWorkspace> &wsMatrix);
77std::vector<InputSpectraToFit>
makeNames(
const std::string &inputList,
int default_wi,
int default_spec) {
78 std::vector<InputSpectraToFit> nameList;
85 for (
const auto &input : names) {
87 std::string
name = params[0];
89 int spec = default_spec;
90 if (params.
count() > 1) {
91 std::string
index = params[1];
92 if (
index.size() > 2 &&
index.substr(0, 2) ==
"sp") {
93 spec = boost::lexical_cast<int>(
index.substr(2));
94 wi = SpecialIndex::NOT_SET;
95 }
else if (
index.size() > 1 &&
index[0] ==
'i') {
96 wi = boost::lexical_cast<int>(
index.substr(1));
97 spec = SpecialIndex::NOT_SET;
98 }
else if (!
index.empty() &&
index[0] ==
'v') {
103 if (params.
count() > 2 && !params[2].empty()) {
104 period = lexCast<int>(params[2],
"Incorrect value for a period: " + params[2]);
108 if (!workspaceOptional)
111 auto wsg = std::dynamic_pointer_cast<API::WorkspaceGroup>(workspaceOptional.value());
112 auto wsMatrix = std::dynamic_pointer_cast<API::MatrixWorkspace>(workspaceOptional.value());
116 }
else if (wsMatrix) {
123 int wi,
int spec,
int period,
const std::optional<API::Workspace_sptr> &workspaceOptional,
124 const std::shared_ptr<API::MatrixWorkspace> &wsMatrix) {
127void addGroupWorkspace(std::vector<InputSpectraToFit> &nameList,
double start,
double end,
int wi,
int spec,
int period,
128 const std::shared_ptr<API::WorkspaceGroup> &wsg) {
129 const std::vector<std::string> wsNames = wsg->getNames();
131 for (
const auto &wsName : wsNames) {
133 std::dynamic_pointer_cast<API::MatrixWorkspace>(API::AnalysisDataService::Instance().retrieve(wsName))) {
151 const std::shared_ptr<API::MatrixWorkspace> &ws,
int workspaceIndex,
int spectrumNumber,
152 double start,
double end,
int period,
const bool &workspaceOptional) {
153 auto mws = workspaceOptional ? ws :
nullptr;
154 if (workspaceIndex >= 0) {
155 nameList.emplace_back(
name, workspaceIndex, -1, -1, period, mws);
161 if (spectrumNumber < 0) {
165 if (start < specStart || end > specEnd) {
167 nameList.emplace_back(
"", -1, -1, -1, -1,
nullptr);
171 for (
size_t i = 0; i < axis->
length(); ++i) {
172 double spec = double(axis->
spectraNo(i));
173 int wsIdx =
static_cast<int>(i);
174 if (spec >= start && spec <= end) {
175 nameList.emplace_back(
name, wsIdx, spec, -1, period, mws);
179 for (
size_t i = 0; i < axis->
length(); ++i) {
181 int wsIdx =
static_cast<int>(i);
182 if (j == spectrumNumber) {
183 nameList.emplace_back(
name, wsIdx, j, -1, period, mws);
189 double numericStart = (*axis)(0);
190 double numericEnd = (*axis)(axis->
length() - 1);
192 if (workspaceIndex <= SpecialIndex::WHOLE_RANGE) {
193 start = numericStart;
197 if (start < numericStart || end > numericEnd) {
199 nameList.emplace_back(
"", -1, -1, -1, -1,
nullptr);
203 for (
size_t i = 0; i < axis->
length(); ++i) {
204 double value = (*axis)(i);
205 int wsIdx =
static_cast<int>(i);
207 nameList.emplace_back(
name, wsIdx, -1,
value, period, mws);
213std::optional<API::Workspace_sptr>
getWorkspace(
const std::string &workspaceName,
int period) {
214 if (API::AnalysisDataService::Instance().doesExist(workspaceName)) {
215 return API::AnalysisDataService::Instance().retrieve(workspaceName);
217 std::string::size_type i = workspaceName.find_last_of(
'.');
218 if (i == std::string::npos) {
219 g_log.
warning() <<
"Cannot open file " << workspaceName <<
"\n";
222 auto load = Mantid::API::AlgorithmManager::Instance().createUnmanaged(
"Load");
223 load->setChild(
true);
225 load->setPropertyValue(
"FileName", workspaceName);
226 load->setProperty(
"OutputWorkspace",
"__NotUsed");
227 load->setRethrows(
false);
229 if (load->isExecuted()) {
232 if (std::dynamic_pointer_cast<DataObjects::Workspace2D>(rws)) {
237 std::string propName =
"OUTPUTWORKSPACE_" +
std::to_string(period);
238 if (load->existsProperty(propName)) {
double value
The value of the point.
IPeaksWorkspace_sptr workspace
std::map< DeltaEMode::Type, std::string > index
Class to represent the axis of a workspace.
virtual specnum_t spectraNo(const std::size_t &index) const
Get the spectrum number.
virtual std::size_t length() const =0
Get the length of the axis.
virtual bool isSpectra() const
Returns true is the axis is a Spectra axis.
The Logger class is in charge of the publishing messages from the framework through various channels.
void warning(const std::string &msg)
Logs at warning level.
@ TOK_IGNORE_EMPTY
ignore empty tokens
@ TOK_TRIM
remove leading and trailing whitespace from tokens
std::size_t count() const
Get the total number of tokens.
std::shared_ptr< WorkspaceGroup > WorkspaceGroup_sptr
shared pointer to Mantid::API::WorkspaceGroup
std::shared_ptr< Workspace > Workspace_sptr
shared pointer to Mantid::API::Workspace
Kernel::Logger g_log("DetermineSpinStateOrder")
Type lexCast(std::string input, const std::string &errorMessage)
MANTID_CURVEFITTING_DLL void appendInputSpectraToList(std::vector< InputSpectraToFit > &nameList, const std::string &name, const std::shared_ptr< API::MatrixWorkspace > &ws, int workspaceIndex, int spectrumNumber, double start, double end, int period, const bool &workspaceOptional)
Get a workspace.
void parseValueRange(const std::string &index, double &start, double &end, int &wi, int &spec)
MANTID_CURVEFITTING_DLL std::optional< API::Workspace_sptr > getWorkspace(const std::string &name, int period)
void addGroupWorkspace(std::vector< InputSpectraToFit > &nameList, double start, double end, int wi, int spec, int period, const std::shared_ptr< API::WorkspaceGroup > &wsg)
void addMatrixworkspace(std::vector< InputSpectraToFit > &nameList, double start, double end, const std::string &name, int wi, int spec, int period, const std::optional< API::Workspace_sptr > &workspaceOptional, const std::shared_ptr< API::MatrixWorkspace > &wsMatrix)
MANTID_CURVEFITTING_DLL std::vector< InputSpectraToFit > makeNames(const std::string &inputList, int default_wi, int default_spec)
Create a list of input workspace names.
std::string to_string(const wide_integer< Bits, Signed > &n)