From f5fd1d8449ac909017fc697fa74bd76ddab17415 Mon Sep 17 00:00:00 2001
From: Jolanta Sztuk-Dambietz <jsztuk@max-display003.desy.de>
Date: Fri, 3 Sep 2021 15:52:10 +0200
Subject: [PATCH] add HIBEF AGIPD500K and fix some issue with retrieval of
 conditions

---
 notebooks/AGIPD/AGIPD_FF_Histogramming.ipynb  |  12 ++
 ...aracterize_AGIPD_Gain_FlatFields_NBC.ipynb | 166 +++++++++---------
 ...terize_AGIPD_Gain_FlatFields_Summary.ipynb |  59 ++++---
 3 files changed, 133 insertions(+), 104 deletions(-)

diff --git a/notebooks/AGIPD/AGIPD_FF_Histogramming.ipynb b/notebooks/AGIPD/AGIPD_FF_Histogramming.ipynb
index 9324d0a31..caca0264e 100644
--- a/notebooks/AGIPD/AGIPD_FF_Histogramming.ipynb
+++ b/notebooks/AGIPD/AGIPD_FF_Histogramming.ipynb
@@ -644,6 +644,18 @@
    "display_name": "Python 3",
    "language": "python",
    "name": "python3"
+  },
+  "language_info": {
+   "codemirror_mode": {
+    "name": "ipython",
+    "version": 3
+   },
+   "file_extension": ".py",
+   "mimetype": "text/x-python",
+   "name": "python",
+   "nbconvert_exporter": "python",
+   "pygments_lexer": "ipython3",
+   "version": "3.8.11"
   }
  },
  "nbformat": 4,
diff --git a/notebooks/AGIPD/Characterize_AGIPD_Gain_FlatFields_NBC.ipynb b/notebooks/AGIPD/Characterize_AGIPD_Gain_FlatFields_NBC.ipynb
index ad171fe35..80a904f8d 100644
--- a/notebooks/AGIPD/Characterize_AGIPD_Gain_FlatFields_NBC.ipynb
+++ b/notebooks/AGIPD/Characterize_AGIPD_Gain_FlatFields_NBC.ipynb
@@ -69,7 +69,8 @@
     "bias_voltage = 300  # Bias voltage\n",
     "acq_rate = 0. # the detector acquisition rate, use 0 to try to auto-determine\n",
     "gain_setting = 0.1 # the gain setting, use 0.1 to try to auto-determine\n",
-    "photon_energy = 8.05 # photon energy in keV"
+    "photon_energy = 8.05 # photon energy in keV\n",
+    "integration_time = -1 # integration time, negative values for auto-detection."
    ]
   },
   {
@@ -376,31 +377,35 @@
    "metadata": {},
    "outputs": [],
    "source": [
-    "fig = plt.figure(figsize=(16,7))\n",
-    "ax = fig.add_subplot(121)\n",
+    "fig, (ax1, ax2) = plt.subplots(1, 2)\n",
+    "fig.set_size_inches(16, 7)\n",
     "for i, shape in enumerate(shapes):\n",
     "    idx = shape[3]\n",
-    "    plt.errorbar(x[idx], hist[idx], np.sqrt(hist[idx]),\n",
-    "         marker='+', ls=''\n",
-    "         )\n",
+    "    ax1.errorbar(\n",
+    "        x[idx], hist[idx],\n",
+    "        np.sqrt(hist[idx]),\n",
+    "        marker='+', ls='',\n",
+    "    )\n",
     "    yg = gaussian(x[idx], *shape[:3])\n",
     "    l = f'Peak {i}: {shape[1]:0.1f} $ \\pm $ {shape[2]:0.2f} ADU'\n",
-    "    plt.plot(x[idx], yg, label=l)\n",
-    "plt.grid(True)\n",
-    "plt.xlabel(\"Signal [ADU]\")\n",
-    "plt.ylabel(\"Counts\")\n",
-    "plt.legend(ncol=2)\n",
-    "\n",
-    "\n",
-    "ax2 = fig.add_subplot(122)\n",
-    "fig2 = xana.simplePlot(d, \n",
-    "                       use_axis=ax2,\n",
-    "                       x_label='Signal [ADU]', \n",
-    "                       y_label='Counts',\n",
-    "                       secondpanel=True, y_log=False, \n",
-    "                       x_range=(frange[0], frange[1]), \n",
-    "                       y_range=(1., np.max(hist)*1.6), \n",
-    "                       legend='top-left-frame-ncol2')"
+    "    ax1.plot(x[idx], yg, label=l)\n",
+    "ax1.grid(True)\n",
+    "ax1.set_xlabel(\"Signal [ADU]\")\n",
+    "ax1.set_ylabel(\"Counts\")\n",
+    "ax1.legend(ncol=2)\n",
+    "\n",
+    "_ = xana.simplePlot(\n",
+    "    d,\n",
+    "    use_axis=ax2,\n",
+    "    x_label='Signal [ADU]',\n",
+    "    y_label='Counts',\n",
+    "    secondpanel=True, y_log=False,\n",
+    "    x_range=(frange[0], frange[1]),\n",
+    "    y_range=(1., np.max(hist)*1.6),\n",
+    "    legend='top-left-frame-ncol2',\n",
+    ")\n",
+    "\n",
+    "plt.show()"
    ]
   },
   {
@@ -493,33 +498,39 @@
    "metadata": {},
    "outputs": [],
    "source": [
-    "labels = ['Noise peak [ADU]',\n",
-    "         'First photon peak [ADU]',\n",
-    "         f\"gain [ADU/keV], $\\gamma$={photon_energy} [keV]\",\n",
-    "         \"$\\chi^2$/nDOF\",\n",
-    "         'Fraction of bad pixels']\n",
-    "for i, key in enumerate(['g0mean', 'g1mean', 'gain', 'chi2_ndof', 'mask']):\n",
+    "labels = [\n",
+    "    \"Noise peak [ADU]\",\n",
+    "    \"First photon peak [ADU]\",\n",
+    "    f\"gain [ADU/keV] $\\gamma$={photon_energy} [keV]\",\n",
+    "    \"$\\chi^2$/nDOF\",\n",
+    "    \"Fraction of bad pixels\",\n",
+    "]\n",
     "\n",
+    "for i, key in enumerate(['g0mean', 'g1mean', 'gain', 'chi2_ndof', 'mask']):\n",
     "    fig = plt.figure(figsize=(20,5))\n",
     "    ax = fig.add_subplot(121)\n",
     "    data = fit_result[key]\n",
     "    if key == 'mask':\n",
-    "        data = data>0\n",
+    "        data = data > 0\n",
     "        vmin, vmax = [0, 1]\n",
     "    else:\n",
     "        vmin, vmax = get_range(data, 5)\n",
-    "    _ = heatmapPlot(np.mean(data, axis=0).T,\n",
-    "                    add_panels=False, cmap='viridis', use_axis=ax,\n",
-    "                    vmin=vmin, vmax=vmax, lut_label=labels[i] )\n",
-    " \n",
+    "    _ = heatmapPlot(\n",
+    "        np.mean(data, axis=0).T,\n",
+    "        add_panels=False, cmap='viridis', use_axis=ax,\n",
+    "        vmin=vmin, vmax=vmax, lut_label=labels[i]\n",
+    "    )\n",
+    "\n",
     "    if key != 'mask':\n",
     "        vmin, vmax = get_range(data, 7)\n",
-    "        ax1 = fig.add_subplot(122)\n",
-    "        _ = xana.histPlot(ax1,data.flatten(), \n",
-    "                      bins=45,range=[vmin, vmax],\n",
-    "                      log=True,color='red',histtype='stepfilled')\n",
-    "        plt.xlabel(labels[i])\n",
-    "        plt.ylabel(\"Counts\")   "
+    "        ax = fig.add_subplot(122)\n",
+    "        _ = xana.histPlot(\n",
+    "            ax, data.flatten(),\n",
+    "            bins=45,range=[vmin, vmax],\n",
+    "            log=True,color='red',histtype='stepfilled'\n",
+    "        )\n",
+    "        ax.set_xlabel(labels[i])\n",
+    "        ax.set_ylabel(\"Counts\")"
    ]
   },
   {
@@ -536,15 +547,15 @@
    "outputs": [],
    "source": [
     "fig = plt.figure(figsize=(10, 5))\n",
-    "ax0 = fig.add_subplot(111)\n",
-    "a = ax0.hist(hist_std.flatten(), bins=100, range=(0,100) )\n",
-    "ax0.plot([intensity_lim, intensity_lim], [0, np.nanmax(a[0])], linewidth=1.5, color='red' ) \n",
-    "ax0.set_xlabel('Histogram width [ADU]', fontsize=14)\n",
-    "ax0.set_ylabel('Number of histograms', fontsize=14)\n",
-    "ax0.set_title(f'{hist_std[hist_std<intensity_lim].shape[0]} histograms below threshold in {intensity_lim} ADU',\n",
+    "ax = fig.add_subplot(111)\n",
+    "a = ax.hist(hist_std.flatten(), bins=100, range=(0,100) )\n",
+    "ax.plot([intensity_lim, intensity_lim], [0, np.nanmax(a[0])], linewidth=1.5, color='red' ) \n",
+    "ax.set_xlabel('Histogram width [ADU]', fontsize=14)\n",
+    "ax.set_ylabel('Number of histograms', fontsize=14)\n",
+    "ax.set_title(f'{hist_std[hist_std<intensity_lim].shape[0]} histograms below threshold in {intensity_lim} ADU',\n",
     "              fontsize=14, fontweight='bold')\n",
-    "ax0.grid()\n",
-    "plt.yscale('log')"
+    "ax.grid()\n",
+    "ax.set_yscale('log')"
    ]
   },
   {
@@ -554,7 +565,7 @@
    "outputs": [],
    "source": [
     "def plot_par_distr(par):\n",
-    "    fig = plt.figure(figsize=(16,5))\n",
+    "    fig = plt.figure(figsize=(16, 5))\n",
     "    sel = fit_result['mask'] == 0\n",
     "    \n",
     "    for i in range(n_peaks_fit) :\n",
@@ -575,8 +586,8 @@
     "                    log=True,color='g',\n",
     "                    label='good fits only',\n",
     "                   )\n",
-    "        plt.xlabel(f\"g{i} {par} [ADU]\")\n",
-    "        plt.legend()\n",
+    "        ax.set_xlabel(f\"g{i} {par} [ADU]\")\n",
+    "        ax.legend()\n",
     "        \n",
     "plot_par_distr('mean')\n",
     "plot_par_distr('sigma')"
@@ -592,11 +603,11 @@
     "\n",
     "dsets = {'d01 [ADU]':fit_result[f\"g1mean\"]-fit_result[f\"g0mean\"],\n",
     "         'gain [ADU/keV]':fit_result[f\"gain\"],\n",
-    "         'gain relative to module mean':fit_result[f\"gain\"]/np.mean(gain_mean),\n",
+    "         'gain relative to module mean':fit_result[f\"gain\"]/np.nanmean(gain_mean),\n",
     "        }\n",
     "fig = plt.figure(figsize=(16,5))\n",
     "for i, (par, data) in enumerate(dsets.items()):\n",
-    "    ax = fig.add_subplot(1,3,i+1)\n",
+    "    ax = fig.add_subplot(1, 3, i+1)\n",
     "    plt_range=get_range(data, 10)\n",
     "    num_bins = 100\n",
     "    _ = xana.histPlot(ax,data.flatten(), \n",
@@ -609,14 +620,14 @@
     "                log=True,color='g',\n",
     "                label='good fits only',\n",
     "               )\n",
-    "    plt.xlabel(f\"{par}\")\n",
-    "    plt.legend()\n",
+    "    ax.set_xlabel(f\"{par}\")\n",
+    "    ax.legend()\n",
     "    if 'd01' in par :\n",
-    "        plt.axvline(d0_lim[0])\n",
-    "        plt.axvline(d0_lim[1])\n",
+    "        ax.axvline(d0_lim[0])\n",
+    "        ax.axvline(d0_lim[1])\n",
     "    if 'rel' in par :\n",
-    "        plt.axvline(gain_lim[0])\n",
-    "        plt.axvline(gain_lim[1])"
+    "        ax.axvline(gain_lim[0])\n",
+    "        ax.axvline(gain_lim[1])"
    ]
   },
   {
@@ -631,9 +642,7 @@
   {
    "cell_type": "code",
    "execution_count": null,
-   "metadata": {
-    "scrolled": false
-   },
+   "metadata": {},
    "outputs": [],
    "source": [
     "def plot_error_band(key, x, ax):\n",
@@ -646,15 +655,14 @@
     "    std = np.nanstd(cdata, axis=(1,2))\n",
     "    mad = np.nanmedian(np.abs(cdata - median[:,None,None]), axis=(1,2))\n",
     "\n",
-    "    ax0 = fig.add_subplot(111)\n",
-    "    ax0.plot(x, mean, 'k', color='#3F7F4C', label=\" mean value \")\n",
-    "    ax0.plot(x, median, 'o', color='red', label=\" median value \")\n",
-    "    ax0.fill_between(x, mean-std, mean+std,\n",
+    "    ax.plot(x, mean, 'k', color='#3F7F4C', label=\" mean value \")\n",
+    "    ax.plot(x, median, 'o', color='red', label=\" median value \")\n",
+    "    ax.fill_between(x, mean-std, mean+std,\n",
     "                     alpha=0.6, edgecolor='#3F7F4C', facecolor='#7EFF99',\n",
     "                     linewidth=1, linestyle='dashdot', antialiased=True,\n",
     "                     label=\" mean value $ \\pm $ std \")\n",
     "\n",
-    "    ax0.fill_between(x, median-mad, median+mad,\n",
+    "    ax.fill_between(x, median-mad, median+mad,\n",
     "                     alpha=0.3, edgecolor='red', facecolor='red',\n",
     "                     linewidth=1, linestyle='dashdot', antialiased=True,\n",
     "                     label=\" median value $ \\pm $ mad \")\n",
@@ -664,7 +672,7 @@
     "        cerr[fit_result['mask']>0] = np.nan\n",
     "        \n",
     "        meanerr = np.nanmean(cerr, axis=(1,2))\n",
-    "        ax0.fill_between(x, mean-meanerr, mean+meanerr,\n",
+    "        ax.fill_between(x, mean-meanerr, mean+meanerr,\n",
     "                 alpha=0.6, edgecolor='#089FFF', facecolor='#089FFF',\n",
     "                 linewidth=1, linestyle='dashdot', antialiased=True,\n",
     "                 label=\" mean fit error \")\n",
@@ -675,13 +683,13 @@
     "for i, key in enumerate(['g0mean', 'g1mean', 'gain', 'chi2_ndof']):\n",
     "\n",
     "    fig = plt.figure(figsize=(10, 5))\n",
-    "    ax0 = fig.add_subplot(111)\n",
-    "    plot_error_band(key, x, ax0)\n",
+    "    ax = fig.add_subplot(111)\n",
+    "    plot_error_band(key, x, ax)\n",
     "\n",
-    "    ax0.set_xlabel('Memory Cell ID', fontsize=14)\n",
-    "    ax0.set_ylabel(labels[i], fontsize=14)\n",
-    "    ax0.grid()\n",
-    "    _ = ax0.legend()"
+    "    ax.set_xlabel('Memory Cell ID', fontsize=14)\n",
+    "    ax.set_ylabel(labels[i], fontsize=14)\n",
+    "    ax.grid()\n",
+    "    ax.legend()"
    ]
   },
   {
@@ -697,8 +705,8 @@
    "metadata": {},
    "outputs": [],
    "source": [
-    "fig = plt.figure(figsize=(10, 5))\n",
-    "ax = fig.add_subplot(111)\n",
+    "fig, ax = plt.subplots()\n",
+    "fig.set_size_inches(10, 5)\n",
     "\n",
     "n_bars = 8\n",
     "x = np.arange(n_bars)\n",
@@ -750,10 +758,10 @@
     "         'No Entry',\n",
     "         'Gain deviation']\n",
     "\n",
-    "ax.bar(x, y2, width, label='Only this cut')\n",
-    "ax.bar(x, y, width, label='Cut flow')\n",
+    "plt.bar(x, y2, width, label='Only this cut')\n",
+    "plt.bar(x, y, width, label='Cut flow')\n",
     "plt.xticks(x, labels, rotation=90)\n",
-    "plt.ylim(y[5]-0.5,100)\n",
+    "plt.ylim(y[5]-0.5, 100)\n",
     "plt.grid(True)\n",
     "plt.legend()\n",
     "plt.show()"
@@ -776,7 +784,7 @@
    "name": "python",
    "nbconvert_exporter": "python",
    "pygments_lexer": "ipython3",
-   "version": "3.6.7"
+   "version": "3.8.11"
   }
  },
  "nbformat": 4,
diff --git a/notebooks/AGIPD/Characterize_AGIPD_Gain_FlatFields_Summary.ipynb b/notebooks/AGIPD/Characterize_AGIPD_Gain_FlatFields_Summary.ipynb
index 6d734bf99..9673c4959 100644
--- a/notebooks/AGIPD/Characterize_AGIPD_Gain_FlatFields_Summary.ipynb
+++ b/notebooks/AGIPD/Characterize_AGIPD_Gain_FlatFields_Summary.ipynb
@@ -104,7 +104,7 @@
     ")\n",
     "from dateutil import parser\n",
     "from extra_data import RunDirectory, stack_detector_data\n",
-    "from extra_geom import AGIPD_1MGeometry\n",
+    "from extra_geom import AGIPD_1MGeometry, AGIPD_500K2GGeometry\n",
     "from iCalibrationDB import Conditions, Constants, Detectors\n",
     "from iminuit import Minuit\n",
     "from IPython.display import HTML, Latex, Markdown, display\n",
@@ -131,10 +131,10 @@
    "outputs": [],
    "source": [
     "# Get operation conditions\n",
-    "\n",
     "filename = glob.glob(f\"{raw_folder}/r{run:04d}/*-AGIPD[0-1][0-9]-*\")[0]\n",
     "channel = int(re.findall(r\".*-AGIPD([0-9]+)-.*\", filename)[0])\n",
     "control_fname = f'{raw_folder}/r{run:04d}/RAW-R{run:04d}-{karabo_da_control}-S00000.h5'\n",
+    "h5path_ctrl = h5path_ctrl.format(karabo_id_control)\n",
     "\n",
     "# Evaluate number of memory cells\n",
     "mem_cells = get_num_cells(filename, karabo_id, channel)\n",
@@ -142,8 +142,11 @@
     "    raise ValueError(f\"No raw images found in {filename}\")\n",
     "\n",
     "# Evaluate aquisition rate\n",
+    "fast_paths = (filename, karabo_id, channel)\n",
+    "slow_paths = (control_fname, karabo_id_control)\n",
+    "\n",
     "if acq_rate == 0.:\n",
-    "    acq_rate = get_acq_rate((filename, karabo_id, channel))\n",
+    "    acq_rate = get_acq_rate(fast_paths,slow_paths)\n",
     "\n",
     "# Evaluate creation time\n",
     "creation_time = None\n",
@@ -166,8 +169,8 @@
     "\n",
     "# Evaluate integration time\n",
     "if integration_time < 0:\n",
-    "    integration_time = get_integration_time(filename, h5path_ctrl)\n",
-    "            \n",
+    "    integration_time = get_integration_time(control_fname, h5path_ctrl)\n",
+    "\n",
     "# Evaluate detector instance for mapping\n",
     "instrument = karabo_id.split(\"_\")[0]\n",
     "if instrument == \"HED\":\n",
@@ -367,13 +370,17 @@
    "metadata": {},
    "outputs": [],
    "source": [
-    "# Define AGIPD geometry\n",
-    "geom = AGIPD_1MGeometry.from_quad_positions(quad_pos=[\n",
-    "    (-525, 625),\n",
-    "    (-550, -10),\n",
-    "    (520, -160),\n",
-    "    (542.5, 475),\n",
-    "])"
+    "#Define AGIPD geometry\n",
+    "#To do: find the better way to do it?\n",
+    "if instrument == \"HED\":\n",
+    "    geom = AGIPD_500K2GGeometry.from_origin()\n",
+    "else:\n",
+    "    geom = AGIPD_1MGeometry.from_quad_positions(quad_pos=[\n",
+    "        (-525, 625),\n",
+    "        (-550, -10),\n",
+    "        (520, -160),\n",
+    "        (542.5, 475),\n",
+    "    ])"
    ]
   },
   {
@@ -458,14 +465,14 @@
     "        }\n",
     "fig = plt.figure(figsize=(16,5))\n",
     "for i, (par, data) in enumerate(dsets.items()):\n",
-    "    ax = fig.add_subplot(1,3,i+1)\n",
+    "    ax = fig.add_subplot(1, 3, i+1)\n",
     "    plt_range= np.nanmin(data), np.nanmax(data)\n",
     "    if 'd01' in par :\n",
-    "        plt.axvline(d0_lim[0])\n",
-    "        plt.axvline(d0_lim[1])\n",
+    "        ax.axvline(d0_lim[0])\n",
+    "        ax.axvline(d0_lim[1])\n",
     "    elif 'rel' in par :\n",
-    "        plt.axvline(gain_lim[0])\n",
-    "        plt.axvline(gain_lim[1])\n",
+    "        ax.axvline(gain_lim[0])\n",
+    "        ax.axvline(gain_lim[1])\n",
     "    num_bins = 100\n",
     "    _ = ax.hist(data.flatten(), \n",
     "                  bins= num_bins,range=plt_range,\n",
@@ -477,8 +484,8 @@
     "                log=True,color='g',\n",
     "                label='good fits only',\n",
     "               )\n",
-    "    plt.xlabel(f\"{par}\")\n",
-    "    plt.legend()"
+    "    ax.set_xlabel(f\"{par}\")\n",
+    "    ax.legend()"
    ]
   },
   {
@@ -522,7 +529,8 @@
     "                        use_axis=ax,\n",
     "                        legend='top-left-frame-ncol8',)\n",
     "    ylim = ax.get_ylim()\n",
-    "    ax.set_ylim(ylim[0], ylim[1] + np.abs(ylim[1]-ylim[0])*0.2)"
+    "    ax.set_ylim(ylim[0], ylim[1] + np.abs(ylim[1]-ylim[0])*0.2)\n",
+    "    ax.grid()"
    ]
   },
   {
@@ -583,10 +591,10 @@
     "    run_data = RunDirectory(run_folder, include)\n",
     "    if tid:\n",
     "        tid, data = run_data.select('*/DET/*', source).train_from_id(tid)\n",
-    "        return tid, stack_detector_data(data, source)\n",
+    "        return tid, stack_detector_data(data, source,modules=nmods)\n",
     "    else:\n",
     "        for tid, data in run_data.select('*/DET/*', source).trains(require_all=True):\n",
-    "            return tid, stack_detector_data(data, source)\n",
+    "            return tid, stack_detector_data(data, source,modules=nmods)\n",
     "    return None, None\n",
     "\n",
     "\n",
@@ -647,7 +655,8 @@
     "fig = plt.figure(figsize=(20,20))\n",
     "ax = fig.add_subplot(111)\n",
     "cdata = np.nanmean(corrected, axis=0)\n",
-    "vmin, vmax = get_range(cdata, 5)\n",
+    "#Remove me: the scale for comparision should be the same as for orig data\n",
+    "#vmin, vmax = get_range(cdata, 5)\n",
     "ax = geom.plot_data_fast(cdata, ax=ax, cmap=\"jet\", vmin=vmin, vmax=vmax, figsize=(20,20))\n",
     "_ = ax.set_title(\"Corrected data, mean across one train\")"
    ]
@@ -672,7 +681,7 @@
     "clap = np.zeros_like(cdata)\n",
     "olap = np.zeros_like(odata)\n",
     "\n",
-    "for i in range(16) :\n",
+    "for i in range(nmods) :\n",
     "    clap[i] = np.abs(laplace(cdata[i].astype(float)/cmax))\n",
     "    olap[i] = np.abs(laplace(odata[i].astype(float)/omax))\n",
     "\n",
@@ -826,7 +835,7 @@
    "name": "python",
    "nbconvert_exporter": "python",
    "pygments_lexer": "ipython3",
-   "version": "3.6.7"
+   "version": "3.8.11"
   }
  },
  "nbformat": 4,
-- 
GitLab