diff --git a/notebooks/Gotthard2/Characterize_Darks_Gotthard2_NBC.ipynb b/notebooks/Gotthard2/Characterize_Darks_Gotthard2_NBC.ipynb index 3a1edb65e7aa4b7208ec5886b500a9afc9c0c559..1ae5f953bca5285e1d617ea521e478fa711b8ba0 100644 --- a/notebooks/Gotthard2/Characterize_Darks_Gotthard2_NBC.ipynb +++ b/notebooks/Gotthard2/Characterize_Darks_Gotthard2_NBC.ipynb @@ -106,17 +106,22 @@ "out_folder = Path(out_folder)\n", "out_folder.mkdir(parents=True, exist_ok=True)\n", "\n", + "if not karabo_id_control:\n", + " karabo_id_control = karabo_id\n", + "\n", "ctrl_src = ctrl_source_template.format(karabo_id_control, control_template)\n", + "run_nums = gotthard2lib.sort_dark_runs_by_gain(\n", + " raw_folder=in_folder,\n", + " runs=run_nums,\n", + " ctrl_src=ctrl_src,\n", + ")\n", "\n", "# Read report path to associate it later with injected constants.\n", "report = get_report(metadata_folder)\n", "\n", "# Run's creation time:\n", "creation_time = calcat_creation_time(in_folder, run_high, creation_time)\n", - "print(f\"Creation time: {creation_time}\")\n", - "\n", - "if not karabo_id_control:\n", - " karabo_id_control = karabo_id" + "print(f\"Creation time: {creation_time}\")" ] }, { @@ -154,7 +159,6 @@ "step_timer.start()\n", "run_dcs_dict = dict()\n", "\n", - "ctrl_src = ctrl_source_template.format(karabo_id_control, control_template)\n", "conditions = {\n", " \"bias_voltage\": set(),\n", " \"exposure_time\": set(),\n", diff --git a/src/cal_tools/gotthard2/gotthard2lib.py b/src/cal_tools/gotthard2/gotthard2lib.py index 586c4af0120769f0afca6bcdd8f24f9d9858645a..061bea183c596e4cd48efbe250ec4f07bdabadc9 100644 --- a/src/cal_tools/gotthard2/gotthard2lib.py +++ b/src/cal_tools/gotthard2/gotthard2lib.py @@ -1,3 +1,7 @@ +from logging import warning +from pathlib import Path +from typing import List, Union + import extra_data @@ -58,3 +62,45 @@ class Gotthard2Ctrl(): return bool( self.run_dc[ self.ctrl_src, "reverseSlaveReadOutMode"].as_single_value()) + + +def sort_dark_runs_by_gain( + raw_folder: Union[str, Path], + runs: List[int], + ctrl_src: str, +): + """Sort the three dark runs based /RUN/.../settings/. + The expected value options are [dynamicgain, fixgain1, fixgain2] + Args: + raw_folder (Union[str, Path]): The raw folder for the three runs to be sorted. + runs (list): A list of three dark runs. + ctrl_src: The CTRL source for checking `settings` dataset value. + Return: + List: Ordered list of runs. + """ + + run_to_setting = dict() + expected_settings = ["dynamicgain", "fixgain1", "fixgain2"] + assert len(set(runs)) == 3, f"A list of {len(runs)} runs is provided, three different dark runs are expected." # noqa + + for r in runs: + run_to_setting[r] = extra_data.RunDirectory( + Path(raw_folder) / f"r{r:04d}").get_run_value( + ctrl_src, "settings") + + if len(set(run_to_setting.values())) < 3: + raise ValueError( + f"Incorrect gain settings for the provided dark runs: {run_to_setting}." + f" The expected settings for these runs are {expected_settings}.") + + sorted_run_to_setting = dict(sorted( + run_to_setting.items(), + key=lambda x: expected_settings.index(x[1]))) + + sorted_runs = list(sorted_run_to_setting.keys()) + if list(run_to_setting.values()) != expected_settings: + warning( + "Dark runs were incorrectly sorted and " + f"have now been corrected to: {sorted_runs}.") + + return sorted_runs