From a5388fee2d8509b36548094899222440b4fdd532 Mon Sep 17 00:00:00 2001
From: Johannes Niskanen <niskanen@max-wne001.desy.de>
Date: Fri, 3 Jun 2022 10:34:55 +0200
Subject: [PATCH] Some nasty bugs fixed.

---
 src/toolbox_scs/detectors/hrixs.py | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

diff --git a/src/toolbox_scs/detectors/hrixs.py b/src/toolbox_scs/detectors/hrixs.py
index 96a0a8e..d1b574a 100644
--- a/src/toolbox_scs/detectors/hrixs.py
+++ b/src/toolbox_scs/detectors/hrixs.py
@@ -536,21 +536,23 @@ class hRIXS:
             # if the leftover tail is more than a half of full length, add it too
             if data.spectrum.shape[0]%ImageGrouping > ImageGrouping/2:
                 spectra.append(np.sum(data.spectrum[Ngroups*ImageGrouping::].to_numpy(),axis=0))
-                xhits.append(np.hstack(xhits[Ngroups*ImageGrouping::].to_numpy()))
+                xhits.append(np.hstack(data.xhits[Ngroups*ImageGrouping::].to_numpy()))
         elif ImageGrouping.lower() == 'run' or ImageGrouping.lower() == 'runs':
             #********************************************
             # Group data by runs
             #********************************************
+            energy  = data_list[0].energy.to_numpy()
             spectra = []
             xhits   = []
             for rundata in data_list:
                 runspectrum = np.zeros(energy.shape)
                 runxhits    = []
-                for spec,hits in zip(rundata.spectrum,rundata.xhits):
-                    runspectrum += spec.to_numpy()
-                    runxhits.append(hits.to_numpy())
+                for spec,hits in zip(rundata.spectrum.to_numpy(),rundata.xhits.to_numpy()):
+                    runspectrum += spec
+                    runxhits.append(hits)
+                runxhits = np.hstack(runxhits)
                 spectra.append(runspectrum)
-                xhits.append(np.hstack(runxhits))
+                xhits.append(runxhits)
         else:
             raise Exception('align_readouts() needs a reasonable grouping argument')
         #********************************************
@@ -597,6 +599,9 @@ class hRIXS:
                     maxpos   = np.argmax(corr)
                     shift    = maxpos-corr_len
                     peak_posis.append(x[maxipos0+shift])
+        #********************************************
+        # Alignment based on Gaussian fitting
+        #********************************************
         elif method.lower() == 'gauss_fit':
             if fit_tol == None:
                 raise Exception('Gauss fit requires a tolerance value.')
@@ -673,7 +678,7 @@ class hRIXS:
                 ret.append(np.interp(grid,x,y))
             spectrum_aligned = np.array(ret)
         elif method.lower() == 'gauss_fit':
-            e0  = peak_posis[0]
+            e0  = int(peak_posis[0])
             energy_aligned = np.linspace(self.Y_RANGE.start, self.Y_RANGE.stop, self.BINS)-e0
             ret = []
             for position,hits in zip(peak_posis,xhits):
-- 
GitLab