From 462fb5b6c0d94c5ca4865b05f3ca93afe2c81e95 Mon Sep 17 00:00:00 2001 From: dachengx Date: Wed, 2 Aug 2023 09:00:00 -0500 Subject: [PATCH] Raise error when there is no key in the json file --- alea/template_source.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/alea/template_source.py b/alea/template_source.py index 9a26b57a..5c9dd001 100644 --- a/alea/template_source.py +++ b/alea/template_source.py @@ -353,10 +353,16 @@ def _get_json_spectrum(filename): Args: filename (str): Name of the JSON file. + + Todo: + Define the format of the JSON file clearly. """ with open(filename, "r") as f: contents = json.load(f) - logging.debug(contents["description"]) + if "description" in contents: + logging.debug(contents["description"]) + if "coordinate_system" not in contents: + raise ValueError("Coordinate system not in JSON file.") esyst = contents["coordinate_system"][0][1] ret = interp1d( np.linspace(*esyst), contents["map"], @@ -369,9 +375,11 @@ def build_histogram(self): histname = self.config["histname"].format(**self.format_named_parameters) h = template_to_multihist(templatename, histname) - spectrum = self.config["spectrum"] + if "spectrum_name" not in self.config: + raise ValueError("spectrum_name not in config") + spectrum_name = self.config["spectrum_name"] if isinstance(spectrum, str): - spectrum = self._get_json_spectrum(spectrum.format(**self.format_named_parameters)) + spectrum = self._get_json_spectrum(spectrum_name.format(**self.format_named_parameters)) # Perform E-scaling, assume first axis is energy ecenters = h.bin_centers[0]