|
| 1 | + |
| 2 | +%matplotlib tk |
| 3 | + |
| 4 | +from pathlib import Path |
| 5 | +import numpy as np |
| 6 | +import matplotlib.pyplot as plt |
| 7 | + |
| 8 | +from astropy.io import fits |
| 9 | +from scipy.integrate import simpson |
| 10 | +from scipy.interpolate import interp1d |
| 11 | + |
| 12 | + |
| 13 | +def band_photspec(E, norm, Epeak, alpha, beta): |
| 14 | + E0 = Epeak / (2 + alpha) |
| 15 | + |
| 16 | + fE = np.zeros(E.size) |
| 17 | + |
| 18 | + mask = E < (alpha - beta) * E0 |
| 19 | + fE[mask] = norm * (E[mask] / 100)**alpha * np.exp(-(E[mask] / E0)) |
| 20 | + fE[~mask] = norm * ((alpha - beta) * E0 / 100)**(alpha - beta) * np.exp(beta - alpha) * (E[~mask]/100)**beta |
| 21 | + |
| 22 | + return fE |
| 23 | + |
| 24 | + |
| 25 | +# grbname |
| 26 | +grbname = "GRB180120A" |
| 27 | + |
| 28 | +# read data and response |
| 29 | +hist1 = np.loadtxt(f"/home/sujay/local/data/3ml-test/czti_pol_fit_verification/source_hist_{grbname:s}.txt", delimiter=',') |
| 30 | +hist2 = np.loadtxt("/home/sujay/local/data/3ml-test/czti_pol_fit_verification/source_hist_GRB180427A.txt", delimiter=',') |
| 31 | +hist3 = np.loadtxt("/home/sujay/local/data/3ml-test/czti_pol_fit_verification/source_hist_GRB190530A.txt", delimiter=',') |
| 32 | + |
| 33 | +rspHDU = fits.open(f"/home/sujay/local/data/3ml-test/czti_pol_fit_verification/CZTI_POLRSP_{grbname:s}.prsp") |
| 34 | + |
| 35 | +# pol and spectral params |
| 36 | +fit_paramHDU = fits.open(f"/home/sujay/local/data/3ml-test/czti_pol_fit_verification/AstroSat_CZTI_polarization_results_{grbname:s}.fits") |
| 37 | +norm = fit_paramHDU[1].data['Value'][0] |
| 38 | +alpha = fit_paramHDU[1].data['Value'][1] |
| 39 | +epeak = fit_paramHDU[1].data['Value'][2] |
| 40 | +beta = fit_paramHDU[1].data['Value'][3] |
| 41 | +t90 = 26.9 |
| 42 | +print(norm, alpha, beta, epeak) |
| 43 | + |
| 44 | +esim = ((rspHDU[1].data['ENERG_LO'] + rspHDU[1].data['ENERG_HI']) / 2) |
| 45 | +pasim = rspHDU[2].data['PA_IN'] |
| 46 | +pol_matrix = rspHDU[4].data.T |
| 47 | +unpol_matrix = rspHDU[5].data.T |
| 48 | + |
| 49 | +# convolve with GRB spectrum |
| 50 | +weights = band_photspec(esim, norm, epeak, alpha, beta) # ph/s/cm2/keV |
| 51 | +pol_hist_grb_scaled = simpson(pol_matrix * weights[:, None, None], esim, axis=0) # cnt/s |
| 52 | +unpol_hist_grb_scaled = simpson(unpol_matrix * weights[:, None], esim, axis=0) # cnt/s |
| 53 | +pa = fit_paramHDU[1].data['Value'][5] |
| 54 | +pf = fit_paramHDU[1].data['Value'][4]/100 |
| 55 | + |
| 56 | +pol_hist_interp_scaled = interp1d(pasim, pol_hist_grb_scaled, axis=0, kind='cubic', fill_value='extrapolate') |
| 57 | +pol_hist_grb_scaled_val = pol_hist_interp_scaled(pa) |
| 58 | + |
| 59 | +hist_model_scaled = pol_hist_grb_scaled_val * pf + (1 - pf)*unpol_hist_grb_scaled |
| 60 | + |
| 61 | +# read direct band sim histograms |
| 62 | +direct_hist_path = Path(f"/home/sujay/local/data/3ml-test/czti_pol_fit_verification/old_pipeline_mu_0_100/{grbname:s}/") |
| 63 | + |
| 64 | +unpol_hist_grb_direct = np.loadtxt(direct_hist_path.joinpath(f"unpol/hist_EventFileSimul{grbname[:-1]:s}_mass.txt.ver2.dat_100_600_thr_20.txt")) |
| 65 | +pol_hist_grb_direct = np.zeros((18, 8)) |
| 66 | + |
| 67 | +for i, fname in enumerate(sorted(direct_hist_path.glob("*.txt"))[:-1]): |
| 68 | + pol_hist_grb_direct[i, :] = np.loadtxt(fname) |
| 69 | + |
| 70 | + |
| 71 | +pol_hist_interp_direct = interp1d(pasim, pol_hist_grb_scaled, axis=0, kind='cubic', fill_value='extrapolate') |
| 72 | +pol_hist_grb_direct_val = pol_hist_interp_scaled(pa) |
| 73 | +hist_model_direct = pol_hist_grb_direct_val * pf + (1 - pf)*unpol_hist_grb_direct |
| 74 | + |
| 75 | +# plot scaled and unscaled |
| 76 | +scat_bins = np.arange(0, 360, 45) |
| 77 | +fig, ax = plt.subplots(1, 1, figsize=(8, 6)) |
| 78 | +ax.plot(scat_bins, unpol_hist_grb_direct/unpol_hist_grb_direct.mean(), ds="steps-mid", label="Direct") |
| 79 | +ax.plot(scat_bins, unpol_hist_grb_scaled/unpol_hist_grb_scaled.mean(), ds="steps-mid", label="Scaled") |
| 80 | +ax.legend() |
| 81 | +fig.suptitle("Unpolarized histogram comparison") |
| 82 | + |
| 83 | +fig, ax = plt.subplots(1, 1, figsize=(8, 6)) |
| 84 | +ax.errorbar(scat_bins, hist1[:, 1]/hist1[:, 1].mean(), yerr = hist1[:, 2]/hist1[:, 1].mean(), fmt='o', label=f"{grbname:s}, $\\theta = 15.89^\circ$") |
| 85 | +#ax.errorbar(scat_bins, hist2[:, 1]/hist2[:, 1].mean(), yerr = hist2[:, 2]/hist2[:, 1].mean(), fmt='o', label='GRB108427A, $\\theta = 40.81^\circ$') |
| 86 | +#ax.errorbar(scat_bins, hist3[:, 1]/hist3[:, 1].mean(), yerr = hist3[:, 2]/hist3[:, 1].mean(), fmt='o', label='GRB190530A, $\\theta = 154.5^\circ$') |
| 87 | +plt.legend() |
| 88 | +ax.plot(scat_bins, hist_model_scaled/hist_model_scaled.mean(), ds='steps-mid', label="Scaled") |
| 89 | +ax.plot(scat_bins, hist_model_direct/hist_model_direct.mean(), ds='steps-mid', label="Direct") |
| 90 | +ax.legend() |
| 91 | + |
| 92 | + |
| 93 | +fig, ax = plt.subplots(1, 1, figsize=(8, 6)) |
| 94 | +ax.errorbar(scat_bins, hist1[:, 1]/hist1[:, 1].mean(), yerr = hist1[:, 2]/hist1[:, 1].mean(), fmt='o', label=f"{grbname:s}, $\\theta = 15.89^\circ$") |
| 95 | +ax.errorbar(scat_bins, hist2[:, 1]/hist2[:, 1].mean(), yerr = hist2[:, 2]/hist2[:, 1].mean(), fmt='o', label='GRB108427A, $\\theta = 40.81^\circ$') |
| 96 | +ax.errorbar(scat_bins, hist3[:, 1]/hist3[:, 1].mean(), yerr = hist3[:, 2]/hist3[:, 1].mean(), fmt='o', label='GRB190530A, $\\theta = 154.5^\circ$') |
| 97 | +plt.legend() |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | + |
0 commit comments