-
Notifications
You must be signed in to change notification settings - Fork 122
/
PDDetermineCharacterizations.cpp
472 lines (417 loc) · 17 KB
/
PDDetermineCharacterizations.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
// 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/PDDetermineCharacterizations.h"
#include "MantidAPI/ITableWorkspace.h"
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidAPI/Run.h"
#include "MantidKernel/ArrayProperty.h"
#include "MantidKernel/PropertyManager.h"
#include "MantidKernel/PropertyManagerDataService.h"
#include "MantidKernel/Strings.h"
#include "MantidKernel/TimeSeriesProperty.h"
namespace Mantid {
namespace Algorithms {
using Mantid::API::ITableWorkspace_const_sptr;
using Mantid::API::WorkspaceProperty;
using Mantid::Kernel::ArrayProperty;
using Mantid::Kernel::Direction;
using Mantid::Kernel::PropertyManagerDataService;
using Mantid::Kernel::PropertyWithValue;
using Mantid::Kernel::TimeSeriesProperty;
namespace { // anonymous namespace
const std::string CHAR_PROP_NAME("Characterizations");
const std::string FREQ_PROP_NAME("FrequencyLogNames");
const std::string WL_PROP_NAME("WaveLengthLogNames");
} // namespace
// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(PDDetermineCharacterizations)
/// Algorithms name for identification. @see Algorithm::name
const std::string PDDetermineCharacterizations::name() const {
return "PDDetermineCharacterizations";
}
/// Algorithm's version for identification. @see Algorithm::version
int PDDetermineCharacterizations::version() const { return 1; }
/// Algorithm's category for identification. @see Algorithm::category
const std::string PDDetermineCharacterizations::category() const {
return "Workflow\\Diffraction\\UsesPropertyManager";
}
/// Algorithm's summary for use in the GUI and help. @see Algorithm::summary
const std::string PDDetermineCharacterizations::summary() const {
return "Determines the characterizations of a workspace.";
}
/**
* These should match those in LoadPDCharacterizations
* - "frequency" double
* - "wavelength" (double)
* - "bank" (integer)
* - "container" (string)
* - "vanadium" (string)
* - "vanadium_background" (string)
* - "empty" (string)
* - "d_min" (string)
* - "d_max" (string)
* - "tof_min" (double)
* - "tof_max" (double)
* - "wavelength_min" (double)
* - "wavelength_max" (double)
* @return The list of expected column names
*/
std::vector<std::string> getColumnNames() {
return {"frequency",
"wavelength",
"bank",
"container",
"vanadium",
"vanadium_background",
"empty_environment",
"empty_instrument",
"d_min",
"d_max",
"tof_min",
"tof_max",
"wavelength_min",
"wavelength_max"};
}
/// More intesive input checking. @see Algorithm::validateInputs
std::map<std::string, std::string>
PDDetermineCharacterizations::validateInputs() {
std::map<std::string, std::string> result;
ITableWorkspace_const_sptr characterizations = getProperty(CHAR_PROP_NAME);
if (!bool(characterizations))
return result;
std::vector<std::string> expectedNames = getColumnNames();
std::vector<std::string> names = characterizations->getColumnNames();
if (names.size() < expectedNames.size()) { // allow for extra columns
std::stringstream msg;
msg << "Encountered invalid number of columns in "
<< "TableWorkspace. Found " << names.size() << " expected "
<< expectedNames.size();
result[CHAR_PROP_NAME] = msg.str();
} else {
for (auto &expectedName : expectedNames) {
if (std::find(names.begin(), names.end(), expectedName) == names.end()) {
std::stringstream msg;
msg << "Failed to find column named " << expectedName;
result[CHAR_PROP_NAME] = msg.str();
}
}
}
return result;
}
/// Initialize the algorithm's properties.
void PDDetermineCharacterizations::init() {
declareProperty(
std::make_unique<WorkspaceProperty<>>(
"InputWorkspace", "", Direction::Input, API::PropertyMode::Optional),
"Workspace with logs to help identify frequency and wavelength");
declareProperty(
std::make_unique<WorkspaceProperty<API::ITableWorkspace>>(
CHAR_PROP_NAME, "", Direction::Input, API::PropertyMode::Optional),
"Table of characterization information");
declareProperty("ReductionProperties", "__pd_reduction_properties",
"Property manager name for the reduction");
const std::string defaultMsg =
" run to use. 0 to use value in table, -1 to not use.";
declareProperty(
std::make_unique<Kernel::ArrayProperty<int32_t>>("BackRun", "0"),
"Empty container" + defaultMsg);
declareProperty(
std::make_unique<Kernel::ArrayProperty<int32_t>>("NormRun", "0"),
"Normalization" + defaultMsg);
declareProperty(
std::make_unique<Kernel::ArrayProperty<int32_t>>("NormBackRun", "0"),
"Normalization background" + defaultMsg);
declareProperty(
std::make_unique<Kernel::ArrayProperty<int32_t>>("EmptyEnv", "0"),
"Empty sample environment" + defaultMsg);
declareProperty(
std::make_unique<Kernel::ArrayProperty<int32_t>>("EmptyInstr", "0"),
"Empty instrument" + defaultMsg);
std::vector<std::string> defaultFrequencyNames{"SpeedRequest1", "Speed1",
"frequency", "skf1.speed"};
declareProperty(std::make_unique<Kernel::ArrayProperty<std::string>>(
FREQ_PROP_NAME, std::move(defaultFrequencyNames)),
"Candidate log names for frequency");
std::vector<std::string> defaultWavelengthNames{"LambdaRequest", "lambda",
"skf12.lambda"};
declareProperty(std::make_unique<Kernel::ArrayProperty<std::string>>(
WL_PROP_NAME, std::move(defaultWavelengthNames)),
"Candidate log names for wave length");
}
/**
* Compare two numbers to be in agreement within 5%
* @param left
* @param right
* @return
*/
bool closeEnough(const double left, const double right) {
// the same value
const double diff = fabs(left - right);
if (diff == 0.)
return true;
// same within 5%
const double relativeDiff = diff * 2 / (left + right);
return relativeDiff < .05;
}
/// Fill in the property manager from the correct line in the table
void PDDetermineCharacterizations::getInformationFromTable(
const double frequency, const double wavelength,
const std::string &canName) {
size_t numRows = m_characterizations->rowCount();
for (size_t i = 0; i < numRows; ++i) {
const double rowFrequency =
m_characterizations->getRef<double>("frequency", i);
const double rowWavelength =
m_characterizations->getRef<double>("wavelength", i);
if (closeEnough(frequency, rowFrequency) &&
closeEnough(wavelength, rowWavelength)) {
// declare how the row was chosen
g_log.information() << "Using information from row " << i
<< " with frequency = " << rowFrequency
<< " and wavelength = " << rowWavelength << "\n";
m_propertyManager->setProperty("frequency", frequency);
m_propertyManager->setProperty("wavelength", wavelength);
// what bank number this should be called - only used at POWGEN
m_propertyManager->setProperty(
"bank", m_characterizations->getRef<int>("bank", i));
// data ranges
m_propertyManager->setPropertyValue(
"d_min", m_characterizations->getRef<std::string>("d_min", i));
m_propertyManager->setPropertyValue(
"d_max", m_characterizations->getRef<std::string>("d_max", i));
m_propertyManager->setProperty(
"tof_min", m_characterizations->getRef<double>("tof_min", i));
m_propertyManager->setProperty(
"tof_max", m_characterizations->getRef<double>("tof_max", i));
m_propertyManager->setProperty(
"wavelength_min",
m_characterizations->getRef<double>("wavelength_min", i));
m_propertyManager->setProperty(
"wavelength_max",
m_characterizations->getRef<double>("wavelength_max", i));
// characterization run numbers
m_propertyManager->setProperty(
"vanadium", m_characterizations->getRef<std::string>("vanadium", i));
m_propertyManager->setProperty(
"vanadium_background",
m_characterizations->getRef<std::string>("vanadium_background", i));
m_propertyManager->setProperty(
"container",
m_characterizations->getRef<std::string>("container", i));
m_propertyManager->setProperty(
"empty_environment",
m_characterizations->getRef<std::string>("empty_environment", i));
m_propertyManager->setProperty(
"empty_instrument",
m_characterizations->getRef<std::string>("empty_instrument", i));
// something special if the container was specified
if (!canName.empty()) {
const auto columnNames = m_characterizations->getColumnNames();
if (std::find(columnNames.begin(), columnNames.end(), canName) ==
columnNames.end()) {
g_log.warning() << "Failed to find container name \"" << canName
<< "\" in characterizations table \""
<< m_characterizations->getName()
<< " - using default container value\n";
} else {
const auto canRuns =
m_characterizations->getRef<std::string>(canName, i);
g_log.information()
<< "Updating container identifier to \"" << canRuns << "\"\n";
m_propertyManager->setProperty("container", canRuns);
}
}
return;
}
}
g_log.warning("Failed to find compatible row in characterizations table");
}
/**
* Get a value from one of a set of logs.
* @param run
* @param propName
* @return
*/
double PDDetermineCharacterizations::getLogValue(API::Run &run,
const std::string &propName) {
std::vector<std::string> names = getProperty(propName);
std::string label = "frequency";
if (propName == WL_PROP_NAME)
label = "wavelength";
std::unordered_set<std::string> validUnits;
if (propName == WL_PROP_NAME) {
validUnits.insert("Angstrom");
validUnits.insert("A");
} else {
validUnits.insert("Hz");
}
for (auto &name : names) {
if (run.hasProperty(name)) {
const std::string units = run.getProperty(name)->units();
if (validUnits.find(units) != validUnits.end()) {
double value = run.getLogAsSingleValue(name);
if (value == 0.) {
std::stringstream msg;
msg << "'" << name << "' has a mean value of zero " << units;
g_log.information(msg.str());
} else {
std::stringstream msg;
msg << "Found " << label << " in log '" << name
<< "' with mean value " << value << " " << units;
g_log.information(msg.str());
return value;
}
} else {
std::stringstream msg;
msg << "When looking at " << name << " log encountered unknown units '"
<< units << "' for " << label << ":" << units;
g_log.warning(msg.str());
}
}
}
// generate an exception if it gets here because the log wasn't found
std::stringstream msg;
msg << "Failed to determine " << label << " because none of the logs ";
for (auto &name : names) {
msg << "\"" << name << "\" ";
}
msg << "exist";
throw std::runtime_error(msg.str());
}
/// Set the default values in the property manager
void PDDetermineCharacterizations::setDefaultsInPropManager() {
if (!m_propertyManager->existsProperty("frequency")) {
m_propertyManager->declareProperty(
std::make_unique<PropertyWithValue<double>>("frequency", 0.));
}
if (!m_propertyManager->existsProperty("wavelength")) {
m_propertyManager->declareProperty(
std::make_unique<PropertyWithValue<double>>("wavelength", 0.));
}
if (!m_propertyManager->existsProperty("bank")) {
m_propertyManager->declareProperty(
std::make_unique<PropertyWithValue<int>>("bank", 1));
}
if (!m_propertyManager->existsProperty("d_min")) {
m_propertyManager->declareProperty(
std::make_unique<ArrayProperty<double>>("d_min"));
}
if (!m_propertyManager->existsProperty("d_max")) {
m_propertyManager->declareProperty(
std::make_unique<ArrayProperty<double>>("d_max"));
}
if (!m_propertyManager->existsProperty("tof_min")) {
m_propertyManager->declareProperty(
std::make_unique<PropertyWithValue<double>>("tof_min", 0.));
}
if (!m_propertyManager->existsProperty("tof_max")) {
m_propertyManager->declareProperty(
std::make_unique<PropertyWithValue<double>>("tof_max", 0.));
}
if (!m_propertyManager->existsProperty("wavelength_min")) {
m_propertyManager->declareProperty(
std::make_unique<PropertyWithValue<double>>("wavelength_min", 0.));
}
if (!m_propertyManager->existsProperty("wavelength_max")) {
m_propertyManager->declareProperty(
std::make_unique<PropertyWithValue<double>>("wavelength_max", 0.));
}
if (!m_propertyManager->existsProperty("vanadium")) {
m_propertyManager->declareProperty(
std::make_unique<ArrayProperty<int32_t>>("vanadium", "0"));
}
if (!m_propertyManager->existsProperty("vanadium_background")) {
m_propertyManager->declareProperty(
std::make_unique<ArrayProperty<int32_t>>("vanadium_background", "0"));
}
if (!m_propertyManager->existsProperty("container")) {
m_propertyManager->declareProperty(
std::make_unique<ArrayProperty<int32_t>>("container", "0"));
}
if (!m_propertyManager->existsProperty("empty_environment")) {
m_propertyManager->declareProperty(
std::make_unique<ArrayProperty<int32_t>>("empty_environment", "0"));
}
if (!m_propertyManager->existsProperty("empty_instrument")) {
m_propertyManager->declareProperty(
std::make_unique<ArrayProperty<int32_t>>("empty_instrument", "0"));
}
}
/**
* Set the run number in the property manager from algoritm inputs.
* @param inputName
* @param propName
*/
void PDDetermineCharacterizations::overrideRunNumProperty(
const std::string &inputName, const std::string &propName) {
if (this->isDefault(inputName))
return;
std::vector<int32_t> runnumbers = this->getProperty(inputName);
if ((!runnumbers.empty()) && (runnumbers[0] != 0)) {
if (runnumbers[0] < 0) {
runnumbers.erase(runnumbers.begin(), runnumbers.end());
runnumbers.emplace_back(0);
}
m_propertyManager->setProperty(propName, runnumbers);
}
}
/// Execute the algorithm.
void PDDetermineCharacterizations::exec() {
// setup property manager to return
const std::string managerName = getPropertyValue("ReductionProperties");
if (PropertyManagerDataService::Instance().doesExist(managerName)) {
m_propertyManager =
PropertyManagerDataService::Instance().retrieve(managerName);
} else {
m_propertyManager = std::make_shared<Kernel::PropertyManager>();
PropertyManagerDataService::Instance().addOrReplace(managerName,
m_propertyManager);
}
setDefaultsInPropManager();
m_characterizations = getProperty(CHAR_PROP_NAME);
if (bool(m_characterizations) && (m_characterizations->rowCount() > 0)) {
API::MatrixWorkspace_sptr inputWS = getProperty("InputWorkspace");
auto run = inputWS->mutableRun();
double frequency = getLogValue(run, FREQ_PROP_NAME);
double wavelength = getLogValue(run, WL_PROP_NAME);
// determine the container name
std::string container;
if (run.hasProperty("SampleContainer")) {
const auto containerProp = run.getLogData("SampleContainer");
// the property is normally a TimeSeriesProperty
const auto containerPropSeries =
dynamic_cast<TimeSeriesProperty<std::string> *>(containerProp);
if (containerPropSeries) {
// assume that only the first value matters
container = containerPropSeries->valuesAsVector().front();
} else {
// try as a normal Property
container = containerProp->value();
}
// remove whitespace from the value
container = Kernel::Strings::replaceAll(container, " ", "");
}
getInformationFromTable(frequency, wavelength, container);
}
overrideRunNumProperty("BackRun", "container");
overrideRunNumProperty("NormRun", "vanadium");
overrideRunNumProperty("NormBackRun", "vanadium_background");
overrideRunNumProperty("EmptyEnv", "empty_environment");
overrideRunNumProperty("EmptyInstr", "empty_instrument");
std::vector<std::string> expectedNames = getColumnNames();
for (auto &expectedName : expectedNames) {
if (m_propertyManager->existsProperty(expectedName)) {
g_log.debug() << expectedName << ":"
<< m_propertyManager->getPropertyValue(expectedName)
<< "\n";
} else {
g_log.warning() << expectedName << " DOES NOT EXIST\n";
}
}
}
} // namespace Algorithms
} // namespace Mantid