-
Notifications
You must be signed in to change notification settings - Fork 122
/
PoldiPeakCollection.cpp
214 lines (172 loc) · 6.16 KB
/
PoldiPeakCollection.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
#include "MantidSINQ/PoldiUtilities/PoldiPeakCollection.h"
#include "MantidAPI/TableRow.h"
#include "MantidAPI/WorkspaceFactory.h"
#include "MantidAPI/LogManager.h"
#include "boost/format.hpp"
#include "boost/algorithm/string/join.hpp"
#include "MantidSINQ/PoldiUtilities/MillerIndicesIO.h"
#include "MantidSINQ/PoldiUtilities/UncertainValueIO.h"
namespace Mantid {
namespace Poldi {
using namespace Mantid::API;
using namespace Mantid::DataObjects;
PoldiPeakCollection::PoldiPeakCollection(IntensityType intensityType) :
m_peaks(),
m_intensityType(intensityType),
m_profileFunctionName()
{
}
PoldiPeakCollection::PoldiPeakCollection(TableWorkspace_sptr workspace) :
m_peaks()
{
if(workspace) {
constructFromTableWorkspace(workspace);
}
}
PoldiPeakCollection_sptr PoldiPeakCollection::clone()
{
PoldiPeakCollection_sptr clone(new PoldiPeakCollection(m_intensityType));
clone->setProfileFunctionName(m_profileFunctionName);
for(size_t i = 0; i < m_peaks.size(); ++i) {
clone->addPeak(m_peaks[i]->clone());
}
return clone;
}
size_t PoldiPeakCollection::peakCount() const
{
return m_peaks.size();
}
void PoldiPeakCollection::addPeak(PoldiPeak_sptr newPeak)
{
m_peaks.push_back(newPeak);
}
PoldiPeak_sptr PoldiPeakCollection::peak(size_t index) const
{
if(index >= m_peaks.size()) {
throw std::range_error("Peak access index out of range.");
}
return m_peaks[index];
}
PoldiPeakCollection::IntensityType PoldiPeakCollection::intensityType() const
{
return m_intensityType;
}
void PoldiPeakCollection::setProfileFunctionName(std::string newProfileFunction)
{
m_profileFunctionName = newProfileFunction;
}
std::string PoldiPeakCollection::getProfileFunctionName() const
{
return m_profileFunctionName;
}
bool PoldiPeakCollection::hasProfileFunctionName() const
{
return !m_profileFunctionName.empty();
}
TableWorkspace_sptr PoldiPeakCollection::asTableWorkspace()
{
TableWorkspace_sptr peaks = boost::dynamic_pointer_cast<TableWorkspace>(WorkspaceFactory::Instance().createTable());
prepareTable(peaks);
dataToTableLog(peaks);
peaksToTable(peaks);
return peaks;
}
void PoldiPeakCollection::prepareTable(TableWorkspace_sptr table)
{
table->addColumn("str", "HKL");
table->addColumn("str", "d");
table->addColumn("str", "Q");
table->addColumn("str", "Intensity");
table->addColumn("str", "FWHM (rel.)");
}
void PoldiPeakCollection::dataToTableLog(TableWorkspace_sptr table)
{
LogManager_sptr tableLog = table->logs();
tableLog->addProperty<std::string>("IntensityType", intensityTypeToString(m_intensityType));
tableLog->addProperty<std::string>("ProfileFunctionName", m_profileFunctionName);
}
void PoldiPeakCollection::peaksToTable(TableWorkspace_sptr table)
{
for(std::vector<PoldiPeak_sptr>::const_iterator peak = m_peaks.begin(); peak != m_peaks.end(); ++peak) {
TableRow newRow = table->appendRow();
newRow << MillerIndicesIO::toString((*peak)->hkl())
<< UncertainValueIO::toString((*peak)->d())
<< UncertainValueIO::toString((*peak)->q())
<< UncertainValueIO::toString((*peak)->intensity())
<< UncertainValueIO::toString((*peak)->fwhm(PoldiPeak::Relative));
}
}
void PoldiPeakCollection::constructFromTableWorkspace(TableWorkspace_sptr tableWorkspace)
{
if(checkColumns(tableWorkspace)) {
size_t newPeakCount = tableWorkspace->rowCount();
m_peaks.resize(newPeakCount);
recoverDataFromLog(tableWorkspace);
for(size_t i = 0; i < newPeakCount; ++i) {
TableRow nextRow = tableWorkspace->getRow(i);
std::string hklString, dString, qString, intensityString, fwhmString;
nextRow >> hklString >> dString >> qString >> intensityString >> fwhmString;
PoldiPeak_sptr peak = PoldiPeak::create(MillerIndicesIO::fromString(hklString),
UncertainValueIO::fromString(dString),
UncertainValueIO::fromString(intensityString),
UncertainValueIO::fromString(fwhmString));
m_peaks[i] = peak;
}
}
}
bool PoldiPeakCollection::checkColumns(TableWorkspace_sptr tableWorkspace)
{
if(tableWorkspace->columnCount() != 5) {
return false;
}
std::vector<std::string> shouldNames;
shouldNames.push_back("HKL");
shouldNames.push_back("d");
shouldNames.push_back("Q");
shouldNames.push_back("Intensity");
shouldNames.push_back("FWHM (rel.)");
std::vector<std::string> columnNames = tableWorkspace->getColumnNames();
return columnNames == shouldNames;
}
void PoldiPeakCollection::recoverDataFromLog(TableWorkspace_sptr tableWorkspace)
{
LogManager_sptr tableLog = tableWorkspace->logs();
m_intensityType = intensityTypeFromString(getIntensityTypeFromLog(tableLog));
m_profileFunctionName = getProfileFunctionNameFromLog(tableLog);
}
std::string PoldiPeakCollection::getIntensityTypeFromLog(LogManager_sptr tableLog)
{
return getStringValueFromLog(tableLog, "IntensityType");
}
std::string PoldiPeakCollection::getProfileFunctionNameFromLog(LogManager_sptr tableLog)
{
return getStringValueFromLog(tableLog, "ProfileFunctionName");
}
std::string PoldiPeakCollection::getStringValueFromLog(LogManager_sptr logManager, std::string valueName)
{
if(logManager->hasProperty(valueName)) {
return logManager->getPropertyValueAsType<std::string>(valueName);
}
return "";
}
std::string PoldiPeakCollection::intensityTypeToString(PoldiPeakCollection::IntensityType type) const
{
switch(type) {
case Maximum:
return "Maximum";
case Integral:
return "Integral";
}
throw std::runtime_error("Unkown intensity type can not be processed.");
}
PoldiPeakCollection::IntensityType PoldiPeakCollection::intensityTypeFromString(std::string typeString) const
{
std::string lowerCaseType(typeString);
std::transform(lowerCaseType.begin(), lowerCaseType.end(), lowerCaseType.begin(), ::tolower);
if(lowerCaseType == "integral") {
return Integral;
}
return Maximum;
}
}
}