From af019974cf34f0e96cd91658879fc73b795c652d Mon Sep 17 00:00:00 2001
From: ahmedk <karim.ahmed@xfel.eu>
Date: Thu, 25 Jan 2024 12:43:41 +0100
Subject: [PATCH] Remove unneeded parameters, read conditions the same, and
 rename is_strixel to be used like the correction notebook

---
 ...ngfrau_Create_Fit_Spectra_Histos_NBC.ipynb | 39 +++++-------
 .../Jungfrau_Create_Gain_maps_NBC.ipynb       | 61 +++++++++----------
 ...ungfrau_gain_Spectra_Fit_Summary_NBC.ipynb |  1 -
 .../Jungfrau_gain_map_Summary_NBC.ipynb       |  3 +-
 4 files changed, 45 insertions(+), 59 deletions(-)

diff --git a/notebooks/Jungfrau/Jungfrau_Create_Fit_Spectra_Histos_NBC.ipynb b/notebooks/Jungfrau/Jungfrau_Create_Fit_Spectra_Histos_NBC.ipynb
index be566bb2c..3e9b18eee 100644
--- a/notebooks/Jungfrau/Jungfrau_Create_Fit_Spectra_Histos_NBC.ipynb
+++ b/notebooks/Jungfrau/Jungfrau_Create_Fit_Spectra_Histos_NBC.ipynb
@@ -8,7 +8,7 @@
     "\n",
     "Author: European XFEL Detector Group, Version: 1.0\n",
     "\n",
-    "Creates histograms from flat fields.\n",
+    "Creates histograms from flat fields and store it in HDF5 files then perform fitting based on the selecting `fit_func`.\n",
     "\n",
     "Histograms are on a pixel-by-pixel, memory cell by memory cell basis to save memory and space, histogram range are to be optimized around the desired peak."
    ]
@@ -27,35 +27,29 @@
     "chunked_trains = 1000  # Number of trains per chunk.\n",
     "gains = [0, 1, 2]  # gain bit values\n",
     "karabo_id = \"SPB_IRDA_JF4M\"  # karabo prefix of Jungfrau devices\n",
-    "karabo_da = [\"\"]\n",
+    "karabo_da = [\"\"]  # Detector's data aggregators. Leave empty to derive from CALCAT.\n",
     "creation_time = ''  # To overwrite the measured creation_time. Required Format: YYYY-MM-DD HR:MN:SC e.g. \"2022-06-28 13:00:00\"\n",
+    "\n",
+    "# Parameter conditions\n",
     "bias_voltage = -1  # bias voltage - Set to -1 to derive from CONTROL file.\n",
     "integration_time = -1.  # integration time - Set to -1 to derive from CONTROL file.\n",
     "gain_mode = 0  # number of memory cells - Set to -1 to derive from CONTROL file.\n",
-    "gain_setting = 0\n",
+    "gain_setting = -1  # gain setting parameter conditions (High or low CDS) - Set to -1 to derive from CONTROL file.\n",
     "memory_cells = -1  # number of memory cells - Set to -1 to derive from CONTROL file.\n",
-    "sc_start = -1  # storage cell start value - should be derived from CONTROL file\n",
+    "sc_start = -1  # storage cell start value - should be derived from CONTROL file.\n",
+    "\n",
     "h_bins_s = 2  # bin width in ADC units of the histogram\n",
     "h_range = (-50, 450.) # histogram range in ADC units\n",
     "block_size = [256, 64] # dimension of the chunks in 'row' and 'col'\n",
-    "det_src_template = '{}/DET/{}:daqOutput'\n",
     "control_src_template = '{}/DET/CONTROL'\n",
     "extra_dims = ['cell', 'row', 'col']  # labels for the DataArray dims after the first\n",
     "fit_func = 'CHARGE_SHARING'  # function used to fit the single photon peak\n",
-    "off_sub = True\n",
-    "adc_fit = True\n",
-    "is_strixel = False\n",
-    "chunk_size = 10\n",
-    "_h_range = (150, 350.) ## range of the histogram in x-axis units\n",
+    "fit_h_range = (150, 350.)  # range of the histogram in x-axis units used in fitting\n",
     "\n",
-    "# CALCAT API parameters\n",
-    "cal_db_interface = 'tcp://max-exfl-cal-001:8020'  # the database interface to use\n",
-    "cal_db_timeout = 180000\n",
-    "\n",
-    "rebin = 1\n",
     "# parameters for the peak finder\n",
     "n_sigma = 5.  # n of sigma above pedestal threshold\n",
     "ratio = 0.99  # ratio of the next peak amplitude in the peak_finder\n",
+    "rebin = 1\n",
     "\n",
     "\n",
     "def find_das(in_folder, runs, karabo_da):\n",
@@ -162,16 +156,14 @@
     "\n",
     "    if memory_cells < 0:\n",
     "        memory_cells, sc_start = ctrl_data.get_memory_cells()\n",
-    "\n",
     "    if integration_time < 0:\n",
     "        integration_time = ctrl_data.get_integration_time()\n",
-    "\n",
     "    if bias_voltage < 0:\n",
     "        bias_voltage = ctrl_data.get_bias_voltage()\n",
-    "\n",
-    "    # gain_setting = ctrl_data.get_gain_setting() TODO: Where is the gain_setting?\n",
-    "    # if gain_mode < 0:\n",
-    "    #     gain_mode = ctrl_data.get_gain_mode()\n",
+    "    if gain_setting < 0:\n",
+    "        gain_setting = ctrl_data.get_gain_setting()\n",
+    "    if gain_mode < 0:\n",
+    "        gain_mode = ctrl_data.get_gain_mode()\n",
     "\n",
     "    return (\n",
     "        bias_voltage,\n",
@@ -248,7 +240,6 @@
     "    modules=karabo_da,\n",
     "    memory_cells=memory_cells,\n",
     "    integration_time=integration_time,\n",
-    "    # TODO: No gain setting 1? I use 0 for now as I don't see gain_setting used anywhere for the 3 notebooks.\n",
     "    gain_setting=gain_setting,\n",
     "    gain_mode=gain_mode,\n",
     "    client=rest_cfg.calibration_client(),\n",
@@ -487,7 +478,7 @@
     "    _edges = np.array(edges[da])\n",
     "    x = (_edges[1:] + _edges[:-1]) / 2.0\n",
     "\n",
-    "    x, _h_spectra = jungfrau_ff.set_histo_range(x, h_spectra[da], _h_range)\n",
+    "    x, _h_spectra = jungfrau_ff.set_histo_range(x, h_spectra[da], fit_h_range)\n",
     "    print(f'adjusting histogram range: {x[0]} - {x[-1]}')\n",
     "    print(f'Histo shape updated from {h_spectra[da].shape} to {_h_spectra.shape}')\n",
     "    print(f'x-axis: {x.shape}')\n",
@@ -539,7 +530,7 @@
     "        dset_noi = fout.create_dataset(\n",
     "            \"noise_map\",\n",
     "            data=const_data[da][\"Noise10Hz\"])\n",
-    "        fout.attrs[\"memory_cells\"] = memory_cells  # TODO: Why memory cells are not saved here. What about the other conditions??\n",
+    "        fout.attrs[\"memory_cells\"] = memory_cells\n",
     "        fout.attrs[\"integration_time\"] = integration_time\n",
     "        fout.attrs[\"bias_voltage\"] = bias_voltage\n",
     "        fout.attrs[\"gain_setting\"] = gain_setting\n",
diff --git a/notebooks/Jungfrau/Jungfrau_Create_Gain_maps_NBC.ipynb b/notebooks/Jungfrau/Jungfrau_Create_Gain_maps_NBC.ipynb
index b471f83bc..46eb9474a 100755
--- a/notebooks/Jungfrau/Jungfrau_Create_Gain_maps_NBC.ipynb
+++ b/notebooks/Jungfrau/Jungfrau_Create_Gain_maps_NBC.ipynb
@@ -33,30 +33,30 @@
     "karabo_da = ['']\n",
     "fit_func = 'CHARGE_SHARING'  # function used to fit the single photon peak\n",
     "gains = [0, 1, 2]\n",
+    "\n",
     "# Parameter conditions\n",
     "bias_voltage = -1  # detector bias voltage\n",
     "integration_time = -1.  # the detector acquisition rate, use 0 to try to auto-determine\n",
-    "gain_setting = 0\n",
+    "gain_setting = -1  # gain setting parameter conditions (High or low CDS) - Set to -1 to derive from CONTROL file.\n",
     "gain_mode = 0\n",
-    "memory_cells = -1\n",
-    "adc_fit = True\n",
-    "is_strixel = False\n",
+    "memory_cells = -1  # number of memory cells - Set to -1 to derive from CONTROL file.\n",
     "# Condition limits\n",
     "bias_voltage_lims = [0, 200]\n",
     "integration_time_lims = [0.1, 1000]\n",
+    "\n",
+    "adc_fit = True\n",
+    "strixel_sensor = \"\"  # reordering for strixel detector layout. Possible strixels to choose from are A0123 and A1256.\n",
     "spectra_fit_temp = 'R{:04d}_{}_Gain_Spectra_{}_{}_Fit.h5'  # 'R{:04d}_{proposal.upper()}_Gain_Spectra_{da}_{fit_func}_Fit.h5'\n",
     "creation_time = \"\"  # To overwrite the measured creation_time. Required Format: YYYY-MM-DD HR:MN:SC e.g. \"2022-06-28 13:00:00\"\n",
     "g_map_old = ''  # '/gpfs/exfel/data/user/mramilli/jungfrau/module_PSI_gainmaps/M302/gainMaps_M302_2022-02-23.h5'  # old gain map file path to calculate gain ratios G0/G1 and G1/G2. Set to \"\" to get last gain constant from the database.\n",
     "old_gain_dataset_name = 'gain_map_g0'  # name of the data structure in the old gain map\n",
     "correct_offset = False  # correct the photon peak value with a pedestal fit position\n",
-    "db_output = False\n",
-    "local_output = True\n",
-    "send_bpix = False  # TODO: check why separate BPx from Gain.\n",
+    "db_output = False  # A boolean to inject the gain maps to the database.\n",
+    "local_output = True  # A boolean to store a local version of the constants in the out-folder\n",
     "g0_fit_dataset = 'gainMap_fit'  # name of the data structure in the fit files\n",
     "E_ph = 8.048  # photon energy of the peak fitted\n",
     "badpixel_threshold_sigma = 3.  # number of std in gain distribution to mark bad pixels\n",
     "roi = [0, 1024, 0, 256]  # ROI to consider to evaluate gain distribution (for bad pixels evaluation)\n",
-    "control_src_template = '{}/DET/CONTROL'\n",
     "\n",
     "# CALCAT API parameters\n",
     "cal_db_interface = \"tcp://max-exfl-cal001:8020\"  # the database interface to use\n",
@@ -128,23 +128,6 @@
     "in_folder = Path(in_folder)"
    ]
   },
-  {
-   "cell_type": "code",
-   "execution_count": null,
-   "metadata": {},
-   "outputs": [],
-   "source": [
-    "run_folder = in_folder / f'r{run:04d}'\n",
-    "control_src = control_src_template.format(karabo_id, karabo_da[0])\n",
-    "\n",
-    "ctrl_data = JungfrauCtrl(RunDirectory(run_folder), control_src)\n",
-    "\n",
-    "if memory_cells < 0:\n",
-    "    memory_cells, _ = ctrl_data.get_memory_cells()\n",
-    "\n",
-    "print(f'Memory  cells: {memory_cells}')"
-   ]
-  },
   {
    "cell_type": "code",
    "execution_count": null,
@@ -157,9 +140,10 @@
     "        out_folder / spectra_fit_temp.format(run, proposal.upper(), da, fit_func),\n",
     "        'r'\n",
     "    ) as f:\n",
-    "        if is_strixel:\n",
+    "        if strixel_sensor:\n",
     "            g0_new[da] = np.transpose(\n",
-    "                from_strixel(np.transpose(f[g0_fit_dataset][()])))\n",
+    "                from_strixel(np.transpose(f[g0_fit_dataset][()])),\n",
+    "                kind=strixel_sensor)\n",
     "        else:\n",
     "            g0_new[da] = f[g0_fit_dataset][()]\n",
     "\n",
@@ -173,9 +157,21 @@
     "\n",
     "        if bias_voltage < 0 and 'bias_voltage' in f.attrs.keys():\n",
     "            bias_voltage = np.float32(f.attrs['bias_voltage'])\n",
-    "            print(f'bias voltage: {bias_voltage}')\n",
+    "            print(f'Bias voltage: {bias_voltage}')\n",
+    "        else:\n",
+    "            print(f'Bias voltage not found!\\nUsing default value {bias_voltage}')\n",
+    "\n",
+    "        if gain_setting < 0 and 'gain_setting' in f.attrs.keys():\n",
+    "            gain_setting = np.bool(f.attrs['gain_setting'])\n",
+    "            print(f'Gain setting: {gain_setting}')\n",
+    "        else:\n",
+    "            print(f'Gain setting not found!\\nUsing default value {gain_setting}')\n",
+    "\n",
+    "        if memory_cells < 0 and 'memory_cells' in f.attrs.keys():\n",
+    "            memory_cells = np.bool(f.attrs['memory_cells'])\n",
+    "            print(f'Memory cells: {memory_cells}')\n",
     "        else:\n",
-    "            print(f'bias voltage not found!\\nUsing default value {bias_voltage}')\n",
+    "            print(f'Memory cells not found!\\nUsing default value {memory_cells}')\n",
     "\n",
     "        # Run's creation time:\n",
     "        creation_time = calcat_creation_time(in_folder, run, creation_time)\n",
@@ -239,7 +235,7 @@
     "    modules=karabo_da,\n",
     "    memory_cells=memory_cells,\n",
     "    integration_time=integration_time,\n",
-    "    gain_setting=gain_setting,  # TODO: No gain setting 1? I use 0 for now as I don't see gain_setting used anywhere for the 3 notebooks.\n",
+    "    gain_setting=gain_setting,\n",
     "    gain_mode=gain_mode,\n",
     "    client=rest_cfg.calibration_client(),\n",
     ")\n",
@@ -341,8 +337,9 @@
     "            'r'\n",
     "        ) as fc:\n",
     "            corr = np.array(fc[g0_fit_dataset])\n",
-    "            if is_strixel:\n",
-    "                corr = np.transpose(from_strixel(np.transpose(corr)))\n",
+    "            if strixel_sensor:\n",
+    "                corr = np.transpose(\n",
+    "                    from_strixel(np.transpose(corr)), kind=strixel_sensor)\n",
     "\n",
     "            g0_new[da] -= corr\n",
     "\n",
diff --git a/notebooks/Jungfrau/Jungfrau_gain_Spectra_Fit_Summary_NBC.ipynb b/notebooks/Jungfrau/Jungfrau_gain_Spectra_Fit_Summary_NBC.ipynb
index 21e94d626..178fbd031 100644
--- a/notebooks/Jungfrau/Jungfrau_gain_Spectra_Fit_Summary_NBC.ipynb
+++ b/notebooks/Jungfrau/Jungfrau_gain_Spectra_Fit_Summary_NBC.ipynb
@@ -52,7 +52,6 @@
     "%matplotlib inline\n",
     "\n",
     "from cal_tools.plotting import init_jungfrau_geom\n",
-    "from cal_tools.tools import CalibrationMetadata\n",
     "from XFELDetAna.plotting.simpleplot import simplePlot\n",
     "from cal_tools.restful_config import calibration_client\n",
     "from cal_tools.calcat_interface import CalCatApi"
diff --git a/notebooks/Jungfrau/Jungfrau_gain_map_Summary_NBC.ipynb b/notebooks/Jungfrau/Jungfrau_gain_map_Summary_NBC.ipynb
index 3f70fcc90..aabbc5147 100644
--- a/notebooks/Jungfrau/Jungfrau_gain_map_Summary_NBC.ipynb
+++ b/notebooks/Jungfrau/Jungfrau_gain_map_Summary_NBC.ipynb
@@ -24,8 +24,7 @@
     "# Parameters used to access raw data.\n",
     "karabo_da = []  # list of data aggregators, which corresponds to different JF modules. This is only needed for the detectors of one module.\n",
     "karabo_id = \"SPB_IRDA_JF4M\"  # detector identifier.\n",
-    "creation_time = \"\"  # To overwrite the measured creation_time. Required Format: YYYY-MM-DD HR:MN:SC e.g. \"2022-06-28 13:00:00\"\n",
-    "g0_fit_dataset = 'gainMap_fit'  # name of the data structure in the fit files"
+    "creation_time = \"\"  # To overwrite the measured creation_time. Required Format: YYYY-MM-DD HR:MN:SC e.g. \"2022-06-28 13:00:00\""
    ]
   },
   {
-- 
GitLab