forked from tudo-astroparticlephysics/LST_mono_analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
theta2_wobble.py
276 lines (234 loc) · 8.85 KB
/
theta2_wobble.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import click
import operator
import plotting
import calculation
from multiprocessing import Pool, cpu_count
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from fact.io import read_h5py
import astropy.units as u
from astropy import table
from astropy.io import fits
from astropy.coordinates import SkyCoord
from fact.analysis.statistics import li_ma_significance
from pyirf.binning import (
create_bins_per_decade,
add_overflow_bins,
create_histogram_table,
)
from pyirf.cuts import evaluate_binned_cut
from pyirf.sensitivity import calculate_sensitivity, estimate_background
from pyirf.spectral import CRAB_HEGRA
import matplotlib
if matplotlib.get_backend() == 'pgf':
from matplotlib.backends.backend_pgf import PdfPages
else:
from matplotlib.backends.backend_pdf import PdfPages
COLUMN_MAP = {
'obs_id': 'obs_id',
'event_id': 'event_id',
'gamma_energy_prediction': 'reco_energy',
'source_alt_prediction': 'reco_alt',
'source_az_prediction': 'reco_az',
'alt_tel': 'pointing_alt',
'az_tel': 'pointing_az',
'gammaness': 'gh_score',
}
UNIT_MAP = {
'reco_energy': u.TeV,
'reco_alt': u.rad,
'reco_az': u.rad,
'pointing_alt': u.rad,
'pointing_az': u.rad
}
MAX_BG_RADIUS = 1 * u.deg
@click.command()
@click.argument('output', type=click.Path(exists=False, dir_okay=False))
@click.argument('data', nargs=-1, type=click.Path(exists=True, dir_okay=False))
@click.argument('source', type=str)
@click.argument('theta2_cut', type=float)
@click.argument('threshold', type=float)
@click.option(
'--n_offs', type=int, default=5,
help='Number of OFF regions (default = 5)'
)
@click.option(
'--n_jobs', type=int, default=-1,
help='Number of processors used (default = -1)'
)
def main(output, data, source, theta2_cut, threshold, n_offs, n_jobs):
outdir = output.split('/')[0]
src = SkyCoord.from_name(source)
if n_jobs == -1:
n_jobs = cpu_count()
with Pool(n_jobs) as pool:
results = np.array(
pool.starmap(
calculation.read_run_calculate_thetas,
[(run, threshold, src, n_offs) for run in data]
), dtype=object
)
df_selected = pd.concat(results[:,0], ignore_index=True)
ontime = np.sum(results[:,1])
theta = np.concatenate(results[:,2])
df_selected5 = pd.concat(results[:,3], ignore_index=True)
theta_off = np.concatenate(results[:,4])
##############################################################################################################
# plots
##############################################################################################################
figures = []
figures.append(plt.figure())
ax = figures[-1].add_subplot(1, 1, 1)
plotting.theta2(
theta.deg**2,
theta_off.deg**2,
1/n_offs, theta2_cut, threshold,
source, ontime=ontime,
ax=ax
)
ax.set_title('Theta calculated in ICRS using astropy')
"""
figures.append(plt.figure())
ax = figures[-1].add_subplot(1, 1, 1)
plotting.theta2(
theta_pyirf.deg**2,
theta_off_pyirf.deg**2,
1/n_offs, theta2_cut, r'\mathrm{energy-dependent}',
source, ontime=ontime,
ax=ax
)
ax.set_title(r'Energy-dependent $t_\gamma$ optimised using pyirf')
# plot using pyirf theta cuts
figures.append(plt.figure())
ax = figures[-1].add_subplot(1, 1, 1)
ax.hist(
theta_pyirf.deg**2,
bins=100, range=[0,1], histtype='step', color='r', label='ON'
)
ax.hist(
theta_off_pyirf.deg**2,
bins=100, range=[0,1], histtype='stepfilled', color='tab:blue', alpha=0.5, label='OFF',
weights=np.full_like(theta_off_pyirf.deg**2, 1/n_offs)
)
txt = rf'''Source: {source}, $t_\mathrm{{obs}} = {ontime.to_value(u.hour):.2f} \mathrm{{h}}$
$N_\mathrm{{on}} = {n_on},\, N_\mathrm{{off}} = {n_off},\, \alpha = {1/n_offs:.2f}$
$N_\mathrm{{exc}} = {n_exc_mean:.0f} \pm {n_exc_std:.0f},\, S_\mathrm{{Li&Ma}} = {li_ma:.2f}$
'''
ax.text(0.5, 0.95, txt, transform=ax.transAxes, va='top', ha='center')
ax.set_xlabel(r'$\theta^2 \,\, / \,\, \mathrm{deg}^2$')
ax.set_xlim(0,1)
ax.legend()
ax.set_title(r'Energy-dependent $t_\gamma$ and $\theta_\mathrm{max}^2$ optimised using pyirf')
##############################################################################################################
# sensitivity
##############################################################################################################
sensitivity_bins = add_overflow_bins(
create_bins_per_decade(
10 ** -1.8 * u.TeV, 10 ** 2.41 * u.TeV, bins_per_decade=5
)
)
# gh_cuts and theta_cuts_opt in line 119f
gammas = plotting.to_astropy_table(
df_pyirf, # df_pyirf has pyirf gh cuts already applied
column_map=COLUMN_MAP,
unit_map=UNIT_MAP,
theta=theta_pyirf,
t_obs=ontime
)
background = plotting.to_astropy_table(
df_pyirf5,
column_map=COLUMN_MAP,
unit_map=UNIT_MAP,
theta=theta_off_pyirf,
t_obs=ontime
)
gammas["selected_theta"] = evaluate_binned_cut(
gammas["theta"], gammas["reco_energy"], theta_cuts_opt, operator.le
)
background["selected_theta"] = evaluate_binned_cut(
background["theta"], background["reco_energy"], theta_cuts_opt, operator.le
)
# calculate sensitivity
signal_hist = create_histogram_table(
gammas[gammas["selected_theta"]], bins=sensitivity_bins
)
background_hist = estimate_background(
background[background["selected_theta"]],
reco_energy_bins=sensitivity_bins,
theta_cuts=theta_cuts_opt,
alpha=1/n_offs,
background_radius=MAX_BG_RADIUS,
)
sensitivity = calculate_sensitivity(
signal_hist, background_hist, alpha=1/n_offs
)
# scale relative sensitivity by Crab flux to get the flux sensitivity
spectrum = CRAB_HEGRA
sensitivity["flux_sensitivity"] = (
sensitivity["relative_sensitivity"] * spectrum(sensitivity['reco_energy_center'])
)
##############################################################################################################
# sensitivity using unoptimised cuts
##############################################################################################################
gammas_unop = plotting.to_astropy_table(
df_selected, # df_selected has gammaness > threshold already applied
column_map=COLUMN_MAP,
unit_map=UNIT_MAP,
theta=theta,
t_obs=ontime
)
background_unop = plotting.to_astropy_table(
df_selected5,
column_map=COLUMN_MAP,
unit_map=UNIT_MAP,
theta=theta_off,
t_obs=ontime
)
gammas_unop["selected_theta"] = gammas_unop["theta"].to_value(u.deg) <= np.sqrt(0.03)
background_unop["selected_theta"] = background_unop["theta"].to_value(u.deg) <= np.sqrt(0.03)
theta_cut_unop = theta_cuts_opt
theta_cut_unop['cut'] = np.sqrt(0.03) * u.deg
# calculate sensitivity
signal_hist_unop = create_histogram_table(
gammas_unop[gammas_unop["selected_theta"]], bins=sensitivity_bins
)
background_hist_unop = estimate_background(
background_unop[background_unop["selected_theta"]],
reco_energy_bins=sensitivity_bins,
theta_cuts=theta_cut_unop,
alpha=1/n_offs,
background_radius=MAX_BG_RADIUS,
)
sensitivity_unop = calculate_sensitivity(
signal_hist_unop, background_hist_unop, alpha=1/n_offs
)
# scale relative sensitivity by Crab flux to get the flux sensitivity
sensitivity_unop["flux_sensitivity"] = (
sensitivity_unop["relative_sensitivity"] * spectrum(sensitivity_unop['reco_energy_center'])
)
# write fits file and create plot
hdus = [
fits.PrimaryHDU(),
fits.BinTableHDU(sensitivity, name="SENSITIVITY"),
fits.BinTableHDU(sensitivity_unop, name="SENSITIVITY_UNOP")
]
fits.HDUList(hdus).writeto(f'{outdir}/sensitivity_{source}.fits.gz', overwrite=True)
figures.append(plt.figure())
ax = figures[-1].add_subplot(1, 1, 1)
for s, label in zip(
[sensitivity, sensitivity_unop],
['pyirf optimised cuts', rf'$\theta^2 < {theta2_cut}$ and gh_score$> {threshold}$']
): plotting.plot_sensitivity(s, label=label, ax=ax)
# plot Magic sensitivity for reference
magic = table.QTable.read('notebooks/magic_sensitivity_2014.ecsv')
plotting.plot_sensitivity(magic, label='MAGIC 2014', ax=ax, magic=True)
ax.set_title(f'Minimal Flux Satisfying Requirements for 50 hours \n(based on {ontime.to_value(u.hour):.2f}h of {source} observations)')
"""
# save plots
with PdfPages(output) as pdf:
for fig in figures:
fig.tight_layout()
pdf.savefig(fig)
if __name__ == '__main__':
main()