def zfix(s, cut='llh', nbins=100, thetaMax=40., minE=5.0, plot=False, out=False): from icecube.photospline import spglam as glam thetaMax *= degree zbins = np.linspace(1, np.cos(thetaMax), nbins+1)[::-1] ebins = getEbins() t = np.log10(s['MC_energy']) r = np.log10(s['ML_energy']) z = np.pi - s['zenith'] # Calculate cut values c0 = np.logical_not(np.isnan(r)) ecut = r >= minE c0 *= ecut if cut != None: c0 *= s['cuts'][cut] # Store median and standard deviation info x1 = np.cos(z)[c0] if x1.min() > zbins.min(): zbins = zbins[zbins >= x1.min()] y = (r - t)[c0] medians, sigL, sigR, vars = getMedian(x1, y, zbins) w = 1/vars nknots = 30 step_scale = 2/5. step = (zbins.max() - zbins.min()) * step_scale mids = (zbins[1:] + zbins[:-1]) / 2. axes = [mids] knots = [np.linspace(zbins.min()-step, zbins.max()+step, nknots)] tab = glam.fit(medians, w, axes, knots, order=(4), penalties={2:1e-4}) if plot: fig, ax = plt.subplots() #ax.set_title('Energy Resolution vs Reconstructed Zenith', fontsize=18) ax.set_xlabel(r'$\cos(\mathrm{\theta_{reco}})$', fontsize=16) ax.set_ylabel(r'$\log_{10}(E_{\mathrm{LLH}}/E_{\mathrm{true}})$', fontsize=16) lw = 2 ms = 7*lw pltParams = dict(fmt='.', lw=lw, ms=ms) # Energy resolution vs zenith zbins = np.linspace(1, np.cos(thetaMax), 21)[::-1] x = getMids(zbins) medians, sigL, sigR, vars = getMedian(x1, y, zbins) ax.errorbar(x, medians, yerr=(sigL,sigR), **pltParams) # Spline fit fitx = np.linspace(x.min(), x.max(), len(x)*3) fit = glam.grideval(tab, [fitx]) ax.plot(fitx, fit) ax.set_xlim(0.8,1) if out: plt.savefig(out) plt.show() fit = glam.grideval(tab, [np.cos(z)]) return r - fit
def makeTable(bintype='logdist', plot=False): # Starting parameters my.setupShowerLLH(verbose=False) s = load_sim(bintype=bintype) outFile = '%s/IT73_sim/Zfix_%s.fits' % (my.llh_data, bintype) nbins = 100 thetaMax = 40. minE = 4.0 thetaMax *= np.pi / 180. zbins = np.linspace(1, np.cos(thetaMax), nbins+1)[::-1] ebins = getEbins() t = np.log10(s['MC_energy']) r = np.log10(s['ML_energy']) z = np.pi - s['zenith'] # Calculate cut values c0 = s['cuts']['llh'] ecut = r >= minE c0 *= ecut # Store median and standard deviation info x1 = np.cos(z)[c0] if x1.min() > zbins.min(): zbins = zbins[zbins >= x1.min()] y = (r - t)[c0] medians, sigL, sigR, vars = getMedian(x1, y, zbins) w = 1/vars nknots = 30 step_scale = 2/5. step = (zbins.max() - zbins.min()) * step_scale mids = (zbins[1:] + zbins[:-1]) / 2. axes = [mids] knots = [np.linspace(zbins.min()-step, zbins.max()+step, nknots)] tab = glam.fit(medians, w, axes, knots, order=(4), penalties={2:1e-4}) if os.path.exists(outFile): os.remove(outFile) splinefitstable.write(tab, outFile) if plot: fig, ax = plt.subplots() ax.set_title('Energy Resolution vs Reconstructed Zenith', fontsize=18) ax.set_xlabel('Cos(zenith)', fontsize=16) ax.set_ylabel('Ereco - Etrue (median)', fontsize=16) lw = 2 ms = 7*lw pltParams = dict(fmt='.', lw=lw, ms=ms) # Energy resolution vs zenith x = getMids(zbins) ax.errorbar(x, medians, yerr=(sigL,sigR), **pltParams) # Spline fit fitx = np.linspace(x.min(), x.max(), len(x)*3) fit = glam.grideval(tab, [fitx]) ax.plot(fitx, fit) plt.show()
def core_res(s, cut=None, nbins=40, minE=4, title=True, zcorrect=False, out=False): # Setup plot fig, ax = plt.subplots() if title: ax.set_title('Core Resolution vs True Energy', fontsize=18) ax.set_xlabel(r'$\log_{10}(E/\mathrm{GeV})$', fontsize=16) ax.set_ylabel(r'$\vec{x}_{\mathrm{LLH}} - \vec{x}_{\mathrm{true}} [m]$', fontsize=16) lw = 2 ms = 7*lw pltParams = dict(fmt='.', lw=lw, ms=ms) # Group into larger bins in energy Ebins = getEbins() ebins = np.linspace(Ebins.min(), Ebins.max(), nbins+1) if ebins[-1] != Ebins[-1]: ebins.append(Ebins[-1]) # Plot Ereco histograms for each energy bin in Etrue r = np.log10(s['ML_energy']) if zcorrect: r = zfix(s) c0 = np.logical_not(np.isnan(s['ML_energy'])) ecut = (r >= minE) c0 *= ecut if cut != None: c0 *= s['cuts'][cut] t = np.log10(s['MC_energy'])[c0] tx, ty = s['MC_x'][c0], s['MC_y'][c0] rx, ry = s['ML_x'][c0], s['ML_y'][c0] # Store median and standard deviation info x = getMids(ebins) y = np.sqrt((rx-tx)**2 + (ry-ty)**2) medians, sigL, sigR, vars = getMedian(t, y, ebins) ax.errorbar(x, medians, yerr=(sigL,sigR), **pltParams) if out: plt.savefig(out) plt.show()
def eres(s, cut=None, xaxis='energy', nbins=20, minE=4.0, rt='t', thetaMax=55., zcorrect=False, title=True, out=False, ax=None): titleDict = {'zenith':'Zenith Angle', 'energy':'Energy', 'core':'Core Position'} xDict = {'zenith':r'$\cos(\mathrm{\theta_{true}})$', 'energy':r'$\log_{10}(E/\mathrm{GeV})$', 'core':'Core Position (m)'} rtDict = {'r':'Reconstructed', 't':'True'} # Setup plot if ax == None: fig, ax = plt.subplots() if title: ax.set_title('Energy Resolution vs %s %s' % \ (rtDict[rt], titleDict[xaxis]), fontsize=18) ax.set_xlabel(xDict[xaxis], fontsize=16) ax.set_ylabel(r'$\log_{10}(E_{\mathrm{LLH}}/E_{\mathrm{true}})$', fontsize=16) lw = 2 ms = 7*lw pltParams = dict(fmt='.', lw=lw, ms=ms) if xaxis == 'energy': Ebins = getEbins() bins = np.linspace(Ebins.min(), Ebins.max(), nbins+1) if xaxis == 'zenith': bins = np.linspace(1, np.cos(thetaMax * degree), nbins+1)[::-1] if xaxis == 'core': bins = np.linspace(0, 1000, nbins+1) t = np.log10(s['MC_energy']) r = np.log10(s['ML_energy']) if zcorrect: r = zfix(s) if rt == 't': e = t z = s['MC_zenith'] cx, cy = s['MC_x'], s['MC_y'] if rt == 'r': e = r z = np.pi - s['zenith'] cx, cy = s['ML_x'], s['ML_y'] # Calculate cut values c0 = np.logical_not(np.isnan(r)) #ecut = (t >= minE) if rt=='t' else (r >= minE) ecut = (r >= minE) c0 *= ecut if cut != None: c0 *= s['cuts'][cut] # Store median and standard deviation info x = getMids(bins) x1 = {'energy':e, 'zenith':np.cos(z), 'core':np.sqrt(cx**2+cy**2)} x1 = x1[xaxis] y = r - t medians, sigL, sigR, vars = getMedian(x1[c0], y[c0], bins) #if xaxis == 'zenith': # x = (np.arccos(x) / degree)[::-1] # medians, sigL, sigR = medians[::-1], sigL[::-1], sigR[::-1] ax.errorbar(x, medians, yerr=(sigL,sigR), **pltParams) if out: plt.savefig(out) #if ax == None: plt.show()