def calibrate_dom(
        dom_id,
        data,
        detector,
        livetime=None,
        fit_ang_dist=False,
        scale_mc_to_data=True,
        ad_fit_shape='pexp',
        fit_background=True,
        ctmin=-1.
):
    if isinstance(data, str):
        filename = data
        loaders = {
            '.h5': load_k40_coincidences_from_hdf5,
            '.root': load_k40_coincidences_from_rootfile
        }
        try:
            loader = loaders[os.path.splitext(filename)[1]]
        except KeyError:
            log.critical('File format not supported.')
            raise IOError
        else:
            data, livetime = loader(filename, dom_id)
    combs = np.array(list(combinations(range(31), 2)))
    angles = calculate_angles(detector, combs)
    cos_angles = np.cos(angles)
    angles = angles[cos_angles >= ctmin]
    data = data[cos_angles >= ctmin]
    combs = combs[cos_angles >= ctmin]
    try:
        fit_res = fit_delta_ts(data, livetime, fit_background=fit_background)
        rates, means, sigmas, popts, pcovs = fit_res
    except:
        return 0
    rate_errors = np.array([np.diag(pc)[2] for pc in pcovs])
    scale_factor = None
    if fit_ang_dist:
        fit_res = fit_angular_distribution(
            angles, rates, rate_errors, shape=ad_fit_shape
        )
        fitted_rates, exp_popts, exp_pcov = fit_res
    else:
        mc_fitted_rates = exponential_polinomial(np.cos(angles), *MC_ANG_DIST)
        if scale_mc_to_data:
            scale_factor = np.mean(rates[angles < 1.5]) /  \
                np.mean(mc_fitted_rates[angles < 1.5])
        else:
            scale_factor = 1.
        fitted_rates = mc_fitted_rates * scale_factor
        exp_popts = []
        exp_pcov = []
        print('Using angular distribution from Monte Carlo')
    if not fit_background:
        minimize_weights = calculate_weights(fitted_rates, data)
    else:
        minimize_weights = fitted_rates
    opt_t0s = minimize_t0s(means, minimize_weights, combs)
    opt_sigmas = minimize_sigmas(sigmas, minimize_weights, combs)
    opt_qes = minimize_qes(fitted_rates, rates, minimize_weights, combs)
    corrected_means = correct_means(means, opt_t0s.x, combs)
    corrected_rates = correct_rates(rates, opt_qes.x, combs)
    rms_means, rms_corrected_means = calculate_rms_means(
        means, corrected_means
    )
    rms_rates, rms_corrected_rates = calculate_rms_rates(
        rates, fitted_rates, corrected_rates
    )
    cos_angles = np.cos(angles)
    return_data = {
        'opt_t0s': opt_t0s,
        'opt_qes': opt_qes,
        'data': data,
        'means': means,
        'rates': rates,
        'fitted_rates': fitted_rates,
        'angles': angles,
        'corrected_means': corrected_means,
        'corrected_rates': corrected_rates,
        'rms_means': rms_means,
        'rms_corrected_means': rms_corrected_means,
        'rms_rates': rms_rates,
        'rms_corrected_rates': rms_corrected_rates,
        'gaussian_popts': popts,
        'livetime': livetime,
        'exp_popts': exp_popts,
        'exp_pcov': exp_pcov,
        'scale_factor': scale_factor,
        'opt_sigmas': opt_sigmas,
        'sigmas': sigmas,
        'combs': combs
    }
    return return_data