From 9428e194fac4817a96d4374a527b5014498d8084 Mon Sep 17 00:00:00 2001
From: Philipp Schmidt <philipp.schmidt@xfel.eu>
Date: Fri, 1 Sep 2023 15:06:27 +0200
Subject: [PATCH] Use virtual full train trigger if both FEL and PPL are
 ignored for REMI

---
 .../REMI/REMI_Digitize_and_Transform.ipynb    | 75 +++++++++++++------
 1 file changed, 51 insertions(+), 24 deletions(-)

diff --git a/notebooks/REMI/REMI_Digitize_and_Transform.ipynb b/notebooks/REMI/REMI_Digitize_and_Transform.ipynb
index ddb1c0c17..59108f84e 100644
--- a/notebooks/REMI/REMI_Digitize_and_Transform.ipynb
+++ b/notebooks/REMI/REMI_Digitize_and_Transform.ipynb
@@ -412,32 +412,59 @@
     "    train_triggers['fel'] = [pos in fel_pos for pos in all_pos]\n",
     "    train_triggers['ppl'] = [pos in ppl_pos for pos in all_pos]\n",
     "\n",
-    "with timing('find_triggers'):\n",
-    "    psh.map(trigger_by_ppt, ppt_data)\n",
     "    \n",
-    "if (np.unique(triggers['pulse'][1:] - triggers['pulse'][:-1]) > 0).sum() > 1:\n",
-    "    # There is more than one delta between pulse entries across all pulses. This is not\n",
-    "    # necessarily a problem, as the pattern could simply have changed in between trains\n",
-    "    # with each train being split properly.\n",
-    "    # If there's more than one delta in a single train, this likely points to a mismatch\n",
-    "    # of FEL and PPL repetition rate. This is most likely not intended.\n",
+    "if ignore_fel and ignore_ppl:\n",
+    "    # Both FEL and PPL are ignored, use virtual full train triggers.\n",
+    "    print('WARNING: Both FEL and PPL pulses are ignored, '\n",
+    "          'virtual trigger is inserted covering the entire train')\n",
     "    \n",
-    "    one = np.uint64(1)  # Because np.uint64 + int = np.float64\n",
-    "    pulse_deltas = set()\n",
-    "\n",
-    "    for pulse_id, (offset, count) in enumerate(zip(pulse_offsets, pulse_counts)):\n",
-    "        deltas = triggers['pulse'][offset+one:offset+count] - triggers['pulse'][offset:offset+count-one]\n",
-    "\n",
-    "        if len(np.unique(deltas)) > 1:\n",
-    "            for delta in deltas:\n",
-    "                pulse_deltas.add(delta)\n",
-    "\n",
-    "    if len(pulse_deltas) > 1:\n",
-    "        delta_str = ', '.join([str(x) for x in sorted(pulse_deltas)])\n",
-    "        warning(f'Different pulse lengths (PPT: {delta_str}) encountered within single trains, '\n",
-    "                f'separated pulse spectra may split up signals!')\n",
-    "    else:\n",
-    "        warning('Different pulse lengths encountered across trains, separation may be unstable!')"
+    "    # Overwrite global pulse statistics computed before,\n",
+    "    num_pulses = len(dc.train_ids)\n",
+    "    pulse_counts[:] = 1\n",
+    "    pulse_counts = pulse_counts.astype(np.int32)\n",
+    "    pulse_offsets = np.arange(len(pulse_counts)).astype(np.int32)\n",
+    "\n",
+    "    # Obtain minimal trace length.\n",
+    "    min_trace_len = min([\n",
+    "        dc[src, key].entry_shape[0]\n",
+    "        for det_name in remi['detector'].keys()\n",
+    "        for src, key in remi.get_detector_sourcekeys(det_name)\n",
+    "    ])\n",
+    "\n",
+    "    triggers['start'] = first_pulse_offset\n",
+    "    triggers['stop'] = min_trace_len\n",
+    "    triggers['offset'] = 0.0\n",
+    "    triggers['pulse'] = -1\n",
+    "    triggers['fel'] = False\n",
+    "    triggers['ppl'] = False    \n",
+    "    \n",
+    "else:\n",
+    "    with timing('find_triggers'):\n",
+    "        psh.map(trigger_by_ppt, ppt_data)\n",
+    "    \n",
+    "    if (np.unique(triggers['pulse'][1:] - triggers['pulse'][:-1]) > 0).sum() > 1:\n",
+    "        # There is more than one delta between pulse entries across all pulses. This is not\n",
+    "        # necessarily a problem, as the pattern could simply have changed in between trains\n",
+    "        # with each train being split properly.\n",
+    "        # If there's more than one delta in a single train, this likely points to a mismatch\n",
+    "        # of FEL and PPL repetition rate. This is most likely not intended.\n",
+    "\n",
+    "        one = np.uint64(1)  # Because np.uint64 + int = np.float64\n",
+    "        pulse_deltas = set()\n",
+    "\n",
+    "        for pulse_id, (offset, count) in enumerate(zip(pulse_offsets, pulse_counts)):\n",
+    "            deltas = triggers['pulse'][offset+one:offset+count] - triggers['pulse'][offset:offset+count-one]\n",
+    "\n",
+    "            if len(np.unique(deltas)) > 1:\n",
+    "                for delta in deltas:\n",
+    "                    pulse_deltas.add(delta)\n",
+    "\n",
+    "        if len(pulse_deltas) > 1:\n",
+    "            delta_str = ', '.join([str(x) for x in sorted(pulse_deltas)])\n",
+    "            warning(f'Different pulse lengths (PPT: {delta_str}) encountered within single trains, '\n",
+    "                    f'separated pulse spectra may split up signals!')\n",
+    "        else:\n",
+    "            warning('Different pulse lengths encountered across trains, separation may be unstable!')"
    ]
   },
   {
-- 
GitLab