Skip to content

Commit

Permalink
Minor change
Browse files Browse the repository at this point in the history
  • Loading branch information
dachengx committed Jul 31, 2023
1 parent cd0a594 commit 1f9251b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
12 changes: 6 additions & 6 deletions alea/examples/configs/unbinned_wimp_statistical_model.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,13 @@ likelihood_config:
class: alea.template_source.CombinedSource
parameters:
- er_rate_multiplier
weight_1: 0.2
weight_-1: 0.3
weight_names: [weight_1, weight_-1]
histnames: [er_template, er_template, er_template,]
weight_a: 0.2
weight_b: 0.3
weight_names: [weight_a, weight_b] # not meaningful, just an example
histnames: [er_template, er_template, er_template]
template_filenames: [er_template_0.h5, er_template_1.h5, er_template_-1.h5]
histogram_multiplier: 200
histogram_scale_factor: 3
histogram_multiplier: 100 # absolute rate, /year
histogram_scale_factor: 2

- name: wimp
histname: wimp_template
Expand Down
21 changes: 14 additions & 7 deletions alea/template_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from blueice import HistogramPdfSource
from inference_interface import template_to_multihist

logging.basicConfig(level=logging.INFO)
logging.basicConfig(level=logging.debug)
can_check_binning = True


Expand Down Expand Up @@ -128,7 +128,7 @@ def apply_slice_args(self, h, slice_args=None):
"""
if slice_args is None:
slice_args = self.config.get("slice_args", {})
if type(slice_args) is dict:
if isinstance(slice_args, dict):
slice_args = [slice_args]

for sa in slice_args:
Expand Down Expand Up @@ -231,6 +231,8 @@ class CombinedSource(TemplateSource, HistogramPdfSource):
Source that inherits structure from TH2DSource by Jelle,
but takes in lists of histogram names.
The first histogram is the base histogram, and the rest are added to it with weights.
Currently the weights are hardcoded in the config, can not be changed in the fit.
In other words, the weight can not be shape parameter.
:param weights: Weights of the 2nd to the last histograms.
:param histnames: List of filenames containing the histograms.
Expand All @@ -257,9 +259,9 @@ def build_histogram(self):
"weights must be 1 shorter than histnames, templatenames")

slice_args = self.config.get("slice_args", {})
if type(slice_args) is dict:
if isinstance(slice_args, dict):
slice_args = [slice_args]
if all(type(sa) is dict for sa in slice_args):
if all(isinstance(sa, dict) for sa in slice_args):
# Recognize as a single slice_args for all histograms
slice_argss = [slice_args] * len(histnames)
else:
Expand Down Expand Up @@ -310,17 +312,22 @@ def build_histogram(self):
histograms[0] += h_comp * weights[i]
h = histograms[0]

logging.debug("Setting zero for NaNs and Infs in combined template.")
h.histogram[np.isinf(h.histogram)] = 0.
h.histogram[np.isnan(h.histogram)] = 0.
h.histogram[h.histogram < 0] = 0.

# Set pdf values that are below 0 to zero:
if np.min(h.histogram) < 0:
raise AssertionError(
f"There are bins for source {templatename} with negative entries.")

logging.info(
logging.debug(
"Normalising combined template, "
"the absolute rate is defined in histogram_multiplier")
h = h / h.n
h *= self.config.get("histogram_multiplier", 1)
logging.info("Applying slice fraction to combined template")
logging.debug("Applying slice fraction to combined template")
h *= slice_fractions[0]

# Fix the bin sizes
Expand Down Expand Up @@ -364,7 +371,7 @@ def build_histogram(self):
h = template_to_multihist(templatename, histname)

spectrum = self.config["spectrum"]
if type(spectrum) is str:
if isinstance(spectrum, str):
spectrum = self._get_json_spectrum(spectrum.format(**self.format_named_parameters))

# Perform E-scaling, assume first axis is energy
Expand Down

0 comments on commit 1f9251b

Please sign in to comment.