Mantid
Loading...
Searching...
No Matches
PeakColumn.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
12
13#include <boost/variant/get.hpp>
14
15using namespace Mantid::Kernel;
16
17namespace Mantid::DataObjects {
18namespace {
20Kernel::Logger g_log("PeakColumn");
21
23size_t NCELL_ITEM_CACHED = 100;
24
29class ColumnNameToType {
30public:
31 ColumnNameToType() {
32 // Assume double if not in this map
33 m_type_index.emplace("DetID", "int");
34 m_type_index.emplace("RunNumber", "int");
35 m_type_index.emplace("h", "double");
36 m_type_index.emplace("k", "double");
37 m_type_index.emplace("l", "double");
38 m_type_index.emplace("Wavelength", "double");
39 m_type_index.emplace("Energy", "double");
40 m_type_index.emplace("TOF", "double");
41 m_type_index.emplace("DSpacing", "double");
42 m_type_index.emplace("Intens", "double");
43 m_type_index.emplace("SigInt", "double");
44 m_type_index.emplace("Intens/SigInt", "double");
45 m_type_index.emplace("BinCount", "double");
46 m_type_index.emplace("BankName", "str");
47 m_type_index.emplace("Row", "double");
48 m_type_index.emplace("Col", "double");
49 m_type_index.emplace("QLab", "V3D");
50 m_type_index.emplace("QSample", "V3D");
51 m_type_index.emplace("PeakNumber", "int");
52 m_type_index.emplace("TBar", "double");
53 m_type_index.emplace("IntHKL", "V3D");
54 m_type_index.emplace("IntMNP", "V3D");
55 }
57 inline const auto &data() const { return m_type_index; }
58
59private:
60 std::unordered_map<std::string, std::string> m_type_index;
61};
68const std::string typeFromName(const std::string &name) {
69 static ColumnNameToType typeIndex;
70 auto iter = typeIndex.data().find(name);
71 if (iter != typeIndex.data().end()) {
72 return iter->second;
73 } else {
74 throw std::runtime_error("PeakColumn - Unknown column name: \"" + name +
75 "\""
76 "Peak column names/types must be explicitly marked in PeakColumn.cpp");
77 }
78}
79} // namespace
80
81//----------------------------------------------------------------------------------------------
86template <class T>
87PeakColumn<T>::PeakColumn(std::vector<T> &peaks, const std::string &name) : m_peaks(peaks), m_oldRows() {
88 this->m_name = name;
89 this->m_type = typeFromName(name); // Throws if the name is unknown
90 const std::string key = "PeakColumn.hklPrec";
91 auto hklPrec = ConfigService::Instance().getValue<int>(key);
92 this->m_hklPrec = hklPrec.value_or(2);
93 if (!hklPrec.has_value()) {
94 g_log.information() << "In PeakColumn constructor, did not find any value for '" << key
95 << "' from the Config Service. Using default: " << this->m_hklPrec << "\n";
96 }
97}
98
100template <class T> const std::type_info &PeakColumn<T>::get_type_info() const {
101 // This is horrible copy-and-paste with the method below. The whole thing
102 // around columns could be much better implemented using templates & traits to
103 // avoid this
104 // type of thing!
105
106 if (type() == "double") {
107 return typeid(double);
108 } else if (type() == "int") {
109 return typeid(int);
110 } else if (type() == "str") {
111 return typeid(std::string);
112 } else if (type() == "V3D") {
113 return typeid(V3D);
114 } else {
115 throw std::runtime_error("PeakColumn::get_type_info() - Unknown column type: " + m_name);
116 }
117}
118
120template <class T> const std::type_info &PeakColumn<T>::get_pointer_type_info() const {
121 if (type() == "double") {
122 return typeid(double *);
123 } else if (type() == "int") {
124 return typeid(int *);
125 } else if (type() == "str") {
126 return typeid(std::string *);
127 } else if (type() == "V3D") {
128 return typeid(V3D *);
129 } else {
130 throw std::runtime_error("PeakColumn::get_pointer_type_info() -: " + m_name);
131 }
132}
133
134//-------------------------------------------------------------------------------------
140template <class T> void PeakColumn<T>::print(size_t index, std::ostream &s) const {
141 T &peak = m_peaks[index];
142 auto fullPeak = dynamic_cast<Peak *>(&peak); // additional printout for class Peak
143
144 s.imbue(std::locale("C"));
145 std::ios::fmtflags fflags(s.flags());
146 if (m_name == "RunNumber")
147 s << peak.getRunNumber();
148 else if (m_name == "DetID" && fullPeak) {
149 s << fullPeak->getDetectorID();
150 } else if (m_name == "BankName" && fullPeak)
151 s << fullPeak->getBankName();
152 else if (m_name == "QLab")
153 s << peak.getQLabFrame();
154 else if (m_name == "QSample")
155 s << peak.getQSampleFrame();
156 else if (m_name == "h") {
157 s << std::fixed << std::setprecision(m_hklPrec) << peak.getH();
158 } else if (m_name == "k") {
159 s << std::fixed << std::setprecision(m_hklPrec) << peak.getK();
160 } else if (m_name == "l") {
161 s << std::fixed << std::setprecision(m_hklPrec) << peak.getL();
162 } else if (m_name == "PeakNumber") {
163 s << peak.getPeakNumber();
164 } else if (m_name == "IntHKL") {
165 s << peak.getIntHKL();
166 } else if (m_name == "IntMNP") {
167 s << peak.getIntMNP();
168 } else
169 s << peak.getValueByColName(m_name);
170 s.flags(fflags);
171}
172
173//-------------------------------------------------------------------------------------
179template <class T> void PeakColumn<T>::read(size_t index, const std::string &text) {
180 // Don't modify read-only ones
181 if (this->getReadOnly() || index >= m_peaks.size())
182 return;
183
184 // Convert to a double
185 double val = 0;
186 int success = Strings::convert(text, val);
187
188 if (success == 0) {
189 g_log.error() << "Could not convert string '" << text << "' to a number.\n";
190 return;
191 }
192 setPeakHKLOrRunNumber(index, val);
193}
194
200template <class T> void PeakColumn<T>::read(const size_t index, std::istringstream &in) {
201 if (this->getReadOnly() || index >= m_peaks.size())
202 return;
203
204 double val;
205 try {
206 in >> val;
207 } catch (std::exception &e) {
208 g_log.error() << "Could not convert input to a number. " << e.what() << '\n';
209 return;
210 }
211
212 setPeakHKLOrRunNumber(index, val);
213}
214
215//-------------------------------------------------------------------------------------
217template <class T> bool PeakColumn<T>::getReadOnly() const {
218 return !((m_name == "h") || (m_name == "k") || (m_name == "l") || (m_name == "RunNumber"));
219}
220
221//-------------------------------------------------------------------------------------
223template <class T> bool PeakColumn<T>::isBool() const { return false; }
224
225template <class T> bool PeakColumn<T>::isNumber() const { return false; }
226
228template <class T> long int PeakColumn<T>::sizeOfData() const {
229 return sizeof(double) * static_cast<long int>(m_peaks.size());
230}
231
238template <class T> void PeakColumn<T>::resize(size_t count) {
240 throw Exception::NotImplementedError("PeakColumn::resize - Peaks must be "
241 "added through the PeaksWorkspace "
242 "interface.");
243}
244
251template <class T> void PeakColumn<T>::insert(size_t index) {
253 throw Exception::NotImplementedError("PeakColumn::insert - Peaks must be "
254 "inserted through the PeaksWorkspace "
255 "interface.");
256}
257
264template <class T> void PeakColumn<T>::remove(size_t index) {
266 throw Exception::NotImplementedError("PeakColumn::remove - Peaks must be "
267 "remove through the PeaksWorkspace "
268 "interface.");
269}
270
276template <class T> void *PeakColumn<T>::void_pointer(size_t index) {
277 const auto *constThis = const_cast<const PeakColumn *>(this);
278 return const_cast<void *>(constThis->void_pointer(index));
279}
280
286template <class T> const void *PeakColumn<T>::void_pointer(size_t index) const {
287 const T &peak = m_peaks[index];
288 auto fullPeak = dynamic_cast<const DataObjects::Peak *>(&peak); // additional methods available in class Peak
289
290 // The cell() api requires that the value exist somewhere in memory, however,
291 // some of the values from a Peak are calculated on the fly so a reference
292 // cannot be returned. Instead we cache a value for the last NCELL_ITEM_CACHED
293 // accesses and return a reference to this
294
295 m_oldRows.push_front(CacheValueType());
296 if (m_oldRows.size() > NCELL_ITEM_CACHED) {
297 m_oldRows.pop_back();
298 }
299 auto &value = m_oldRows.front(); // A reference to the actual stored variant
300
301 if (type() == "double") {
302 value = peak.getValueByColName(m_name); // Assign the value to the store
303 return boost::get<double>(&value); // Given a pointer it will return a pointer
304 } else if (m_name == "RunNumber") {
305 value = peak.getRunNumber();
306 return boost::get<int>(&value);
307 } else if (m_name == "PeakNumber") {
308 value = peak.getPeakNumber();
309 return boost::get<int>(&value);
310 } else if (m_name == "DetID" && fullPeak) {
311 value = fullPeak->getDetectorID();
312 return boost::get<int>(&value);
313 } else if (m_name == "BankName" && fullPeak) {
314 value = fullPeak->getBankName();
315 return boost::get<std::string>(&value);
316 } else if (m_name == "QLab") {
317 value = peak.getQLabFrame();
318 return boost::get<Kernel::V3D>(&value);
319 } else if (m_name == "QSample") {
320 value = peak.getQSampleFrame();
321 return boost::get<Kernel::V3D>(&value);
322 } else if (m_name == "IntHKL") {
323 value = peak.getIntHKL();
324 return boost::get<Kernel::V3D>(&value);
325 } else if (m_name == "IntMNP") {
326 value = peak.getIntMNP();
327 return boost::get<Kernel::V3D>(&value);
328 } else {
329 throw std::runtime_error("void_pointer() - Unknown peak column name or type: " + m_name);
330 }
331}
332
333template <class T> PeakColumn<T> *PeakColumn<T>::clone() const {
334 auto temp = new PeakColumn<T>(this->m_peaks, this->m_name);
335 return temp;
336}
337
338template <class T> double PeakColumn<T>::toDouble(size_t /*index*/) const {
339 throw std::runtime_error("PeakColumn::toDouble() not implemented, PeakColumn "
340 "is has no general write access");
341}
342
343template <class T> void PeakColumn<T>::fromDouble(size_t /*index*/, double /*value*/) {
344 throw std::runtime_error("fromDouble() not implemented, PeakColumn is has no "
345 "general write access");
346}
347
348template <class T> void PeakColumn<T>::setPeakHKLOrRunNumber(const size_t index, const double val) {
349 T &peak = m_peaks[index];
350 if (m_name == "h")
351 peak.setH(val);
352 else if (m_name == "k")
353 peak.setK(val);
354 else if (m_name == "l")
355 peak.setL(val);
356 else if (m_name == "RunNumber")
357 peak.setRunNumber(static_cast<int>(val));
358 else
359 throw std::runtime_error("Unexpected column " + m_name + " being set.");
360}
361
362template class PeakColumn<Peak>;
363template class PeakColumn<LeanElasticPeak>;
364
365} // namespace Mantid::DataObjects
std::string name
Definition Run.cpp:60
double value
The value of the point.
Definition FitMW.cpp:51
std::map< DeltaEMode::Type, std::string > index
int count
counter
Definition Matrix.cpp:37
std::unordered_map< std::string, std::string > m_type_index
#define UNUSED_ARG(x)
Function arguments are sometimes unused in certain implmentations but are required for documentation ...
Definition System.h:48
std::string m_type
type
Definition Column.h:190
const std::string & name() const
Name (caption) of the column.
Definition Column.h:43
std::string m_name
name
Definition Column.h:189
PeakColumn : a Column sub-class used to display peak information as a TableWorkspace.
Definition PeakColumn.h:28
void fromDouble(size_t i, double value) override
Assign from double.
const std::type_info & get_type_info() const override
Returns typeid for the data in the column.
void * void_pointer(size_t index) override
Pointer to a data element.
void remove(size_t index) override
Removes an item.
bool isBool() const override
Specialized type check.
void insert(size_t index) override
Inserts an item.
bool getReadOnly() const override
PeakColumn(std::vector< T > &peaks, const std::string &name)
Construct a column with a reference to the peaks list, a name & type.
void setPeakHKLOrRunNumber(const size_t index, const double val)
Sets the correct value in the referenced peak.
const std::type_info & get_pointer_type_info() const override
Returns typeid for the pointer type to the data element in the column.
int m_hklPrec
Precision of hkl in table workspace.
Definition PeakColumn.h:93
bool isNumber() const override
Are elements of the column interpretable as a number?
void read(size_t index, const std::string &text) override
Read in some text and convert to a number in the PeaksWorkspace.
double toDouble(size_t i) const override
Cast to double.
void resize(size_t count) override
Sets the new column size.
boost::variant< double, int, std::string, Kernel::V3D > CacheValueType
Type of the row cache value.
Definition PeakColumn.h:96
long int sizeOfData() const override
Must return overall memory size taken by the column.
PeakColumn * clone() const override
Clone.
void print(size_t index, std::ostream &s) const override
Prints.
Structure describing a single-crystal peak.
Definition Peak.h:34
double getValueByColName(std::string colName) const override
Helper function for displaying/sorting peaks.
Definition Peak.cpp:603
Marks code as not implemented yet.
Definition Exception.h:138
The Logger class is in charge of the publishing messages from the framework through various channels.
Definition Logger.h:51
void error(const std::string &msg)
Logs at error level.
Definition Logger.cpp:108
void information(const std::string &msg)
Logs at information level.
Definition Logger.cpp:136
Class for 3D vectors.
Definition V3D.h:34
Kernel::Logger g_log("ExperimentInfo")
static logger object
int convert(const std::string &A, T &out)
Convert a string into a number.
Definition Strings.cpp:696