예제 #1
0
def reco_energy_plot(config, out=False, batch=False):

    # Load median file information
    simBase = getSimBase(config)
    inFile = '%s_median.npy' % simBase
    d = np.load(inFile)
    d = d.item()
    xbins, ybins, ebins = d['xbins'], d['ybins'], d['ebins']

    # Calculate median energy value from bin
    energies = d['medians']
    energies = energies.T

    # Trim down the top of the grid
    ycts = energies.sum(axis=1)
    yidx = np.where(ycts==0)[0][0] + 2
    ybins = ybins[:yidx]
    energies = energies[:yidx]

    emin, emax = energies[energies!=0].min(), energies[energies!=0].max()
    emin, emax = 3.75, 8
    ebins = [emin] + getEbins() + [emax]

    fig = plt.figure(figsize=(8,6))
    ax = fig.add_subplot(111)
    X, Y = np.meshgrid(xbins, ybins)
    cmap = plt.cm.jet
    cmap = cmap_discretize(cmap, ebins)
    cmap.set_under('white')
    tPars = {'fontsize':16}

    p = ax.pcolor(X, Y, energies, cmap=cmap, vmin=emin, vmax=emax)
    cb = fig.colorbar(p, ax=ax, ticks=ebins)
    cb.ax.set_yticklabels(['%.2f' % ebin for ebin in ebins])
    cb.set_label(r'$\mathrm{log}_{10}(E/\mathrm{GeV})$',
            rotation=270, labelpad=20, **tPars)
    #ax.set_title('Median Energy vs Zenith and Nchannel')
    ax.set_xlabel(r'$\mathrm{cos}(\theta_\mathrm{reco})$', **tPars)
    ax.set_ylabel(r'$\mathrm{log}_{10}(N_\mathrm{channel})$', **tPars)
    ax.set_xlim(xbins.min(), xbins.max())
    ax.set_ylim(ybins.min(), ybins.max())
    if out != False:
        #outFile = '%s/%s_Median_Energy' % (outPrefix, config)
        plt.savefig(out, dpi=300, bbox_inches='tight')
    if not batch:
        plt.show()
예제 #2
0
def eres(config, out=False, batch=False, old=False):

    # Basic setup
    fig, ax = plt.subplots()
    lw = 2
    ms = 7*lw
    pltParams = {'fmt':'.', 'lw':lw, 'ms':ms}

    d = {}
    configs = ['IC59','IC79','IC86','IC86-II','IC86-III','IC86-IV']
    if config in configs:
        configs = [config]

    # Energy binning information
    eList = getEbins() + [100]
    eStrings = ['%s' % float(i) for i in eList]
    ePairs = [[eStrings[i], eStrings[i+1]] for i in range(len(eStrings)-1)]
    minE, maxE = np.asarray(eList[:-1]), np.asarray(eList[1:])
    if maxE[-1] == 100:
        maxE[-1] = maxE[-2] + 1
    emids = (minE + maxE) / 2.
    xL = emids - minE
    xR = maxE - emids
    xR[-1] = 0

    # Read in distribution information
    for cfg in configs:
        d[cfg] = {}
        for key in ['median','sigL','sigR']:
            d[cfg][key] = []
        for emin, emax in ePairs:
            median, sigL, sigR = readDist(cfg, emin, emax, old=old)
            d[cfg]['median'] += [median]
            d[cfg]['sigL']   += [sigL]
            d[cfg]['sigR']   += [sigR]

        if len(configs) > 1:
            pltParams['label'] = cfg
            ax.errorbar(emids, d[cfg]['median'],
                    yerr=[d[cfg]['sigL'], d[cfg]['sigR']], **pltParams)
            emids += .02

        else:
            #ax.errorbar(emids, d[cfg]['median'], xerr=[xL, xR], 
            #        yerr=[d[cfg]['sigL'], d[cfg]['sigR']], **pltParams)
            #ax.arrow(emids[-1], d[cfg]['median'][-1], 0.5, 0.0,
            #        fc='b', ec='b', head_width=.1, head_length=.2)
            ax.errorbar(emids, d[cfg]['median'], 
                    yerr=[d[cfg]['sigL'], d[cfg]['sigR']], **pltParams)

    # Create combined distribution (start unweighted)
    ave_median = np.average([d[key]['median'] for key in d.keys()], axis=0)
    ave_sigL = np.average([d[key]['sigL'] for key in d.keys()], axis=0)
    ave_sigR = np.average([d[key]['sigR'] for key in d.keys()], axis=0)

    if len(configs) > 1:
        ax.errorbar(emids, ave_median, yerr=[ave_sigL, ave_sigR],
                fmt='kx', label='Total')

    ax.set_xlim(3.5, 8)
    tPars = {'fontsize':16}
    #ax.set_xlabel(r'Reconstructed Energy ($log_{10}(E/\mathrm{GeV})$)',
    #        **tPars)
    ax.set_xlabel(r'Energy of Bin Center ($log_{10}(E/\mathrm{GeV})$)',
            **tPars)
    ax.set_ylabel(r'True Energy ($log_{10}(E/\mathrm{GeV})$)', **tPars)
    #ax.set_title('Energy Distributions for Cuts', fontsize=16)
    if len(configs) > 1:
        plt.legend(loc='lower right')

    if out != False:
        plt.savefig(out, dpi=300, bbox_inches='tight')
    if not batch:
        plt.show()
예제 #3
0
def spline(config, inFile, outFile, plot=False):

    # Load input median file
    d = np.load(inFile)
    d = d.item()
    xbins, ybins = d['xbins'], d['ybins']
    energies = d['medians']
    vars = d['var']
    vars[vars<1e-15] = np.inf   # Variances sufficiently close to 0 aren't real
    w = 1/vars
    emin, emax = energies[energies!=0].min(), energies.max()
    ebins = [emin] + getEbins() + [emax]

    axes, knots = [],[]
    binList = [xbins, ybins]
    nknots = 30
    step_scale = 2/5.
    for bins in binList:
        mids = (bins[1:]+bins[:-1])/2.
        axes += [mids]
        step = (bins.max() - bins.min()) * step_scale
        knots += [np.linspace(bins.min()-step, bins.max()+step, nknots)]

    tab = glam.fit(energies, w, axes, knots, order=(4), \
            penalties={2:1e-3})

    if plot:

        # Look at spline fit with finer binning
        fitaxes = [[],[]]
        fitaxes[0] = np.linspace(xbins.min(),xbins.max(),len(xbins)*3)
        fitaxes[1] = np.linspace(ybins.min(),ybins.max(),len(ybins)*3)

        fit = glam.grideval(tab, fitaxes)
        #err_fit = 10**glam.grideval(err_tab,axes)

        fig = plt.figure(figsize=(17,6))
        mpl.rc("font", family="serif")
        X, Y = np.meshgrid(axes[0], axes[1])
        fitX, fitY = np.meshgrid(fitaxes[0], fitaxes[1])

        # Setup custom colormap
        cmap = plt.cm.jet
        cmap = cmap_discretize(cmap, ebins)
        cmap.set_under('white')

        ax = fig.add_subplot(121)
        p = ax.pcolor(fitX, fitY, fit.T, cmap=cmap, vmin=emin, vmax=emax)
        cb = fig.colorbar(p, ax=ax)
        ax.set_title('Median Energy')
        ax.set_xlabel('cos(zenith)')
        ax.set_ylabel('log10(Nchannel)')
        ax.set_xlim(xbins.min(), xbins.max())
        ax.set_ylim(ybins.min(), ybins.max())

        ax = fig.add_subplot(122)
        p = ax.pcolor(X, Y, energies.T, cmap=cmap, vmin=emin, vmax=emax)
        ax.set_title('Median Energy')
        ax.set_xlabel('cos(zenith)')
        ax.set_ylabel('log10(Nchannel)')
        ax.set_xlim(xbins.min(), xbins.max())
        ax.set_ylim(ybins.min(), ybins.max())
        plt.show()

    if outFile:
        if os.path.exists(outFile):
            os.remove(outFile)
        splinefitstable.write(tab, outFile)