Mantid
Loading...
Searching...
No Matches
SetInstrumentParameter.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 +
16
17#include <boost/algorithm/string.hpp>
18
19namespace Mantid::Algorithms {
20
21// Register the algorithm into the AlgorithmFactory
22DECLARE_ALGORITHM(SetInstrumentParameter)
23
24using namespace Kernel;
25using namespace Geometry;
26using namespace API;
27
28//----------------------------------------------------------------------------------------------
30const std::string SetInstrumentParameter::name() const { return "SetInstrumentParameter"; }
31
33int SetInstrumentParameter::version() const { return 1; }
34
36const std::string SetInstrumentParameter::category() const { return "DataHandling\\Instrument"; }
37
38//----------------------------------------------------------------------------------------------
39
40//----------------------------------------------------------------------------------------------
44 declareProperty(std::make_unique<WorkspaceProperty<Workspace>>("Workspace", "", Direction::InOut,
45 std::make_shared<InstrumentValidator>()),
46 "Workspace to add the log entry to");
47 declareProperty("ComponentName", "",
48 "The name of the component to attach "
49 "the parameter to. Default: the whole "
50 "instrument");
51 declareProperty(std::make_unique<ArrayProperty<detid_t>>("DetectorList"),
52 "The detector ID list to attach the parameter to. If set "
53 "this will override any ComponentName");
54 declareProperty("ParameterName", "", std::make_shared<MandatoryValidator<std::string>>(),
55 "The name that will identify the parameter");
56
57 std::vector<std::string> propOptions{"String", "Number", "Bool"};
58 declareProperty("ParameterType", "String", std::make_shared<StringListValidator>(propOptions),
59 "The type that the parameter value will be.");
60
61 declareProperty("Value", "", "The content of the Parameter");
62}
63
65std::map<std::string, std::string> SetInstrumentParameter::validateInputs() {
66 std::map<std::string, std::string> errors;
67 const std::set<std::string> allowedBoolValues{"1", "0", "true", "false"};
68 const std::string type = getPropertyValue("ParameterType");
69 std::string val = getPropertyValue("Value");
70
71 if (type == "Bool") {
72 boost::algorithm::to_lower(val);
73 if (allowedBoolValues.find(val) == allowedBoolValues.end()) {
74 errors["Value"] = "Invalid value for Bool type.";
75 }
76 } else if (type == "Number") {
77 int intVal;
78 double dblVal;
79 if (!Strings::convert(val, intVal) && !Strings::convert(val, dblVal)) {
80 errors["Value"] = "Invalid value for Number type.";
81 }
82 }
83
84 return errors;
85}
86
87//----------------------------------------------------------------------------------------------
91 // A pointer to the workspace to add a log to
92 Workspace_sptr ws = getProperty("Workspace");
93 MatrixWorkspace_sptr inputW = std::dynamic_pointer_cast<MatrixWorkspace>(ws);
94 DataObjects::PeaksWorkspace_sptr inputP = std::dynamic_pointer_cast<DataObjects::PeaksWorkspace>(ws);
95 // Get some stuff from the input workspace
96 Instrument_sptr inst;
97 if (inputW) {
98 inst = std::const_pointer_cast<Instrument>(inputW->getInstrument());
99 if (!inst)
100 throw std::runtime_error("Could not get a valid instrument from the "
101 "MatrixWorkspace provided as input");
102 } else if (inputP) {
103 inst = std::const_pointer_cast<Instrument>(inputP->getInstrument());
104 if (!inst)
105 throw std::runtime_error("Could not get a valid instrument from the "
106 "PeaksWorkspace provided as input");
107 } else {
108 throw std::runtime_error("Could not get a valid instrument from the "
109 "workspace which does not seem to be valid as "
110 "input (must be either MatrixWorkspace or "
111 "PeaksWorkspace");
112 }
113
114 // get the data that the user wants to add
115 std::string cmptName = getProperty("ComponentName");
116 const std::vector<detid_t> detectorList = getProperty("DetectorList");
117 std::string paramName = getProperty("ParameterName");
118 std::string paramType = getProperty("ParameterType");
119 std::string paramValue = getPropertyValue("Value");
120
121 Strings::strip(cmptName);
122 Strings::strip(paramName);
123 Strings::strip(paramValue);
124
125 std::vector<IDetector_const_sptr> dets;
126 std::vector<IComponent_const_sptr> cmptList;
127 // set default to whole instrument
128 cmptList.emplace_back(inst);
129
130 if (!detectorList.empty()) {
131 dets = inst->getDetectors(detectorList);
132 } else if (cmptName.length() > 0) {
133 // get all matching cmpts
134 cmptList = inst->getAllComponentsWithName(cmptName);
135 }
136
137 if (inputW) {
138 auto &paramMap = inputW->instrumentParameters();
139 if (!dets.empty()) {
140 for (auto &det : dets) {
141 addParameter(paramMap, det.get(), paramName, paramType, paramValue);
142 }
143 } else {
144 if (!cmptList.empty()) {
145 for (auto &cmpt : cmptList) {
146 addParameter(paramMap, cmpt.get(), paramName, paramType, paramValue);
147 }
148 } else {
149 g_log.warning("Could not find the component requested.");
150 }
151 }
152 }
153 if (inputP) {
154 auto &paramMap = inputP->instrumentParameters();
155 if (!dets.empty()) {
156 for (auto &det : dets) {
157 addParameter(paramMap, det.get(), paramName, paramType, paramValue);
158 }
159 } else {
160 if (!cmptList.empty()) {
161 for (auto &cmpt : cmptList) {
162 addParameter(paramMap, cmpt.get(), paramName, paramType, paramValue);
163 }
164 } else {
165 g_log.warning("Could not find the component requested.");
166 }
167 }
168 }
169}
170
179 const Mantid::Geometry::IComponent *cmptId, const std::string &paramName,
180 const std::string &paramType, const std::string &paramValue) const {
181
182 // remove existing parameters first
183 pmap.clearParametersByName(paramName, cmptId);
184 // then add the new one
185 if (paramType == "String") {
186 pmap.addString(cmptId, paramName, paramValue);
187 } else if (paramType == "Number") {
188 int intVal;
189 if (Strings::convert(paramValue, intVal)) {
190 pmap.addInt(cmptId, paramName, intVal);
191 } else {
192 double dblVal;
193 Strings::convert(paramValue, dblVal);
194 pmap.addDouble(cmptId, paramName, dblVal);
195 }
196 } else if (paramType == "Bool") {
197 std::string paramValueLower(paramValue);
198 boost::algorithm::to_lower(paramValueLower);
199 bool paramBoolValue = (paramValueLower == "true" || paramValue == "1");
200 pmap.addBool(cmptId, paramName, paramBoolValue);
201 }
202}
203
204} // namespace Mantid::Algorithms
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
IntArray detectorList
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
std::string getPropertyValue(const std::string &name) const override
Get the value of a property as a string.
Definition: Algorithm.cpp:2026
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
A property class for workspaces.
void exec() override
Execute the algorithm.
const std::string category() const override
Algorithm's category for identification.
int version() const override
Algorithm's version for identification.
void init() override
Initialize the algorithm's properties.
void addParameter(Mantid::Geometry::ParameterMap &pmap, const Mantid::Geometry::IComponent *cmptId, const std::string &paramName, const std::string &paramType, const std::string &paramValue) const
Adds a parameter to the component.
std::map< std::string, std::string > validateInputs() override
Perform validation of ALL the input properties of the algorithm.
const std::string name() const override
Algorithm's name for identification.
base class for Geometric IComponent
Definition: IComponent.h:51
Support for a property that holds an array of values.
Definition: ArrayProperty.h:28
void warning(const std::string &msg)
Logs at warning level.
Definition: Logger.cpp:86
Validator to check that a property is not left empty.
std::shared_ptr< Workspace > Workspace_sptr
shared pointer to Mantid::API::Workspace
Definition: Workspace_fwd.h:20
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
std::shared_ptr< PeaksWorkspace > PeaksWorkspace_sptr
Typedef for a shared pointer to a peaks workspace.
std::shared_ptr< Instrument > Instrument_sptr
Shared pointer to an instrument object.
MANTID_KERNEL_DLL std::string strip(const std::string &A)
strip pre/post spaces
Definition: Strings.cpp:397
int convert(const std::string &A, T &out)
Convert a string into a number.
Definition: Strings.cpp:665
Generate a tableworkspace to store the calibration results.
@ InOut
Both an input & output workspace.
Definition: Property.h:55