diff --git a/config/binning_example_2d.json b/config/binning_example_2d.json new file mode 100644 index 0000000..34a5a29 --- /dev/null +++ b/config/binning_example_2d.json @@ -0,0 +1,10 @@ +{ + "combined_bins": [ + {"y_bin": [0, 250], "x_bins": [0.0, 0.5, 0.7, 0.8, 0.85, 0.9, 0.91, 0.92, 0.93, 0.94, 0.95, 0.96, 0.97, 0.98, 0.99, 1.0]}, + {"y_bin": [250, 275], "x_bins": [0.0, 0.5, 0.7, 0.8, 0.85, 0.9, 0.91, 0.92, 0.93, 0.94, 0.95, 0.96, 0.97, 0.98, 0.99, 1.0]}, + {"y_bin": [275, 300], "x_bins": [0.0, 0.5, 0.7, 0.8, 0.85, 0.9, 0.91, 0.92, 0.93, 0.94, 0.95, 0.96, 0.97, 0.98, 0.99, 1.0]}, + {"y_bin": [300, 325], "x_bins": [0.0, 0.5, 0.7, 0.8, 0.85, 0.9, 0.91, 0.92, 0.93, 0.94, 0.95, 0.96, 0.97, 0.98, 0.99, 1.0]}, + {"y_bin": [325, 350], "x_bins": [0.0, 0.5, 0.7, 0.8, 0.85, 0.9, 0.91, 0.92, 0.93, 0.94, 0.95, 0.96, 0.97, 0.98, 0.99, 1.0]}, + {"y_bin": [350, 500], "x_bins": [0.0, 0.5, 0.7, 0.8, 0.85, 0.9, 0.91, 0.92, 0.93, 0.94, 0.95, 0.96, 0.97, 0.98, 0.99, 1.0]} + ] + } \ No newline at end of file diff --git a/dc_make/binner.py b/dc_make/binner.py index 707f0ae..d6ee768 100644 --- a/dc_make/binner.py +++ b/dc_make/binner.py @@ -1,10 +1,12 @@ import json from StatInference.common.tools import listToVector, rebinAndFill, importROOT +import FLAF.Common.HistHelper as HistHelper ROOT = importROOT() class Binner: def __init__(self, hist_bins): self.hist_bins = [] + self.linearize = False if type(hist_bins) == str: #Load json with open(hist_bins) as f: @@ -15,8 +17,14 @@ def __init__(self, hist_bins): }) elif hist_bins is not None: raise RuntimeError("Incompatible hist_bins format") - for entry in self.hist_bins: - entry['bins'] = listToVector(entry['bins'], 'double') + + # Loaded the string, now what is the object? + if type(self.hist_bins) == dict: + if "combined_bins" in self.hist_bins: + self.linearize = True + else: + for entry in self.hist_bins: + entry['bins'] = listToVector(entry['bins'], 'double') def applyBinning(self, era, channel, category, model_params, hist): if len(self.hist_bins) == 0: @@ -30,16 +38,18 @@ def entry_passes(entry): for param_key, param_value in model_params.items(): if not (param_key not in entry or param_value in entry[param_key]): return False return True - - for entry in self.hist_bins: - # print(entry) - if entry_passes(entry): new_binning.append(entry['bins']) - if len(new_binning) <= 0: - raise RuntimeError(f"No binning found for era/channel/category/params {era}/{channel}/{category}/{model_params}") - if len(new_binning) >= 2: - raise RuntimeError(f"Multiple binnings found for era/channel/category/params {era}/{channel}/{category}/{model_params}") - - new_hist = ROOT.TH1F(hist.GetName(), hist.GetTitle(), len(new_binning[0]) - 1,new_binning[0].data()) - rebinAndFill(new_hist, hist) + + if self.linearize: + new_hist = HistHelper.RebinHisto(hist, self.hist_bins, hist.GetTitle()) + else: + for entry in self.hist_bins: + # print(entry) + if entry_passes(entry): new_binning.append(entry['bins']) + if len(new_binning) <= 0: + raise RuntimeError(f"No binning found for era/channel/category/params {era}/{channel}/{category}/{model_params}") + if len(new_binning) >= 2: + raise RuntimeError(f"Multiple binnings found for era/channel/category/params {era}/{channel}/{category}/{model_params}") + new_hist = ROOT.TH1F(hist.GetName(), hist.GetTitle(), len(new_binning[0]) - 1,new_binning[0].data()) + rebinAndFill(new_hist, hist) return new_hist diff --git a/dc_make/maker.py b/dc_make/maker.py index 6152143..7d70d33 100644 --- a/dc_make/maker.py +++ b/dc_make/maker.py @@ -178,14 +178,14 @@ def getShape(self, process, era, channel, category, model_params, unc_name=None, for subp in process.subprocesses: hist_name = f"{channel}/{category}/{subp}" if unc_name and unc_scale: - hist_name += f"_{unc_name}{unc_scale}" + hist_name += f"_{unc_name}_{unc_scale}" subhist = file.Get(hist_name) if subhist == None: raise RuntimeError(f"Cannot find histogram {hist_name} in {file.GetName()}") hists.append(self.hist_binner.applyBinning(era, channel, category, model_params, subhist)) else: if unc_name and unc_scale: - hist_name += f"_{unc_name}{unc_scale}" + hist_name += f"_{unc_name}_{unc_scale}" hist = file.Get(hist_name) if hist == None: raise RuntimeError(f"Cannot find histogram {hist_name} in {file.GetName()}")