From 46401b9b82a8ef35eaa680bcb9cdfa226b0e40c6 Mon Sep 17 00:00:00 2001
From: Harriet Brown <harriet.brown@stfc.ac.uk>
Date: Thu, 17 Oct 2019 16:04:15 +0100
Subject: [PATCH] Correct CalculatePlaczekSelfScattering and POLARIS script

CalculatePlaczekSelfScattering outputs non-distrebution workspace
correction to CalculatePlaczekSelfScattering algorithm and POLARIS
_generate_grouped_ts_pdf function makes self scattering correction
to the correct order of magnetude after processing
---
 .../Algorithms/src/CalculatePlaczekSelfScattering.cpp     | 5 +++--
 .../isis_powder/polaris_routines/polaris_algs.py          | 8 ++++----
 2 files changed, 7 insertions(+), 6 deletions(-)

diff --git a/Framework/Algorithms/src/CalculatePlaczekSelfScattering.cpp b/Framework/Algorithms/src/CalculatePlaczekSelfScattering.cpp
index db46d119f72..1d6a92b9c5c 100644
--- a/Framework/Algorithms/src/CalculatePlaczekSelfScattering.cpp
+++ b/Framework/Algorithms/src/CalculatePlaczekSelfScattering.cpp
@@ -117,7 +117,7 @@ void CalculatePlaczekSelfScattering::exec() {
   std::vector<double> eps1;
   const double lambdaD = 1.44;
   for (size_t i = 0; i < xLambda.size() - 1; i++) {
-    double xTerm = (xLambda[i] + dx) / lambdaD;
+    double xTerm = -(xLambda[i] + dx) / lambdaD;
     eps1.push_back(xTerm * exp(xTerm) / (1.0 - exp(xTerm)));
   }
   /* Placzek
@@ -168,7 +168,7 @@ void CalculatePlaczekSelfScattering::exec() {
             2.0 * (term1 + term2 - 3) * sinThetaBy2 * sinThetaBy2 *
             summationTerm;
         x[xIndex] = wavelength.singleToTOF(xLambda[xIndex]);
-        y[xIndex] = inelasticPlaczekSelfCorrection;
+        y[xIndex] = 1 + inelasticPlaczekSelfCorrection;
       }
       x.back() = wavelength.singleToTOF(xLambda.back());
     } else {
@@ -181,6 +181,7 @@ void CalculatePlaczekSelfScattering::exec() {
   }
   auto incidentUnit = inWS->getAxis(0)->unit();
   outputWS->getAxis(0)->unit() = incidentUnit;
+  outputWS->setDistribution(FALSE);
   setProperty("OutputWorkspace", outputWS);
 }
 
diff --git a/scripts/Diffraction/isis_powder/polaris_routines/polaris_algs.py b/scripts/Diffraction/isis_powder/polaris_routines/polaris_algs.py
index 644d82acf73..6a069710c64 100644
--- a/scripts/Diffraction/isis_powder/polaris_routines/polaris_algs.py
+++ b/scripts/Diffraction/isis_powder/polaris_routines/polaris_algs.py
@@ -151,16 +151,14 @@ def _generate_grouped_ts_pdf(run_number, focused_ws, q_lims, cal_file_name, samp
     width_x = (max_x-min_x)/x_data.size
     fit_spectra = mantid.FitIncidentSpectrum(InputWorkspace=monitor,
                                              BinningForCalc=[min_x, 1*width_x, max_x],
-                                             BinningForFit=[min_x, 1*width_x, max_x],
+                                             BinningForFit=[min_x, 10*width_x, max_x],
                                              FitSpectrumWith="CubicSpline")
     placzek = mantid.CalculatePlaczekSelfScattering(InputWorkspace=raw_ws, IncidentSpecta=fit_spectra)
-    mantid.ConvertFromDistribution(Workspace=placzek)
     cal_workspace = mantid.LoadCalFile(InputWorkspace=placzek,
                                        CalFileName=cal_file_name,
                                        Workspacename='cal_workspace',
                                        MakeOffsetsWorkspace=False,
                                        MakeMaskWorkspace=False)
-    placzek = mantid.AlignDetectors(InputWorkspace=placzek, CalibrationFile=cal_file_name)
     placzek = mantid.DiffractionFocussing(InputWorkspace=placzek, GroupingFilename=cal_file_name)
     n_pixel = np.zeros(placzek.getNumberHistograms())
     for i in range(cal_workspace.getNumberHistograms()):
@@ -169,8 +167,10 @@ def _generate_grouped_ts_pdf(run_number, focused_ws, q_lims, cal_file_name, samp
             n_pixel[int(grouping[0]-1)] += 1
     correction_ws = mantid.CreateWorkspace(DataY=n_pixel, DataX=[0, 1], NSpec=placzek.getNumberHistograms())
     placzek = mantid.Divide(LHSWorkspace=placzek, RHSWorkspace=correction_ws)
+    mantid.ConvertToDistribution(Workspace=placzek)
     placzek = mantid.ConvertUnits(InputWorkspace=placzek, Target="MomentumTransfer", EMode='Elastic')
     placzek = mantid.RebinToWorkspace(WorkspaceToRebin=placzek, WorkspaceToMatch=focused_data_combined)
+    mantid.ConvertFromDistribution(Workspace=placzek)
     mantid.Subtract(LHSWorkspace=focused_data_combined,
                     RHSWorkspace=placzek,
                     OutputWorkspace=focused_data_combined)
@@ -197,10 +197,10 @@ def _generate_grouped_ts_pdf(run_number, focused_ws, q_lims, cal_file_name, samp
         q_min[i] = pdf_x_array[np.amin(np.where(pdf_x_array >= q_min[i]))]
         q_max[i] = pdf_x_array[np.amax(np.where(pdf_x_array <= q_max[i]))]
         bin_width = min(pdf_x_array[1] - pdf_x_array[0], bin_width)
-    focused_data_combined = mantid.CropWorkspaceRagged(InputWorkspace=focused_data_combined, XMin=q_min, XMax=q_max)
     mantid.MatchSpectra(InputWorkspace=focused_data_combined,
                         OutputWorkspace=focused_data_combined,
                         ReferenceSpectrum=1)
+    focused_data_combined = mantid.CropWorkspaceRagged(InputWorkspace=focused_data_combined, XMin=q_min, XMax=q_max)
     focused_data_combined = mantid.Rebin(InputWorkspace=focused_data_combined,
                                          Params=[min(q_min), bin_width, max(q_max)])
     focused_data_combined = mantid.SumSpectra(InputWorkspace=focused_data_combined,
-- 
GitLab