|
2 | 2 |
|
3 | 3 | import fnmatch
|
4 | 4 | import logging
|
5 |
| -from typing import Any, Callable, Dict, List, Literal, Optional |
| 5 | +from typing import Any, Callable, Dict, List, Literal, Optional, Tuple |
6 | 6 |
|
7 | 7 | import boost_histogram as bh
|
8 | 8 |
|
|
38 | 38 | # (which returns a histogram) into a function that returns None
|
39 | 39 | WrapperFunc = Callable[[UserTemplateFunc], ProcessorFunc]
|
40 | 40 |
|
| 41 | +# type of tuple capturing all relevant information to obtain a template histogram |
| 42 | +# this includes region, sample, systematic and template (up/down) |
| 43 | +TemplateHistogramInformation = Tuple[ |
| 44 | + Dict[str, Any], Dict[str, Any], Dict[str, Any], Optional[Literal["Up", "Down"]] |
| 45 | +] |
| 46 | + |
41 | 47 |
|
42 | 48 | class Router:
|
43 | 49 | """Holds user-defined processing functions and matches functions to templates.
|
@@ -257,33 +263,18 @@ def _find_template_builder_match(
|
257 | 263 | return None
|
258 | 264 |
|
259 | 265 |
|
260 |
| -def apply_to_all_templates( |
261 |
| - config: Dict[str, Any], |
262 |
| - default_func: ProcessorFunc, |
263 |
| - *, |
264 |
| - match_func: Optional[MatchFunc] = None, |
265 |
| -) -> None: |
266 |
| - """Applies the supplied function ``default_func`` to all templates. |
267 |
| -
|
268 |
| - The templates are specified by the configuration file. The function takes four |
269 |
| - arguments in this order: |
270 |
| -
|
271 |
| - - the dict specifying region information |
272 |
| - - the dict specifying sample information |
273 |
| - - the dict specifying systematic information |
274 |
| - - the template being considered: "Up", "Down", or None for the nominal template |
275 |
| -
|
276 |
| - In addition it is possible to specify a function that returns custom overrides. If |
277 |
| - one is found for a given template, it is used instead of the default. |
| 266 | +def required_templates(config: Dict[str, Any]) -> List[TemplateHistogramInformation]: |
| 267 | + """Returns relevant information needed to produce all required template histograms. |
278 | 268 |
|
279 | 269 | Args:
|
280 | 270 | config (Dict[str, Any]): cabinetry configuration
|
281 |
| - default_func (ProcessorFunc): function to be called for every template by |
282 |
| - default |
283 |
| - match_func: (Optional[MatchFunc], optional): function that returns user-defined |
284 |
| - functions to override the call to ``default_func``, defaults to None (then |
285 |
| - it is not used) |
| 271 | +
|
| 272 | + Returns: |
| 273 | + List[TemplateHistogramInformation]: list of relevant information for each |
| 274 | + template histogram |
286 | 275 | """
|
| 276 | + all_templates = [] |
| 277 | + |
287 | 278 | for region in config["Regions"]:
|
288 | 279 | log.debug(f" in region {region['Name']}")
|
289 | 280 |
|
@@ -321,22 +312,52 @@ def apply_to_all_templates(
|
321 | 312 | f"{' ' + template if template is not None else ''}"
|
322 | 313 | )
|
323 | 314 |
|
324 |
| - func_override = None |
325 |
| - if match_func is not None: |
326 |
| - # check whether a user-defined function was registered that |
327 |
| - # matches this region-sample-systematic-template |
328 |
| - systematic_name = ( |
329 |
| - systematic["Name"] if template is not None else "" |
330 |
| - ) |
331 |
| - func_override = match_func( |
332 |
| - region["Name"], sample["Name"], systematic_name, template |
333 |
| - ) |
334 |
| - if func_override is not None: |
335 |
| - # call the user-defined function |
336 |
| - log.debug( |
337 |
| - f"executing user-defined override {func_override.__name__}" |
338 |
| - ) |
339 |
| - func_override(region, sample, systematic, template) |
340 |
| - else: |
341 |
| - # call the provided default function |
342 |
| - default_func(region, sample, systematic, template) |
| 315 | + all_templates.append((region, sample, systematic, template)) |
| 316 | + |
| 317 | + return all_templates |
| 318 | + |
| 319 | + |
| 320 | +def apply_to_templates( |
| 321 | + default_func: ProcessorFunc, # BREAKING API CHANGE |
| 322 | + template_list: List[TemplateHistogramInformation], |
| 323 | + *, |
| 324 | + match_func: Optional[MatchFunc] = None, |
| 325 | +) -> None: |
| 326 | + """Applies the supplied function ``default_func`` to all templates. |
| 327 | +
|
| 328 | + The templates are specified by the configuration file. The function takes four |
| 329 | + arguments in this order: |
| 330 | +
|
| 331 | + - the dict specifying region information |
| 332 | + - the dict specifying sample information |
| 333 | + - the dict specifying systematic information |
| 334 | + - the template being considered: "Up", "Down", or None for the nominal template |
| 335 | +
|
| 336 | + In addition it is possible to specify a function that returns custom overrides. If |
| 337 | + one is found for a given template, it is used instead of the default. |
| 338 | +
|
| 339 | + Args: |
| 340 | + default_func (ProcessorFunc): function to be called for every template by |
| 341 | + default |
| 342 | + template_list (List[TemplateHistogramInformation]): list of template information |
| 343 | + to apply function to |
| 344 | + match_func: (Optional[MatchFunc], optional): function that returns user-defined |
| 345 | + functions to override the call to ``default_func``, defaults to None (then |
| 346 | + it is not used) |
| 347 | + """ |
| 348 | + for region, sample, systematic, template in template_list: |
| 349 | + func_override = None |
| 350 | + if match_func is not None: |
| 351 | + # check whether a user-defined function was registered that |
| 352 | + # matches this region-sample-systematic-template |
| 353 | + systematic_name = systematic["Name"] if template is not None else "" |
| 354 | + func_override = match_func( |
| 355 | + region["Name"], sample["Name"], systematic_name, template |
| 356 | + ) |
| 357 | + if func_override is not None: |
| 358 | + # call the user-defined function |
| 359 | + log.debug(f"executing user-defined override {func_override.__name__}") |
| 360 | + func_override(region, sample, systematic, template) |
| 361 | + else: |
| 362 | + # call the provided default function |
| 363 | + default_func(region, sample, systematic, template) |
0 commit comments