/
MantidMatrixCurve.cpp
370 lines (323 loc) · 11.4 KB
/
MantidMatrixCurve.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
#include "MantidMatrixCurve.h"
#include <qpainter.h>
#include <qwt_symbol.h>
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidAPI/AnalysisDataService.h"
#include "MantidQtAPI/QwtWorkspaceSpectrumData.h"
#include "MantidQtAPI/QwtWorkspaceBinData.h"
#include "../Graph.h"
#include "../ApplicationWindow.h"
#include "../MultiLayer.h"
#include "ErrorBarSettings.h"
#include "MantidKernel/ReadLock.h"
using namespace Mantid::API;
using namespace MantidQt::API;
using Mantid::Kernel::ReadLock;
namespace
{
/// static logger
Mantid::Kernel::Logger g_log("MantidMatrixCurve");
}
/**
* @param name :: The curve's name - shown in the legend
* @param wsName :: The workspace name.
* @param g :: The Graph widget which will display the curve
* @param index :: The index of the spectrum or bin in the workspace
* @param indexType :: Enum indicating whether the index is a spectrum/bin index
* @param err :: True if the errors are to be plotted
* @param distr :: True if it is a distribution
* @param style :: CurveType style to use
*..@throw Mantid::Kernel::Exception::NotFoundError if the workspace cannot be found
* @throw std::invalid_argument if the index is out of range for the given workspace
*/
MantidMatrixCurve::MantidMatrixCurve(const QString&,const QString& wsName,Graph* g,int index, IndexDir indexType,
bool err,bool distr, Graph::CurveType style)
:MantidCurve(err),
m_wsName(wsName),
m_index(index), m_indexType(indexType)
{
if(!g)
{
throw std::invalid_argument("MantidMatrixCurve::MantidMatrixCurve - NULL graph pointer not allowed");
}
init(g,distr,style);
}
/**
* @param wsName :: The workspace name.
* @param g :: The Graph widget which will display the curve
* @param index :: The index of the spectrum or bin in the workspace
* @param indexType :: Enum indicating whether the index is a spectrum/bin index
* @param err :: True if the errors are to be plotted
* @param distr :: True if it is a distribution
* @param style :: CurveType style to use
* @throw std::invalid_argument if the index is out of range for the given workspace
*/
MantidMatrixCurve::MantidMatrixCurve(const QString& wsName,Graph* g,int index, IndexDir indexType, bool err,bool distr, Graph::CurveType style)
:MantidCurve(err),
m_wsName(wsName),
m_index(index), m_indexType(indexType)
{
init(g,distr,style);
}
MantidMatrixCurve::MantidMatrixCurve(const MantidMatrixCurve& c)
:MantidCurve(createCopyName(c.title().text()), c.m_drawErrorBars, c.m_drawAllErrorBars),
m_wsName(c.m_wsName),
m_index(c.m_index), m_indexType(c.m_indexType),
m_xUnits(c.m_xUnits),
m_yUnits(c.m_yUnits)
{
setData(c.data());
observePostDelete();
connect( this, SIGNAL(resetData(const QString&)), this, SLOT(dataReset(const QString&)) );
observeAfterReplace();
observeADSClear();
}
/**
* @param g :: The Graph widget which will display the curve
* @param distr :: True for a distribution
* @param style :: The curve type to use
*/
void MantidMatrixCurve::init(Graph* g,bool distr,Graph::CurveType style)
{
// Will throw if name not found but return NULL ptr if the type is incorrect
MatrixWorkspace_const_sptr workspace = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(m_wsName.toStdString() );
if ( !workspace ) // The respective *Data classes will check for index validity
{
std::stringstream ss;
ss << "Workspace named '" << m_wsName.toStdString()
<< "' found but it is not a MatrixWorkspace. ID='"
<< AnalysisDataService::Instance().retrieve(m_wsName.toStdString() )->id() << "'";
throw std::invalid_argument(ss.str());
}
// Set the curve name if it the non-naming constructor was called
if ( this->title().isEmpty() )
{
// If there's only one spectrum in the workspace, title is simply workspace name
if (workspace->getNumberHistograms() == 1) this->setTitle(m_wsName);
else this->setTitle(createCurveName(workspace));
}
Mantid::API::MatrixWorkspace_const_sptr matrixWS = boost::dynamic_pointer_cast<const Mantid::API::MatrixWorkspace>(workspace);
//we need to censor the data if there is a log scale because it can't deal with negative values, only the y-axis has been found to be problem so far
const bool log = g->isLog(QwtPlot::yLeft);
// Y units are the same for both spectrum and bin plots, e.g. counts
m_yUnits.reset(new Mantid::Kernel::Units::Label(matrixWS->YUnit(), matrixWS->YUnitLabel()));
if(m_indexType == Spectrum) // Spectrum plot
{
QwtWorkspaceSpectrumData data(*matrixWS,m_index, log,distr);
setData(data);
// For spectrum plots, X axis are actual X axis, e.g. TOF
m_xUnits = matrixWS->getAxis(0)->unit();
}
else // Bin plot
{
QwtWorkspaceBinData data(*matrixWS, m_index,log);
setData(data);
// For bin plots, X axis are "spectra axis", e.g. spectra numbers
m_xUnits = matrixWS->getAxis(1)->unit();
}
if (!m_xUnits)
{
m_xUnits.reset(new Mantid::Kernel::Units::Empty());
}
int lineWidth = 1;
MultiLayer* ml = dynamic_cast<MultiLayer*>(g->parent()->parent()->parent());
if (style == Graph::Unspecified || (ml && ml->applicationWindow()->applyCurveStyleToMantid) )
{
applyStyleChoice(style, ml, lineWidth);
}
else if (matrixWS->isHistogramData() && !matrixWS->isDistribution())
{
setStyle(QwtPlotCurve::Steps);
setCurveAttribute(Inverted,true);// this is the Steps style modifier that makes horizontal steps
}
else
{
setStyle(QwtPlotCurve::Lines);
}
g->insertCurve(this,lineWidth);
// set the option to draw all error bars from the global settings
if ( hasErrorBars() )
{
setErrorBars( true, g->multiLayer()->applicationWindow()->drawAllErrors );
}
// Initialise error bar colour to match curve colour
m_errorSettings->m_color = pen().color();
m_errorSettings->setWidth(pen().widthF());
connect(g,SIGNAL(axisScaleChanged(int,bool)),this,SLOT(axisScaleChanged(int,bool)));
observePostDelete();
connect( this, SIGNAL(resetData(const QString&)), this, SLOT(dataReset(const QString&)) );
observeAfterReplace();
observeADSClear();
}
MantidMatrixCurve::~MantidMatrixCurve()
{
observePostDelete(false);
observeAfterReplace(false);
observeADSClear(false);
}
/**
* Clone the curve for the use by a particular Graph
*/
MantidMatrixCurve* MantidMatrixCurve::clone(const Graph* g)const
{
MantidMatrixCurve* mc = new MantidMatrixCurve(*this);
if (g)
{
mc->setDrawAsDistribution(g->isDistribution());
}
return mc;
}
void MantidMatrixCurve::loadData()
{
// This should only be called for waterfall plots
// Calculate the offsets...
computeWaterfallOffsets();
}
void MantidMatrixCurve::setData(const QwtData &data)
{
if (!dynamic_cast<const MantidQwtWorkspaceData*>(&data))
throw std::runtime_error("Only MantidQwtWorkspaceData can be set to a MantidMatrixCurve");
PlotCurve::setData(data);
}
QwtDoubleRect MantidMatrixCurve::boundingRect() const
{
return MantidCurve::boundingRect();
}
void MantidMatrixCurve::draw(QPainter *p,
const QwtScaleMap &xMap, const QwtScaleMap &yMap,
const QRect &rect) const
{
PlotCurve::draw(p,xMap,yMap,rect);
if (m_drawErrorBars)// drawing error bars
{
const MantidQwtWorkspaceData* d = dynamic_cast<const MantidQwtWorkspaceData*>(&data());
if (!d)
{
throw std::runtime_error("Only MantidQwtWorkspaceData can be set to a MantidMatrixCurve");
}
p->translate(d_x_offset,-d_y_offset); // For waterfall plots (will be zero otherwise)
// Don't really know why you'd want errors on a waterfall plot, but just in case...
doDraw(p,xMap,yMap,rect,d);
}
}
void MantidMatrixCurve::itemChanged()
{
QwtWorkspaceSpectrumData* d = dynamic_cast<QwtWorkspaceSpectrumData*>(&data());
if (d && d->m_isHistogram)
{
if (style() == Steps) d->m_binCentres = false;
else
d->m_binCentres = true;
}
PlotCurve::itemChanged();
}
/**
* Create the name for a curve from the following input:
* @param ws :: Pointer to workspace
*/
QString MantidMatrixCurve::createCurveName(const boost::shared_ptr<const Mantid::API::MatrixWorkspace> ws)
{
QString name = m_wsName + "-";
if(m_indexType == Spectrum) name += QString::fromStdString(ws->getAxis(1)->label(m_index));
else name += "bin-" + QString::number(m_index);
return name;
}
/** Resets the data if wsName is the name of this workspace
* @param wsName :: The name of a workspace which data has been changed in the data service.
*/
void MantidMatrixCurve::dataReset(const QString& wsName)
{
if (m_wsName != wsName) return;
const std::string wsNameStd = wsName.toStdString();
Mantid::API::MatrixWorkspace_sptr mws;
try
{
Mantid::API::Workspace_sptr base = Mantid::API::AnalysisDataService::Instance().retrieve(wsNameStd);
mws = boost::dynamic_pointer_cast<Mantid::API::MatrixWorkspace>(base);
}
catch(std::runtime_error&)
{
g_log.information() << "Workspace " << wsNameStd
<< " could not be found - plotted curve(s) deleted\n";
mws = Mantid::API::MatrixWorkspace_sptr();
}
if (!mws) return;
// Acquire a read-lock on the matrix workspace data
ReadLock _lock(*mws);
try
{
const auto *newData = mantidData()->copyWithNewSource(*mws);
setData(*newData);
delete newData;
// Queue this plot to be updated once all QwtWorkspaceSpectrumData objects for this workspace have been
emit dataUpdated();
}
catch(std::range_error &)
{
// Get here if the new workspace has fewer spectra and the plotted one no longer exists
g_log.information() << "Workspace " << wsNameStd
<< " now has fewer spectra - plotted curve(s) deleted\n";
postDeleteHandle(wsNameStd);
}
}
void MantidMatrixCurve::afterReplaceHandle(const std::string& wsName,const boost::shared_ptr<Mantid::API::Workspace> ws)
{
(void) ws;
invalidateBoundingRect();
emit resetData(QString::fromStdString(wsName));
}
/* This method saves the curve details to a string.
* Useful for loading/saving mantid project.
*/
QString MantidMatrixCurve::saveToString()
{
QString s;
s = "MantidMatrixCurve\t" + m_wsName + "\t sp \t" + QString::number(m_index) + "\t"
+ QString::number(m_drawErrorBars) + "\t" + QString::number(isDistribution()) + "\t";
return s;
}
/// Returns the workspace index if a spectrum is plotted and -1 if it is a bin.
int MantidMatrixCurve::workspaceIndex()const
{
if (dynamic_cast<const QwtWorkspaceSpectrumData*>(mantidData()) != 0)
{
return m_index;
}
return -1;
}
MantidQwtMatrixWorkspaceData * MantidMatrixCurve::mantidData()
{
auto * d = dynamic_cast<MantidQwtMatrixWorkspaceData*>(&data());
return d;
}
const MantidQwtMatrixWorkspaceData * MantidMatrixCurve::mantidData() const
{
const auto *d = dynamic_cast<const MantidQwtMatrixWorkspaceData*>(&data());
return d;
}
/// Enables/disables drawing as distribution, ie dividing each y-value by the bin width.
bool MantidMatrixCurve::setDrawAsDistribution(bool on)
{
if( auto *d = dynamic_cast<QwtWorkspaceSpectrumData*>(&data()))
{
return d->setAsDistribution(on);
}
else return false;
}
/// Returns whether the curve is plotted as a distribution
bool MantidMatrixCurve::isDistribution() const
{
if( auto *d = dynamic_cast<const QwtWorkspaceSpectrumData*>(&data()))
{
return d->m_isDistribution;
}
else return false;
}
bool MantidMatrixCurve::isHistogramData() const
{
if( auto *d = dynamic_cast<const QwtWorkspaceSpectrumData*>(&data()))
{
return d->m_isHistogram;
}
else return false;
}