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 +
10#include "MantidKernel/Logger.h"
13
14#include <boost/variant/get.hpp>
15
16using namespace Mantid::Kernel;
17
18namespace Mantid::DataObjects {
19namespace {
21Kernel::Logger g_log("PeakColumn");
22
24size_t NCELL_ITEM_CACHED = 100;
25
30class ColumnNameToType {
31public:
32 ColumnNameToType() {
33 // Assume double if not in this map
34 m_type_index.emplace("DetID", "int");
35 m_type_index.emplace("RunNumber", "int");
36 m_type_index.emplace("h", "double");
37 m_type_index.emplace("k", "double");
38 m_type_index.emplace("l", "double");
39 m_type_index.emplace("Wavelength", "double");
40 m_type_index.emplace("Energy", "double");
41 m_type_index.emplace("TOF", "double");
42 m_type_index.emplace("DSpacing", "double");
43 m_type_index.emplace("Intens", "double");
44 m_type_index.emplace("SigInt", "double");
45 m_type_index.emplace("Intens/SigInt", "double");
46 m_type_index.emplace("BinCount", "double");
47 m_type_index.emplace("BankName", "str");
48 m_type_index.emplace("Row", "double");
49 m_type_index.emplace("Col", "double");
50 m_type_index.emplace("QLab", "V3D");
51 m_type_index.emplace("QSample", "V3D");
52 m_type_index.emplace("PeakNumber", "int");
53 m_type_index.emplace("TBar", "double");
54 m_type_index.emplace("IntHKL", "V3D");
55 m_type_index.emplace("IntMNP", "V3D");
56 }
57
58 inline const auto &data() const { return m_type_index; }
60private:
61 std::unordered_map<std::string, std::string> m_type_index;
62};
63
69const std::string typeFromName(const std::string &name) {
70 static ColumnNameToType typeIndex;
71 auto iter = typeIndex.data().find(name);
72 if (iter != typeIndex.data().end()) {
73 return iter->second;
74 } else {
75 throw std::runtime_error("PeakColumn - Unknown column name: \"" + name +
76 "\""
77 "Peak column names/types must be explicitly marked in PeakColumn.cpp");
78 }
80} // namespace
82//----------------------------------------------------------------------------------------------
87template <class T>
88PeakColumn<T>::PeakColumn(std::vector<T> &peaks, const std::string &name) : m_peaks(peaks), m_oldRows() {
89 this->m_name = name;
90 this->m_type = typeFromName(name); // Throws if the name is unknown
91 const std::string key = "PeakColumn.hklPrec";
92 auto hklPrec = ConfigService::Instance().getValue<int>(key);
93 this->m_hklPrec = hklPrec.value_or(2);
94 if (!hklPrec.has_value()) {
95 g_log.information() << "In PeakColumn constructor, did not find any value for '" << key
96 << "' from the Config Service. Using default: " << this->m_hklPrec << "\n";
97 }
98}
99
101template <class T> const std::type_info &PeakColumn<T>::get_type_info() const {
102 // This is horrible copy-and-paste with the method below. The whole thing
103 // around columns could be much better implemented using templates & traits to
104 // avoid this
105 // type of thing!
106
107 if (type() == "double") {
108 return typeid(double);
109 } else if (type() == "int") {
110 return typeid(int);
111 } else if (type() == "str") {
112 return typeid(std::string);
113 } else if (type() == "V3D") {
114 return typeid(V3D);
115 } else {
116 throw std::runtime_error("PeakColumn::get_type_info() - Unknown column type: " + m_name);
117 }
118}
119
121template <class T> const std::type_info &PeakColumn<T>::get_pointer_type_info() const {
122 if (type() == "double") {
123 return typeid(double *);
124 } else if (type() == "int") {
125 return typeid(int *);
126 } else if (type() == "str") {
127 return typeid(std::string *);
128 } else if (type() == "V3D") {
129 return typeid(V3D *);
130 } else {
131 throw std::runtime_error("PeakColumn::get_pointer_type_info() -: " + m_name);
132 }
133}
134
135//-------------------------------------------------------------------------------------
141template <class T> void PeakColumn<T>::print(size_t index, std::ostream &s) const {
142 T &peak = m_peaks[index];
143 auto fullPeak = dynamic_cast<Peak *>(&peak); // additional printout for class Peak
144
145 s.imbue(std::locale("C"));
146 std::ios::fmtflags fflags(s.flags());
147 if (m_name == "RunNumber")
148 s << peak.getRunNumber();
149 else if (m_name == "DetID" && fullPeak) {
150 s << fullPeak->getDetectorID();
151 } else if (m_name == "BankName" && fullPeak)
152 s << fullPeak->getBankName();
153 else if (m_name == "QLab")
154 s << peak.getQLabFrame();
155 else if (m_name == "QSample")
156 s << peak.getQSampleFrame();
157 else if (m_name == "h") {
158 s << std::fixed << std::setprecision(m_hklPrec) << peak.getH();
159 } else if (m_name == "k") {
160 s << std::fixed << std::setprecision(m_hklPrec) << peak.getK();
161 } else if (m_name == "l") {
162 s << std::fixed << std::setprecision(m_hklPrec) << peak.getL();
163 } else if (m_name == "PeakNumber") {
164 s << peak.getPeakNumber();
165 } else if (m_name == "IntHKL") {
166 s << peak.getIntHKL();
167 } else if (m_name == "IntMNP") {
168 s << peak.getIntMNP();
169 } else
170 s << peak.getValueByColName(m_name);
171 s.flags(fflags);
172}
173
174//-------------------------------------------------------------------------------------
180template <class T> void PeakColumn<T>::read(size_t index, const std::string &text) {
181 // Don't modify read-only ones
182 if (this->getReadOnly() || index >= m_peaks.size())
183 return;
184
185 // Convert to a double
186 double val = 0;
187 int success = Strings::convert(text, val);
188
189 if (success == 0) {
190 g_log.error() << "Could not convert string '" << text << "' to a number.\n";
191 return;
192 }
193 setPeakHKLOrRunNumber(index, val);
194}
195
201template <class T> void PeakColumn<T>::read(const size_t index, std::istringstream &in) {
202 if (this->getReadOnly() || index >= m_peaks.size())
203 return;
204
205 double val;
206 try {
207 in >> val;
208 } catch (std::exception &e) {
209 g_log.error() << "Could not convert input to a number. " << e.what() << '\n';
210 return;
211 }
212
213 setPeakHKLOrRunNumber(index, val);
214}
215
216//-------------------------------------------------------------------------------------
218template <class T> bool PeakColumn<T>::getReadOnly() const {
219 return !((m_name == "h") || (m_name == "k") || (m_name == "l") || (m_name == "RunNumber"));
220}
221
222//-------------------------------------------------------------------------------------
224template <class T> bool PeakColumn<T>::isBool() const { return false; }
225
226template <class T> bool PeakColumn<T>::isNumber() const { return false; }
227
229template <class T> long int PeakColumn<T>::sizeOfData() const {
230 return sizeof(double) * static_cast<long int>(m_peaks.size());
231}
232
239template <class T> void PeakColumn<T>::resize(size_t count) {
241 throw Exception::NotImplementedError("PeakColumn::resize - Peaks must be "
242 "added through the PeaksWorkspace "
243 "interface.");
244}
245
252template <class T> void PeakColumn<T>::insert(size_t index) {
254 throw Exception::NotImplementedError("PeakColumn::insert - Peaks must be "
255 "inserted through the PeaksWorkspace "
256 "interface.");
257}
258
265template <class T> void PeakColumn<T>::remove(size_t index) {
267 throw Exception::NotImplementedError("PeakColumn::remove - Peaks must be "
268 "remove through the PeaksWorkspace "
269 "interface.");
270}
271
277template <class T> void *PeakColumn<T>::void_pointer(size_t index) {
278 const auto *constThis = const_cast<const PeakColumn *>(this);
279 return const_cast<void *>(constThis->void_pointer(index));
280}
281
287template <class T> const void *PeakColumn<T>::void_pointer(size_t index) const {
288 const T &peak = m_peaks[index];
289 auto fullPeak = dynamic_cast<const DataObjects::Peak *>(&peak); // additional methods available in class Peak
290
291 // The cell() api requires that the value exist somewhere in memory, however,
292 // some of the values from a Peak are calculated on the fly so a reference
293 // cannot be returned. Instead we cache a value for the last NCELL_ITEM_CACHED
294 // accesses and return a reference to this
295
296 m_oldRows.push_front(CacheValueType());
297 if (m_oldRows.size() > NCELL_ITEM_CACHED) {
298 m_oldRows.pop_back();
299 }
300 auto &value = m_oldRows.front(); // A reference to the actual stored variant
301
302 if (type() == "double") {
303 value = peak.getValueByColName(m_name); // Assign the value to the store
304 return boost::get<double>(&value); // Given a pointer it will return a pointer
305 } else if (m_name == "RunNumber") {
306 value = peak.getRunNumber();
307 return boost::get<int>(&value);
308 } else if (m_name == "PeakNumber") {
309 value = peak.getPeakNumber();
310 return boost::get<int>(&value);
311 } else if (m_name == "DetID" && fullPeak) {
312 value = fullPeak->getDetectorID();
313 return boost::get<int>(&value);
314 } else if (m_name == "BankName" && fullPeak) {
315 value = fullPeak->getBankName();
316 return boost::get<std::string>(&value);
317 } else if (m_name == "QLab") {
318 value = peak.getQLabFrame();
319 return boost::get<Kernel::V3D>(&value);
320 } else if (m_name == "QSample") {
321 value = peak.getQSampleFrame();
322 return boost::get<Kernel::V3D>(&value);
323 } else if (m_name == "IntHKL") {
324 value = peak.getIntHKL();
325 return boost::get<Kernel::V3D>(&value);
326 } else if (m_name == "IntMNP") {
327 value = peak.getIntMNP();
328 return boost::get<Kernel::V3D>(&value);
329 } else {
330 throw std::runtime_error("void_pointer() - Unknown peak column name or type: " + m_name);
331 }
332}
333
334template <class T> PeakColumn<T> *PeakColumn<T>::clone() const {
335 auto temp = new PeakColumn<T>(this->m_peaks, this->m_name);
336 return temp;
337}
338
339template <class T> double PeakColumn<T>::toDouble(size_t /*index*/) const {
340 throw std::runtime_error("PeakColumn::toDouble() not implemented, PeakColumn "
341 "is has no general write access");
342}
343
344template <class T> void PeakColumn<T>::fromDouble(size_t /*index*/, double /*value*/) {
345 throw std::runtime_error("fromDouble() not implemented, PeakColumn is has no "
346 "general write access");
347}
348
349template <class T> void PeakColumn<T>::setPeakHKLOrRunNumber(const size_t index, const double val) {
350 T &peak = m_peaks[index];
351 if (m_name == "h")
352 peak.setH(val);
353 else if (m_name == "k")
354 peak.setK(val);
355 else if (m_name == "l")
356 peak.setL(val);
357 else if (m_name == "RunNumber")
358 peak.setRunNumber(static_cast<int>(val));
359 else
360 throw std::runtime_error("Unexpected column " + m_name + " being set.");
361}
362
363template class PeakColumn<Peak>;
364template class PeakColumn<LeanElasticPeak>;
365
366} // 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:44
std::string m_type
type
Definition Column.h:187
const std::string & name() const
Name (caption) of the column.
Definition Column.h:43
std::string m_name
name
Definition Column.h:186
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:627
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:35
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:697