From ef3fa905b60642588555b89847b8fdf6704d8408 Mon Sep 17 00:00:00 2001
From: Martyn Gigg <martyn.gigg@gmail.com>
Date: Tue, 27 Oct 2015 15:22:41 +0000
Subject: [PATCH] Fix bug with std::accumulate call.

The initial value had the wrong type so each additional was converting
the double to an int leading to the incorrect result.
Refs #14129
---
 .../Algorithms/src/CreateSampleWorkspace.cpp  |  2 +-
 .../test/CreateSampleWorkspaceTest.h          | 39 ++++++++++++-------
 docs/source/algorithms/AlignDetectors-v1.rst  |  2 +-
 docs/source/algorithms/CompressEvents-v1.rst  |  4 +-
 .../CreateFlatEventWorkspace-v1.rst           | 20 +++++-----
 .../algorithms/CreateSampleWorkspace-v1.rst   |  4 +-
 .../DiffractionEventCalibrateDetectors-v1.rst |  2 +-
 docs/source/algorithms/FilterBadPulses-v1.rst |  4 +-
 .../source/algorithms/FilterByLogValue-v1.rst |  4 +-
 docs/source/algorithms/FilterByTime-v1.rst    |  6 +--
 docs/source/algorithms/FilterByXValue-v1.rst  |  8 ++--
 docs/source/algorithms/SignalOverError-v1.rst | 20 +++++-----
 .../algorithms/SumEventsByLogValue-v1.rst     |  8 ++--
 docs/source/algorithms/UnwrapSNS-v1.rst       |  2 +-
 14 files changed, 67 insertions(+), 58 deletions(-)

diff --git a/Framework/Algorithms/src/CreateSampleWorkspace.cpp b/Framework/Algorithms/src/CreateSampleWorkspace.cpp
index 4976102fe90..3394e539cf9 100644
--- a/Framework/Algorithms/src/CreateSampleWorkspace.cpp
+++ b/Framework/Algorithms/src/CreateSampleWorkspace.cpp
@@ -349,7 +349,7 @@ EventWorkspace_sptr CreateSampleWorkspace::createEventWorkspace(
 
   // we need to normalise the results and then multiply by the number of events
   // to find the events per bin
-  double sum_of_elems = std::accumulate(yValues.begin(), yValues.end(), 0);
+  double sum_of_elems = std::accumulate(yValues.begin(), yValues.end(), 0.0);
   double event_distrib_factor = numEvents / sum_of_elems;
   std::transform(yValues.begin(), yValues.end(), yValues.begin(),
                  std::bind1st(std::multiplies<double>(), event_distrib_factor));
diff --git a/Framework/Algorithms/test/CreateSampleWorkspaceTest.h b/Framework/Algorithms/test/CreateSampleWorkspaceTest.h
index 4b93b2303a2..2049f1706b7 100644
--- a/Framework/Algorithms/test/CreateSampleWorkspaceTest.h
+++ b/Framework/Algorithms/test/CreateSampleWorkspaceTest.h
@@ -217,16 +217,25 @@ public:
     // Name of the output workspace.
     std::string outWSName("CreateSampleWorkspaceTest_OutputWS_event");
 
-    // Retrieve the workspace from data service. TODO: Change to your desired
-    // type
-    MatrixWorkspace_sptr ws = createSampleWorkspace(outWSName, "Event");
-    if (!ws)
-      return;
-    TS_ASSERT_DELTA(ws->readY(0)[20], 30, 0.0001);
-    TS_ASSERT_DELTA(ws->readY(0)[40], 30, 0.0001);
-    TS_ASSERT_DELTA(ws->readY(0)[50], 1030, 0.0001);
-    TS_ASSERT_DELTA(ws->readY(0)[60], 30, 0.0001);
-    TS_ASSERT_DELTA(ws->readY(0)[80], 30, 0.0001);
+    auto ws = boost::dynamic_pointer_cast<IEventWorkspace>(createSampleWorkspace(outWSName, "Event"));
+    TS_ASSERT(ws);
+    if(!ws) return;
+
+    // The number of events per spectrum is not simply nhist*events_per_hist
+    // The algorithm computes the number of events required per bin to give the
+    // total per spectrum as requested but can only add integer numbers of events
+    // so the number added will always be less than expected.
+    // In this case the background has 99 bins at 0.3 & 1 bin at 10.3 => total sum=40
+    //    signal_scale_factor = events_per_spec/40 = 1000/40=25  =>
+    //      background Y = 0.3*25 = 7.5 => int(7.5)=7
+    //      peak Y = 10.3*25 = 257.5 => int(257.5)=257
+    // Therefore in total we lose 0.5*100*200=10000 events
+    TS_ASSERT_EQUALS(ws->getNumberEvents(), 190000);
+    TS_ASSERT_DELTA(ws->readY(0)[20], 7, 0.0001);
+    TS_ASSERT_DELTA(ws->readY(0)[40], 7, 0.0001);
+    TS_ASSERT_DELTA(ws->readY(0)[50], 257, 0.0001);
+    TS_ASSERT_DELTA(ws->readY(0)[60], 7, 0.0001);
+    TS_ASSERT_DELTA(ws->readY(0)[80], 7, 0.0001);
 
     // Remove workspace from the data service.
     AnalysisDataService::Instance().remove(outWSName);
@@ -354,11 +363,11 @@ public:
     MatrixWorkspace_sptr ws = createSampleWorkspace(outWSName, "Event");
     if (!ws)
       return;
-    TS_ASSERT_DELTA(ws->readY(0)[20], 30, 50);
-    TS_ASSERT_DELTA(ws->readY(0)[40], 30, 50);
-    TS_ASSERT_DELTA(ws->readY(0)[50], 1030, 50);
-    TS_ASSERT_DELTA(ws->readY(0)[60], 30, 50);
-    TS_ASSERT_DELTA(ws->readY(0)[80], 3, 50);
+    TS_ASSERT_DELTA(ws->readY(0)[20], 7, 50);
+    TS_ASSERT_DELTA(ws->readY(0)[40], 7, 50);
+    TS_ASSERT_DELTA(ws->readY(0)[50], 257, 50);
+    TS_ASSERT_DELTA(ws->readY(0)[60], 7, 50);
+    TS_ASSERT_DELTA(ws->readY(0)[80], 7, 50);
 
     // Remove workspace from the data service.
     AnalysisDataService::Instance().remove(outWSName);
diff --git a/docs/source/algorithms/AlignDetectors-v1.rst b/docs/source/algorithms/AlignDetectors-v1.rst
index 556b10a9def..d6289740766 100644
--- a/docs/source/algorithms/AlignDetectors-v1.rst
+++ b/docs/source/algorithms/AlignDetectors-v1.rst
@@ -79,7 +79,7 @@ Output:
 .. testoutput:: ExAlignDetectors
 
     Peak in dSpace 2.66413186052
-    Peak from calibration 2.56009958218
+    Peak from calibration 2.5596132087
 
 
 .. categories::
diff --git a/docs/source/algorithms/CompressEvents-v1.rst b/docs/source/algorithms/CompressEvents-v1.rst
index af0a11a7a80..d716a23eec6 100644
--- a/docs/source/algorithms/CompressEvents-v1.rst
+++ b/docs/source/algorithms/CompressEvents-v1.rst
@@ -54,8 +54,8 @@ Output:
 .. testoutput:: CompressEvents
     :options: +NORMALIZE_WHITESPACE
 
-    The unfiltered workspace ws has 8000 events and a peak value of 1030.00
-    The compressed workspace ws still has 8000 events and a peak value of 1030.00
+    The unfiltered workspace ws has 1900 events and a peak value of 257.00
+    The compressed workspace ws still has 1900 events and a peak value of 257.00
     However it now takes up less memory.
 
 
diff --git a/docs/source/algorithms/CreateFlatEventWorkspace-v1.rst b/docs/source/algorithms/CreateFlatEventWorkspace-v1.rst
index 305a3ea415f..4f92d24c4e0 100644
--- a/docs/source/algorithms/CreateFlatEventWorkspace-v1.rst
+++ b/docs/source/algorithms/CreateFlatEventWorkspace-v1.rst
@@ -38,16 +38,16 @@ Output:
 
     The values for every 10th bin.
     bin     ws      wsOut
-    0       16.00   16.00
-    10      16.00   16.00
-    20      16.00   16.00
-    30      572.00  16.00
-    40      16.00   16.00
-    50      16.00   16.00
-    60      461.00  16.00
-    70      16.00   16.00
-    80      16.00   16.00
-    90      16.00   16.00
+    0       6.00    6.00
+    10      6.00    6.00
+    20      6.00    6.00
+    30      214.00  6.00
+    40      6.00   6.00
+    50      6.00   6.00
+    60      172.00  6.00
+    70      6.00   6.00
+    80      6.00   6.00
+    90      6.00   6.00
 
 
 
diff --git a/docs/source/algorithms/CreateSampleWorkspace-v1.rst b/docs/source/algorithms/CreateSampleWorkspace-v1.rst
index 04e566cc4a1..8c1e5387d56 100644
--- a/docs/source/algorithms/CreateSampleWorkspace-v1.rst
+++ b/docs/source/algorithms/CreateSampleWorkspace-v1.rst
@@ -99,9 +99,9 @@ Output:
 
    Number of spectra: 200
    Number of bins: 100
-   Number of events: 800000
+   Number of events: 190000
    Event Workspaces come with bins set by default to a bin width of 200.0
-   Each spectra has a level backgound of 30.0 counts and a peak in the centre of 1030.0 counts.
+   Each spectra has a level backgound of 7.0 counts and a peak in the centre of 257.0 counts.
 
 **Example - Using the preset functions:**
 
diff --git a/docs/source/algorithms/DiffractionEventCalibrateDetectors-v1.rst b/docs/source/algorithms/DiffractionEventCalibrateDetectors-v1.rst
index 6989dace535..521f4ad05d8 100644
--- a/docs/source/algorithms/DiffractionEventCalibrateDetectors-v1.rst
+++ b/docs/source/algorithms/DiffractionEventCalibrateDetectors-v1.rst
@@ -39,7 +39,7 @@ Output:
 .. testoutput:: ExDiffractionEventCalibrateDetectors
 
     Peak in dSpace 2.69077317913
-    Peak from calibration 2.67124691143
+    Peak from calibration 2.68342207235
 
 
 .. categories::
diff --git a/docs/source/algorithms/FilterBadPulses-v1.rst b/docs/source/algorithms/FilterBadPulses-v1.rst
index af338711ece..c46cd5a239e 100644
--- a/docs/source/algorithms/FilterBadPulses-v1.rst
+++ b/docs/source/algorithms/FilterBadPulses-v1.rst
@@ -40,8 +40,8 @@ Output:
 
 .. testoutput:: Filter
 
-    The number of events that remain: 4058 
-    compared to the number in the unfiltered workspace: 8000
+    The number of events that remain: 950 
+    compared to the number in the unfiltered workspace: 1900
 
 
 .. categories::
diff --git a/docs/source/algorithms/FilterByLogValue-v1.rst b/docs/source/algorithms/FilterByLogValue-v1.rst
index 5f12dab6a63..30ffb52cc8d 100644
--- a/docs/source/algorithms/FilterByLogValue-v1.rst
+++ b/docs/source/algorithms/FilterByLogValue-v1.rst
@@ -108,8 +108,8 @@ Output:
 
 .. testoutput:: FilterByLogValue
 
-   The unfiltered workspace ws has 8000 events and a peak value of 1030.00
-   The filtered workspace wsOut has 4058 events and a peak value of 502.00
+   The unfiltered workspace ws has 1900 events and a peak value of 257.00
+   The filtered workspace wsOut has 950 events and a peak value of 131.00
 
 
 .. categories::
diff --git a/docs/source/algorithms/FilterByTime-v1.rst b/docs/source/algorithms/FilterByTime-v1.rst
index 3274a8a26f7..ee384e79350 100644
--- a/docs/source/algorithms/FilterByTime-v1.rst
+++ b/docs/source/algorithms/FilterByTime-v1.rst
@@ -61,9 +61,9 @@ Output:
 
 .. testoutput:: ExFilter
 
-    The number of events within the relative Filter: 4058
-    The number of events within the Aboslute Filter: 1322
-    Compared to the number in the unfiltered workspace: 8000
+    The number of events within the relative Filter: 950
+    The number of events within the Aboslute Filter: 315
+    Compared to the number in the unfiltered workspace: 1900
 
 
 .. categories::
diff --git a/docs/source/algorithms/FilterByXValue-v1.rst b/docs/source/algorithms/FilterByXValue-v1.rst
index fdfa8866e9e..0870c0cbf37 100644
--- a/docs/source/algorithms/FilterByXValue-v1.rst
+++ b/docs/source/algorithms/FilterByXValue-v1.rst
@@ -35,8 +35,8 @@ Output:
 
 .. testoutput:: ExFilterTofByMax
 
-    8000 events before filtering
-    6500 events after filtering
+    1900 events before filtering
+    1550 events after filtering
 
 **Example: Applying Max and Min in Wavelength**
 
@@ -53,8 +53,8 @@ Output:
 
 .. testoutput:: ExFilterWavelengthByMinMax
 
-    8000 events before filtering
-    4653 events after filtering
+    1900 events before filtering
+    1118 events after filtering
 
 
 .. categories::
diff --git a/docs/source/algorithms/SignalOverError-v1.rst b/docs/source/algorithms/SignalOverError-v1.rst
index 7af4426cfe1..2b5d42cabf8 100644
--- a/docs/source/algorithms/SignalOverError-v1.rst
+++ b/docs/source/algorithms/SignalOverError-v1.rst
@@ -36,16 +36,16 @@ Output:
 
     Values from every 10th bin
     bin     Y       E       Y_New
-    0       30.00   5.48    5.48
-    10      30.00   5.48    5.48
-    20      30.00   5.48    5.48
-    30      30.00   5.48    5.48
-    40      30.00   5.48    5.48
-    50      1030.00 32.09   32.09
-    60      30.00   5.48    5.48
-    70      30.00   5.48    5.48
-    80      30.00   5.48    5.48
-    90      30.00   5.48    5.48
+    0       7.00   2.65    2.65
+    10      7.00   2.65    2.65
+    20      7.00   2.65    2.65
+    30      7.00   2.65    2.65
+    40      7.00   2.65    2.65
+    50      257.00 16.03   16.03
+    60      7.00   2.65    2.65
+    70      7.00   2.65    2.65
+    80      7.00   2.65    2.65
+    90      7.00   2.65    2.65
 
 
 .. categories::
diff --git a/docs/source/algorithms/SumEventsByLogValue-v1.rst b/docs/source/algorithms/SumEventsByLogValue-v1.rst
index 77a9b2cbdfa..5ec1435e499 100644
--- a/docs/source/algorithms/SumEventsByLogValue-v1.rst
+++ b/docs/source/algorithms/SumEventsByLogValue-v1.rst
@@ -80,10 +80,10 @@ Output:
 .. testoutput:: Single-Spectrum
 
     Events were split into 3 sections based on the log 'Log2FilterBy'.
-     section 1: 2720.00
-     section 2: 2643.00
-     section 3: 2637.00
-    Totalling 8000 events, matching the 8000 events in the input workspace
+     section 1: 615.00
+     section 2: 629.00
+     section 3: 656.00
+    Totalling 1900 events, matching the 1900 events in the input workspace
 
 
 .. categories::
diff --git a/docs/source/algorithms/UnwrapSNS-v1.rst b/docs/source/algorithms/UnwrapSNS-v1.rst
index 985747860f3..7331b37c348 100644
--- a/docs/source/algorithms/UnwrapSNS-v1.rst
+++ b/docs/source/algorithms/UnwrapSNS-v1.rst
@@ -61,7 +61,7 @@ Output:
 
 .. testoutput:: ExUnwrapSNS
 
-    Input 30.0
+    Input 7.0
     Output 0.0
 
 .. categories::
-- 
GitLab