def test_equa_from_dir(self): random.seed(0) t1 = dataclasses.I3Time() t1.set_utc_cal_date(2000, 1, 1, 0, 0, 0, 0.0) mjd1 = t1.mod_julian_day_double t1.set_utc_cal_date(2030, 1, 1, 0, 0, 0, 0.0) mjd2 = t1.mod_julian_day_double for n in range(1000): t1.set_mod_julian_time_double(random.uniform(mjd1, mjd2)) d = dataclasses.I3Direction( random.uniform(0, math.pi), random.uniform(0, 2 * math.pi), ) eq = astro.I3GetEquatorialFromDirection(d, t1) dprime = astro.I3GetDirectionFromEquatorial(eq, t1) self.assert_almost_equal(d.zenith, dprime.zenith, 5e-6) self.assert_almost_equal(d.azimuth * math.sin(d.zenith), dprime.azimuth * math.sin(d.zenith), 6e-6) self.assert_less( astro.angular_distance(d.azimuth, math.pi / 2 - d.zenith, dprime.azimuth, math.pi / 2 - dprime.zenith), 6e-6)
def test_dir_from_equa(self): random.seed(0) t1 = dataclasses.I3Time() t1.set_utc_cal_date(2000, 1, 1, 0, 0, 0, 0.0) mjd1 = t1.mod_julian_day_double t1.set_utc_cal_date(2030, 1, 1, 0, 0, 0, 0.0) mjd2 = t1.mod_julian_day_double for n in range(1000): t1.set_mod_julian_time_double(random.uniform(mjd1, mjd2)) eq = astro.I3Equatorial(random.uniform(0, 2 * math.pi), random.uniform(-math.pi / 2, math.pi / 2)) d = astro.I3GetDirectionFromEquatorial(eq, t1) eqprime = astro.I3GetEquatorialFromDirection(d, t1) self.assert_almost_equal(eq.ra * math.cos(eq.dec), eqprime.ra * math.cos(eqprime.dec), 4e-6) self.assert_almost_equal(eq.dec, eqprime.dec, 4e-6) self.assert_less( astro.angular_distance(eq.ra, eq.dec, eqprime.ra, eqprime.dec), 5e-6)
def oversample(tmp_weight, tmp_nu_type, tmp_energy_true, tmp_energy_reco, tmp_zenith_true, tmp_zenith_reco, tmp_azimuth_true, tmp_azimuth_reco, nOversampling): stime = time.mktime(time.strptime("1/1/2022/00/00/00", '%m/%d/%Y/%H/%M/%S')) etime = time.mktime(time.strptime("1/1/2023/00/00/00", '%m/%d/%Y/%H/%M/%S')) oversampled_psi_true = np.zeros((nOversampling,len(tmp_weight))) oversampled_psi_reco = np.zeros((nOversampling,len(tmp_weight))) oversampled_RA_reco = np.zeros((nOversampling,len(tmp_weight))) oversampled_Dec_reco = np.zeros((nOversampling,len(tmp_weight))) for i in range(nOversampling): eventTime = stime + random.random() * (etime - stime) eventTime = apTime(eventTime,format='unix').mjd RA_true, DEC_true = astro.dir_to_equa(tmp_zenith_true,tmp_azimuth_true,eventTime) RA_reco, DEC_reco = astro.dir_to_equa(tmp_zenith_reco,tmp_azimuth_reco,eventTime) oversampled_psi_true[i] = astro.angular_distance(RA_true,DEC_true,GC_Pos[0],GC_Pos[1]) oversampled_psi_reco[i] = astro.angular_distance(RA_reco,DEC_reco,GC_Pos[0],GC_Pos[1]) oversampled_RA_reco[i] = RA_reco oversampled_Dec_reco[i] = DEC_reco # append n versions oversampled_weight = np.tile(tmp_weight/nOversampling,nOversampling) oversampled_nu_type = np.tile(tmp_nu_type,nOversampling) oversampled_energy_true = np.tile(tmp_energy_true,nOversampling) oversampled_energy_reco = np.tile(tmp_energy_reco,nOversampling) return oversampled_weight, oversampled_nu_type, oversampled_energy_true, oversampled_energy_reco, oversampled_psi_true.flatten(), oversampled_psi_reco.flatten(), oversampled_RA_reco.flatten() , oversampled_Dec_reco.flatten()
def test_gal_from_sg(self): random.seed(0) for n in range(10000): sg = astro.I3SuperGalactic(random.uniform(0,2*math.pi), random.uniform(-math.pi/2,math.pi/2), ) gal = astro.I3GetGalacticFromSuperGalactic(sg) sgprime = astro.I3GetSuperGalacticFromGalactic(gal) self.assert_almost_equal(math.cos(sg.b)*sg.l,math.cos(sgprime.b)*sgprime.l,1e-9) self.assert_almost_equal(sg.b,sgprime.b,1e-10) self.assert_less(astro.angular_distance(sg.l,sg.b,sgprime.l,sgprime.b),1e-10)
def test_sg_from_equa(self): random.seed(0) for n in range(10000): eq = astro.I3Equatorial(random.uniform(0,2* math.pi), random.uniform(-math.pi/2,math.pi/2), ) sg = astro.I3GetSuperGalacticFromEquatorial(eq) eqprime = astro.I3GetEquatorialFromSuperGalactic(sg) self.assert_almost_equal(math.cos(eq.dec)*eq.ra ,math.cos(eqprime.dec)*eqprime.ra,1e-9) self.assert_almost_equal(eq.dec,eqprime.dec,1e-10) self.assert_less(astro.angular_distance(eq.ra,eq.dec,eqprime.ra,eqprime.dec),1e-10)
def test_equa_from_gal(self): random.seed(0) for n in range(10000): gal = astro.I3Galactic(random.uniform(0,2*math.pi), random.uniform(-math.pi/2,math.pi/2), ) eq = astro.I3GetEquatorialFromGalactic(gal) galprime = astro.I3GetGalacticFromEquatorial(eq) self.assert_almost_equal(math.cos(gal.b)*gal.l,math.cos(galprime.b)*galprime.l,1e-9) self.assert_almost_equal(gal.b,galprime.b,1e-10) self.assert_less(astro.angular_distance(gal.l,gal.b,galprime.l,galprime.b),1e-10)
def plot_error(year): #The following currently devoted to making energy and angular error plots for a year of data. # init mc 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_angular_error, (ax1, ax2) = plt.subplots(ncols=2, figsize=(10, 5)) dec = np.arcsin(mc["sinDec"]) dec_corr = np.arcsin(mc_corr["sinDec"]) angdist = np.degrees(dpsi) angdist_corr = np.degrees(dpsi_corr) ax1.hist([np.log10(np.degrees(mc["sigma"])) for i in gamma], label=[r"$\sigma$ - $\gamma={0:.1f}$".format(g) for g in gamma], linestyle='dashed', 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) ax1.hist( [np.log10(angdist) for i in gamma], label=[r"$\Delta \psi$ - $\gamma={0:.1f}$".format(g) for g in gamma], weights=[mc["ow"] * mc["trueE"]**(-g) for g in gamma], linestyle='solid', color=[colors[g] for g in range(len(gamma))], histtype="step", bins=100, normed=True) ax1.hist([np.log10(np.degrees(mc_corr["sigma"])) for i in gamma], label=[r"$\sigma$ - $\gamma={0:.1f}$".format(g) for g in gamma], linestyle='dashdot', 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) ax1.hist([np.log10(angdist_corr) for i in gamma], label=[ r"'Corrected' $\Delta \psi$ - $\gamma={0:.1f}$".format(g) for g in gamma ], weights=[mc_corr["ow"] * mc_corr["trueE"]**(-g) for g in gamma], linestyle='dotted', color=[colors_corr[g] for g in range(len(gamma))], histtype="step", bins=100, normed=True) ax1.set_title("Reco MC Angular Error Check - IC{}".format(str(year))) ax1.set_xlabel(r"log$\sigma_{ang}$ (degrees)") ax1.set_ylabel("Relative Abundance") ax1.set_ylim(0, 1.5) ax2.hist([(np.degrees(mc["sigma"])) for i in gamma], label=[r"$\sigma$ - $\gamma={0:.1f}$".format(g) for g in gamma], linestyle='dashed', weights=[mc["ow"] * mc["trueE"]**(-g) for g in gamma], color=[colors[g] for g in range(len(gamma))], histtype="step", bins=1000, normed=True) ax2.hist( [(angdist) for i in gamma], label=[r"$\Delta \psi$ - $\gamma={0:.1f}$".format(g) for g in gamma], weights=[mc["ow"] * mc["trueE"]**(-g) for g in gamma], linestyle='solid', color=[colors[g] for g in range(len(gamma))], histtype="step", bins=1000, normed=True) ax2.hist([(np.degrees(mc_corr["sigma"])) for i in gamma], label=[ r"'corrected' $\sigma$ - $\gamma={0:.1f}$".format(g) for g in gamma ], linestyle='dashdot', 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, normed=True) ax2.hist( [(angdist_corr) for i in gamma], label=[r"$\Delta \psi$ - $\gamma={0:.1f}$".format(g) for g in gamma], weights=[mc_corr["ow"] * mc_corr["trueE"]**(-g) for g in gamma], linestyle='dotted', color=[colors_corr[g] for g in range(len(gamma))], histtype="step", bins=1000, normed=True) ax2.legend(loc="upper right") ax2.set_xlim(0, 5) ax2.set_ylim(0, 3.5) ax2.set_xlabel(r"$\sigma_{ang}$ (degrees)") fig_angular_error.savefig(filename_plots + 'angular_error_hists_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 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)))