-
Notifications
You must be signed in to change notification settings - Fork 122
/
SetInstrumentParameter.cpp
215 lines (192 loc) · 7.89 KB
/
SetInstrumentParameter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#include "MantidAlgorithms/SetInstrumentParameter.h"
#include "MantidAPI/InstrumentValidator.h"
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidDataObjects/PeaksWorkspace.h"
#include "MantidGeometry/Instrument.h"
#include "MantidKernel/ArrayProperty.h"
#include "MantidKernel/ListValidator.h"
#include "MantidKernel/MandatoryValidator.h"
#include "MantidKernel/Strings.h"
#include <boost/algorithm/string.hpp>
namespace Mantid {
namespace Algorithms {
// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(SetInstrumentParameter)
using namespace Kernel;
using namespace Geometry;
using namespace API;
//----------------------------------------------------------------------------------------------
/// Algorithm's name for identification. @see Algorithm::name
const std::string SetInstrumentParameter::name() const {
return "SetInstrumentParameter";
}
/// Algorithm's version for identification. @see Algorithm::version
int SetInstrumentParameter::version() const { return 1; }
/// Algorithm's category for identification. @see Algorithm::category
const std::string SetInstrumentParameter::category() const {
return "DataHandling\\Instrument";
}
//----------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------
/** Initialize the algorithm's properties.
*/
void SetInstrumentParameter::init() {
declareProperty(std::make_unique<WorkspaceProperty<Workspace>>(
"Workspace", "", Direction::InOut,
std::make_shared<InstrumentValidator>()),
"Workspace to add the log entry to");
declareProperty("ComponentName", "",
"The name of the component to attach "
"the parameter to. Default: the whole "
"instrument");
declareProperty(std::make_unique<ArrayProperty<detid_t>>("DetectorList"),
"The detector ID list to attach the parameter to. If set "
"this will override any ComponentName");
declareProperty("ParameterName", "",
std::make_shared<MandatoryValidator<std::string>>(),
"The name that will identify the parameter");
std::vector<std::string> propOptions{"String", "Number", "Bool"};
declareProperty("ParameterType", "String",
std::make_shared<StringListValidator>(propOptions),
"The type that the parameter value will be.");
declareProperty("Value", "", "The content of the Parameter");
}
/// @copydoc Algorithm::validateInputs()
std::map<std::string, std::string> SetInstrumentParameter::validateInputs() {
std::map<std::string, std::string> errors;
const std::set<std::string> allowedBoolValues{"1", "0", "true", "false"};
const std::string type = getPropertyValue("ParameterType");
std::string val = getPropertyValue("Value");
if (type == "Bool") {
boost::algorithm::to_lower(val);
if (allowedBoolValues.find(val) == allowedBoolValues.end()) {
errors["Value"] = "Invalid value for Bool type.";
}
} else if (type == "Number") {
int intVal;
double dblVal;
if (!Strings::convert(val, intVal) && !Strings::convert(val, dblVal)) {
errors["Value"] = "Invalid value for Number type.";
}
}
return errors;
}
//----------------------------------------------------------------------------------------------
/** Execute the algorithm.
*/
void SetInstrumentParameter::exec() {
// A pointer to the workspace to add a log to
Workspace_sptr ws = getProperty("Workspace");
MatrixWorkspace_sptr inputW = std::dynamic_pointer_cast<MatrixWorkspace>(ws);
DataObjects::PeaksWorkspace_sptr inputP =
std::dynamic_pointer_cast<DataObjects::PeaksWorkspace>(ws);
// Get some stuff from the input workspace
Instrument_sptr inst;
if (inputW) {
inst = std::const_pointer_cast<Instrument>(inputW->getInstrument());
if (!inst)
throw std::runtime_error("Could not get a valid instrument from the "
"MatrixWorkspace provided as input");
} else if (inputP) {
inst = std::const_pointer_cast<Instrument>(inputP->getInstrument());
if (!inst)
throw std::runtime_error("Could not get a valid instrument from the "
"PeaksWorkspace provided as input");
} else {
throw std::runtime_error("Could not get a valid instrument from the "
"workspace which does not seem to be valid as "
"input (must be either MatrixWorkspace or "
"PeaksWorkspace");
}
// get the data that the user wants to add
std::string cmptName = getProperty("ComponentName");
const std::vector<detid_t> detectorList = getProperty("DetectorList");
std::string paramName = getProperty("ParameterName");
std::string paramType = getProperty("ParameterType");
std::string paramValue = getPropertyValue("Value");
Strings::strip(cmptName);
Strings::strip(paramName);
Strings::strip(paramValue);
std::vector<IDetector_const_sptr> dets;
std::vector<IComponent_const_sptr> cmptList;
// set default to whole instrument
cmptList.emplace_back(inst);
if (!detectorList.empty()) {
dets = inst->getDetectors(detectorList);
} else if (cmptName.length() > 0) {
// get all matching cmpts
cmptList = inst->getAllComponentsWithName(cmptName);
}
if (inputW) {
auto ¶mMap = inputW->instrumentParameters();
if (!dets.empty()) {
for (auto &det : dets) {
addParameter(paramMap, det.get(), paramName, paramType, paramValue);
}
} else {
if (!cmptList.empty()) {
for (auto &cmpt : cmptList) {
addParameter(paramMap, cmpt.get(), paramName, paramType, paramValue);
}
} else {
g_log.warning("Could not find the component requested.");
}
}
}
if (inputP) {
auto ¶mMap = inputP->instrumentParameters();
if (!dets.empty()) {
for (auto &det : dets) {
addParameter(paramMap, det.get(), paramName, paramType, paramValue);
}
} else {
if (!cmptList.empty()) {
for (auto &cmpt : cmptList) {
addParameter(paramMap, cmpt.get(), paramName, paramType, paramValue);
}
} else {
g_log.warning("Could not find the component requested.");
}
}
}
}
/** Adds a parameter to the component
* @param pmap The parameter map to use
* @param cmptId The component id to add the the parameter to
* @param paramName The parameter name to use
* @param paramType The parameter type
* @param paramValue The parameter value as a string
*/
void SetInstrumentParameter::addParameter(
Mantid::Geometry::ParameterMap &pmap,
const Mantid::Geometry::IComponent *cmptId, const std::string ¶mName,
const std::string ¶mType, const std::string ¶mValue) const {
// remove existing parameters first
pmap.clearParametersByName(paramName, cmptId);
// then add the new one
if (paramType == "String") {
pmap.addString(cmptId, paramName, paramValue);
} else if (paramType == "Number") {
int intVal;
if (Strings::convert(paramValue, intVal)) {
pmap.addInt(cmptId, paramName, intVal);
} else {
double dblVal;
Strings::convert(paramValue, dblVal);
pmap.addDouble(cmptId, paramName, dblVal);
}
} else if (paramType == "Bool") {
std::string paramValueLower(paramValue);
boost::algorithm::to_lower(paramValueLower);
bool paramBoolValue = (paramValueLower == "true" || paramValue == "1");
pmap.addBool(cmptId, paramName, paramBoolValue);
}
}
} // namespace Algorithms
} // namespace Mantid