Module utils

Utility functions for the upsilon analysis.

upsilon_analysis.utils.y_bin_edges(y_min, y_max, n_bins)[source]

A generator that yields bin edges.

Parameters:
  • y_min (float) – The lower edge of the first bin.
  • y_max (float) – The upper edge of the last bin.
  • n_bins (int) – The number of bins.
Return type:

float

Guarantees that y_min is the first item yielded and y_max is the last, without roundoff errors.

upsilon_analysis.utils.pt_bin_edges(pt_min, pt_max, bin_width)[source]

A generator that makes the bins larger above a certain threshold.

Parameters:
  • pt_min (float) – The lower edge of the first bin.
  • pt_max (float) – The upper edge of the last bin.
  • bin_width (float) – The width bins (but see below).
Return type:

float

Yields bin edges from pt_min to pt_max (both end are always present). Above 40 GeV/c, bins are made progressively wider:

  • above 40 GeV/c they are 1.5x as wide;
  • above 45 GeV/c they are 2x as wide;
  • above 50 GeV/c they are 2.5x as wide;
  • above 60 GeV/c they are 5x as wide;
  • above 70 GeV/c they are 15x as wide.
upsilon_analysis.utils.bins(edges)[source]

Roughly equivalent to zip(edges[:-1], edges[1:]).

Parameters:edges (Iterable[float]) – The bins’ edges.
Return type:tuple[float, float]

Actually edges can also be an iterable that does not support slicing, so it is a more general implementation than that with zip (altough probably less efficient).

upsilon_analysis.utils.static_variables(**kwargs)[source]

Decorator to define C-like static variables for a function.

Using the decorator like in:

@static_variables(variable=value)
def func(...):
    ...

is equivalent to:

def func(...):
    ...
func.variable = value

so the “static variable” can be accessed from within the function by writing func.variable.

class upsilon_analysis.utils.GausParameters[source]

A namedtuple for the results of a fit with a Gaussian.

Variables:
  • a (float) – Number of occurrences fitted (see get_gaus_parameters).
  • m (float) – Fitted pole mass (Gaussian’s mean).
  • sigma (float) – Fitted width (Gaussian’s standard deviation).
class upsilon_analysis.utils.LineParameters[source]

A named tuple for the results of a fit with a line.

Variables:
  • q (float) – Constant / y-intercept.
  • m (float) – Slope / coefficient of x.
class upsilon_analysis.utils.FitResults[source]

A named tuple for the results of the global fit.

Variables:
  • y1 (GausParameters) – First resonance.
  • y2 (GausParameters) – Second resonance.
  • y3 (GausParameters) – Third resonance.
  • bkg (LineParameters) – Background.
  • chi2 (float) – Chi-squared of the fit.
  • ndf (float) – Number of degrees of freedom of the fit.
  • nevt (int) – Total number of events in the fitted histogram.
upsilon_analysis.utils.get_gaus_parameters(ga, bin_width, hist_range=(8.5, 11.5))[source]

Gets a GausParameters from a TF1.

Parameters:
  • ga (ROOT.TF1) – The TF1 with the parameters to get.
  • bin_width (float) – The width of the bin of the fitted histogram.
  • hist_range (tuple[float, float], optional) – The range of the fitted histogram.
Return type:

GausParameters

This function takes a TF1 defined by the formula “gaus(0)” as input and returns a GausParameters where the field a is the total number of occurrences given by the fit (while the other fields are the usual mean and sigma). This function is necessary because “gaus(0)” is not normalized.

The argument ga is the TF1 with the parameters set by the fit.

Assuming the histogram being fitted has fixed-width bins, the integral of the gaussian will be equal to the integral of the histogram, which is the total number of entries times the bin width.

Note that this also works if the fit option I (using the integral of the function in the bin rather than its value at the center) is used, since ROOT normalizes histogram integrals to the bin width.

Warning

Do not forget to provide the range if the default hist_range is not correct.

upsilon_analysis.utils.print_fit_results(results, file=None)[source]

Print fit results in CSV format to file (default stdout).

Parameters:
  • results (dict[tuple[float, float], dict[tuple[float, float], FitResults]]) – A dictionary like that returned by core.fit_histograms
  • file – The file to write to. Default is stdout.
Returns:

None

upsilon_analysis.utils.sort_bins(iterable)[source]

Takes an iterable of bins and returns a sorted list of them.

Parameters:iterable (Iterable[tuple[float, float]]) – The iterable with the bins to sort.
Raises:RuntimeError – If the bins are not valid or overlap.
Return type:list[tuple[float, float]]

This function takes an iterable of bins, given as tuples (bin_low, bin_high), and returns a list of the same bins, but in ascending order.

The following requirements must be met:

  • all bins must be well defined, i.e. the lower limit must be strictly less than the upper limit;
  • bins must not overlap;

if they are not, RuntimeError will be raised.

upsilon_analysis.utils.uniques(iterable)[source]

Yields only uniques values out of a sorted iterable.

Parameters:iterable – A sorted iterable of objects that support the != operator.

Loops over iterable (which is assumed to be sorted) and yields its items only if they are unique (i.e. different from the previous, since they are sorted).

Warning

This function is a very simple and fast implementation for the specific case of sorted iterables.

If iterable is not sorted, the same item may be yielded multiple times.