Algorithm.tcc 6.93 KB
Newer Older
1
2
3
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4
5
//   NScD Oak Ridge National Laboratory, European Spallation Source,
//   Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6
// SPDX - License - Identifier: GPL - 3.0 +
7
#pragma once
8

9
10
11
#include "MantidAPI/Algorithm.h"
#include "MantidAPI/IndexProperty.h"
#include "MantidAPI/WorkspaceProperty.h"
LamarMoore's avatar
LamarMoore committed
12
#include "MantidIndexing/SpectrumIndexSet.h"
13
14
15
16
17
18
19
20
21
22
23
24
25
26

namespace {
template <typename T1, typename T2>
void setWorkspaceProperty(Mantid::API::WorkspaceProperty<T1> *wsProp,
                          const T2 &wksp, const boost::true_type &) {
  *wsProp = wksp;
}

template <typename T1, typename T2>
void setWorkspaceProperty(Mantid::API::WorkspaceProperty<T1> *wsProp,
                          const T2 &wksp, const boost::false_type &) {
  wsProp->setValue(wksp);
}
} // namespace
27
28
29
30
31
32
33
34
35

namespace Mantid {
namespace API {
/** Declare a property which defines the workspace and allowed index types, as
* well as a property for capturing the indices all at once. This method is
* only enabled if T is convertible to MatrixWorkspace.
@param propertyName Name of property which will be reserved
@param allowedIndexTypes combination of allowed index types. Default
IndexType::WorkspaceIndex
36
37
@param wsPropArgs a parameter pack of arguments forwarded to WorkspaceProperty.
Can contain PropertyMode, LockMode, and validators.
38
39
@param doc Property documentation string.
*/
40
41
template <typename T, const int AllowedIndexTypes, typename... WSPropArgs,
          typename>
LamarMoore's avatar
LamarMoore committed
42
void Algorithm::declareWorkspaceInputProperties(const std::string &propertyName,
43
44
                                                const std::string &doc,
                                                WSPropArgs &&... wsPropArgs) {
45
  auto wsProp = std::make_unique<WorkspaceProperty<T>>(
46
47
      propertyName, "", Kernel::Direction::Input,
      std::forward<WSPropArgs>(wsPropArgs)...);
LamarMoore's avatar
LamarMoore committed
48
49
50
  const auto &wsPropRef = *wsProp;
  declareProperty(std::move(wsProp), doc);

51
52
  auto indexTypePropName =
      IndexTypeProperty::generatePropertyName(propertyName);
Sam Jenkins's avatar
Sam Jenkins committed
53
54
  auto indexTypeProp =
      std::make_unique<IndexTypeProperty>(indexTypePropName, AllowedIndexTypes);
LamarMoore's avatar
LamarMoore committed
55
56
  const auto &indexTypePropRef = *indexTypeProp;

57
58
59
  declareProperty(std::move(indexTypeProp),
                  "The type of indices in the optional index set; For optimal "
                  "performance WorkspaceIndex should be preferred;");
LamarMoore's avatar
LamarMoore committed
60

61
  auto indexPropName = IndexProperty::generatePropertyName(propertyName);
62
  declareProperty(std::make_unique<IndexProperty>(indexPropName, wsPropRef,
Sam Jenkins's avatar
Sam Jenkins committed
63
                                                  indexTypePropRef),
64
65
66
67
68
69
                  "An optional set of spectra that will be processed by the "
                  "algorithm; If not set, all spectra will be processed; The "
                  "indices in this list can be workspace indices or possibly "
                  "spectrum numbers, depending on the selection made for the "
                  "index type; Indices are entered as a comma-separated list "
                  "of values, and/or ranges; For example, '4,6,10-20,1000';");
70

71
72
73
  m_reservedList.emplace_back(propertyName);
  m_reservedList.emplace_back(indexTypePropName);
  m_reservedList.emplace_back(indexPropName);
74
75
}

76
template <typename T1, typename T2, typename WsType>
LamarMoore's avatar
LamarMoore committed
77
78
void Algorithm::doSetInputProperties(const std::string &name, const T1 &wksp,
                                     IndexType type, const T2 &list) {
79
  if (!isCompoundProperty(name))
LamarMoore's avatar
LamarMoore committed
80
81
82
83
    throw std::runtime_error(
        "Algorithm::setWorkspaceInputProperties can only be used "
        "with properties declared using "
        "declareWorkspaceInputProperties.");
84

85
  auto *wsProp =
86
      dynamic_cast<WorkspaceProperty<WsType> *>(getPointerToProperty(name));
87
  auto *indexTypeProp = dynamic_cast<IndexTypeProperty *>(
88
      getPointerToProperty(IndexTypeProperty::generatePropertyName(name)));
LamarMoore's avatar
LamarMoore committed
89
  auto *indexProp = dynamic_cast<IndexProperty *>(
90
      getPointerToProperty(IndexProperty::generatePropertyName(name)));
91

92
93
  setWorkspaceProperty<WsType, T1>(
      wsProp, wksp, boost::is_convertible<T1, boost::shared_ptr<WsType>>());
94
95
96
97
98
99
100
101
102
103

  *indexTypeProp = type;

  *indexProp = list;
}

/** Mechanism for setting the index property with a workspace shared pointer.
* This method can only be used if T1 is convertible to a MatrixWorkspace and
* T2 is either std::string or std::vector<int>

104
105
106
107
108
109
@param name Property name
@param wksp Workspace as a pointer
@param type Index type IndexType::WorkspaceIndex or IndexType::SpectrumNum
@param list List of indices to be used.
*/
template <typename T1, typename T2, typename, typename>
LamarMoore's avatar
LamarMoore committed
110
111
112
113
void Algorithm::setWorkspaceInputProperties(const std::string &name,
                                            const boost::shared_ptr<T1> &wksp,
                                            IndexType type, const T2 &list) {
  doSetInputProperties<boost::shared_ptr<T1>, T2, T1>(name, wksp, type, list);
114
115
116
117
}

/** Mechanism for setting the index property with a workspace shared pointer.
* This method can only be used if T1 is convertible to a MatrixWorkspace and
118
* T2 is either std::string or std::vector<int64_t>
119

120
@param name Property name
LamarMoore's avatar
LamarMoore committed
121
@param wsName Workspace name as string
122
123
124
125
@param type Index type IndexType::WorkspaceIndex or IndexType::SpectrumNum
@param list List of indices to be used.
*/
template <typename T1, typename T2, typename, typename>
LamarMoore's avatar
LamarMoore committed
126
127
128
129
void Algorithm::setWorkspaceInputProperties(const std::string &name,
                                            const std::string &wsName,
                                            IndexType type, const T2 &list) {
  doSetInputProperties<std::string, T2, T1>(name, wsName, type, list);
130
131
132
133
134
135
136
137
138
139
}

/** Mechanism for retriving the index property. This method can only be used
if T is convertible to a MatrixWorkspace.

@param name Property name
@returns Tuple containing Workspace shared pointer and SpectrumIndexSet
*/
template <typename T, typename>
std::tuple<boost::shared_ptr<T>, Indexing::SpectrumIndexSet>
LamarMoore's avatar
LamarMoore committed
140
Algorithm::getWorkspaceAndIndices(const std::string &name) const {
141
  if (!isCompoundProperty(name))
LamarMoore's avatar
LamarMoore committed
142
143
144
145
    throw std::runtime_error(
        "Algorithm::getWorkspacesAndIndices can only be used "
        "with properties declared using "
        "declareWorkspaceInputProperties.");
146

LamarMoore's avatar
LamarMoore committed
147
148
149
150
151
152
153
154
155
156
  boost::shared_ptr<T> ws = getProperty(name);

  // Not able to use the regular getProperty mechanism because types, in this
  // case SpectrumIndexSet, need to be known upfront. Since SpectrumIndexSet is
  // not declared at a level where it can be used in Kernel, this will not work.
  // There is an issue which has been created which may solve this and other
  // problems related to the property mechanism in the future:
  // https://github.com/mantidproject/mantid/issues/20092

  auto indexProp = dynamic_cast<IndexProperty *>(
157
      getPointerToProperty(IndexProperty::generatePropertyName(name)));
LamarMoore's avatar
LamarMoore committed
158
  Indexing::SpectrumIndexSet indexSet = *indexProp;
159

LamarMoore's avatar
LamarMoore committed
160
  return std::make_tuple(ws, indexSet);
161
162
}
} // namespace API
163
} // namespace Mantid