def plot_ratio(year):
    #The following currently devoted to making energy and angular error plots for a year of data.
    # init likelihood class
    mc = np.load(filename_pickle + "IC{}_MC.npy".format(year))
    dpsi = [
        astro.angular_distance(mc['trueRa'][i], mc['trueDec'][i], mc['ra'][i],
                               np.arcsin(mc['sinDec'][i]))
        for i in range(len(mc))
    ]

    mc_corr = np.load(filename_pickle + "IC{}_corrected_MC.npy".format(year))
    dpsi_corr = [
        astro.angular_distance(mc_corr['trueRa'][i],
                               mc_corr['trueDec'][i], mc_corr['ra'][i],
                               np.arcsin(mc_corr['sinDec'][i]))
        for i in range(len(mc_corr))
    ]

    colors = ['g']  #['b','g','y','r']
    colors_corr = ['r']  #['b','g','y','r']
    gamma = np.array([2.])  #np.linspace(1., 2.7, 4)

    fig_ratio, (ax1, ax2) = plt.subplots(ncols=2, figsize=(10, 5))
    dec = np.arcsin(mc["sinDec"])
    angdist = np.degrees(dpsi)
    dec_corr = np.arcsin(mc_corr["sinDec"])
    angdist_corr = np.degrees(dpsi_corr)

    ax1.hist([np.log10(np.degrees(mc["sigma"]) / (angdist)) for i in gamma],
             label=[
                 r"$\Delta \psi / \sigma$ - $\gamma={0:.1f}$".format(g)
                 for g in gamma
             ],
             linestyle='solid',
             weights=[mc["ow"] * mc["trueE"]**(-g) for g in gamma],
             color=[colors[g] for g in range(len(gamma))],
             histtype="step",
             bins=100,
             normed=True)

    medians = [
        misc.weighted_median(np.log10(np.degrees(mc["sigma"]) / (angdist)),
                             mc["ow"] * mc["trueE"]**(-g)) for g in gamma
    ]

    for g in range(len(gamma)):
        ax1.axvline(medians[g], color=colors[g], linestyle='dotted', alpha=0.3)

    ax1.hist(
        [
            np.log10(np.degrees(mc_corr["sigma"]) / (angdist_corr))
            for i in gamma
        ],
        label=[
            r"'Corrected' $\Delta \psi / \sigma$ - $\gamma={0:.1f}$".format(g)
            for g in gamma
        ],
        linestyle='dotted',
        weights=[mc_corr["ow"] * mc_corr["trueE"]**(-g) for g in gamma],
        color=[colors_corr[g] for g in range(len(gamma))],
        histtype="step",
        bins=100,
        normed=True)

    medians = [
        misc.weighted_median(
            np.log10(np.degrees(mc_corr["sigma"]) / (angdist_corr)),
            mc["ow"] * mc["trueE"]**(-g)) for g in gamma
    ]

    for g in range(len(gamma)):
        ax1.axvline(medians[g], color=colors_corr[g], marker='+', alpha=0.3)

    ax1.set_title(r"Reco MC $\Delta \psi / \sigma$ Check - IC{}".format(
        str(year)))
    ax1.set_xlabel(r"log$\Delta \psi / \sigma_{ang}$")
    ax1.set_ylabel("Relative Abundance")
    ax1.set_ylim(0, 1.5)
    ax1.set_xlim(-2.5, 2.5)
    #ax1.axvline(x=0, color='k')
    ax1.axvline(x=np.log10(1. / 1.1774), color='k')
    ax2.set_xlim(-5, 5)

    ax2.hist([(np.degrees(mc["sigma"])) / (angdist) for i in gamma],
             label=[
                 r"$\Delta \psi / \sigma$ - $\gamma={0:.1f}$".format(g)
                 for g in gamma
             ],
             linestyle='solid',
             weights=[mc["ow"] * mc["trueE"]**(-g) for g in gamma],
             color=[colors[g] for g in range(len(gamma))],
             histtype="step",
             bins=1000,
             range=(0, 5),
             normed=True)

    ax2.hist(
        [(np.degrees(mc_corr["sigma"])) / (angdist_corr) for i in gamma],
        label=[
            r"'Corrected' $\Delta \psi / \sigma$ - $\gamma={0:.1f}$".format(g)
            for g in gamma
        ],
        linestyle='dotted',
        weights=[mc_corr["ow"] * mc_corr["trueE"]**(-g) for g in gamma],
        color=[colors_corr[g] for g in range(len(gamma))],
        histtype="step",
        bins=1000,
        range=(0, 5),
        normed=True)

    ax2.legend(loc="upper right")
    ax2.set_xlim(0, 5)
    ax2.set_ylim(0, 3.5)
    ax2.set_xlabel(r"$\Delta \psi / \sigma_{ang}$")
    fig_ratio.savefig(filename_plots +
                      'dpsi_sigma_ratio_IC{}.pdf'.format(str(year)))
def plotpull2d(year):
    #The following currently devoted to making energy and angular error plots for a year of data.
    # init likelihood class
    mc = np.load(filename_pickle + "IC{}_MC.npy".format(year))
    dpsi = [
        astro.angular_distance(mc['trueRa'][i], mc['trueDec'][i], mc['ra'][i],
                               np.arcsin(mc['sinDec'][i]))
        for i in range(len(mc))
    ]

    mc_corr = np.load(filename_pickle + "IC{}_corrected_MC.npy".format(year))
    dpsi_corr = [
        astro.angular_distance(mc_corr['trueRa'][i],
                               mc_corr['trueDec'][i], mc_corr['ra'][i],
                               np.arcsin(mc_corr['sinDec'][i]))
        for i in range(len(mc_corr))
    ]

    colors = ['g']  #['b','g','y','r']
    colors_corr = ['r']  #['b','g','y','r']
    gamma = np.array([2.])  #np.linspace(1., 2.7, 4)

    fig_pull, (ax) = plt.subplots(ncols=1, figsize=(10, 5))
    dec = np.arcsin(mc["sinDec"])
    angdist = np.degrees(dpsi)
    dec_corr = np.arcsin(mc_corr["sinDec"])
    angdist_corr = np.degrees(dpsi_corr)

    #medians = [misc.weighted_median(np.log10(np.degrees(mc["sigma"])/(angdist)),mc["ow"] * mc["trueE"]**(-g)) for g in gamma]

    #for g in range(len(gamma)):
    #  ax1.axvline(medians[g], color=colors[g], linestyle = 'dotted', alpha = 0.3)

    #ax1.hist([np.log10(np.degrees(mc_corr["sigma"])/(angdist_corr)) for i in gamma], label = [r"'Corrected' $\Delta \psi / \sigma$ - $\gamma={0:.1f}$".format(g) for g in gamma], linestyle = 'dotted',
    #         weights=[mc_corr["ow"] * mc_corr["trueE"]**(-g) for g in gamma], color=[colors_corr[g] for g in range(len(gamma))],
    #         histtype="step", bins=100, normed=True)

    #medians = [misc.weighted_median(np.log10(np.degrees(mc_corr["sigma"])/(angdist_corr)),mc["ow"] * mc["trueE"]**(-g)) for g in gamma]

    #for g in range(len(gamma)):
    #  ax1.axvline(medians[g], color=colors_corr[g], marker='+',alpha = 0.3)

    #ax1.set_title(r"Reco MC $\Delta \psi / \sigma$ Check - IC{}".format(str(year)))
    #ax1.set_xlabel(r"log$\Delta \psi / \sigma_{ang}$")
    #ax1.set_ylabel("Relative Abundance")
    #ax1.set_ylim(0,1.5)
    #ax1.set_xlim(-2.5,2.5)
    #ax1.axvline(x=0, color='k')
    #ax1.axvline(x=np.log10(1./1.1774), color='k')
    #set gamma
    g = 2.0
    pull = np.log10(np.degrees(mc["sigma"]) / (angdist))
    #Need to calc the median of pull for each energyrange:
    pullrange = (-4, 4)
    erange = (3, 8)
    ebins = 50
    pullbins = 200
    eedges = np.linspace(erange[0], erange[1], ebins + 1)
    ezones = zip(eedges[:-1], eedges[1:])
    emid = [np.median(ezone) for ezone in ezones]
    emasks = [
        (np.log10(mc["trueE"]) > ezone[0]) & (np.log10(mc["trueE"]) < ezone[1])
        for ezone in ezones
    ]

    medians = [
        misc.weighted_median(pull[emask],
                             mc["ow"][emask] * mc["trueE"][emask]**(-g))
        for emask in emasks
    ]
    #pull_corr = [(np.degrees(mc_corr["sigma"]))/(angdist_corr) for i in gamma]
    hpull = histlite.hist((np.log10(mc['trueE']), pull),
                          weights=mc["ow"] * mc["trueE"]**(-g),
                          bins=(ebins, pullbins),
                          range=(erange, pullrange),
                          log=(False, False))

    histlite.plot2d(
        ax,
        hpull.normalize([-1]),
        label=[r"$\Delta \psi / \sigma$ - $\gamma={0:.1f}$".format(g)],
        cbar=True,
        cmap='jet',
        color=colors[0])

    ax.scatter(emid, medians, color='white')
    ax.axhline(y=np.log10(1.1774), color='white', linestyle='dashed')

    #ax.hist([(np.degrees(mc_corr["sigma"]))/(angdist_corr) for i in gamma], label = [r"'Corrected' $\Delta \psi / \sigma$ - $\gamma={0:.1f}$".format(g) for g in gamma], linestyle = 'dotted',
    #         weights=[mc_corr["ow"] * mc_corr["trueE"]**(-g) for g in gamma], color=[colors_corr[g] for g in range(len(gamma))],
    #         histtype="step", bins=1000, range = (0,5), normed=True)
    ax.set_title("Pull for IC{}".format(str(year)))
    ax.legend(loc="upper right")
    ax.set_xlim(3, 8)
    ax.set_xlabel("log(trueE[GeV])")
    ax.set_ylim(-2, 2)
    ax.set_ylabel(r"log($\Delta \psi / \sigma_{ang}$)")
    fig_pull.savefig(filename_plots + 'opp_pull_IC{}.pdf'.format(str(year)))
def plot_ratio(year):
    #The following currently devoted to making energy and angular error plots for a year of data.
    # init likelihood class
    if year == 40:
        llh = data_multi.init40(energy=True)
        mc = cache.load(filename_pickle + "IC40/mc.pickle")
        extra = cache.load(filename_pickle + "IC40/dpsi.pickle")
    if year == 59:
        llh = data_multi.init59(energy=True)
        mc = cache.load(filename_pickle + "IC59/mc.pickle")
        extra = cache.load(filename_pickle + "IC59/dpsi.pickle")
    if year == 79:
        llh = data_multi.init79(energy=True)
        mc = cache.load(filename_pickle + "IC79/mc.pickle")
        extra = cache.load(filename_pickle + "IC79/dpsi.pickle")
    elif year == 86:
        llh = data_multi.init86I(energy=True)
        mc = cache.load(filename_pickle + "IC86I/mc.pickle")
        extra = cache.load(filename_pickle + "IC86I/dpsi.pickle")
    dpsi = extra['dpsi']

    print(llh)

    # datatest
    #Currently don't need to remake these plots. but DONT DELETE

    colors = ['b', 'g', 'y', 'r']
    gamma = np.linspace(1., 2.7, 4)

    # fig_energy, (ax1, ax2) = plt.subplots(ncols=2)
    # ax1.hist([llh.exp["logE"]] + [mc["logE"] for i in gamma],
    #          weights=[np.ones(len(llh.exp))]
    #                   + [mc["ow"] * mc["trueE"]**(-g) for g in gamma],
    #          label=["Pseudo-Data"] + [r"$\gamma={0:.1f}$".format(g) for g in gamma], color= ['k'] + [colors[g] for g in range(len(gamma))],
    #          histtype="step", bins=100, log=True, normed=True, cumulative=-1)
    # ax1.legend(loc="best")
    # ax1.set_title("Reconstructed Energy - IC{}".format(str(year)))
    # ax1.set_xlabel("logE")
    # ax1.set_ylabel("Relative Abundance")
    # ax1.hist([llh.exp["logE"]] + [mc["logE"] for i in gamma],
    #          weights=[np.ones(len(llh.exp))]
    #                   + [mc["ow"] * mc["trueE"]**(-g) for g in gamma],
    #          label=["Data"] + [r"$\gamma={0:.1f}$".format(g) for g in gamma], color= ['k'] + [colors[g] for g in range(len(gamma))],
    #          histtype="step", bins=100, log=True, normed=True, cumulative=-1)
    # ax2.set_title("Zoomed In")
    # ax2.set_xlabel("logE")
    # ax2.set_xlim(4,10)
    # ax2.set_ylim(1e-5,1)
    # ax2.hist([llh.exp["logE"]] + [mc["logE"] for i in gamma],
    #          weights=[np.ones(len(llh.exp))]
    #                   + [mc["ow"] * mc["trueE"]**(-g) for g in gamma],
    #          label=["Pseudo-Data"] + [r"$\gamma={0:.1f}$".format(g) for g in gamma], color= ['k'] + [colors[g] for g in range(len(gamma))],
    #          histtype="step", bins=100, log=True, normed=True, cumulative=-1)
    # fig_energy.savefig(filename_plots + 'energy_hists_IC{}.pdf'.format(str(year)))

    fig_ratio, (ax1, ax2) = plt.subplots(ncols=2, figsize=(10, 5))
    dec = np.arcsin(mc["sinDec"])
    angdist = np.degrees(dpsi)

    ax1.hist([np.log10(np.degrees(mc["sigma"]) / (angdist)) for i in gamma],
             label=[
                 r"$\Delta \psi / \sigma$ - $\gamma={0:.1f}$".format(g)
                 for g in gamma
             ],
             linestyle='solid',
             weights=[mc["ow"] * mc["trueE"]**(-g) for g in gamma],
             color=[colors[g] for g in range(len(gamma))],
             histtype="step",
             bins=100,
             normed=True)

    medians = [
        misc.weighted_median(np.log10(np.degrees(mc["sigma"]) / (angdist)),
                             mc["ow"] * mc["trueE"]**(-g)) for g in gamma
    ]

    for g in range(len(gamma)):
        ax1.axvline(medians[g], color=colors[g], alpha=0.3)

    ax1.set_title(r"Reco MC $\Delta \psi / \sigma$ Check - IC{}".format(
        str(year)))
    ax1.set_xlabel(r"log$\Delta \psi / \sigma_{ang}$")
    ax1.set_ylabel("Relative Abundance")
    ax1.set_ylim(0, 1.5)
    ax1.set_xlim(-2.5, 2.5)
    ax1.axvline(x=0, color='k')
    ax1.axvline(x=np.log10(1.1774), color='k')
    ax2.set_xlim(-5, 5)

    ax2.hist([(np.degrees(mc["sigma"])) / (angdist) for i in gamma],
             label=[
                 r"$\Delta \psi / \sigma$ - $\gamma={0:.1f}$".format(g)
                 for g in gamma
             ],
             linestyle='solid',
             weights=[mc["ow"] * mc["trueE"]**(-g) for g in gamma],
             color=[colors[g] for g in range(len(gamma))],
             histtype="step",
             bins=1000,
             range=(0, 5),
             normed=True)

    ax2.legend(loc="upper right")
    ax2.set_xlim(0, 5)
    ax2.set_ylim(0, 3.5)
    ax2.set_xlabel(r"$\Delta \psi / \sigma_{ang}$")
    fig_ratio.savefig(filename_plots +
                      'dpsi_sigma_ratio_IC{}.pdf'.format(str(year)))