From 7e5c23e29de0f75aa5490d16125bb28f8a05af5a Mon Sep 17 00:00:00 2001
From: "Joachim Wuttke (h)" <j.wuttke@fz-juelich.de>
Date: Sun, 29 May 2022 21:55:53 +0200
Subject: [PATCH] + DataUtils::Data::relativeDataDifference(vec,vec); start
 using new fcts

---
 Device/Data/DataUtils.cpp             | 13 ++++
 Device/Data/DataUtils.h               |  1 +
 Device/Histo/HistogramUtils.cpp       |  4 +-
 auto/Wrap/doxygenDevice.i             |  7 +-
 auto/Wrap/libBornAgainDevice.py       |  7 +-
 auto/Wrap/libBornAgainDevice_wrap.cpp | 93 +++++++++++++++++++++++++--
 6 files changed, 114 insertions(+), 11 deletions(-)

diff --git a/Device/Data/DataUtils.cpp b/Device/Data/DataUtils.cpp
index 119d691cf47..7f94a0d3aad 100644
--- a/Device/Data/DataUtils.cpp
+++ b/Device/Data/DataUtils.cpp
@@ -49,6 +49,19 @@ double DataUtils::Data::relativeDataDifference(const Powerfield<double>& dat,
     return diff;
 }
 
+//! Returns relative difference between two data sets sum(dat[i] - ref[i])/ref[i]).
+double DataUtils::Data::relativeDataDifference(const std::vector<double>& dat,
+                                               const std::vector<double>& ref)
+{
+    ASSERT(dat.size() == ref.size());
+    double diff = 0;
+    for (size_t i = 0; i < dat.size(); ++i)
+        diff += Numeric::GetRelativeDifference(dat[i], ref[i]);
+    diff /= dat.size();
+    ASSERT(!std::isnan(diff));
+    return diff;
+}
+
 std::unique_ptr<Powerfield<double>>
 DataUtils::Data::createRelativeDifferenceData(const Powerfield<double>& data,
                                               const Powerfield<double>& reference)
diff --git a/Device/Data/DataUtils.h b/Device/Data/DataUtils.h
index be5fb2e3264..85892f7f578 100644
--- a/Device/Data/DataUtils.h
+++ b/Device/Data/DataUtils.h
@@ -21,6 +21,7 @@
 namespace DataUtils::Data {
 
 //! Returns relative difference between two data sets sum(dat[i] - ref[i])/ref[i]).
+double relativeDataDifference(const std::vector<double>& dat, const std::vector<double>& ref);
 double relativeDataDifference(const Powerfield<double>& dat, const Powerfield<double>& ref);
 
 //! Transforms coordinate on axis into the bin-fraction-coordinate.
diff --git a/Device/Histo/HistogramUtils.cpp b/Device/Histo/HistogramUtils.cpp
index fd2eae0be07..642ae4418ee 100644
--- a/Device/Histo/HistogramUtils.cpp
+++ b/Device/Histo/HistogramUtils.cpp
@@ -75,9 +75,7 @@ double DataUtils::Histo::RelativeDifference(const SimulationResult& dat,
 
 double DataUtils::Histo::getRelativeDifference(const IHistogram& dat, const IHistogram& ref)
 {
-    return DataUtils::Data::relativeDataDifference(
-        *std::unique_ptr<Powerfield<double>>(dat.getData().meanValues()),
-        *std::unique_ptr<Powerfield<double>>(ref.getData().meanValues()));
+    return DataUtils::Data::relativeDataDifference(dat.meanVector(), ref.meanVector());
 }
 
 //! Returns true is relative difference is below threshold; prints informative output
diff --git a/auto/Wrap/doxygenDevice.i b/auto/Wrap/doxygenDevice.i
index 0bdbf1113f0..e18e90e1048 100644
--- a/auto/Wrap/doxygenDevice.i
+++ b/auto/Wrap/doxygenDevice.i
@@ -1290,7 +1290,7 @@ Saves histogram in file. Available formats: *.txt, *.tif, *.int (*.txt.gz, *.tif
 Loads histogram from file, the shape of array in file should match. Available formats: *.txt, *.tif, *.int (*.txt.gz, *.tif.gz, *.int.gz). Only bin content will be loaded, histogram axes remain the same. 
 ";
 
-%feature("docstring")  IHistogram::meanVector "std::vector<double> IHistogram::meanVector() const
+%feature("docstring")  IHistogram::meanVector "std::vector< double > IHistogram::meanVector() const
 ";
 
 
@@ -2319,6 +2319,11 @@ Creates 2D vector from  Powerfield.
 
 
 // File: namespaceDataUtils_1_1Data.xml
+%feature("docstring")  DataUtils::Data::relativeDataDifference "double DataUtils::Data::relativeDataDifference(const std::vector< double > &dat, const std::vector< double > &ref)
+
+Returns relative difference between two data sets sum(dat[i] - ref[i])/ref[i]). 
+";
+
 %feature("docstring")  DataUtils::Data::relativeDataDifference "double DataUtils::Data::relativeDataDifference(const Powerfield< double > &dat, const Powerfield< double > &ref)
 
 Returns relative difference between two data sets sum(dat[i] - ref[i])/ref[i]). 
diff --git a/auto/Wrap/libBornAgainDevice.py b/auto/Wrap/libBornAgainDevice.py
index 96e12b2f5de..48b48018cb2 100644
--- a/auto/Wrap/libBornAgainDevice.py
+++ b/auto/Wrap/libBornAgainDevice.py
@@ -2237,15 +2237,16 @@ class IntensityData(libBornAgainBase.Frame):
 _libBornAgainDevice.IntensityData_swigregister(IntensityData)
 
 
-def relativeDataDifference(dat, ref):
+def relativeDataDifference(*args):
     r"""
+    relativeDataDifference(vdouble1d_t dat, vdouble1d_t ref) -> double
     relativeDataDifference(IntensityData dat, IntensityData ref) -> double
     double DataUtils::Data::relativeDataDifference(const Powerfield< double > &dat, const Powerfield< double > &ref)
 
     Returns relative difference between two data sets sum(dat[i] - ref[i])/ref[i]). 
 
     """
-    return _libBornAgainDevice.relativeDataDifference(dat, ref)
+    return _libBornAgainDevice.relativeDataDifference(*args)
 
 def coordinateToBinf(*args):
     r"""
@@ -4556,7 +4557,7 @@ class IHistogram(object):
     def meanVector(self):
         r"""
         meanVector(IHistogram self) -> vdouble1d_t
-        std::vector<double> IHistogram::meanVector() const
+        std::vector< double > IHistogram::meanVector() const
 
         """
         return _libBornAgainDevice.IHistogram_meanVector(self)
diff --git a/auto/Wrap/libBornAgainDevice_wrap.cpp b/auto/Wrap/libBornAgainDevice_wrap.cpp
index 596774f014b..b8a6346f582 100644
--- a/auto/Wrap/libBornAgainDevice_wrap.cpp
+++ b/auto/Wrap/libBornAgainDevice_wrap.cpp
@@ -27722,7 +27722,50 @@ SWIGINTERN PyObject *IntensityData_swiginit(PyObject *SWIGUNUSEDPARM(self), PyOb
   return SWIG_Python_InitShadowInstance(args);
 }
 
-SWIGINTERN PyObject *_wrap_relativeDataDifference(PyObject *SWIGUNUSEDPARM(self), PyObject *args) {
+SWIGINTERN PyObject *_wrap_relativeDataDifference__SWIG_0(PyObject *SWIGUNUSEDPARM(self), Py_ssize_t nobjs, PyObject **swig_obj) {
+  PyObject *resultobj = 0;
+  std::vector< double,std::allocator< double > > *arg1 = 0 ;
+  std::vector< double,std::allocator< double > > *arg2 = 0 ;
+  int res1 = SWIG_OLDOBJ ;
+  int res2 = SWIG_OLDOBJ ;
+  double result;
+  
+  if ((nobjs < 2) || (nobjs > 2)) SWIG_fail;
+  {
+    std::vector< double,std::allocator< double > > *ptr = (std::vector< double,std::allocator< double > > *)0;
+    res1 = swig::asptr(swig_obj[0], &ptr);
+    if (!SWIG_IsOK(res1)) {
+      SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "relativeDataDifference" "', argument " "1"" of type '" "std::vector< double,std::allocator< double > > const &""'"); 
+    }
+    if (!ptr) {
+      SWIG_exception_fail(SWIG_ValueError, "invalid null reference " "in method '" "relativeDataDifference" "', argument " "1"" of type '" "std::vector< double,std::allocator< double > > const &""'"); 
+    }
+    arg1 = ptr;
+  }
+  {
+    std::vector< double,std::allocator< double > > *ptr = (std::vector< double,std::allocator< double > > *)0;
+    res2 = swig::asptr(swig_obj[1], &ptr);
+    if (!SWIG_IsOK(res2)) {
+      SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "relativeDataDifference" "', argument " "2"" of type '" "std::vector< double,std::allocator< double > > const &""'"); 
+    }
+    if (!ptr) {
+      SWIG_exception_fail(SWIG_ValueError, "invalid null reference " "in method '" "relativeDataDifference" "', argument " "2"" of type '" "std::vector< double,std::allocator< double > > const &""'"); 
+    }
+    arg2 = ptr;
+  }
+  result = (double)DataUtils::Data::relativeDataDifference((std::vector< double,std::allocator< double > > const &)*arg1,(std::vector< double,std::allocator< double > > const &)*arg2);
+  resultobj = SWIG_From_double(static_cast< double >(result));
+  if (SWIG_IsNewObj(res1)) delete arg1;
+  if (SWIG_IsNewObj(res2)) delete arg2;
+  return resultobj;
+fail:
+  if (SWIG_IsNewObj(res1)) delete arg1;
+  if (SWIG_IsNewObj(res2)) delete arg2;
+  return NULL;
+}
+
+
+SWIGINTERN PyObject *_wrap_relativeDataDifference__SWIG_1(PyObject *SWIGUNUSEDPARM(self), Py_ssize_t nobjs, PyObject **swig_obj) {
   PyObject *resultobj = 0;
   Powerfield< double > *arg1 = 0 ;
   Powerfield< double > *arg2 = 0 ;
@@ -27730,10 +27773,9 @@ SWIGINTERN PyObject *_wrap_relativeDataDifference(PyObject *SWIGUNUSEDPARM(self)
   int res1 = 0 ;
   void *argp2 = 0 ;
   int res2 = 0 ;
-  PyObject *swig_obj[2] ;
   double result;
   
-  if (!SWIG_Python_UnpackTuple(args, "relativeDataDifference", 2, 2, swig_obj)) SWIG_fail;
+  if ((nobjs < 2) || (nobjs > 2)) SWIG_fail;
   res1 = SWIG_ConvertPtr(swig_obj[0], &argp1, SWIGTYPE_p_PowerfieldT_double_t,  0  | 0);
   if (!SWIG_IsOK(res1)) {
     SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "relativeDataDifference" "', argument " "1"" of type '" "Powerfield< double > const &""'"); 
@@ -27758,6 +27800,48 @@ fail:
 }
 
 
+SWIGINTERN PyObject *_wrap_relativeDataDifference(PyObject *self, PyObject *args) {
+  Py_ssize_t argc;
+  PyObject *argv[3] = {
+    0
+  };
+  
+  if (!(argc = SWIG_Python_UnpackTuple(args, "relativeDataDifference", 0, 2, argv))) SWIG_fail;
+  --argc;
+  if (argc == 2) {
+    int _v;
+    int res = SWIG_ConvertPtr(argv[0], 0, SWIGTYPE_p_PowerfieldT_double_t, SWIG_POINTER_NO_NULL | 0);
+    _v = SWIG_CheckState(res);
+    if (_v) {
+      int res = SWIG_ConvertPtr(argv[1], 0, SWIGTYPE_p_PowerfieldT_double_t, SWIG_POINTER_NO_NULL | 0);
+      _v = SWIG_CheckState(res);
+      if (_v) {
+        return _wrap_relativeDataDifference__SWIG_1(self, argc, argv);
+      }
+    }
+  }
+  if (argc == 2) {
+    int _v;
+    int res = swig::asptr(argv[0], (std::vector< double,std::allocator< double > >**)(0));
+    _v = SWIG_CheckState(res);
+    if (_v) {
+      int res = swig::asptr(argv[1], (std::vector< double,std::allocator< double > >**)(0));
+      _v = SWIG_CheckState(res);
+      if (_v) {
+        return _wrap_relativeDataDifference__SWIG_0(self, argc, argv);
+      }
+    }
+  }
+  
+fail:
+  SWIG_Python_RaiseOrModifyTypeError("Wrong number or type of arguments for overloaded function 'relativeDataDifference'.\n"
+    "  Possible C/C++ prototypes are:\n"
+    "    DataUtils::Data::relativeDataDifference(std::vector< double,std::allocator< double > > const &,std::vector< double,std::allocator< double > > const &)\n"
+    "    DataUtils::Data::relativeDataDifference(Powerfield< double > const &,Powerfield< double > const &)\n");
+  return 0;
+}
+
+
 SWIGINTERN PyObject *_wrap_coordinateToBinf__SWIG_0(PyObject *SWIGUNUSEDPARM(self), Py_ssize_t nobjs, PyObject **swig_obj) {
   PyObject *resultobj = 0;
   double arg1 ;
@@ -40586,6 +40670,7 @@ static PyMethodDef SwigMethods[] = {
 	 { "IntensityData_swigregister", IntensityData_swigregister, METH_O, NULL},
 	 { "IntensityData_swiginit", IntensityData_swiginit, METH_VARARGS, NULL},
 	 { "relativeDataDifference", _wrap_relativeDataDifference, METH_VARARGS, "\n"
+		"relativeDataDifference(vdouble1d_t dat, vdouble1d_t ref) -> double\n"
 		"relativeDataDifference(IntensityData dat, IntensityData ref) -> double\n"
 		"double DataUtils::Data::relativeDataDifference(const Powerfield< double > &dat, const Powerfield< double > &ref)\n"
 		"\n"
@@ -41996,7 +42081,7 @@ static PyMethodDef SwigMethods[] = {
 		""},
 	 { "IHistogram_meanVector", _wrap_IHistogram_meanVector, METH_O, "\n"
 		"IHistogram_meanVector(IHistogram self) -> vdouble1d_t\n"
-		"std::vector<double> IHistogram::meanVector() const\n"
+		"std::vector< double > IHistogram::meanVector() const\n"
 		"\n"
 		""},
 	 { "IHistogram_swigregister", IHistogram_swigregister, METH_O, NULL},
-- 
GitLab