columnflow.config_util#

Collection of general helpers and utilities.

Summary#

get_root_processes_from_campaign(campaign)

Extracts all root process objects from datasets contained in an order campaign and returns them in a unique object index.

add_shift_aliases(config, shift_source, aliases)

Extracts the two up and down shift instances from a config corresponding to a shift_source (i.e.

get_shifts_from_sources(config, *shift_sources)

Takes a config object and returns a list of shift instances for both directions given a sequence shift_sources.

create_category_combinations(config, ...[, ...])

Given a config object and sequences of categories in a dict, creates all combinations of possible leaf categories at different depths, connects them with parent - child relations (see order.Category) and returns the number of newly created categories.

Functions#

get_root_processes_from_campaign#

get_root_processes_from_campaign(campaign)[source]#

Extracts all root process objects from datasets contained in an order campaign and returns them in a unique object index.

Return type:

UniqueObjectIndex

add_shift_aliases#

add_shift_aliases(config, shift_source, aliases)[source]#

Extracts the two up and down shift instances from a config corresponding to a shift_source (i.e. the name of a shift without directions) and assigns aliases to their auxiliary data.

Aliases should be given in a dictionary, mapping alias targets (keys) to sources (values). In both strings, template variables are injected with fields corresponding to all od.Shift attributes, such as name, id, and direction.

Example:

add_shift_aliases(config, "pdf", {"pdf_weight": "pdf_weight_{direction}"})
# adds {"pdf_weight": "pdf_weight_up"} to the "pdf_up" shift in "config"
# plus {"pdf_weight": "pdf_weight_down"} to the "pdf_down" shift in "config"
Return type:

None

get_shifts_from_sources#

get_shifts_from_sources(config, *shift_sources)[source]#

Takes a config object and returns a list of shift instances for both directions given a sequence shift_sources.

Return type:

list[Shift]

create_category_combinations#

create_category_combinations(config, categories, name_fn, kwargs_fn=None, skip_existing=True, skip_fn=None)[source]#

Given a config object and sequences of categories in a dict, creates all combinations of possible leaf categories at different depths, connects them with parent - child relations (see order.Category) and returns the number of newly created categories.

categories should be a dictionary that maps string names to sequences of categories that should be combined. The names are used as keyword arguments in a callable name_fn that is supposed to return the name of newly created categories (see example below).

Each newly created category is instantiated with this name as well as arbitrary keyword arguments as returned by kwargs_fn. This function is called with the categories (in a dictionary, mapped to the sequence names as given in categories) that contribute to the newly created category and should return a dictionary. If the fields "id" and "selection" are missing, they are filled with reasonable defaults leading to a auto-generated, deterministic id and a list of all parent selection statements.

If the name of a new category is already known to config it is skipped unless skip_existing is False. In addition, skip_fn can be a callable that receives a dictionary mapping group names to categories that represents the combination of categories to be added. In case skip_fn returns True, the combination is skipped.

Example:

categories = {
    "lepton": [cfg.get_category("e"), cfg.get_category("mu")],
    "n_jets": [cfg.get_category("1j"), cfg.get_category("2j")],
    "n_tags": [cfg.get_category("0t"), cfg.get_category("1t")],
}

def name_fn(categories):
    # simple implementation: join names in defined order if existing
    return "__".join(cat.name for cat in categories.values() if cat)

def kwargs_fn(categories):
    # return arguments that are forwarded to the category init
    # (use id "+" here which simply increments the last taken id, see order.Category)
    # (note that this is also the default)
    return {"id": "+"}

create_category_combinations(cfg, categories, name_fn, kwargs_fn)
Return type:

int