Index: src/Jobs/Grid/SamplingGrid.cpp
===================================================================
--- src/Jobs/Grid/SamplingGrid.cpp	(revision c0e8fb2ec177f8ad4be264758ec0f9345719b784)
+++ src/Jobs/Grid/SamplingGrid.cpp	(revision fb3485c1da8740bf16cf170a4d8a9bbbac971be7)
@@ -42,4 +42,5 @@
 #include "Jobs/Grid/SamplingGrid.hpp"
 
+#include <boost/bind.hpp>
 #include <limits>
 
@@ -333,4 +334,12 @@
 }
 
+static void addElements(
+    double &dest,
+    const double &source,
+    const double prefactor)
+{
+  dest += prefactor*(source);
+}
+
 void SamplingGrid::addOntoWindow(
     const double _begin_window[3],
@@ -339,11 +348,32 @@
     const double prefactor)
 {
+  addWindowOntoWindow(
+      begin_window,
+      end_window,
+      _begin_window,
+      _end_window,
+      sampled_grid,
+      _sampled_grid,
+      boost::bind(addElements, _1, _2, boost::cref(prefactor)),
+      thiswindow);
+}
+
+void SamplingGrid::addWindowOntoWindow(
+    const double wbegin[3],
+    const double wend[3],
+    const double other_wbegin[3],
+    const double other_wend[3],
+    sampledvalues_t &sampled_grid,
+    const sampledvalues_t &other_sampled_grid,
+    boost::function<void (double &, const double &)> op,
+    enum LargerWindow choice)
+{
 #ifndef NDEBUG
   for(size_t index=0;index<3;++index) {
-    ASSERT( _begin_window[index] >= begin_window[index],
-        "SamplingGrid::addOntoWindow() - given window starts earlier in component "
+    ASSERT( other_wbegin[index] >= wbegin[index],
+        "SamplingGrid::addWindowOntoWindow() - given smaller window starts earlier than larger window in component "
         +toString(index)+".");
-    ASSERT( _end_window[index] <= end_window[index],
-        "SamplingGrid::addOntoWindow() - given window ends later in component "
+    ASSERT( other_wend[index] <= wend[index],
+        "SamplingGrid::addWindowOntoWindow() - given smaller window ends later than larger window in component "
         +toString(index)+".");
   }
@@ -364,11 +394,11 @@
     const double delta = (double)gridpoints_axis/(end[index] - begin[index]);
     // delta is conversion factor from box length to discrete length, i.e. number of points
-    pre_offset[index] = delta*(_begin_window[index] - begin_window[index])+round_offset;
-    length[index] = delta*(_end_window[index] - _begin_window[index])+round_offset;
-    post_offset[index] = delta*(end_window[index] - _end_window[index])+round_offset;
-    total[index] = delta*(end_window[index] - begin_window[index])+round_offset;
+    pre_offset[index] = delta*(other_wbegin[index] - wbegin[index])+round_offset;
+    length[index] = delta*(other_wend[index] - other_wbegin[index])+round_offset;
+    post_offset[index] = delta*(wend[index] - other_wend[index])+round_offset;
+    total[index] = delta*(wend[index] - wbegin[index])+round_offset;
     // total is used as safe-guard against loss due to discrete conversion
     ASSERT( pre_offset[index]+post_offset[index]+length[index] == total[index],
-        "SamplingGrid::addOntoWindow() - pre, post, and length don't sum up to total for "
+        "SamplingGrid::addWindowOntoWindow() - pre, post, and length don't sum up to total for "
         +toString(index)+"th component.");
   }
@@ -376,13 +406,13 @@
 #ifndef NDEBUG
   const size_t calculated_size = length[0]*length[1]*length[2];
-  ASSERT( calculated_size == _sampled_grid.size(),
-      "SamplingGrid::addOntoWindow() - not enough sampled values given: "
-      +toString(calculated_size)+" != "+toString(_sampled_grid.size())+".");
+  ASSERT( calculated_size == other_sampled_grid.size(),
+      "SamplingGrid::addWindowOntoWindow() - not enough sampled values given: "
+      +toString(calculated_size)+" != "+toString(other_sampled_grid.size())+".");
   ASSERT( calculated_size <=  sampled_grid.size(),
-      "SamplingGrid::addOntoWindow() - not enough sampled values available: "
+      "SamplingGrid::addWindowOntoWindow() - not enough sampled values available: "
       +toString(calculated_size)+" <= "+toString(sampled_grid.size())+".");
   const size_t total_size = total[0]*total[1]*total[2];
   ASSERT( total_size == sampled_grid.size(),
-      "SamplingGrid::addOntoWindow() - total size is not equal to number of present points: "
+      "SamplingGrid::addWindowOntoWindow() - total size is not equal to number of present points: "
       +toString(total_size)+" != "+toString(sampled_grid.size())+".");
 #endif
@@ -390,27 +420,47 @@
 //  size_t counter = 0;
   sampledvalues_t::iterator griditer = sampled_grid.begin();
-  std::advance(griditer, pre_offset[0]*total[1]*total[2]);
-  sampledvalues_t::const_iterator copyiter = _sampled_grid.begin();
+  sampledvalues_t::const_iterator copyiter = other_sampled_grid.begin();
+  if (choice == thiswindow)
+    std::advance(griditer, pre_offset[0]*total[1]*total[2]);
+  else
+    std::advance(copyiter, pre_offset[0]*total[1]*total[2]);
   for(N[0]=0; N[0] < length[0]; ++N[0]) {
-    std::advance(griditer, pre_offset[1]*total[2]);
+    if (choice == thiswindow)
+      std::advance(griditer, pre_offset[1]*total[2]);
+    else
+      std::advance(copyiter, pre_offset[1]*total[2]);
     for(N[1]=0; N[1] < length[1]; ++N[1]) {
-      std::advance(griditer, pre_offset[2]);
+      if (choice == thiswindow)
+        std::advance(griditer, pre_offset[2]);
+      else
+        std::advance(copyiter, pre_offset[2]);
       for(N[2]=0; N[2] < length[2]; ++N[2]) {
         ASSERT( griditer != sampled_grid.end(),
-            "SamplingGrid::addOntoWindow() - griditer is already at end of window.");
-        ASSERT( copyiter != _sampled_grid.end(),
-            "SamplingGrid::addOntoWindow() - griditer is already at end of window.");
-        *griditer++ += prefactor*(*copyiter++);
+            "SamplingGrid::addWindowOntoWindow() - griditer is already at end of window.");
+        ASSERT( copyiter != other_sampled_grid.end(),
+            "SamplingGrid::addWindowOntoWindow() - griditer is already at end of window.");
+        op(*griditer, *copyiter);
+        ++griditer;
+        ++copyiter;
       }
-      std::advance(griditer, post_offset[2]);
+      if (choice == thiswindow)
+        std::advance(griditer, post_offset[2]);
+      else
+        std::advance(copyiter, post_offset[2]);
     }
-    std::advance(griditer, post_offset[1]*total[2]);
+    if (choice == thiswindow)
+      std::advance(griditer, post_offset[1]*total[2]);
+    else
+      std::advance(copyiter, post_offset[1]*total[2]);
   }
 #ifndef NDEBUG
-  std::advance(griditer, post_offset[0]*total[1]*total[2]);
+  if (choice == thiswindow)
+    std::advance(griditer, post_offset[0]*total[1]*total[2]);
+  else
+    std::advance(copyiter, post_offset[0]*total[1]*total[2]);
   ASSERT( griditer == sampled_grid.end(),
-      "SamplingGrid::addOntoWindow() - griditer is not at end of window.");
-  ASSERT( copyiter == _sampled_grid.end(),
-      "SamplingGrid::addOntoWindow() - copyiter is not at end of window.");
+      "SamplingGrid::addWindowOntoWindow() - griditer is not at end of window.");
+  ASSERT( copyiter == other_sampled_grid.end(),
+      "SamplingGrid::addWindowOntoWindow() - copyiter is not at end of window.");
 #endif
   LOG(2, "DEBUG: Grid after adding other is " << sampled_grid << ".");
