From da22c5c0f3619d6e8d47756c1bbe397dabc47a73 Mon Sep 17 00:00:00 2001
From: Harry Jeffery <henry.jeffery@stfc.ac.uk>
Date: Wed, 13 May 2015 15:08:14 +0100
Subject: [PATCH] Refs #11704 Handle min/max betrer in QwtWorkspaceBinData

---
 .../API/inc/MantidQtAPI/QwtWorkspaceBinData.h | 10 ++-
 .../MantidQt/API/src/QwtWorkspaceBinData.cpp  | 83 +++++++++++--------
 2 files changed, 55 insertions(+), 38 deletions(-)

diff --git a/Code/Mantid/MantidQt/API/inc/MantidQtAPI/QwtWorkspaceBinData.h b/Code/Mantid/MantidQt/API/inc/MantidQtAPI/QwtWorkspaceBinData.h
index 1311bcb7cf6..33d3c9ba0bb 100644
--- a/Code/Mantid/MantidQt/API/inc/MantidQtAPI/QwtWorkspaceBinData.h
+++ b/Code/Mantid/MantidQt/API/inc/MantidQtAPI/QwtWorkspaceBinData.h
@@ -65,7 +65,6 @@ protected:
   QwtWorkspaceBinData& operator=(const QwtWorkspaceBinData&); // required by QwtData base class
 
 private:
-
   /// Initialize the object
   void init(const Mantid::API::MatrixWorkspace & workspace);
 
@@ -85,7 +84,14 @@ private:
 
   /// Indicates that the data is plotted on a log y scale
   bool m_logScale;
+
+  /// lowest y value
+  double m_minY;
+
   /// lowest positive y value
-  mutable double m_minPositive;
+  double m_minPositive;
+
+  /// highest y value
+  double m_maxY;
 };
 #endif
diff --git a/Code/Mantid/MantidQt/API/src/QwtWorkspaceBinData.cpp b/Code/Mantid/MantidQt/API/src/QwtWorkspaceBinData.cpp
index bd4163cd836..61f268b467d 100644
--- a/Code/Mantid/MantidQt/API/src/QwtWorkspaceBinData.cpp
+++ b/Code/Mantid/MantidQt/API/src/QwtWorkspaceBinData.cpp
@@ -7,8 +7,7 @@
 /// Constructor
 QwtWorkspaceBinData::QwtWorkspaceBinData(const Mantid::API::MatrixWorkspace &workspace, int binIndex, const bool logScale)
  : m_binIndex(binIndex), m_X(), m_Y(), m_E(), m_xTitle(), m_yTitle(),
-   m_logScale(logScale),
-   m_minPositive(0)
+   m_logScale(logScale)
 {
   init(workspace);
 }
@@ -53,12 +52,11 @@ Return the y value of data point i
 */
 double QwtWorkspaceBinData::y(size_t i) const
 {
-  double tmp = m_Y[i];
-  if (m_logScale && tmp <= 0.)
-  {
-    tmp = m_minPositive;
-  }
-  return tmp;
+  Mantid::signal_t val = m_Y[i];
+  if (m_logScale && val <= 0.)
+    return m_minPositive;
+  else
+    return val;
 }
 
 double QwtWorkspaceBinData::ex(size_t i) const
@@ -90,20 +88,10 @@ size_t QwtWorkspaceBinData::esize() const
  */
 double QwtWorkspaceBinData::getYMin() const
 {
-  double temp = m_Y[0];
-  for(size_t i = 1; i < m_Y.size(); ++i)
-  {
-    if ((boost::math::isnan)(temp) || (boost::math::isinf)(temp))
-      temp = m_Y[i];
-    else if (m_Y[i] < temp && !(boost::math::isnan)(m_Y[i]) &&
-        !(boost::math::isinf)(m_Y[i]))
-      temp = m_Y[i];
-  }
-  if (m_logScale && temp <= 0.)
-  {
-    temp = m_minPositive;
-  }
-  return temp;
+  if (m_logScale)
+    return m_minPositive;
+  else
+    return m_minY;
 }
 
 /**
@@ -112,20 +100,10 @@ double QwtWorkspaceBinData::getYMin() const
  */
 double QwtWorkspaceBinData::getYMax() const
 {
-  double temp = m_Y[0];
-  for(size_t i = 1; i < m_Y.size(); ++i)
-  {
-    if ((boost::math::isnan)(temp) || (boost::math::isinf)(temp))
-      temp = m_Y[i];
-    else if (m_Y[i] > temp && !(boost::math::isnan)(m_Y[i]) &&
-        !(boost::math::isinf)(m_Y[i]))
-      temp = m_Y[i];
-  }
-  if (m_logScale && temp <= 0.)
-  {
-    temp = m_minPositive;
-  }
-  return temp;
+  if (m_logScale && m_maxY <= 0)
+    return m_minPositive;
+  else
+    return m_maxY;
 }
 
 /**
@@ -215,4 +193,37 @@ void QwtWorkspaceBinData::init(const Mantid::API::MatrixWorkspace &workspace)
   // meta data
   m_xTitle = MantidQt::API::PlotAxis(workspace, 1).title();
   m_yTitle = MantidQt::API::PlotAxis(false, workspace).title();
+
+
+  // Calculate the min and max values
+  double curMin = m_Y[0];
+  double curMinPos = m_Y[0];
+  double curMax = m_Y[0];
+  for(size_t i = 1; i < m_Y.size(); ++i)
+  {
+    // skip NaNs
+    if ((boost::math::isnan)(m_Y[i]) || (boost::math::isinf)(m_Y[i]))
+      continue;
+
+    // Try and get rid any starting NaNs as soon as possible
+    if ((boost::math::isnan)(curMin) || (boost::math::isinf)(curMin))
+      curMin = m_Y[i];
+    if ((boost::math::isnan)(curMinPos) || (boost::math::isinf)(curMinPos))
+      curMinPos = m_Y[i];
+    if ((boost::math::isnan)(curMax) || (boost::math::isinf)(curMax))
+      curMax = m_Y[i];
+
+    // Update our values as appropriate
+    if (m_Y[i] < curMin)
+        curMin = m_Y[i];
+    if (m_Y[i] < curMinPos && m_Y[i] > 0)
+        curMinPos = m_Y[i];
+    if (m_Y[i] > curMax)
+      curMax = m_Y[i];
+  }
+
+  // Save the results
+  m_minY = curMin;
+  m_minPositive = curMinPos;
+  m_maxY = curMax;
 }
-- 
GitLab