def get_optimal_bin_size(n, round=True):
    """Helper function to calculate optimal binning

    This function calculates the optimal amount of bins for the number of events n.

    Args:
        n (int): number of events to be binned
        round (bool or int): Round to
    Returns:
        (int): Optimal number of bins

    """

    def roundtobase(n, base=5):
        diff = n % base
        if diff <= base / 2.:
            return n - diff
        else:
            return n - diff + base

    n_opt = int(2 * n**(1/3.0))

    if round:
        base = 5
        if isinstance(round, int):
            base = round
        n_opt = roundtobase(n_opt, base)
    
    if n_opt == 0: 
        n_opt = 1

    return n_opt