From 6b68b1fa8f44d1c584830a307e07847aabcf01a7 Mon Sep 17 00:00:00 2001
From: Karim Ahmed <karim.ahmed@xfel.eu>
Date: Mon, 14 Oct 2019 08:59:11 +0200
Subject: [PATCH] change histogram x axis

---
 cal_tools/cal_tools/agipdlib.py               | 20 +++++++++----------
 .../AGIPD/AGIPD_Correct_and_Verify.ipynb      | 17 ++++++++++------
 2 files changed, 21 insertions(+), 16 deletions(-)

diff --git a/cal_tools/cal_tools/agipdlib.py b/cal_tools/cal_tools/agipdlib.py
index fc5412fb0..23ce7d440 100644
--- a/cal_tools/cal_tools/agipdlib.py
+++ b/cal_tools/cal_tools/agipdlib.py
@@ -1029,16 +1029,13 @@ class AgipdCorrections:
         if self.melt_snow is not False:
             msk |= snowmask
 
-        agipd_base = self.agipd_base
-        allpulses = np.squeeze(self.infile[agipd_base + "image/pulseId"])
-
         # for the first chunk output some statistics
         if cidx == 0:
             copim = copy.copy(im)
             copim[copim < self.median_noise] = np.nan
             bins = (self.bins_signal_low_range, self.max_pulses)
-            rnge = [[-50, 1000], [allpulses[self.rng_pulse_idx[0]],
-                                  allpulses[self.rng_pulse_idx[-1]]]]
+            rnge = [[-50, 1000], [self.rng_pulse_idx[0],
+                                  self.rng_pulse_idx[-1]+1]]
             H, xe, ye = np.histogram2d(np.nanmean(copim, axis=(1, 2)),
                                        pulseId,
                                        bins=bins,
@@ -1046,8 +1043,8 @@ class AgipdCorrections:
             self.hists_signal_low += H
             self.low_edges = (xe, ye)
             bins = (self.bins_signal_high_range, self.max_pulses)
-            rnge = [[0, 200000], [allpulses[self.rng_pulse_idx[0]],
-                                  allpulses[self.rng_pulse_idx[-1]]]]
+            rnge = [[0, 200000], [self.rng_pulse_idx[0],
+                                  self.rng_pulse_idx[-1]+1]]
             H, xe, ye = np.histogram2d(np.nanmean(copim, axis=(1, 2)),
                                        pulseId,
                                        bins=bins,
@@ -1164,10 +1161,13 @@ class AgipdCorrections:
         single_image = self.infile[agipd_base + "image/data"][first_index, ...]
         single_image = np.array(single_image)
 
-
         can_calibrate = (allcells < max_cells)
-        can_calibrate &= (allpulses < np.max(allpulses[np.array(self.rng_pulse_idx)]))
-        
+        can_calibrate &= (allpulses < np.max(allpulses[self.rng_pulse_idx[0]:
+                                                       self.rng_pulse_idx[-1]:
+                                                       self.rng_pulse_idx[1]-
+                                                       self.rng_pulse_idx[0]]
+                                             )
+                          )
         if np.count_nonzero(can_calibrate) == 0:
             return
         allcells = allcells[can_calibrate]
diff --git a/notebooks/AGIPD/AGIPD_Correct_and_Verify.ipynb b/notebooks/AGIPD/AGIPD_Correct_and_Verify.ipynb
index 05f46a010..29fd573ad 100644
--- a/notebooks/AGIPD/AGIPD_Correct_and_Verify.ipynb
+++ b/notebooks/AGIPD/AGIPD_Correct_and_Verify.ipynb
@@ -23,7 +23,7 @@
    "outputs": [],
    "source": [
     "in_folder = \"/gpfs/exfel/exp/MID/201931/p900090/raw\" # the folder to read data from, required\n",
-    "run = 552 # runs to process, required\n",
+    "run = 563 # runs to process, required\n",
     "out_folder =  \"/gpfs/exfel/data/scratch/ahmedk/test/AGIPD_Corr\"  # the folder to output to, required\n",
     "calfile =  \"/gpfs/exfel/data/scratch/haufs/agipd_on_demand/agipd_store_mid.h5\" # path to calibration file. Leave empty if all data should come from DB\n",
     "sequences =  [-1] # sequences to correct, set to -1 for all, range allowed\n",
@@ -515,11 +515,16 @@
     "                            1))\n",
     "    max_pulses = len(rng_pulse_idx)\n",
     "elif len(max_pulses) == 1:\n",
-    "    rng_pulse_idx = list(range(max_pulses[0]))\n",
+    "    rng_pulse_idx = list(range(max_pulses[0]+1))\n",
     "    max_pulses = max_pulses[0]\n",
     "else:\n",
     "    raise ValueError(\"Wrong input for max_pulses {} \\n\".format(max_pulses),\n",
     "                     \"max_pulses should have 3 arguments maximum\")\n",
+    "    \n",
+    "print(\"Chosen {} max Pulses: from {} to {} with a step of {}\".format(max_pulses,\n",
+    "                                                                     rng_pulse_idx[0],\n",
+    "                                                                     rng_pulse_idx[-1],\n",
+    "                                                                     rng_pulse_idx[1]-rng_pulse_idx[0]))\n",
     "\n",
     "bins_gain_vs_signal = (100, 100)\n",
     "bins_signal_low_range = 100\n",
@@ -773,10 +778,10 @@
    },
    "outputs": [],
    "source": [
-    "do_3d_plot(hists_signal_low, low_edges, \"Signal (ADU)\", \"Pulse id\")\n",
-    "do_2d_plot(hists_signal_low, low_edges, \"Signal (ADU)\", \"Pulse id\")\n",
-    "do_3d_plot(hists_signal_high, high_edges, \"Signal (ADU)\", \"Pulse id\")\n",
-    "do_2d_plot(hists_signal_high, high_edges, \"Signal (ADU)\", \"Pulse id\")"
+    "do_3d_plot(hists_signal_low, low_edges, \"Signal (ADU)\", \"Pulse idx\")\n",
+    "do_2d_plot(hists_signal_low, low_edges, \"Signal (ADU)\", \"Pulse idx\")\n",
+    "do_3d_plot(hists_signal_high, high_edges, \"Signal (ADU)\", \"Pulse idx\")\n",
+    "do_2d_plot(hists_signal_high, high_edges, \"Signal (ADU)\", \"Pulse idx\")"
    ]
   },
   {
-- 
GitLab