-
Notifications
You must be signed in to change notification settings - Fork 122
/
StepScan.cpp
677 lines (590 loc) · 23.9 KB
/
StepScan.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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
//----------------------
// Includes
//----------------------
#include "MantidQtCustomInterfaces/StepScan.h"
#include "MantidAPI/AlgorithmManager.h"
#include "MantidAPI/IEventWorkspace.h"
#include "MantidAPI/InstrumentDataService.h"
#include "MantidAPI/LiveListenerFactory.h"
#include "MantidKernel/InstrumentInfo.h"
#include "MantidKernel/TimeSeriesProperty.h"
#include <QFileInfo>
#include <QDesktopServices>
#include <QUrl>
namespace MantidQt
{
namespace CustomInterfaces
{
//Register the class with the factory
DECLARE_SUBWINDOW(StepScan);
using namespace Mantid::Kernel;
using namespace Mantid::API;
/// Constructor
StepScan::StepScan(QWidget *parent)
: UserSubWindow(parent),
m_instrument(ConfigService::Instance().getInstrument().name()),
m_algRunner(new API::AlgorithmRunner(this)),
m_addObserver(*this, &StepScan::handleAddEvent),
m_replObserver(*this, &StepScan::handleReplEvent),
m_replaceObserverAdded(false)
{
}
StepScan::~StepScan()
{
// Stop any async algorithm
m_algRunner->cancelRunningAlgorithm();
// Stop live data collection, if running
m_uiForm.mWRunFiles->stopLiveAlgorithm();
// Disconnect the observers for the mask workspace combobox
AnalysisDataService::Instance().notificationCenter.removeObserver(m_addObserver);
AnalysisDataService::Instance().notificationCenter.removeObserver(m_replObserver);
// Clean up any hidden workspaces created
cleanupWorkspaces();
}
/// Set up the dialog layout
void StepScan::initLayout()
{
m_uiForm.setupUi(this);
// I couldn't see a way to set a validator on a qlineedit in designer
m_uiForm.xmin->setValidator(new QDoubleValidator(m_uiForm.xmin));
m_uiForm.xmax->setValidator(new QDoubleValidator(m_uiForm.xmax));
setWindowTitle(windowTitle() + " - " + QString::fromStdString(m_instrument));
connect( m_uiForm.mWRunFiles, SIGNAL(liveButtonPressed(bool)), SLOT(triggerLiveListener(bool)), Qt::QueuedConnection );
connect( m_uiForm.launchInstView, SIGNAL(clicked()), SLOT(launchInstrumentWindow()) );
connect( m_uiForm.mWRunFiles, SIGNAL(filesFound()), SLOT(loadFile()) );
connect( this, SIGNAL(logsAvailable(const Mantid::API::MatrixWorkspace_const_sptr &)),
SLOT(fillPlotVarCombobox(const Mantid::API::MatrixWorkspace_const_sptr &)) );
connect( m_uiForm.helpButton, SIGNAL(clicked()), SLOT(helpClicked()) );
connect( m_uiForm.startButton, SIGNAL(clicked()), SLOT(runStepScanAlg()) );
connect( m_uiForm.closeButton, SIGNAL(clicked()), this->parent(), SLOT(close()) );
}
void StepScan::cleanupWorkspaces()
{
if ( ! m_inputWSName.empty() )
{
// Get a reference to the analysis data service
auto& ADS = AnalysisDataService::Instance();
// Clean up, checking first that those that may not exist do (to avoid a warning in the log)
ADS.remove( m_inputWSName );
const std::string monitorWSName = m_inputWSName + "_monitors";
if ( ADS.doesExist( monitorWSName ) ) ADS.remove( monitorWSName );
m_inputWSName.clear();
if ( ADS.doesExist( m_plotWSName ) ) ADS.remove( m_plotWSName );
m_plotWSName.clear();
disconnect( SIGNAL(logsUpdated(const Mantid::API::MatrixWorkspace_const_sptr &)) );
}
m_uiForm.startButton->setEnabled(false);
m_uiForm.launchInstView->setEnabled(false);
m_uiForm.plotVariable->setEnabled(false);
// Disconnect anything listening to the comboboxes
m_uiForm.plotVariable->disconnect(SIGNAL(currentIndexChanged(const QString &)));
m_uiForm.normalization->disconnect(SIGNAL(currentIndexChanged(const QString &)));
}
/** Slot that is called when the live data button is clicked
* @param checked Whether the button is being enabled (true) or disabled
*/
void StepScan::triggerLiveListener(bool checked)
{
if ( checked )
{
startLiveListener();
}
else
{
m_uiForm.mWRunFiles->stopLiveAlgorithm();
cleanupWorkspaces();
}
}
void StepScan::startLiveListener()
{
if ( ! LiveListenerFactory::Instance().create(m_instrument,false)->buffersEvents() )
{
QMessageBox::critical(this,"Invalid live stream","This interface requires event data.\nThe live data for " + QString::fromStdString(m_instrument) + " is in histogram form");
m_uiForm.mWRunFiles->liveButtonSetChecked(false);
m_uiForm.mWRunFiles->liveButtonSetEnabled(false);
return;
}
// Remove any previously-loaded workspaces
cleanupWorkspaces();
connect(m_algRunner, SIGNAL(algorithmComplete(bool)), SLOT(startLiveListenerComplete(bool)));
IAlgorithm_sptr startLiveData = AlgorithmManager::Instance().create("StartLiveData");
startLiveData->setProperty("UpdateEvery",5.0);
startLiveData->setProperty("FromNow",false);
startLiveData->setProperty("FromStartOfRun",true);
startLiveData->setProperty("Instrument",m_instrument);
m_inputWSName = "__live";
startLiveData->setProperty("OutputWorkspace",m_inputWSName);
if ( ! startLiveData->validateInputs().empty() )
{
QMessageBox::critical(this,"StartLiveData failed","Unable to start live data collection");
m_uiForm.mWRunFiles->liveButtonSetChecked(false);
return;
}
m_uiForm.mWRunFiles->setLiveAlgorithm(startLiveData);
m_algRunner->startAlgorithm(startLiveData);
}
void StepScan::startLiveListenerComplete(bool error)
{
disconnect(m_algRunner, SIGNAL(algorithmComplete(bool)), this, SLOT(startLiveListenerComplete(bool)));
if ( ! error )
{
// Keep track of the algorithm that's pulling in the live data
m_uiForm.mWRunFiles->setLiveAlgorithm(m_algRunner->getAlgorithm()->getProperty("MonitorLiveData"));
setupOptionControls();
addReplaceObserverOnce();
connect( this, SIGNAL(logsUpdated(const Mantid::API::MatrixWorkspace_const_sptr &)),
SLOT(expandPlotVarCombobox(const Mantid::API::MatrixWorkspace_const_sptr &)) );
}
else
{
QMessageBox::critical(this,"StartLiveData failed","Unable to start live data collection");
m_uiForm.mWRunFiles->liveButtonSetChecked(false);
}
}
void StepScan::loadFile(bool async)
{
const QString filename = m_uiForm.mWRunFiles->getUserInput().asString();
// This handles the fact that mwRunFiles emits the filesFound signal more than
// we want (on some platforms). TODO: Consider dealing with this up in mwRunFiles.
if ( filename != m_inputFilename && m_uiForm.mWRunFiles->isValid() )
{
m_inputFilename = filename;
// Remove any previously-loaded workspaces
cleanupWorkspaces();
IAlgorithm_sptr alg = AlgorithmManager::Instance().create("Load");
try {
alg->setPropertyValue("Filename", filename.toStdString());
if ( m_uiForm.mWRunFiles->getFilenames().size() == 1 )
{
m_inputWSName = "__" + QFileInfo(filename).baseName().toStdString();
}
else
{
m_inputWSName = "__multifiles";
}
alg->setPropertyValue("OutputWorkspace", m_inputWSName);
alg->setProperty("LoadMonitors", true);
}
catch (std::exception&) // Have to catch at this level as different exception types can happen
{
QMessageBox::warning(this,"File loading failed","Is this an event nexus file?");
return;
}
m_uiForm.statusText->setText("<i><font color='darkblue'>Loading data...</font></i>");
if ( async )
{
connect(m_algRunner, SIGNAL(algorithmComplete(bool)), SLOT(loadFileComplete(bool)));
m_algRunner->startAlgorithm(alg);
}
else
{
alg->execute();
loadFileComplete(!alg->isExecuted());
}
}
}
void StepScan::loadFileComplete(bool error)
{
m_uiForm.statusText->clear();
disconnect(m_algRunner, SIGNAL(algorithmComplete(bool)), this, SLOT(loadFileComplete(bool)));
if ( m_inputWSName == "__multifiles" && !error ) error = mergeRuns();
if ( ! error )
{
setupOptionControls();
}
else
{
QMessageBox::warning(this,"File loading failed","Is this an event nexus file?");
}
}
namespace {
class ScopedStatusText
{
public:
ScopedStatusText(QLabel * label, QString labelText) : status_label(label)
{
status_label->setText("<i><font color='darkblue'>" + labelText + "</font></i>");
}
~ScopedStatusText()
{
status_label->clear();
}
private:
QLabel * const status_label;
};
// Small class to handle disabling mouse clicks and showing the busy cursor in an RAII manner.
// Used in the runStepScanAlg below to ensure these things are unset when the method is exited.
class DisableGUI_RAII
{
public:
DisableGUI_RAII(StepScan * gui) : the_gui(gui)
{
QApplication::setOverrideCursor(QCursor(Qt::BusyCursor));
the_gui->setAttribute( Qt::WA_TransparentForMouseEvents );
}
~DisableGUI_RAII()
{
QApplication::restoreOverrideCursor();
the_gui->setAttribute( Qt::WA_TransparentForMouseEvents, false );
}
private:
StepScan * const the_gui;
};
}
bool StepScan::mergeRuns()
{
ScopedStatusText _merging(this->m_uiForm.statusText,"Merging runs...");
// This can be slow and will lock the GUI, but will probably be so rarely used that it's
// not worth making it asynchronous
// Block mouse clicks while the algorithm runs. Also set the busy cursor.
DisableGUI_RAII _blockclicks(this);
// Get hold of the group workspace and go through the entries adding an incrementing scan_index variable
WorkspaceGroup_const_sptr wsGroup = AnalysisDataService::Instance().retrieveWS<WorkspaceGroup>(m_inputWSName);
if ( !wsGroup ) return true; // Shouldn't be possible, but be defensive
for ( size_t i = 0; i < wsGroup->size(); ++i )
{
// Add a scan_index variable to each workspace, counting from 1
MatrixWorkspace_sptr ws = boost::static_pointer_cast<MatrixWorkspace>(wsGroup->getItem(i));
if ( !ws ) return true; // Again, shouldn't be possible (unless there's a group within a group?)
IAlgorithm_sptr addScanIndex = AlgorithmManager::Instance().create("AddSampleLog");
addScanIndex->setPropertyValue("Workspace",ws->name());
addScanIndex->setProperty("LogName","scan_index");
addScanIndex->setProperty("LogType","Number Series");
addScanIndex->setProperty("LogText",Strings::toString(i+1));
if ( ! addScanIndex->execute() ) return true;
// Add a scan_index = 0 to the end time for each workspace
try
{
ws->run().getTimeSeriesProperty<int>("scan_index")->addValue(ws->run().endTime(),0);
} catch (std::runtime_error&) {
/* Swallow the error if there's no run end time. It shouldn't happen for real data. */
}
}
IAlgorithm_sptr merge = AlgorithmManager::Instance().create("MergeRuns");
merge->setPropertyValue("InputWorkspaces",m_inputWSName);
const std::string summedWSName = "__summed_multifiles";
merge->setPropertyValue("OutputWorkspace",summedWSName);
if ( ! merge->execute() ) return true;
m_inputWSName = summedWSName;
return false;
}
void StepScan::setupOptionControls()
{
MatrixWorkspace_const_sptr outWS = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(m_inputWSName);
// Trigger population of the logs combobox
emit logsAvailable( outWS );
fillNormalizationCombobox();
// Enable the button to launch the instrument view (for defining a mask)
m_uiForm.launchInstView->setEnabled(true);
}
void StepScan::launchInstrumentWindow()
{
// Gotta do this in python
std::string pyCode = "instrument_view = getInstrumentView('" + m_inputWSName + "',2)\n"
"instrument_view.show()";
runPythonCode( QString::fromStdString(pyCode) );
// Attach the observers so that if a mask workspace is generated over in the instrument view,
// it is automatically selected by the combobox over here
AnalysisDataService::Instance().notificationCenter.addObserver(m_addObserver);
addReplaceObserverOnce();
}
void StepScan::fillPlotVarCombobox(const MatrixWorkspace_const_sptr& ws)
{
// Hold the name of the scan index log in a common place
const std::string scan_index("scan_index");
// If this has already been set to something, keep track of what
auto currentSetting = m_uiForm.plotVariable->currentText();
// Clear the combobox and immediately re-insert 'scan_index' (so it's the first entry)
m_uiForm.plotVariable->clear();
m_uiForm.plotVariable->addItem( QString::fromStdString(scan_index) );
// First check that the provided workspace has the scan_index - complain if it doesn't
try {
auto scan_index_prop = ws->run().getTimeSeriesProperty<int>(scan_index);
if ( !m_uiForm.mWRunFiles->liveButtonIsChecked() && scan_index_prop->realSize() < 2 )
{
QMessageBox::warning(this,"scan_index log empty","This data does not appear to be an alignment scan");
return;
}
} catch ( std::exception& ) {
QMessageBox::warning(this,"scan_index log not found","Is this an ADARA-style dataset?");
return;
}
expandPlotVarCombobox( ws );
// Set back to whatever it was set to before
m_uiForm.plotVariable->setCurrentIndex(m_uiForm.plotVariable->findText(currentSetting));
// Now that this has been populated, allow the user to select from it
m_uiForm.plotVariable->setEnabled(true);
// Now's the time to enable the start button as well
m_uiForm.startButton->setEnabled(true);
}
void StepScan::expandPlotVarCombobox(const Mantid::API::MatrixWorkspace_const_sptr& ws)
{
// This is unfortunately more or less a copy of SumEventsByLogValue::getNumberSeriesLogs
// but I want to populate the box before running the algorithm
const auto & logs = ws->run().getLogData();
for ( auto log = logs.begin(); log != logs.end(); ++log )
{
const QString logName = QString::fromStdString( (*log)->name() );
// Don't add scan_index - that's already there
if ( logName == "scan_index" ) continue;
// Try to cast to an ITimeSeriesProperty
auto tsp = dynamic_cast<const ITimeSeriesProperty*>(*log);
// Move on to the next one if this is not a TSP
if ( tsp == NULL ) continue;
// Don't keep ones with only one entry
if ( tsp->realSize() < 2 ) continue;
// Now make sure it's either an int or double tsp
if ( dynamic_cast<TimeSeriesProperty<double>* >(*log) || dynamic_cast<TimeSeriesProperty<int>* >(*log))
{
// Add it to the list if it isn't already there
if ( m_uiForm.plotVariable->findText( logName ) == -1 )
{
m_uiForm.plotVariable->addItem( logName );
}
}
}
}
void StepScan::fillNormalizationCombobox()
{
clearNormalizationCombobox();
// Add the monitors to the normalization combobox
try {
MatrixWorkspace_const_sptr monWS = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(m_inputWSName+"_monitors");
for ( std::size_t i = 0; i < monWS->getNumberHistograms(); ++i )
{
const std::string monitorName = monWS->getDetector(i)->getName();
m_uiForm.normalization->addItem( QString::fromStdString( monitorName ) );
}
} catch (Exception::NotFoundError&) {
// No monitors workspace....carry on
}
}
void StepScan::clearNormalizationCombobox()
{
// If there are more than 3 entries in the combobox (nothing, time, proton_charge) then
// remove any stale ones
while ( m_uiForm.normalization->count() > 3 )
{
m_uiForm.normalization->removeItem(m_uiForm.normalization->count()-1);
}
}
IAlgorithm_sptr StepScan::setupStepScanAlg()
{
IAlgorithm_sptr stepScan = AlgorithmManager::Instance().create("StepScan");
// The table should not be hidden, so leave off the prefix
m_tableWSName = m_inputWSName.substr(2) + "_StepScan";
stepScan->setPropertyValue("OutputWorkspace", m_tableWSName);
// ROI masking
const QString maskWS = m_uiForm.maskWorkspace->currentText();
stepScan->setPropertyValue("MaskWorkspace",maskWS.toStdString());
// Filtering on time (or other unit)
const QString xminStr = m_uiForm.xmin->text();
const QString xmaxStr = m_uiForm.xmax->text();
const double xmin = xminStr.toDouble();
const double xmax = xmaxStr.toDouble();
// If both set, check that xmax > xmin
if ( !xminStr.isEmpty() && !xmaxStr.isEmpty() && xmin >= xmax )
{
QMessageBox::critical(this,"Invalid filtering range set","For the filtering range, min has to be less than max");
return IAlgorithm_sptr();
}
if ( ! xminStr.isEmpty() ) stepScan->setProperty("XMin",xmin);
if ( ! xmaxStr.isEmpty() ) stepScan->setProperty("XMax",xmax);
switch (m_uiForm.rangeUnit->currentIndex())
{
case 1:
stepScan->setProperty("RangeUnit","dSpacing");
break;
default:
// The default value for the property is TOF (which is index 0 in the combobox)
break;
}
return stepScan;
}
void StepScan::runStepScanAlg()
{
IAlgorithm_sptr stepScan = setupStepScanAlg();
if ( !stepScan ) return;
// Block mouse clicks while the algorithm runs. Also set the busy cursor.
DisableGUI_RAII _blockclicks(this);
bool algSuccessful;
if ( m_uiForm.mWRunFiles->liveButtonIsChecked() ) // Live data
{
algSuccessful = runStepScanAlgLive(stepScan->toString());
}
else // Offline data
{
// Check just in case the user has deleted the loaded workspace
if ( ! AnalysisDataService::Instance().doesExist(m_inputWSName) )
{
m_inputFilename.clear();
loadFile(false);
}
stepScan->setPropertyValue("InputWorkspace", m_inputWSName);
ScopedStatusText _merging(this->m_uiForm.statusText,"Analyzing scan...");
algSuccessful = stepScan->execute();
}
if ( !algSuccessful )
{
return;
}
// Now that the algorithm's been run, connect up the signal to change the plot variable
connect( m_uiForm.plotVariable, SIGNAL(currentIndexChanged(const QString &)),
SLOT(generateCurve(const QString &)) );
// and the one if the normalisation's been changed
connect( m_uiForm.normalization, SIGNAL(currentIndexChanged(const QString &)),
SLOT(updateForNormalizationChange()) );
// Create the plot for the first time
generateCurve( m_uiForm.plotVariable->currentText() );
}
bool StepScan::runStepScanAlgLive(std::string stepScanProperties)
{
// First stop the currently running live algorithm
IAlgorithm_const_sptr oldMonitorLiveData = m_uiForm.mWRunFiles->stopLiveAlgorithm();
stepScanProperties.erase(0,stepScanProperties.find_first_of('(')+1);
stepScanProperties.erase(stepScanProperties.find_last_of(')'));
std::replace(stepScanProperties.begin(),stepScanProperties.end(),',',';');
IAlgorithm_sptr startLiveData = AlgorithmManager::Instance().create("StartLiveData");
startLiveData->setProperty("Instrument", m_instrument);
startLiveData->setProperty("FromNow",false);
startLiveData->setProperty("FromStartOfRun",true);
startLiveData->setProperty("UpdateEvery",10.0);
startLiveData->setProperty("PreserveEvents",true);
startLiveData->setProperty("PostProcessingAlgorithm","StepScan");
startLiveData->setProperty("PostProcessingProperties",stepScanProperties);
startLiveData->setProperty("EndRunBehavior","Stop");
startLiveData->setProperty("AccumulationWorkspace",m_inputWSName);
startLiveData->setProperty("OutputWorkspace",m_tableWSName);
// The previous listener needs to finish before this one can start
while ( oldMonitorLiveData->isRunning() )
{
Poco::Thread::sleep(100);
}
auto result = startLiveData->executeAsync();
while ( !result.available() )
{
QApplication::processEvents();
}
if ( ! startLiveData->isExecuted() ) return false;
// Keep track of the algorithm that's pulling in the live data
m_uiForm.mWRunFiles->setLiveAlgorithm(startLiveData->getProperty("MonitorLiveData"));
connect( this, SIGNAL(updatePlot(const QString&)), SLOT(generateCurve(const QString&)) );
return true;
}
void StepScan::updateForNormalizationChange()
{
generateCurve( m_uiForm.plotVariable->currentText() );
}
void StepScan::generateCurve( const QString& var )
{
if ( ! AnalysisDataService::Instance().doesExist(m_tableWSName) )
{
QMessageBox::critical(this,"Unable to generate plot","Table workspace "+ QString::fromStdString(m_tableWSName) +"\nhas been deleted!");
return;
}
// Create a matrix workspace out of the variable that's asked for
IAlgorithm_sptr alg = AlgorithmManager::Instance().create("ConvertTableToMatrixWorkspace");
alg->setLogging(false); // Don't log this algorithm
alg->setPropertyValue("InputWorkspace", m_tableWSName);
m_plotWSName = m_tableWSName + "_plot";
alg->setPropertyValue("OutputWorkspace", m_plotWSName);
alg->setPropertyValue("ColumnX", var.toStdString() );
alg->setPropertyValue("ColumnY", "Counts" );
alg->setPropertyValue("ColumnE", "Error" );
if ( ! alg->execute() ) return;
// Now create one for the normalisation, if required
if ( m_uiForm.normalization->currentIndex() != 0 )
{
IAlgorithm_sptr norm = AlgorithmManager::Instance().create("ConvertTableToMatrixWorkspace");
norm->setChild(true);
norm->setLogging(false); // Don't log this algorithm
norm->setPropertyValue("InputWorkspace", m_tableWSName);
norm->setPropertyValue("OutputWorkspace", "dummyName");
norm->setPropertyValue("ColumnX", var.toStdString() );
// TODO: Protect against column being missing (e.g. if monitor not found in data)
norm->setPropertyValue("ColumnY", m_uiForm.normalization->currentText().toStdString() );
if ( ! norm->execute() ) return;
MatrixWorkspace_sptr top = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(m_plotWSName);
MatrixWorkspace_sptr bottom = norm->getProperty("OutputWorkspace");
top /= bottom;
}
plotCurve();
}
void StepScan::plotCurve()
{
// Get the name of the dataset to produce the plot title
std::string title = m_inputWSName.substr(2);
// qtiplot may unhelpfully change '_' to '-' so I need to as well
std::replace(title.begin(), title.end(), '_', '-');
// Figure out the axis titles
const std::string xAxisTitle = m_uiForm.plotVariable->currentText().toStdString();
std::string yAxisTitle = "Counts";
const std::string normalization = m_uiForm.normalization->currentText().toStdString();
if ( normalization == "nothing") /* Do nothing */;
else if ( normalization == "time" ) yAxisTitle += " / second";
else if ( normalization == "proton_charge" ) yAxisTitle += " / picocoulomb";
else yAxisTitle += " / " + normalization;
// Has to be done via python
std::string pyCode = "g = graph('" + title + "')\n"
"if g is None:\n"
" g = plotSpectrum('" + m_plotWSName + "',0,True,type=Layer.Scatter)\n"
" l = g.activeLayer()\n"
" l.legend().hide()\n"
" l.removeTitle()\n"
" setWindowName(g,'" + title + "')\n"
" g.setWindowLabel('Step Scan')\n"
"l = g.activeLayer()\n"
"l.setAxisTitle(Layer.Bottom,'" + xAxisTitle + "')\n"
"l.setAxisTitle(Layer.Left,'" + yAxisTitle + "')";
runPythonCode( QString::fromStdString(pyCode) );
}
void StepScan::handleAddEvent(Mantid::API::WorkspaceAddNotification_ptr pNf)
{
checkForMaskWorkspace(pNf->object_name());
}
void StepScan::handleReplEvent(Mantid::API::WorkspaceAfterReplaceNotification_ptr pNf)
{
checkForMaskWorkspace(pNf->object_name());
checkForResultTableUpdate(pNf->object_name());
checkForVaryingLogs(pNf->object_name());
}
void StepScan::addReplaceObserverOnce()
{
if ( ! m_replaceObserverAdded )
{
AnalysisDataService::Instance().notificationCenter.addObserver(m_replObserver);
m_replaceObserverAdded = true;
}
}
void StepScan::checkForMaskWorkspace(const std::string & wsName)
{
if ( wsName == "MaskWorkspace" )
{
// Make sure the combobox has picked up the new workspace
m_uiForm.maskWorkspace->refresh();
// Now set it to point at the mask workspace
const int index = m_uiForm.maskWorkspace->findText("MaskWorkspace");
if ( index != -1 ) m_uiForm.maskWorkspace->setCurrentIndex(index);
}
}
void StepScan::checkForResultTableUpdate(const std::string& wsName)
{
if ( wsName == m_tableWSName )
{
emit updatePlot( m_uiForm.plotVariable->currentText() );
}
}
void StepScan::checkForVaryingLogs(const std::string& wsName)
{
if ( wsName == m_inputWSName )
{
MatrixWorkspace_const_sptr ws = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(m_inputWSName);
emit logsUpdated( ws );
}
}
void StepScan::helpClicked()
{
QDesktopServices::openUrl(QUrl("http://www.mantidproject.org/Step_Scan_Interface"));
}
} // namespace CustomInterfaces
} // namespace MantidQt