Fit the function to a gaussian.

    Parameters
    ----------
    X: 1d array
        X values
    Y: 1d array
        Y values

    Returns
    -------
    (The return from scipy.optimize.curve_fit)
    popt : array
        Optimal values for the parameters
    pcov : 2d array
        The estimated covariance of popt.

    Notes
    -----
    /!\ This uses a slow curve_fit function! do not use if need speed!

def gauss_fit(X, Y):
    """
    Fit the function to a gaussian.

    Parameters
    ----------
    X: 1d array
        X values
    Y: 1d array
        Y values

    Returns
    -------
    (The return from scipy.optimize.curve_fit)
    popt : array
        Optimal values for the parameters
    pcov : 2d array
        The estimated covariance of popt.

    Notes
    -----
    /!\ This uses a slow curve_fit function! do not use if need speed!
    """
    X = np.asarray(X)
    Y = np.asarray(Y)
    # Can not have negative values
    Y[Y < 0] = 0
    # define gauss function

    def gauss(x, a, x0, sigma):
        return a * np.exp(-(x - x0)**2 / (2 * sigma**2))
    # get first estimation for parameter
    mean = (X * Y).sum() / Y.sum()
    sigma = np.sqrt((Y * ((X - mean)**2)).sum() / Y.sum())
    height = Y.max()
    # fit with curve_fit
    return curve_fit(gauss, X, Y, p0=[height, mean, sigma])