From d55d3db6ddfa8022154b9b7999c5e128a0af3a5b Mon Sep 17 00:00:00 2001 From: Philipp Schmidt <philipp.schmidt@xfel.eu> Date: Sun, 23 Oct 2022 15:41:57 +0200 Subject: [PATCH] Build intensity histograms before and after AGIPD photonization --- .../AGIPD/AGIPD_Correct_and_Verify.ipynb | 30 +++++++++++++++++++ src/cal_tools/agipdlib.py | 22 ++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/notebooks/AGIPD/AGIPD_Correct_and_Verify.ipynb b/notebooks/AGIPD/AGIPD_Correct_and_Verify.ipynb index 9ceff41ac..018dc4445 100644 --- a/notebooks/AGIPD/AGIPD_Correct_and_Verify.ipynb +++ b/notebooks/AGIPD/AGIPD_Correct_and_Verify.ipynb @@ -1270,6 +1270,36 @@ "geom.plot_data_fast(np.log2(mask[cell_idx_preview]), ax=ax, vmin=0, vmax=32, cmap=\"jet\")" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "if round_photons:\n", + " display(Markdown('### Photonization histograms ###'))\n", + " \n", + " x_preround = (agipd_corr.hist_bins_preround[1:] + agipd_corr.hist_bins_preround[:-1]) / 2\n", + " x_postround = (agipd_corr.hist_bins_postround[1:] + agipd_corr.hist_bins_postround[:-1]) / 2\n", + " x_photons = np.arange(0, (x_postround[-1] + 1) / photon_energy)\n", + "\n", + " fig, ax = plt.subplots(ncols=1, nrows=1, clear=True)\n", + " ax.plot(x_preround, agipd_corr.shared_hist_preround, '.-', color='C0')\n", + " ax.bar(x_postround, agipd_corr.shared_hist_postround, photon_energy, color='C1', alpha=0.5)\n", + " ax.set_yscale('log')\n", + " ax.set_ylim(0, max(agipd_corr.shared_hist_preround.max(), agipd_corr.shared_hist_postround.max())*3)\n", + " ax.set_xlim(x_postround[0], x_postround[-1]+1)\n", + " ax.set_xlabel('Photon energy / keV')\n", + " ax.set_ylabel('Intensity')\n", + " ax.vlines(x_photons * photon_energy, *ax.get_ylim(), color='k', linestyle='dashed')\n", + "\n", + " phx = ax.twiny()\n", + " phx.set_xlim(x_postround[0] / photon_energy, (x_postround[-1]+1)/photon_energy)\n", + " phx.set_xticks(x_photons)\n", + " phx.set_xlabel('# Photons')\n", + " pass" + ] + }, { "cell_type": "markdown", "metadata": {}, diff --git a/src/cal_tools/agipdlib.py b/src/cal_tools/agipdlib.py index 535eef0db..03f9680d7 100644 --- a/src/cal_tools/agipdlib.py +++ b/src/cal_tools/agipdlib.py @@ -2,6 +2,7 @@ import os import posixpath import zlib from datetime import datetime +from multiprocessing import Manager from multiprocessing.pool import ThreadPool from typing import Any, Dict, List, Optional, Tuple @@ -416,6 +417,14 @@ class AgipdCorrections: self.mask = {} self.xray_cor = {} + if corr_bools.get('round_photons'): + # Variables related to histogramming. + self.shared_hist_preround = None + self.shared_hist_postround = None + self.hist_bins_preround = np.linspace(-5.0, 60.0, 200) + self.hist_bins_postround = np.arange(-5.5, 60.1) + self.hist_lock = Manager().Lock() + # check if given corr_bools are correct tot_corr_bools = ['only_offset', 'adjust_mg_baseline', 'rel_gain', 'xray_corr', 'blc_noise', 'blc_hmatch', @@ -909,6 +918,8 @@ class AgipdCorrections: # Round keV-normalized intensity to photons. if self.corr_bools.get("round_photons"): + data_hist_preround, _ = np.histogram(data, bins=self.hist_bins_preround) + data /= self.photon_energy np.round(data, out=data) @@ -919,6 +930,13 @@ class AgipdCorrections: msk[bidx] |= BadPixels.VALUE_OUT_OF_RANGE del bidx + data_hist_postround, _ = np.histogram(data * self.photon_energy, + bins=self.hist_bins_postround) + + with self.hist_lock: + self.shared_hist_preround += data_hist_preround + self.shared_hist_postround += data_hist_postround + if np.issubdtype(self.recast_image_fields.get('image'), np.integer): # If the image data is meant to be recast to an integer # type, make sure its values are within its bounds. @@ -1497,6 +1515,10 @@ class AgipdCorrections: self.shared_dict[i]["n_valid_trains"] = sharedmem.empty(1, dtype="i4") # noqa self.shared_dict[i]["valid_trains"] = sharedmem.empty(1024, dtype="u8") # noqa + if self.corr_bools.get("round_photons"): + self.shared_hist_preround = sharedmem.empty(len(self.hist_bins_preround) - 1, dtype="i8") + self.shared_hist_postround = sharedmem.empty(len(self.hist_bins_postround) - 1, dtype="i8") + def validate_selected_pulses( max_pulses: List[int], max_cells: int -- GitLab