-
Notifications
You must be signed in to change notification settings - Fork 122
/
ALCDataLoadingPresenter.cpp
193 lines (157 loc) · 5.78 KB
/
ALCDataLoadingPresenter.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
#include "MantidQtCustomInterfaces/Muon/ALCDataLoadingPresenter.h"
#include "MantidAPI/AlgorithmManager.h"
#include "MantidQtCustomInterfaces/Muon/ALCHelper.h"
#include "MantidQtCustomInterfaces/Muon/MuonAnalysisHelper.h"
#include "MantidQtAPI/AlgorithmInputHistory.h"
#include <Poco/ActiveResult.h>
#include <QApplication>
#include <QFileInfo>
#include <QDir>
#include <sstream>
using namespace Mantid::Kernel;
using namespace Mantid::API;
using namespace MantidQt::API;
namespace MantidQt
{
namespace CustomInterfaces
{
ALCDataLoadingPresenter::ALCDataLoadingPresenter(IALCDataLoadingView* view)
: m_view(view)
{}
void ALCDataLoadingPresenter::initialize()
{
m_view->initialize();
connect(m_view, SIGNAL(loadRequested()), SLOT(load()));
connect(m_view, SIGNAL(firstRunSelected()), SLOT(updateAvailableInfo()));
}
void ALCDataLoadingPresenter::load()
{
m_view->setWaitingCursor();
// Check time limits
if (auto timeRange = m_view->timeRange()) {
double tmin = (*timeRange).first;
double tmax = (*timeRange).second;
if ( tmin >= tmax ){
m_view->restoreCursor();
m_view->displayError("Invalid time interval");
return;
}
} else {
m_view->restoreCursor();
m_view->displayError("No time interval");
return;
}
try
{
IAlgorithm_sptr alg = AlgorithmManager::Instance().create("PlotAsymmetryByLogValue");
alg->setChild(true); // Don't want workspaces in the ADS
alg->setProperty("FirstRun", m_view->firstRun());
alg->setProperty("LastRun", m_view->lastRun());
alg->setProperty("LogValue", m_view->log());
alg->setProperty("Type", m_view->calculationType());
alg->setProperty("DeadTimeCorrType",m_view->deadTimeType());
alg->setProperty("Red",m_view->redPeriod());
// If time limiting requested, set min/max times
if (auto timeRange = m_view->timeRange())
{
alg->setProperty("TimeMin", timeRange->first);
alg->setProperty("TimeMax", timeRange->second);
}
// If corrections from custom file requested, set file property
if (m_view->deadTimeType() == "FromSpecifiedFile") {
alg->setProperty("DeadTimeCorrFile",m_view->deadTimeFile());
}
// If custom grouping requested, set forward/backward groupings
if ( m_view->detectorGroupingType() == "Custom" ) {
alg->setProperty("ForwardSpectra",m_view->getForwardGrouping());
alg->setProperty("BackwardSpectra",m_view->getBackwardGrouping());
}
// If Subtract checkbox is selected, set green period
if ( m_view->subtractIsChecked() ) {
alg->setProperty("Green",m_view->greenPeriod());
}
alg->setPropertyValue("OutputWorkspace", "__NotUsed");
// Execute async so we can show progress bar
Poco::ActiveResult<bool> result(alg->executeAsync());
while( !result.available() )
{
QCoreApplication::processEvents();
}
m_loadedData = alg->getProperty("OutputWorkspace");
// If errors are properly caught, shouldn't happen
assert(m_loadedData);
// If subtract is not checked, only one spectrum,
// else four spectra
if ( !m_view->subtractIsChecked() ) {
assert(m_loadedData->getNumberHistograms() == 1);
} else {
assert(m_loadedData->getNumberHistograms() == 4);
}
// Plot spectrum 0. It is either red period (if subtract is unchecked) or
// red - green (if subtract is checked)
m_view->setDataCurve(*(ALCHelper::curveDataFromWs(m_loadedData, 0)));
}
catch(std::exception& e)
{
m_view->displayError(e.what());
}
m_view->restoreCursor();
}
void ALCDataLoadingPresenter::updateAvailableInfo()
{
Workspace_sptr loadedWs;
try //... to load the first run
{
IAlgorithm_sptr load = AlgorithmManager::Instance().create("LoadMuonNexus");
load->setChild(true); // Don't want workspaces in the ADS
load->setProperty("Filename", m_view->firstRun());
// We need logs only but we have to use LoadMuonNexus
// (can't use LoadMuonLogs as not all the logs would be
// loaded), so we load the minimum amount of data, i.e., one spectrum
load->setPropertyValue("SpectrumMin","1");
load->setPropertyValue("SpectrumMax","1");
load->setPropertyValue("OutputWorkspace", "__NotUsed");
load->execute();
loadedWs = load->getProperty("OutputWorkspace");
}
catch(...)
{
m_view->setAvailableLogs(std::vector<std::string>()); // Empty logs list
m_view->setAvailablePeriods(std::vector<std::string>()); // Empty period list
m_view->setTimeLimits(0,0); // "Empty" time limits
return;
}
// Set logs
MatrixWorkspace_const_sptr ws = MuonAnalysisHelper::firstPeriod(loadedWs);
std::vector<std::string> logs;
const auto& properties = ws->run().getProperties();
for(auto it = properties.begin(); it != properties.end(); ++it)
{
logs.push_back((*it)->name());
}
m_view->setAvailableLogs(logs);
// Set periods
size_t numPeriods = MuonAnalysisHelper::numPeriods(loadedWs);
std::vector<std::string> periods;
for (size_t i=0; i<numPeriods; i++)
{
std::stringstream buffer;
buffer << i + 1;
periods.push_back(buffer.str());
}
m_view->setAvailablePeriods(periods);
// Set time limits
m_view->setTimeLimits(ws->readX(0).front(),ws->readX(0).back());
// Set allowed time range
m_view->setTimeRange (ws->readX(0).front(),ws->readX(0).back());
}
MatrixWorkspace_sptr ALCDataLoadingPresenter::exportWorkspace()
{
if ( m_loadedData ) {
return boost::const_pointer_cast<MatrixWorkspace>(m_loadedData);
} else {
return MatrixWorkspace_sptr();
}
}
} // namespace CustomInterfaces
} // namespace MantidQt