From 41603db359bf2b6ad1d2e0efe800a89f60615187 Mon Sep 17 00:00:00 2001
From: Philipp Schmidt <philipp.schmidt@xfel.eu>
Date: Thu, 7 Oct 2021 14:45:43 +0200
Subject: [PATCH] Add support to only correct AGIPD trains picked by a PPU
 device

---
 .../AGIPD/AGIPD_Correct_and_Verify.ipynb      | 32 +++++++++++++++++++
 1 file changed, 32 insertions(+)

diff --git a/notebooks/AGIPD/AGIPD_Correct_and_Verify.ipynb b/notebooks/AGIPD/AGIPD_Correct_and_Verify.ipynb
index 613b264ad..147757da0 100644
--- a/notebooks/AGIPD/AGIPD_Correct_and_Verify.ipynb
+++ b/notebooks/AGIPD/AGIPD_Correct_and_Verify.ipynb
@@ -40,6 +40,9 @@
     "cal_db_timeout = 30000 # in milli seconds\n",
     "creation_date_offset = \"00:00:00\" # add an offset to creation date, e.g. to get different constants\n",
     "\n",
+    "use_ppu_device = ''  # Device ID for a pulse picker device to only process picked trains, empty string to disable\n",
+    "ppu_train_offset = 0  # When using the pulse picker, offset between the PPU's sequence start and actually picked train\n",
+    "\n",
     "max_cells = 0 # number of memory cells used, set to 0 to automatically infer\n",
     "bias_voltage = 300 # Bias voltage\n",
     "acq_rate = 0. # the detector acquisition rate, use 0 to try to auto-determine\n",
@@ -381,6 +384,29 @@
     "print(f\"• Photon Energy: {photon_energy}\")"
    ]
   },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "# Obtain trains to process if using a pulse picker device.\n",
+    "if use_ppu_device:\n",
+    "    run = RunDirectory(in_folder / f'r{run:04d}')\n",
+    "\n",
+    "    # Will throw an uncaught exception if the device is wrong.\n",
+    "    seq_start = run[use_ppu_device, 'trainTrigger.sequenceStart.value'].ndarray()\n",
+    "\n",
+    "    # The trains picked are the unique values of trainTrigger.sequenceStart\n",
+    "    # minus the first (previous trigger before this run).\n",
+    "    train_ids = np.unique(seq_start)[1:] + ppu_train_offset\n",
+    "\n",
+    "    print(f'PPU device {use_ppu_device} triggered for {len(train_ids)} trains')\n",
+    "\n",
+    "else:\n",
+    "    train_ids = None"
+   ]
+  },
   {
    "cell_type": "code",
    "execution_count": null,
@@ -415,6 +441,7 @@
     "    corr_bools=corr_bools,\n",
     "    gain_mode=gain_mode,\n",
     "    comp_threads=os.cpu_count() // n_cores_files,\n",
+    "    train_ids=train_ids\n",
     ")\n",
     "\n",
     "agipd_corr.baseline_corr_noise_threshold = -blc_noise_threshold\n",
@@ -558,6 +585,11 @@
     "                                                                  [not common_mode]*len(file_batch)))\n",
     "        step_timer.done_step(f'Loading data from files')\n",
     "\n",
+    "        if img_counts == 0:\n",
+    "            # Skip any further processing and output if there are no images to\n",
+    "            # correct in this file.\n",
+    "            continue\n",
+    "\n",
     "        if mask_zero_std:\n",
     "            # Evaluate zero-data-std mask\n",
     "            pool.starmap(agipd_corr.mask_zero_std, itertools.product(\n",
-- 
GitLab