/
QwtWorkspaceSpectrumData.cpp
207 lines (185 loc) · 4.63 KB
/
QwtWorkspaceSpectrumData.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
#include "MantidQtAPI/QwtWorkspaceSpectrumData.h"
#include "MantidQtAPI/PlotAxis.h"
#include <QStringBuilder>
/**
* Construct a QwtWorkspaceSpectrumData object with a source workspace
* @param workspace The workspace containing the data
* @param specIndex Index of the spectrum to plot
* @param logScale If true, plot a log scale
* @param distr If true and the data is histogram then plot the Y values/X bin-width
*/
QwtWorkspaceSpectrumData::QwtWorkspaceSpectrumData(const Mantid::API::MatrixWorkspace & workspace,
int specIndex, const bool logScale, const bool distr)
: m_spec(specIndex),
m_X(workspace.readX(specIndex)),
m_Y(workspace.readY(specIndex)),
m_E(workspace.readE(specIndex)),
m_xTitle(), m_yTitle(),
m_isHistogram(workspace.isHistogramData()),
m_binCentres(false),
m_logScale(logScale),
m_minPositive(0),
m_isDistribution(false)
{
setAsDistribution(distr); // takes into account if this is a histogram or not
m_xTitle = MantidQt::API::PlotAxis(workspace, 0).title();
m_yTitle = MantidQt::API::PlotAxis(workspace).title();
}
/// Virtual copy constructor
QwtWorkspaceSpectrumData *QwtWorkspaceSpectrumData::copy() const
{
return new QwtWorkspaceSpectrumData(*this);
}
/**
* @param workspace A new workspace source
* @return
*/
QwtWorkspaceSpectrumData* QwtWorkspaceSpectrumData::copyWithNewSource(const Mantid::API::MatrixWorkspace & workspace) const
{
return new QwtWorkspaceSpectrumData(workspace, m_spec, m_logScale, m_isDistribution);
}
/** Size of the data set
*/
size_t QwtWorkspaceSpectrumData::size() const
{
if (m_binCentres || !m_isHistogram)
{
return m_Y.size();
}
return m_X.size();
}
/**
Return the x value of data point i
@param i :: Index
@return x X value of data point i
*/
double QwtWorkspaceSpectrumData::x(size_t i) const
{
return m_binCentres ? (m_X[i] + m_X[i+1])/2 : m_X[i];
}
/**
Return the y value of data point i
@param i :: Index
@return y Y value of data point i
*/
double QwtWorkspaceSpectrumData::y(size_t i) const
{
double tmp = i < m_Y.size() ? m_Y[i] : m_Y[m_Y.size()-1];
if (m_isDistribution)
{
tmp /= (m_X[i+1] - m_X[i]);
}
if (m_logScale && tmp <= 0.)
{
tmp = m_minPositive;
}
return tmp;
}
double QwtWorkspaceSpectrumData::ex(size_t i) const
{
return m_isHistogram ? (m_X[i] + m_X[i+1])/2 : m_X[i];
}
double QwtWorkspaceSpectrumData::e(size_t i) const
{
if (m_logScale)
{
if (m_Y[i] <= 0.0)
return 0;
else
return m_E[i];
}
else
return m_E[i];
}
size_t QwtWorkspaceSpectrumData::esize() const
{
return m_E.size();
}
/**
* Depending upon whether the log options have been set.
* @return the lowest y value.
*/
double QwtWorkspaceSpectrumData::getYMin() const
{
auto it = std::min_element(m_Y.begin(), m_Y.end());
double temp = 0;
if(it != m_Y.end())
{
temp = *it;
}
if (m_logScale && temp <= 0.)
{
temp = m_minPositive;
}
return temp;
}
/**
* Depending upon whether the log options have been set.
* @return the highest y value.
*/
double QwtWorkspaceSpectrumData::getYMax() const
{
auto it = std::max_element(m_Y.begin(), m_Y.end());
double temp = 0;
if(it != m_Y.end())
{
temp = *it;
}
if (m_logScale && temp <= 0.)
{
temp = m_minPositive;
}
return temp;
}
/**
* @return A string containin the text to use as an X axis label
*/
QString QwtWorkspaceSpectrumData::getXAxisLabel() const
{
return m_xTitle;
}
/**
* @return A string containin the text to use as an Y axis label
*/
QString QwtWorkspaceSpectrumData::getYAxisLabel() const
{
return m_yTitle;
}
void QwtWorkspaceSpectrumData::setLogScale(bool on)
{
m_logScale = on;
}
void QwtWorkspaceSpectrumData::saveLowestPositiveValue(const double v)
{
if (v > 0) m_minPositive = v;
}
bool QwtWorkspaceSpectrumData::setAsDistribution(bool on)
{
m_isDistribution = on && m_isHistogram;
return m_isDistribution;
}
//-----------------------------------------------------------------------------
// Protected methods
//-----------------------------------------------------------------------------
/**
* @param rhs A source object whose state is copied here
* @return A reference to this object
*/
QwtWorkspaceSpectrumData &QwtWorkspaceSpectrumData::operator=(const QwtWorkspaceSpectrumData & rhs)
{
if(this != &rhs)
{
m_spec = rhs.m_spec;
m_X = rhs.m_X;
m_Y = rhs.m_Y;
m_E = rhs.m_E;
m_xTitle = rhs.m_xTitle;
m_yTitle = rhs.m_yTitle;
m_isHistogram = rhs.m_isHistogram;
m_binCentres = rhs.m_binCentres;
m_logScale = rhs.m_logScale;
m_minPositive = rhs.m_minPositive;
m_isDistribution = rhs.m_isDistribution;
}
return *this;
}