plot_util#

Some utils for plot functions.

Functions:

get_cms_label(ax, llabel)

Helper function to get the CMS label configuration.

round_dynamic(value)

Rounds a value at various scales to a subjective, sensible precision.

inject_label(label, inject, *[, ...])

Injects a string inject into a label at a specific position, determined by different strategies in the following order:

apply_settings(instances, settings[, ...])

applies settings from settings dictionary to a list of order objects containers

hists_merge_cutflow_steps(hists)

Make 'step' axis uniform among a set of histograms.

apply_process_settings(hists[, process_settings])

applies settings from process_settings dictionary to the process_insts; the scale setting is directly applied to the histograms

remove_label_placeholders(label)

rtype:

str

apply_variable_settings(hists, variable_insts)

applies settings from variable_settings dictionary to the variable_insts; the rebin, overflow, underflow, and slice settings are directly applied to the histograms

use_flow_bins(h_in, axis_name[, underflow, ...])

Adds content of the flow bins of axis axis_name of histogram h_in to the first/last bin.

apply_density_to_hists(hists[, density])

Scales number of histogram entries to bin widths.

remove_residual_axis(hists, ax_name[, max_bins])

removes axis named 'ax_name' if existing and there is only a single bin in the axis; raises Exception otherwise

prepare_style_config(config_inst, ...[, ...])

small helper function that sets up a default style config based on the instances of the config, category and variable

prepare_stack_plot_config(hists[, ...])

Prepares a plot config with one entry to create plots containing a stack of backgrounds with uncertainty bands, unstacked processes as lines and data entrys with errorbars.

get_position(minimum, maximum[, factor, ...])

get a relative position between a min and max value based on the scale

join_labels(*labels[, inline_sep, multiline_sep])

rtype:

str

reduce_with(spec, values)

Reduce an array of values to a single value using the function indicated by spec.

broadcast_1d_to_nd(x, final_shape[, axis])

Helper function to broadcast a 1d array x to an nd array with shape final_shape.

broadcast_nminus1d_to_nd(x, final_shape[, axis])

Helper function to broadcast a (n-1)d array x to an nd array with shape final_shape.

get_profile_width(h_in[, axis])

Function that takes a histogram h_in and returns the mean and width when profiling over the axis axis.

get_profile_variations(h_in[, axis])

Returns a profile histogram plus the up and down variations of the profile from a normal histogram with N-1 axes.

blind_sensitive_bins(hists, config_inst, ...)

Function that takes a histogram h_in and blinds the values of the profile over the axis axis that are below a certain threshold threshold.

get_cms_label(ax, llabel)[source]#

Helper function to get the CMS label configuration.

Parameters:
  • ax (Axes) – The axis to plot the CMS label on.

  • llabel (str) – The left label of the CMS label.

Return type:

dict

Returns:

A dictionary with the CMS label configuration.

round_dynamic(value)[source]#

Rounds a value at various scales to a subjective, sensible precision. Rounding rules:

  • 0 -> 0 (int)

  • (0, 1) -> round to 1 significant digit (float)

  • [1, 10) -> round to 1 significant digit (int)

  • [10, inf) -> round to 2 significant digits (int)

Parameters:

value (int | float) – The value to round.

Return type:

int | float

Returns:

The rounded value.

inject_label(label, inject, *, placeholder=None, before_parentheses=False)[source]#

Injects a string inject into a label at a specific position, determined by different strategies in the following order:

  • If placeholder is defined, label should contain a substring "__PLACEHOLDER__"

which is replaced. - Otherwise, if before_parentheses is set to True, the string is inserted before the last pair of parentheses. - Otherwise, the string is appended to the label.

Parameters:
  • label (str) – The label to inject the string inject into.

  • inject (str | int | float) – The string to inject.

  • placeholder (str | None, default: None) – The placeholder to replace in the label.

  • before_parentheses (bool, default: False) – Whether to insert the string before the parentheses in the label.

Return type:

str

Returns:

The updated label.

apply_settings(instances, settings, parent_check=None)[source]#

applies settings from settings dictionary to a list of order objects containers

Parameters:
  • instances (Iterable[od.AuxDataMixin]) – List of order instances to apply settings to.

  • settings (dict[str, Any] | None) – Dictionary of settings to apply on the instances. Each key should correspond to the name of an instance and each value should be a dictionary with attributes that will be set on the instance either as a attribute or as an auxiliary.

  • parent_check (Callable[[od.AuxDataMixin, str], bool] | None, default: None) – Function that checks if an instance has a parent with a given name.

Return type:

None

hists_merge_cutflow_steps(hists)[source]#

Make ‘step’ axis uniform among a set of histograms. Takes a dict of 1D histogram objects with a single ‘step’ axis of type StrCategory, computes the full list of possible ‘step’ values across all histograms, and returns a dict of histograms whose ‘step’ axis has a corresponding, uniform structure. The values and variances inserted for missing ‘step’ are taken from the previous existing step.

Return type:

dict

apply_process_settings(hists, process_settings=None)[source]#

applies settings from process_settings dictionary to the process_insts; the scale setting is directly applied to the histograms

Return type:

dict

remove_label_placeholders(label)[source]#
Return type:

str

apply_variable_settings(hists, variable_insts, variable_settings=None)[source]#

applies settings from variable_settings dictionary to the variable_insts; the rebin, overflow, underflow, and slice settings are directly applied to the histograms

Return type:

dict

use_flow_bins(h_in, axis_name, underflow=True, overflow=True)[source]#

Adds content of the flow bins of axis axis_name of histogram h_in to the first/last bin.

Parameters:
  • h_in (hist.Hist) – Input histogram

  • axis_name (str | int) – Name or index of the axis of interest.

  • underflow (bool, default: True) – Whether to add the content of the underflow bin to the first bin of axis *axis_name.

  • overflow (bool, default: True) – Whether to add the content of the overflow bin to the last bin of axis axis_name.

Return type:

hist.Hist

Returns:

Copy of the histogram with underflow and/or overflow content added to the first/last bin of the histogram.

apply_density_to_hists(hists, density=False)[source]#

Scales number of histogram entries to bin widths.

Return type:

dict

remove_residual_axis(hists, ax_name, max_bins=1)[source]#

removes axis named ‘ax_name’ if existing and there is only a single bin in the axis; raises Exception otherwise

Return type:

dict

prepare_style_config(config_inst, category_inst, variable_inst, density=False, shape_norm=False, yscale='')[source]#

small helper function that sets up a default style config based on the instances of the config, category and variable

Return type:

dict

prepare_stack_plot_config(hists, shape_norm=False, hide_errors=None, **kwargs)[source]#

Prepares a plot config with one entry to create plots containing a stack of backgrounds with uncertainty bands, unstacked processes as lines and data entrys with errorbars.

Return type:

OrderedDict

get_position(minimum, maximum, factor=1.4, logscale=False)[source]#

get a relative position between a min and max value based on the scale

Return type:

float

join_labels(*labels, inline_sep=',', multiline_sep='\\n')[source]#
Return type:

str

reduce_with(spec, values)[source]#

Reduce an array of values to a single value using the function indicated by spec. Intended as a helper for resolving range specifications supplied as strings.

Supported specifiers are: :rtype: float

  • ‘min’: minimum value

  • ‘max’: maximum value

  • ‘maxabs’: the absolute value of the maximum or minimum, whichever is larger

  • ‘minabs’: the absolute value of the maximum or minimum, whichever is smaller

A hyphen (-) can be prefixed to any specifier to return its negative.

Callables can be passed as spec and should take a single array-valued argument and return a single value. Floats passes as specifiers will be returned directly.

broadcast_1d_to_nd(x, final_shape, axis=1)[source]#

Helper function to broadcast a 1d array x to an nd array with shape final_shape. The length of x should be the same as final_shape[axis].

Return type:

array

broadcast_nminus1d_to_nd(x, final_shape, axis=1)[source]#

Helper function to broadcast a (n-1)d array x to an nd array with shape final_shape. final_shape should be the same as x.shape except that the axis axis is missing.

Return type:

array

get_profile_width(h_in, axis=1)[source]#

Function that takes a histogram h_in and returns the mean and width when profiling over the axis axis.

Return type:

tuple[array, array]

get_profile_variations(h_in, axis=1)[source]#

Returns a profile histogram plus the up and down variations of the profile from a normal histogram with N-1 axes. The axis given is profiled over and removed from the final histograms.

Return type:

dict[str, Hist]

blind_sensitive_bins(hists, config_inst, threshold)[source]#

Function that takes a histogram h_in and blinds the values of the profile over the axis axis that are below a certain threshold threshold. The function needs an entry in the process_groups key of the config auxiliary that is called “signals” to know, where the signal processes are defined (regex allowed). The histograms are not changed inplace, but copies of the modified histograms are returned.

Return type:

dict[Process, Hist]