Newer
Older
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
#include "MantidAPI/AnalysisDataService.h"
#include "MantidQtCustomInterfaces/Indirect/CalculatePaalmanPings.h"
#include "MantidQtCustomInterfaces/UserInputValidator.h"
#include "MantidQtMantidWidgets/WorkspaceSelector.h"
#include <QLineEdit>
#include <QList>
#include <QValidator>
#include <QDoubleValidator>
#include <QRegExpValidator>
using namespace Mantid::API;
namespace {
Mantid::Kernel::Logger g_log("CalculatePaalmanPings");
}
namespace MantidQt {
namespace CustomInterfaces {
CalculatePaalmanPings::CalculatePaalmanPings(QWidget *parent)
: CorrectionsTab(parent) {
m_uiForm.setupUi(parent);
connect(m_uiForm.dsSample, SIGNAL(dataReady(const QString &)), this,
SLOT(getBeamWidthFromWorkspace(const QString &)));
QRegExp regex("[A-Za-z0-9\\-\\(\\)]*");
QValidator *formulaValidator = new QRegExpValidator(regex, this);
m_uiForm.leSampleChemicalFormula->setValidator(formulaValidator);
m_uiForm.leCanChemicalFormula->setValidator(formulaValidator);
}
void CalculatePaalmanPings::setup() { doValidation(true); }
void CalculatePaalmanPings::run() {
// Get correct corrections algorithm
QString sampleShape = m_uiForm.cbSampleShape->currentText();
QString algorithmName =
sampleShape.replace(" ", "") + "PaalmanPingsCorrection";
algorithmName = algorithmName.replace(
"Annulus", "Cylinder"); // Use the cylinder algorithm for annulus
API::BatchAlgorithmRunner::AlgorithmRuntimeProps absCorProps;
IAlgorithm_sptr absCorAlgo =
AlgorithmManager::Instance().create(algorithmName.toStdString());
absCorAlgo->initialize();
// Sample details
QString sampleWsName = m_uiForm.dsSample->getCurrentDataName();
MatrixWorkspace_sptr sampleWs =
AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(
sampleWsName.toStdString());
// If not in wavelength then do conversion
Mantid::Kernel::Unit_sptr sampleXUnit = sampleWs->getAxis(0)->unit();
if (sampleXUnit->caption() != "Wavelength") {
g_log.information(
"Sample workspace not in wavelength, need to convert to continue.");
absCorProps["SampleWorkspace"] =
addConvertUnitsStep(sampleWs, "Wavelength");
} else {
absCorProps["SampleWorkspace"] = sampleWsName.toStdString();
}
double sampleNumberDensity = m_uiForm.spSampleNumberDensity->value();
absCorAlgo->setProperty("SampleNumberDensity", sampleNumberDensity);
QString sampleChemicalFormula = m_uiForm.leSampleChemicalFormula->text();
absCorAlgo->setProperty("SampleChemicalFormula",
sampleChemicalFormula.toStdString());
addShapeSpecificSampleOptions(absCorAlgo, sampleShape);
// Can details
bool useCan = m_uiForm.ckUseCan->isChecked();
if (useCan) {
QString canWsName = m_uiForm.dsContainer->getCurrentDataName();
MatrixWorkspace_sptr canWs =
AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(
canWsName.toStdString());
// If not in wavelength then do conversion
Mantid::Kernel::Unit_sptr canXUnit = canWs->getAxis(0)->unit();
if (canXUnit->caption() != "Wavelength") {
g_log.information("Container workspace not in wavelength, need to "
"convert to continue.");
absCorProps["CanWorkspace"] = addConvertUnitsStep(canWs, "Wavelength");
} else {
absCorProps["CanWorkspace"] = canWsName.toStdString();
}
double canNumberDensity = m_uiForm.spCanNumberDensity->value();
absCorAlgo->setProperty("CanNumberDensity", canNumberDensity);
QString canChemicalFormula = m_uiForm.leCanChemicalFormula->text();
absCorAlgo->setProperty("CanChemicalFormula",
canChemicalFormula.toStdString());
addShapeSpecificCanOptions(absCorAlgo, sampleShape);
}
std::string eMode = getEMode(sampleWs);
absCorAlgo->setProperty("EMode", eMode);
if (eMode == "Indirect")
absCorAlgo->setProperty("EFixed", getEFixed(sampleWs));
// Generate workspace names
int nameCutIndex = sampleWsName.lastIndexOf("_");
if (nameCutIndex == -1)
nameCutIndex = sampleWsName.length();
QString correctionType;
switch (m_uiForm.cbSampleShape->currentIndex()) {
case 0:
correctionType = "flt";
break;
case 1:
correctionType = "cyl";
break;
case 2:
correctionType = "ann";
break;
}
sampleWsName.left(nameCutIndex) + "_" + correctionType + "_abs";
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
absCorAlgo->setProperty("OutputWorkspace", outputWsName.toStdString());
// Add corrections algorithm to queue
m_batchAlgoRunner->addAlgorithm(absCorAlgo, absCorProps);
// Add save algorithms if required
bool save = m_uiForm.ckSave->isChecked();
if (save)
addSaveWorkspaceToQueue(outputWsName);
// Run algorithm queue
connect(m_batchAlgoRunner, SIGNAL(batchComplete(bool)), this,
SLOT(absCorComplete(bool)));
m_batchAlgoRunner->executeBatchAsync();
// Set the result workspace for Python script export
m_pythonExportWsName = outputWsName.toStdString();
}
bool CalculatePaalmanPings::validate() { return doValidation(); }
/**
* Does validation on the user input.
*
* @param silent Set to true to avoid creating an error message
* @return True if all user input is valid
*/
bool CalculatePaalmanPings::doValidation(bool silent) {
UserInputValidator uiv;
uiv.checkDataSelectorIsValid("Sample", m_uiForm.dsSample);
// Validate chemical formula
if (uiv.checkFieldIsNotEmpty("Sample Chemical Formula",
m_uiForm.leSampleChemicalFormula,
m_uiForm.valSampleChemicalFormula))
uiv.checkFieldIsValid("Sample Chemical Formula",
m_uiForm.leSampleChemicalFormula,
m_uiForm.valSampleChemicalFormula);
bool useCan = m_uiForm.ckUseCan->isChecked();
if (useCan) {
uiv.checkDataSelectorIsValid("Can", m_uiForm.dsContainer);
// Validate chemical formula
if (uiv.checkFieldIsNotEmpty("Can Chemical Formula",
m_uiForm.leCanChemicalFormula,
m_uiForm.valCanChemicalFormula))
uiv.checkFieldIsValid("Can Chemical Formula",
m_uiForm.leCanChemicalFormula,
m_uiForm.valCanChemicalFormula);
// Ensure sample and container are the same kind of data
QString sampleWsName = m_uiForm.dsSample->getCurrentDataName();
QString sampleType = sampleWsName.right(sampleWsName.length() -
sampleWsName.lastIndexOf("_"));
QString containerWsName = m_uiForm.dsContainer->getCurrentDataName();
QString containerType = containerWsName.right(
containerWsName.length() - containerWsName.lastIndexOf("_"));
g_log.debug() << "Sample type is: " << sampleType.toStdString()
<< std::endl;
g_log.debug() << "Can type is: " << containerType.toStdString()
<< std::endl;
if (containerType != sampleType)
uiv.addErrorMessage(
"Sample and can workspaces must contain the same type of data.");
}
// Show error mssage if needed
if (!uiv.isAllInputValid() && !silent)
emit showMessageBox(uiv.generateErrorMessage());
return uiv.isAllInputValid();
}
/**
* Handles completion of the correction algorithm.
*
* @param error True of the algorithm failed
*/
void CalculatePaalmanPings::absCorComplete(bool error) {
disconnect(m_batchAlgoRunner, SIGNAL(batchComplete(bool)), this,
SLOT(absCorComplete(bool)));
if (error) {
emit showMessageBox("Absorption correction calculation failed.\nSee "
"Results Log for more details.");
return;
}
// Convert the spectrum axis of correction factors to Q
QString sampleWsName = m_uiForm.dsSample->getCurrentDataName();
MatrixWorkspace_sptr sampleWs =
AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(
sampleWsName.toStdString());
WorkspaceGroup_sptr corrections =
AnalysisDataService::Instance().retrieveWS<WorkspaceGroup>(
m_pythonExportWsName);
for (size_t i = 0; i < corrections->size(); i++) {
MatrixWorkspace_sptr factorWs =
boost::dynamic_pointer_cast<MatrixWorkspace>(corrections->getItem(i));
if (!factorWs || !sampleWs)
continue;
std::string eMode = getEMode(sampleWs);
if (eMode == "Indirect") {
API::BatchAlgorithmRunner::AlgorithmRuntimeProps convertSpecProps;
IAlgorithm_sptr convertSpecAlgo =
AlgorithmManager::Instance().create("ConvertSpectrumAxis");
convertSpecAlgo->initialize();
convertSpecAlgo->setProperty("InputWorkspace", factorWs);
convertSpecAlgo->setProperty("OutputWorkspace", factorWs->name());
convertSpecAlgo->setProperty("Target", "ElasticQ");
convertSpecAlgo->setProperty("EMode", "Indirect");
try {
convertSpecAlgo->setProperty("EFixed", getEFixed(factorWs));
} catch (std::runtime_error &) {
}
m_batchAlgoRunner->addAlgorithm(convertSpecAlgo);
}
}
// Run algorithm queue
connect(m_batchAlgoRunner, SIGNAL(batchComplete(bool)), this,
SLOT(postProcessComplete(bool)));
m_batchAlgoRunner->executeBatchAsync();
}
/**
* Handles completion of the post processing algorithms.
*
* @param error True of the algorithm failed
*/
void CalculatePaalmanPings::postProcessComplete(bool error) {
disconnect(m_batchAlgoRunner, SIGNAL(batchComplete(bool)), this,
SLOT(postProcessComplete(bool)));
if (error) {
emit showMessageBox("Correction factor post processing failed.\nSee "
"Results Log for more details.");
return;
}
// Handle Mantid plotting
QString plotType = m_uiForm.cbPlotOutput->currentText();
if (plotType == "Both" || plotType == "Wavelength")
plotSpectrum(QString::fromStdString(m_pythonExportWsName));
if (plotType == "Both" || plotType == "Angle")
plotTimeBin(QString::fromStdString(m_pythonExportWsName));
}
void CalculatePaalmanPings::loadSettings(const QSettings &settings) {
m_uiForm.dsSample->readSettings(settings.group());
m_uiForm.dsContainer->readSettings(settings.group());
}
/**
* Gets the beam width from the instrument parameters on a given workspace
* and update the relevant options on the UI.
*
* @param wsName Name of the workspace
*/
void CalculatePaalmanPings::getBeamWidthFromWorkspace(const QString &wsName) {
auto ws = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(
wsName.toStdString());
if (!ws) {
g_log.warning() << "Failed to find workspace " << wsName.toStdString()
<< std::endl;
return;
}
auto instrument = ws->getInstrument();
const std::string beamWidthParamName = "Workflow.beam-width";
if (instrument->hasParameter(beamWidthParamName)) {
QString beamWidth = QString::fromStdString(
instrument->getStringParameter(beamWidthParamName)[0]);
double beamWidthValue = beamWidth.toDouble();
m_uiForm.spCylBeamWidth->setValue(beamWidthValue);
m_uiForm.spAnnBeamWidth->setValue(beamWidthValue);
}
const std::string beamHeightParamName = "Workflow.beam-height";
if (instrument->hasParameter(beamHeightParamName)) {
QString beamHeight = QString::fromStdString(
instrument->getStringParameter(beamHeightParamName)[0]);
double beamHeightValue = beamHeight.toDouble();
m_uiForm.spCylBeamHeight->setValue(beamHeightValue);
m_uiForm.spAnnBeamHeight->setValue(beamHeightValue);
}
}
/**
* Sets algorithm properties specific to the sample for a given shape.
*
* @param alg Algorithm to set properties of
* @param shape Sample shape
*/
void CalculatePaalmanPings::addShapeSpecificSampleOptions(IAlgorithm_sptr alg,
QString shape) {
if (shape == "FlatPlate") {
double sampleThickness = m_uiForm.spFlatSampleThickness->value();
alg->setProperty("SampleThickness", sampleThickness);
double sampleAngle = m_uiForm.spFlatSampleAngle->value();
alg->setProperty("SampleAngle", sampleAngle);
} else if (shape == "Cylinder") {
alg->setProperty("SampleInnerRadius", 0.0);
double sampleOuterRadius = m_uiForm.spCylSampleOuterRadius->value();
alg->setProperty("SampleOuterRadius", sampleOuterRadius);
double beamWidth = m_uiForm.spCylBeamWidth->value();
alg->setProperty("BeamWidth", beamWidth);
double beamHeight = m_uiForm.spCylBeamHeight->value();
alg->setProperty("BeamHeight", beamHeight);
double stepSize = m_uiForm.spCylStepSize->value();
alg->setProperty("StepSize", stepSize);
} else if (shape == "Annulus") {
double sampleInnerRadius = m_uiForm.spAnnSampleInnerRadius->value();
alg->setProperty("SampleInnerRadius", sampleInnerRadius);
double sampleOuterRadius = m_uiForm.spAnnSampleOuterRadius->value();
alg->setProperty("SampleOuterRadius", sampleOuterRadius);
double beamWidth = m_uiForm.spAnnBeamWidth->value();
alg->setProperty("BeamWidth", beamWidth);
double beamHeight = m_uiForm.spAnnBeamHeight->value();
alg->setProperty("BeamHeight", beamHeight);
double stepSize = m_uiForm.spAnnStepSize->value();
alg->setProperty("StepSize", stepSize);
}
}
/**
* Sets algorithm properties specific to the container for a given shape.
*
* @param alg Algorithm to set properties of
* @param shape Sample shape
*/
void CalculatePaalmanPings::addShapeSpecificCanOptions(IAlgorithm_sptr alg,
QString shape) {
if (shape == "FlatPlate") {
double canFrontThickness = m_uiForm.spFlatCanFrontThickness->value();
alg->setProperty("CanFrontThickness", canFrontThickness);
double canBackThickness = m_uiForm.spFlatCanBackThickness->value();
alg->setProperty("SampleThickness", canBackThickness);
} else if (shape == "Cylinder") {
double canOuterRadius = m_uiForm.spCylCanOuterRadius->value();
alg->setProperty("CanOuterRadius", canOuterRadius);
} else if (shape == "Annulus") {
double canOuterRadius = m_uiForm.spAnnCanOuterRadius->value();
alg->setProperty("CanOuterRadius", canOuterRadius);
}
}
} // namespace CustomInterfaces
} // namespace MantidQt