From 31381d375bb432d0b2e882af22e59ac214e8f34a Mon Sep 17 00:00:00 2001
From: Joachim Wuttke <j.wuttke@fz-juelich.de>
Date: Tue, 26 Sep 2023 10:14:37 +0200
Subject: [PATCH] Datafield::crop[2] no longer delete error bars

---
 Device/Data/Datafield.cpp                      | 16 +++++++++++-----
 auto/Examples/fit/specular/Pt_layer_fit.py     |  8 +++++++-
 auto/MiniExamples/fit/specular/Pt_layer_fit.py |  8 +++++++-
 rawEx/fit/specular/Pt_layer_fit.py             |  6 ++++++
 4 files changed, 31 insertions(+), 7 deletions(-)

diff --git a/Device/Data/Datafield.cpp b/Device/Data/Datafield.cpp
index 08038dc68f1..6ab5ce4a88b 100644
--- a/Device/Data/Datafield.cpp
+++ b/Device/Data/Datafield.cpp
@@ -273,14 +273,20 @@ Datafield* Datafield::crop(double xmin, double xmax) const
 {
     const auto xclipped = std::make_unique<Scale>(xAxis().clipped(xmin, xmax));
 
-    std::vector<double> out(size());
+    const size_t N = size();
+    std::vector<double> out(N);
+    std::vector<double> errout(hasErrorSigmas() ? N : 0);
     size_t iout = 0;
-    for (size_t i = 0; i < size(); ++i) {
+    for (size_t i = 0; i < N; ++i) {
         const double x = frame().projectedCoord(i, 0);
-        if (xclipped->rangeComprises(x))
-            out[iout++] = m_values[i];
+        if (xclipped->rangeComprises(x)) {
+            out[iout] = m_values[i];
+            if (hasErrorSigmas())
+                errout[iout] = m_errSigmas[i];
+            ++iout;
+        }
     }
-    return new Datafield(frame().clone(), out);
+    return new Datafield(frame().clone(), out, errout);
 }
 
 #ifdef BORNAGAIN_PYTHON
diff --git a/auto/Examples/fit/specular/Pt_layer_fit.py b/auto/Examples/fit/specular/Pt_layer_fit.py
index 81b1a746674..bd4f17520fe 100755
--- a/auto/Examples/fit/specular/Pt_layer_fit.py
+++ b/auto/Examples/fit/specular/Pt_layer_fit.py
@@ -104,7 +104,7 @@ if __name__ == '__main__':
     }
 
     startPnB = {
-        "intensity": (1., 0.7, 1.3),
+        "intensity": (1., 0.8, 1.2),
         "q_offset": (0.01, -0.02, 0.02),
         "q_res/q": (0.01, 0, 0.02),
         "t_pt/nm": (50, 45, 55),
@@ -125,9 +125,15 @@ if __name__ == '__main__':
     flags = ba.ImportSettings1D("q (1/angstrom)", "#", "", 1, 2, 3, 4)
     data = ba.readData1D(fpath, ba.csv1D, flags)
 
+    # Initial plot
+
     r = get_simulation(qzs, initialP | fixedP).simulate().npArray()
     plot(qzs, r, data, initialP)
 
+    # Restrict data to given q range
+
+    data = data.crop(qmin, qmax)
+
     # Fit:
 
     fit_objective = ba.FitObjective()
diff --git a/auto/MiniExamples/fit/specular/Pt_layer_fit.py b/auto/MiniExamples/fit/specular/Pt_layer_fit.py
index 4f3bba2f104..3a32ac836a5 100755
--- a/auto/MiniExamples/fit/specular/Pt_layer_fit.py
+++ b/auto/MiniExamples/fit/specular/Pt_layer_fit.py
@@ -104,7 +104,7 @@ if __name__ == '__main__':
     }
 
     startPnB = {
-        "intensity": (1., 0.7, 1.3),
+        "intensity": (1., 0.8, 1.2),
         "q_offset": (0.01, -0.02, 0.02),
         "q_res/q": (0.01, 0, 0.02),
         "t_pt/nm": (50, 45, 55),
@@ -125,9 +125,15 @@ if __name__ == '__main__':
     flags = ba.ImportSettings1D("q (1/angstrom)", "#", "", 1, 2, 3, 4)
     data = ba.readData1D(fpath, ba.csv1D, flags)
 
+    # Initial plot
+
     r = get_simulation(qzs, initialP | fixedP).simulate().npArray()
     plot(qzs, r, data, initialP)
 
+    # Restrict data to given q range
+
+    data = data.crop(qmin, qmax)
+
     # Fit:
 
     fit_objective = ba.FitObjective()
diff --git a/rawEx/fit/specular/Pt_layer_fit.py b/rawEx/fit/specular/Pt_layer_fit.py
index 70a25952906..bae2abf7f92 100755
--- a/rawEx/fit/specular/Pt_layer_fit.py
+++ b/rawEx/fit/specular/Pt_layer_fit.py
@@ -125,9 +125,15 @@ if __name__ == '__main__':
     flags = ba.ImportSettings1D("q (1/angstrom)", "#", "", 1, 2, 3, 4)
     data = ba.readData1D(fpath, ba.csv1D, flags)
 
+    # Initial plot
+
     r = get_simulation(qzs, initialP | fixedP).simulate().npArray()
     plot(qzs, r, data, initialP)
 
+    # Restrict data to given q range
+
+    data = data.crop(qmin, qmax)
+
     # Fit:
 
     fit_objective = ba.FitObjective()
-- 
GitLab