Skip to content
Snippets Groups Projects
Commit fa37a99a authored by Antti Soininen's avatar Antti Soininen
Browse files

Fix a bug in PeakColumn::read().

Re #18366
parent 1f537432
No related merge requests found
...@@ -80,6 +80,7 @@ private: ...@@ -80,6 +80,7 @@ private:
typedef boost::variant<double, int, std::string, Kernel::V3D> CacheValueType; typedef boost::variant<double, int, std::string, Kernel::V3D> CacheValueType;
/// ///
mutable std::list<CacheValueType> m_oldRows; mutable std::list<CacheValueType> m_oldRows;
void setPeakHKLOrRunNumber(const size_t index, const double val);
}; };
} // namespace Mantid } // namespace Mantid
......
...@@ -165,36 +165,18 @@ void PeakColumn::print(size_t index, std::ostream &s) const { ...@@ -165,36 +165,18 @@ void PeakColumn::print(size_t index, std::ostream &s) const {
*/ */
void PeakColumn::read(size_t index, const std::string &text) { void PeakColumn::read(size_t index, const std::string &text) {
// Don't modify read-only ones // Don't modify read-only ones
if (this->getReadOnly()) if (this->getReadOnly() || index >= m_peaks.size())
return; return;
// Avoid going out of bounds
if (size_t(index) >= m_peaks.size())
return;
// Reference to the peak in the workspace
Peak &peak = m_peaks[index];
// Convert to a double // Convert to a double
double val = 0; double val = 0;
int success = Strings::convert(text, val); int success = Strings::convert(text, val);
int ival = static_cast<int>(val);
if (success == 0) { if (success == 0) {
g_log.error() << "Could not convert string '" << text << "' to a number.\n"; g_log.error() << "Could not convert string '" << text << "' to a number.\n";
return; return;
} }
setPeakHKLOrRunNumber(index, val);
if (m_name == "h")
peak.setH(val);
else if (m_name == "k")
peak.setK(val);
else if (m_name == "l")
peak.setL(val);
else if (m_name == "RunNumber")
peak.setRunNumber(ival);
else
throw std::runtime_error("Unexpected column " + m_name + " being set.");
} }
/** Read in from stream and convert to a number in the PeaksWorkspace /** Read in from stream and convert to a number in the PeaksWorkspace
...@@ -203,9 +185,19 @@ void PeakColumn::read(size_t index, const std::string &text) { ...@@ -203,9 +185,19 @@ void PeakColumn::read(size_t index, const std::string &text) {
* @param in :: input stream * @param in :: input stream
*/ */
void PeakColumn::read(const size_t index, std::istream &in) { void PeakColumn::read(const size_t index, std::istream &in) {
std::string s; if (this->getReadOnly() || index >= m_peaks.size())
in >> s; return;
read(index, s);
double val;
try {
in >> val;
}
catch (std::exception &e) {
g_log.error() << "Could not convert input to a number.\n";
return;
}
setPeakHKLOrRunNumber(index, val);
} }
//------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------
...@@ -332,5 +324,19 @@ void PeakColumn::fromDouble(size_t /*index*/, double /*value*/) { ...@@ -332,5 +324,19 @@ void PeakColumn::fromDouble(size_t /*index*/, double /*value*/) {
"general write access"); "general write access");
} }
void PeakColumn::setPeakHKLOrRunNumber(const size_t index, const double val) {
Peak &peak = m_peaks[index];
if (m_name == "h")
peak.setH(val);
else if (m_name == "k")
peak.setK(val);
else if (m_name == "l")
peak.setL(val);
else if (m_name == "RunNumber")
peak.setRunNumber(static_cast<int>(val));
else
throw std::runtime_error("Unexpected column " + m_name + " being set.");
}
} // namespace Mantid } // namespace Mantid
} // namespace DataObjects } // namespace DataObjects
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment