-
Notifications
You must be signed in to change notification settings - Fork 122
/
IndirectDataReduction.cpp
553 lines (450 loc) · 17.1 KB
/
IndirectDataReduction.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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
//----------------------
// Includes
//----------------------
#include "MantidQtCustomInterfaces/Indirect/IndirectDataReduction.h"
#include "MantidAPI/AlgorithmManager.h"
#include "MantidAPI/AnalysisDataService.h"
#include "MantidAPI/ExperimentInfo.h"
#include "MantidKernel/ConfigService.h"
#include "MantidQtAPI/HelpWindow.h"
#include "MantidQtAPI/ManageUserDirectories.h"
#include "MantidQtCustomInterfaces/Indirect/IndirectMoments.h"
#include "MantidQtCustomInterfaces/Indirect/IndirectSqw.h"
#include "MantidQtCustomInterfaces/Indirect/IndirectSymmetrise.h"
#include "MantidQtCustomInterfaces/Indirect/IndirectTransmission.h"
#include "MantidQtCustomInterfaces/Indirect/ISISCalibration.h"
#include "MantidQtCustomInterfaces/Indirect/ISISDiagnostics.h"
#include "MantidQtCustomInterfaces/Indirect/ISISEnergyTransfer.h"
#include <QDesktopServices>
#include <QDir>
#include <QMessageBox>
#include <QUrl>
//Add this class to the list of specialised dialogs in this namespace
namespace MantidQt
{
namespace CustomInterfaces
{
DECLARE_SUBWINDOW(IndirectDataReduction);
}
}
namespace
{
Mantid::Kernel::Logger g_log("IndirectDataReduction");
}
using namespace Mantid::API;
using namespace Mantid::Geometry;
using namespace MantidQt::CustomInterfaces;
using namespace MantidQt;
/**
* Default constructor for class. Initialises interface pointers to NULL values.
* @param parent :: This is a pointer to the "parent" object in Qt, most likely the main MantidPlot window.
*/
IndirectDataReduction::IndirectDataReduction(QWidget *parent) :
UserSubWindow(parent),
m_instrument(""),
m_settingsGroup("CustomInterfaces/IndirectDataReduction"),
m_algRunner(new MantidQt::API::AlgorithmRunner(this)),
m_changeObserver(*this, &IndirectDataReduction::handleConfigChange)
{
// Signals to report load instrument algo result
connect(m_algRunner, SIGNAL(algorithmComplete(bool)), this, SLOT(instrumentLoadingDone(bool)));
Mantid::Kernel::ConfigService::Instance().addObserver(m_changeObserver);
}
/**
* Destructor
*/
IndirectDataReduction::~IndirectDataReduction()
{
Mantid::Kernel::ConfigService::Instance().removeObserver(m_changeObserver);
// Make sure no algos are running after the window has been closed
m_algRunner->cancelRunningAlgorithm();
saveSettings();
}
/**
* On user clicking the "help" button on the interface, directs their request to the relevant
* interface's helpClicked() function.
*/
void IndirectDataReduction::helpClicked()
{
MantidQt::API::HelpWindow::showCustomInterface(NULL, QString("Indirect_DataReduction"));
}
/**
* Called when the user clicks the Python export button.
*/
void IndirectDataReduction::exportTabPython()
{
QString tabName = m_uiForm.twIDRTabs->tabText(m_uiForm.twIDRTabs->currentIndex());
m_tabs[tabName].second->exportPythonScript();
}
/**
* This is the function called when the "Run" button is clicked. It will call the relevent function
* in the subclass.
*/
void IndirectDataReduction::runClicked()
{
QString tabName = m_uiForm.twIDRTabs->tabText(m_uiForm.twIDRTabs->currentIndex());
m_tabs[tabName].second->runTab();
}
/**
* Sets up Qt UI file and connects signals, slots.
*/
void IndirectDataReduction::initLayout()
{
m_uiForm.setupUi(this);
// Do not allow running until setup and instrument laoding are done
updateRunButton(false, "Loading UI", "Initialising user interface components...");
// Create the tabs
addTab<ISISEnergyTransfer>("ISIS Energy Transfer");
addTab<ISISCalibration>("ISIS Calibration");
addTab<ISISDiagnostics>("ISIS Diagnostics");
addTab<IndirectTransmission>("Transmission");
addTab<IndirectSymmetrise>("Symmetrise");
addTab<IndirectSqw>("S(Q, w)");
addTab<IndirectMoments>("Moments");
// Connect "?" (Help) Button
connect(m_uiForm.pbHelp, SIGNAL(clicked()), this, SLOT(helpClicked()));
// Connect the Python export buton
connect(m_uiForm.pbPythonExport, SIGNAL(clicked()), this, SLOT(exportTabPython()));
// Connect the "Run" button
connect(m_uiForm.pbRun, SIGNAL(clicked()), this, SLOT(runClicked()));
// Connect the "Manage User Directories" Button
connect(m_uiForm.pbManageDirectories, SIGNAL(clicked()), this, SLOT(openDirectoryDialog()));
// Reset the Run button state when the tab is changed
connect(m_uiForm.twIDRTabs, SIGNAL(currentChanged(int)), this, SLOT(updateRunButton()));
// Handle instrument configuration changes
connect(m_uiForm.iicInstrumentConfiguration, SIGNAL(instrumentConfigurationUpdated(const QString &, const QString &, const QString &)),
this, SLOT(instrumentSetupChanged(const QString &, const QString &, const QString &)));
// Update the instrument configuration across the UI
m_uiForm.iicInstrumentConfiguration->newInstrumentConfiguration();
std::string facility = Mantid::Kernel::ConfigService::Instance().getString("default.facility");
filterUiForFacility(QString::fromStdString(facility));
emit newInstrumentConfiguration();
}
/**
* This function is ran after initLayout(), and runPythonCode is unavailable before this function
* has run (because of the setup of the base class). For this reason, "setup" functions that require
* Python scripts are located here.
*/
void IndirectDataReduction::initLocalPython()
{
// select starting instrument
readSettings();
}
/**
* Called when any of the instrument configuration options are changed.
*
* Used to notify tabs that rely on the instrument config when the config changes.
*
* @param instrumentName Name of selected instrument
* @param analyser Name of selected analyser bank
* @param reflection Name of selected reflection mode
*/
void IndirectDataReduction::instrumentSetupChanged(const QString & instrumentName, const QString & analyser,
const QString & reflection)
{
m_instWorkspace = loadInstrumentIfNotExist(instrumentName.toStdString(), analyser.toStdString(), reflection.toStdString());
instrumentLoadingDone(m_instWorkspace == NULL);
if(m_instWorkspace != NULL)
emit newInstrumentConfiguration();
}
/**
* Loads an empty instrument into a workspace and returns a pointer to it.
*
* If an analyser and reflection are supplied then the corresponding IPF is also loaded.
* The workspace is not stored in ADS.
*
* @param instrumentName Name of the instrument to load
* @param analyser Analyser being used (optional)
* @param reflection Relection being used (optional)
* @returns Pointer to instrument workspace
*/
Mantid::API::MatrixWorkspace_sptr IndirectDataReduction::loadInstrumentIfNotExist(std::string instrumentName,
std::string analyser, std::string reflection)
{
std::string idfDirectory = Mantid::Kernel::ConfigService::Instance().getString("instrumentDefinition.directory");
try
{
std::string parameterFilename = idfDirectory + instrumentName + "_Definition.xml";
IAlgorithm_sptr loadAlg = AlgorithmManager::Instance().create("LoadEmptyInstrument");
loadAlg->setChild(true);
loadAlg->initialize();
loadAlg->setProperty("Filename", parameterFilename);
loadAlg->setProperty("OutputWorkspace", "__IDR_Inst");
loadAlg->execute();
MatrixWorkspace_sptr instWorkspace = loadAlg->getProperty("OutputWorkspace");
// Load the IPF if given an analyser and reflection
if(!analyser.empty() && !reflection.empty())
{
std::string ipfFilename = idfDirectory + instrumentName + "_" + analyser + "_" + reflection + "_Parameters.xml";
IAlgorithm_sptr loadParamAlg = AlgorithmManager::Instance().create("LoadParameterFile");
loadParamAlg->setChild(true);
loadParamAlg->initialize();
loadParamAlg->setProperty("Filename", ipfFilename);
loadParamAlg->setProperty("Workspace", instWorkspace);
loadParamAlg->execute();
}
return instWorkspace;
}
catch(std::exception &ex)
{
g_log.error() << "Failed to load instrument with error: "
<< ex.what() << std::endl;
return NULL;
}
}
/**
* Gets details for the current instrument configuration.
*
* @return Map of information ID to value
*/
std::map<QString, QString> IndirectDataReduction::getInstrumentDetails()
{
std::map<QString, QString> instDetails;
std::string instrumentName = m_uiForm.iicInstrumentConfiguration->getInstrumentName().toStdString();
std::string analyser = m_uiForm.iicInstrumentConfiguration->getAnalyserName().toStdString();
std::string reflection = m_uiForm.iicInstrumentConfiguration->getReflectionName().toStdString();
instDetails["instrument"] = QString::fromStdString(instrumentName);
instDetails["analyser"] = QString::fromStdString(analyser);
instDetails["reflection"] = QString::fromStdString(reflection);
// List of values to get from IPF
std::vector<std::string> ipfElements;
ipfElements.push_back("analysis-type");
ipfElements.push_back("spectra-min");
ipfElements.push_back("spectra-max");
ipfElements.push_back("efixed-val");
ipfElements.push_back("peak-start");
ipfElements.push_back("peak-end");
ipfElements.push_back("back-start");
ipfElements.push_back("back-end");
ipfElements.push_back("rebin-default");
ipfElements.push_back("cm-1-convert-choice");
ipfElements.push_back("save-ascii-choice");
ipfElements.push_back("fold-frames-choice");
// In the IRIS IPF there is no fmica component
if(instrumentName == "IRIS" && analyser == "fmica")
analyser = "mica";
if(m_instWorkspace == NULL)
return instDetails;
// Get the instrument
auto instrument = m_instWorkspace->getInstrument();
if(instrument == NULL)
return instDetails;
// Get the analyser component
auto component = instrument->getComponentByName(analyser);
// For each parameter we want to get
for(auto it = ipfElements.begin(); it != ipfElements.end(); ++it)
{
try
{
std::string key = *it;
QString value = getInstrumentParameterFrom(instrument, key);
if(value.isEmpty() && component != NULL)
QString value = getInstrumentParameterFrom(component, key);
instDetails[QString::fromStdString(key)] = value;
}
// In the case that the parameter does not exist
catch(Mantid::Kernel::Exception::NotFoundError &nfe)
{
UNUSED_ARG(nfe);
g_log.warning() << "Could not find parameter " << *it
<< " in instrument " << instrumentName
<< std::endl;
}
}
return instDetails;
}
/**
* Gets a parameter from an instrument component as a string.
*
* @param comp Instrument component
* @param param Parameter name
* @return Value as QString
*/
QString IndirectDataReduction::getInstrumentParameterFrom(Mantid::Geometry::IComponent_const_sptr comp, std::string param)
{
QString value;
if(!comp->hasParameter(param))
return "";
// Determine it's type and call the corresponding get function
std::string paramType = comp->getParameterType(param);
if(paramType == "string")
value = QString::fromStdString(comp->getStringParameter(param)[0]);
if(paramType == "double")
value = QString::number(comp->getNumberParameter(param)[0]);
return value;
}
/**
* Tasks to be carried out after an empty instument has finished loading
*/
void IndirectDataReduction::instrumentLoadingDone(bool error)
{
if(error)
{
g_log.error("Instument loading failed!");
updateRunButton(false, "No Instrument", "No instrument is currently loaded.");
return;
}
updateRunButton();
}
/**
* Remove the Poco observer on the config service when the interfaces is closed.
*
* @param close CLose event (unused)
*/
void IndirectDataReduction::closeEvent(QCloseEvent* close)
{
UNUSED_ARG(close);
Mantid::Kernel::ConfigService::Instance().removeObserver(m_changeObserver);
}
/**
* Handles configuration values being changed.
*
* Currently checks for data search paths and default facility.
*
* @param pNf Poco notification
*/
void IndirectDataReduction::handleConfigChange(Mantid::Kernel::ConfigValChangeNotification_ptr pNf)
{
std::string key = pNf->key();
std::string value = pNf->curValue();
if(key == "datasearch.directories" || key == "defaultsave.directory")
{
readSettings();
}
else if(key == "default.facility")
{
QString facility = QString::fromStdString(value);
m_uiForm.iicInstrumentConfiguration->setFacility(facility);
filterUiForFacility(facility);
}
}
/**
* Read Qt settings for the interface.
*/
void IndirectDataReduction::readSettings()
{
// Set values of m_dataDir and m_saveDir
m_dataDir = QString::fromStdString(Mantid::Kernel::ConfigService::Instance().getString("datasearch.directories"));
m_dataDir.replace(" ", "");
if(m_dataDir.length() > 0)
m_dataDir = m_dataDir.split(";", QString::SkipEmptyParts)[0];
m_saveDir = QString::fromStdString(Mantid::Kernel::ConfigService::Instance().getString("defaultsave.directory"));
QSettings settings;
// Load settings for MWRunFile widgets
//TODO
/* settings.beginGroup(m_settingsGroup + "DataFiles"); */
/* settings.setValue("last_directory", m_dataDir); */
/* m_uiForm.ind_runFiles->readSettings(settings.group()); */
/* m_uiForm.cal_leRunNo->readSettings(settings.group()); */
/* m_uiForm.slice_inputFile->readSettings(settings.group()); */
/* settings.endGroup(); */
/* settings.beginGroup(m_settingsGroup + "ProcessedFiles"); */
/* settings.setValue("last_directory", m_saveDir); */
/* m_uiForm.ind_calibFile->readSettings(settings.group()); */
/* m_uiForm.ind_mapFile->readSettings(settings.group()); */
/* m_uiForm.slice_dsCalibFile->readSettings(settings.group()); */
/* m_uiForm.moment_dsInput->readSettings(settings.group()); */
/* m_uiForm.sqw_dsSampleInput->readSettings(settings.group()); */
/* settings.endGroup(); */
// Load the last used instrument
settings.beginGroup(m_settingsGroup);
QString instrumentName = settings.value("instrument-name", "").toString();
if(!instrumentName.isEmpty())
m_uiForm.iicInstrumentConfiguration->setInstrument(instrumentName);
QString analyserName = settings.value("analyser-name", "").toString();
if(!analyserName.isEmpty())
m_uiForm.iicInstrumentConfiguration->setAnalyser(analyserName);
QString reflectionName = settings.value("reflection-name", "").toString();
if(!reflectionName.isEmpty())
m_uiForm.iicInstrumentConfiguration->setReflection(reflectionName);
settings.endGroup();
}
/**
* Save settings to a persistent storage.
*/
void IndirectDataReduction::saveSettings()
{
QSettings settings;
settings.beginGroup(m_settingsGroup);
QString instrumentName = m_uiForm.iicInstrumentConfiguration->getInstrumentName();
settings.setValue("instrument-name", instrumentName);
QString analyserName = m_uiForm.iicInstrumentConfiguration->getAnalyserName();
settings.setValue("analyser-name", analyserName);
QString reflectionName = m_uiForm.iicInstrumentConfiguration->getReflectionName();
settings.setValue("reflection-name", reflectionName);
settings.endGroup();
}
/**
* Filters the displayed tabs based on the current facility.
*
* @param facility Name of facility
*/
void IndirectDataReduction::filterUiForFacility(QString facility)
{
g_log.information() << "Facility selected: "
<< facility.toStdString()
<< std::endl;
QStringList enabledTabs;
// Add facility specific tabs
if(facility == "ISIS")
enabledTabs << "ISIS Energy Transfer"
<< "ISIS Calibration"
<< "ISIS Diagnostics";
// These tabs work at any facility (always at end of tabs)
enabledTabs << "Transmission" << "Symmetrise" << "S(Q, w)" << "Moments";
// First remove all tabs
while(m_uiForm.twIDRTabs->count() > 0)
{
// Disconnect the instrument changed signal
QString tabName = m_uiForm.twIDRTabs->tabText(0);
disconnect(this, SIGNAL(newInstrumentConfiguration()),
m_tabs[tabName].second, SIGNAL(newInstrumentConfiguration()));
// Remove the tab
m_uiForm.twIDRTabs->removeTab(0);
g_log.debug() << "Removing tab " << tabName.toStdString()
<< std::endl;
}
// Add the required tabs
for(auto it = enabledTabs.begin(); it != enabledTabs.end(); ++it)
{
// Connect the insturment changed signal
connect(this, SIGNAL(newInstrumentConfiguration()),
m_tabs[*it].second, SIGNAL(newInstrumentConfiguration()));
// Add the tab
m_uiForm.twIDRTabs->addTab(m_tabs[*it].first, *it);
g_log.debug() << "Adding tab " << (*it).toStdString()
<< std::endl;
}
}
/**
* Handles showing the manage directory dialog box.
*/
void IndirectDataReduction::openDirectoryDialog()
{
MantidQt::API::ManageUserDirectories *ad = new MantidQt::API::ManageUserDirectories(this);
ad->show();
ad->setFocus();
}
/**
* Slot to wrap the protected showInformationBox method defined
* in UserSubWindow and provide access to composed tabs.
*
* @param message The message to display in the message box
*/
void IndirectDataReduction::showMessageBox(const QString& message)
{
showInformationBox(message);
}
/**
* Slot to allow setting the state of the Run button.
*
* @param enabled If the button is clickable
* @param message Message shown on the button
* @param tooltip Tooltip shown when hovering over button
*/
void IndirectDataReduction::updateRunButton(bool enabled, QString message, QString tooltip)
{
m_uiForm.pbRun->setEnabled(enabled);
m_uiForm.pbRun->setText(message);
m_uiForm.pbRun->setToolTip(tooltip);
}