|
1 | 1 | import asyncio
|
2 | 2 | import json
|
3 | 3 |
|
| 4 | +import cabinetry |
| 5 | +from cabinetry.contrib import histogram_reader |
4 | 6 | import hist
|
5 | 7 | import matplotlib as mpl
|
6 | 8 | import matplotlib.pyplot as plt
|
@@ -97,31 +99,31 @@ def save_histograms(all_histograms, fileset, filename):
|
97 | 99 |
|
98 | 100 | with uproot.recreate(filename) as f:
|
99 | 101 | for region in ["4j1b", "4j2b"]:
|
100 |
| - f[f"{region}_pseudodata"] = pseudo_data[120j::hist.rebin(2), region] |
| 102 | + f[f"{region}_pseudodata"] = pseudo_data[:, region] |
101 | 103 | for sample in nominal_samples:
|
102 | 104 | sample_name = sample.split("__")[0]
|
103 |
| - f[f"{region}_{sample_name}"] = all_histograms[120j::hist.rebin(2), region, sample_name, "nominal"] |
| 105 | + f[f"{region}_{sample_name}"] = all_histograms[:, region, sample_name, "nominal"] |
104 | 106 |
|
105 | 107 | # b-tagging variations
|
106 | 108 | for i in range(4):
|
107 | 109 | for direction in ["up", "down"]:
|
108 | 110 | variation_name = f"btag_var_{i}_{direction}"
|
109 |
| - f[f"{region}_{sample_name}_{variation_name}"] = all_histograms[120j::hist.rebin(2), region, sample_name, variation_name] |
| 111 | + f[f"{region}_{sample_name}_{variation_name}"] = all_histograms[:, region, sample_name, variation_name] |
110 | 112 |
|
111 | 113 | # jet energy scale variations
|
112 | 114 | for variation_name in ["pt_scale_up", "pt_res_up"]:
|
113 |
| - f[f"{region}_{sample_name}_{variation_name}"] = all_histograms[120j::hist.rebin(2), region, sample_name, variation_name] |
| 115 | + f[f"{region}_{sample_name}_{variation_name}"] = all_histograms[:, region, sample_name, variation_name] |
114 | 116 |
|
115 | 117 | # ttbar modeling
|
116 |
| - f[f"{region}_ttbar_ME_var"] = all_histograms[120j::hist.rebin(2), region, "ttbar", "ME_var"] |
117 |
| - f[f"{region}_ttbar_PS_var"] = all_histograms[120j::hist.rebin(2), region, "ttbar", "PS_var"] |
| 118 | + f[f"{region}_ttbar_ME_var"] = all_histograms[:, region, "ttbar", "ME_var"] |
| 119 | + f[f"{region}_ttbar_PS_var"] = all_histograms[:, region, "ttbar", "PS_var"] |
118 | 120 |
|
119 |
| - f[f"{region}_ttbar_scaledown"] = all_histograms[120j :: hist.rebin(2), region, "ttbar", "scaledown"] |
120 |
| - f[f"{region}_ttbar_scaleup"] = all_histograms[120j :: hist.rebin(2), region, "ttbar", "scaleup"] |
| 121 | + f[f"{region}_ttbar_scaledown"] = all_histograms[:, region, "ttbar", "scaledown"] |
| 122 | + f[f"{region}_ttbar_scaleup"] = all_histograms[:, region, "ttbar", "scaleup"] |
121 | 123 |
|
122 | 124 | # W+jets scale
|
123 |
| - f[f"{region}_wjets_scale_var_down"] = all_histograms[120j :: hist.rebin(2), region, "wjets", "scale_var_down"] |
124 |
| - f[f"{region}_wjets_scale_var_up"] = all_histograms[120j :: hist.rebin(2), region, "wjets", "scale_var_up"] |
| 125 | + f[f"{region}_wjets_scale_var_down"] = all_histograms[:, region, "wjets", "scale_var_down"] |
| 126 | + f[f"{region}_wjets_scale_var_up"] = all_histograms[:, region, "wjets", "scale_var_up"] |
125 | 127 |
|
126 | 128 |
|
127 | 129 | class ServiceXDatasetGroup():
|
@@ -153,3 +155,25 @@ def get_data_rootfiles_uri(self, query, as_signed_url=True, title="Untitled"):
|
153 | 155 | files_per_process.update({process: all_files[parent_key[self.filelist[:,1]==process]]})
|
154 | 156 |
|
155 | 157 | return files_per_process
|
| 158 | + |
| 159 | + |
| 160 | +def get_cabinetry_rebinning_router(config, rebinning): |
| 161 | + # perform re-binning in cabinetry by providing a custom function reading histograms |
| 162 | + # will eventually be replaced via https://github.com/scikit-hep/cabinetry/issues/412 |
| 163 | + rebinning_router = cabinetry.route.Router() |
| 164 | + |
| 165 | + # this reimplements some of cabinetry.templates.collect |
| 166 | + general_path = config["General"]["InputPath"] |
| 167 | + variation_path = config["General"].get("VariationPath", None) |
| 168 | + |
| 169 | + # define a custom template builder function that is executed for data samples |
| 170 | + @rebinning_router.register_template_builder() |
| 171 | + def build_data_hist(region, sample, systematic, template): |
| 172 | + # get path to histogram |
| 173 | + histo_path = cabinetry.templates.collector._histo_path(general_path, variation_path, region, sample, systematic, template) |
| 174 | + h = hist.Hist(histogram_reader.with_uproot(histo_path)) # turn from boost-histogram into hist |
| 175 | + # perform re-binning |
| 176 | + h = h[rebinning] |
| 177 | + return h |
| 178 | + |
| 179 | + return rebinning_router |
0 commit comments