Commit 3c6c07a170128ba6acb7981e2eb98ce7b9a1a8fc
1 parent
24f5f1c6
Enhance show_irf.py script
Showing
1 changed file
with
96 additions
and
28 deletions
examples/show_irf.py
| ... | ... | @@ -24,6 +24,8 @@ import gammalib |
| 24 | 24 | import cscripts |
| 25 | 25 | try: |
| 26 | 26 | import matplotlib.pyplot as plt |
| 27 | + from matplotlib import ticker | |
| 28 | + from matplotlib.colors import LogNorm | |
| 27 | 29 | plt.figure() |
| 28 | 30 | plt.close() |
| 29 | 31 | except (ImportError, RuntimeError): |
| ... | ... | @@ -106,7 +108,9 @@ def plot_aeff(sub, aeff, emin=None, emax=None, tmin=None, tmax=None, |
| 106 | 108 | image.append(row) |
| 107 | 109 | |
| 108 | 110 | # Plot image |
| 109 | - sub.imshow(image, extent=[emin,emax,tmin,tmax], aspect=0.5) | |
| 111 | + c = sub.imshow(image, extent=[emin,emax,tmin,tmax], aspect=0.5, vmin=1e6, norm=LogNorm()) | |
| 112 | + cbar = plt.colorbar(c, orientation='horizontal', shrink=0.8) | |
| 113 | + cbar.set_label('cm$^2$') | |
| 110 | 114 | |
| 111 | 115 | # Show boundary contours |
| 112 | 116 | contours = sub.contour(logenergies, thetas, image, [0.0], colors=('white')) |
| ... | ... | @@ -188,7 +192,7 @@ def plot_psf(sub, psf, emin=None, emax=None, tmin=None, tmax=None, |
| 188 | 192 | |
| 189 | 193 | # Get containment radius value |
| 190 | 194 | value = psf.containment_radius(0.68, logenergy, theta*gammalib.deg2rad) * \ |
| 191 | - gammalib.rad2deg | |
| 195 | + gammalib.rad2deg * 60.0 | |
| 192 | 196 | |
| 193 | 197 | # Append value |
| 194 | 198 | row.append(value) |
| ... | ... | @@ -197,7 +201,12 @@ def plot_psf(sub, psf, emin=None, emax=None, tmin=None, tmax=None, |
| 197 | 201 | image.append(row) |
| 198 | 202 | |
| 199 | 203 | # Plot image |
| 200 | - sub.imshow(image, extent=[emin,emax,tmin,tmax], aspect=0.5, vmin=0.0, vmax=0.5) | |
| 204 | + c = sub.imshow(image, extent=[emin,emax,tmin,tmax], aspect=0.5, vmin=0.0, vmax=18.0) | |
| 205 | + cbar = plt.colorbar(c, orientation='horizontal', shrink=0.8) | |
| 206 | + tick_locator = ticker.MaxNLocator(nbins=5) | |
| 207 | + cbar.locator = tick_locator | |
| 208 | + cbar.update_ticks() | |
| 209 | + cbar.set_label('arcmin') | |
| 201 | 210 | |
| 202 | 211 | # Show boundary contours |
| 203 | 212 | contours = sub.contour(logenergies, thetas, image, [0.0], colors=('white')) |
| ... | ... | @@ -250,6 +259,12 @@ def plot_edisp(sub, edisp, emin=None, emax=None, tmin=None, tmax=None, |
| 250 | 259 | if emax == None: |
| 251 | 260 | emax = edisp.table().axis_hi(ieng, neng-1) |
| 252 | 261 | |
| 262 | + # Determine migration range | |
| 263 | + imigra = edisp.table().axis('MIGRA') | |
| 264 | + nmigra = edisp.table().axis_bins(imigra) | |
| 265 | + migramin = edisp.table().axis_lo(imigra, 0) | |
| 266 | + migramax = edisp.table().axis_hi(imigra, nmigra-1) | |
| 267 | + | |
| 253 | 268 | # Determine offset angle range |
| 254 | 269 | itheta = edisp.table().axis('THETA') |
| 255 | 270 | ntheta = edisp.table().axis_bins(itheta) |
| ... | ... | @@ -264,18 +279,22 @@ def plot_edisp(sub, edisp, emin=None, emax=None, tmin=None, tmax=None, |
| 264 | 279 | |
| 265 | 280 | # Set axes |
| 266 | 281 | denergy = (emax - emin)/(nengs-1) |
| 282 | + dmigra = (migramax - migramin)/(nmigra-1) | |
| 267 | 283 | dtheta = (tmax - tmin)/(nthetas-1) |
| 268 | 284 | logenergies = [emin+i*denergy for i in range(nengs)] |
| 285 | + migras = [migramin+i*dmigra for i in range(nmigra)] | |
| 269 | 286 | thetas = [tmax-i*dtheta for i in range(nthetas)] |
| 270 | 287 | |
| 271 | - # Initialise image | |
| 272 | - image = [] | |
| 288 | + # Initialise images | |
| 289 | + image_mean = [] | |
| 290 | + image_std = [] | |
| 273 | 291 | |
| 274 | 292 | # Loop over offset angles |
| 275 | 293 | for theta in thetas: |
| 276 | 294 | |
| 277 | - # Initialise row | |
| 278 | - row = [] | |
| 295 | + # Initialise rows | |
| 296 | + row_mean = [] | |
| 297 | + row_std = [] | |
| 279 | 298 | |
| 280 | 299 | # Compute detx and dety |
| 281 | 300 | detx = theta*gammalib.deg2rad |
| ... | ... | @@ -284,26 +303,73 @@ def plot_edisp(sub, edisp, emin=None, emax=None, tmin=None, tmax=None, |
| 284 | 303 | # Loop over energies |
| 285 | 304 | for logenergy in logenergies: |
| 286 | 305 | |
| 287 | - # Get no migration energy dispersion value | |
| 288 | - value = edisp(logenergy, logenergy, theta*gammalib.deg2rad) | |
| 306 | + # Compute mean migration | |
| 307 | + mean = 0.0 | |
| 308 | + std = 0.0 | |
| 309 | + num = 0.0 | |
| 310 | + for migra in migras: | |
| 311 | + if migra > 0.0: | |
| 312 | + logobs = math.log10(migra) + logenergy | |
| 313 | + value = edisp(logobs, logenergy, theta*gammalib.deg2rad) | |
| 314 | + mean += migra * value | |
| 315 | + std += migra * migra * value | |
| 316 | + num += value | |
| 317 | + if num > 0.0: | |
| 318 | + mean /= num | |
| 319 | + std /= num | |
| 320 | + arg = std - mean * mean | |
| 321 | + if arg > 0.0: | |
| 322 | + std = math.sqrt(arg) | |
| 323 | + else: | |
| 324 | + std = 0.0 | |
| 289 | 325 | |
| 290 | 326 | # Append value |
| 291 | - row.append(value) | |
| 327 | + row_mean.append(mean) | |
| 328 | + row_std.append(std) | |
| 292 | 329 | |
| 293 | - # Append row | |
| 294 | - image.append(row) | |
| 330 | + # Append rows | |
| 331 | + image_mean.append(row_mean) | |
| 332 | + image_std.append(row_std) | |
| 333 | + | |
| 334 | + # First subplot | |
| 335 | + f1 = plt.subplot(223) | |
| 295 | 336 | |
| 296 | 337 | # Plot image |
| 297 | - sub.imshow(image, extent=[emin,emax,tmin,tmax], aspect=0.5) | |
| 338 | + c1 = f1.imshow(image_mean, extent=[emin,emax,tmin,tmax], aspect=0.5) | |
| 339 | + cbar1 = plt.colorbar(c1, orientation='horizontal', shrink=0.8) | |
| 340 | + tick_locator = ticker.MaxNLocator(nbins=5) | |
| 341 | + cbar1.locator = tick_locator | |
| 342 | + cbar1.update_ticks() | |
| 343 | + cbar1.set_label('E$_{reco}$ / E$_{true}$') | |
| 298 | 344 | |
| 299 | 345 | # Show boundary contours |
| 300 | - contours = sub.contour(logenergies, thetas, image, [0.0], colors=('white')) | |
| 301 | - sub.clabel(contours, inline=1, fontsize=8) | |
| 346 | + contours = f1.contour(logenergies, thetas, image_mean, [0.0], colors=('white')) | |
| 347 | + f1.clabel(contours, inline=1, fontsize=8) | |
| 302 | 348 | |
| 303 | 349 | # Plot title and axis |
| 304 | - sub.set_title('No migration energy dispersion') | |
| 305 | - sub.set_xlabel('log10(E/TeV)') | |
| 306 | - sub.set_ylabel('Offset angle (deg)') | |
| 350 | + f1.set_title('Mean of energy dispersion') | |
| 351 | + f1.set_xlabel('log10(E/TeV)') | |
| 352 | + f1.set_ylabel('Offset angle (deg)') | |
| 353 | + | |
| 354 | + # Second subplot | |
| 355 | + f2 = plt.subplot(224) | |
| 356 | + | |
| 357 | + # Plot image | |
| 358 | + c2 = f2.imshow(image_std, extent=[emin,emax,tmin,tmax], aspect=0.5) | |
| 359 | + cbar2 = plt.colorbar(c2, orientation='horizontal', shrink=0.8) | |
| 360 | + tick_locator = ticker.MaxNLocator(nbins=5) | |
| 361 | + cbar2.locator = tick_locator | |
| 362 | + cbar2.update_ticks() | |
| 363 | + cbar2.set_label('E$_{reco}$ / E$_{true}$') | |
| 364 | + | |
| 365 | + # Show boundary contours | |
| 366 | + contours = f2.contour(logenergies, thetas, image_std, [0.0], colors=('white')) | |
| 367 | + f2.clabel(contours, inline=1, fontsize=8) | |
| 368 | + | |
| 369 | + # Plot title and axis | |
| 370 | + f2.set_title('Standard deviation of energy dispersion') | |
| 371 | + f2.set_xlabel('log10(E/TeV)') | |
| 372 | + f2.set_ylabel('Offset angle (deg)') | |
| 307 | 373 | |
| 308 | 374 | # Return |
| 309 | 375 | return |
| ... | ... | @@ -386,7 +452,9 @@ def plot_bkg(sub, bkg, emin=None, emax=None, tmin=None, tmax=None, |
| 386 | 452 | image.append(row) |
| 387 | 453 | |
| 388 | 454 | # Plot image |
| 389 | - sub.imshow(image, extent=[emin,emax,tmin,tmax], aspect=0.5) | |
| 455 | + c = sub.imshow(image, extent=[emin,emax,tmin,tmax], aspect=0.5, norm=LogNorm()) | |
| 456 | + cbar = plt.colorbar(c, orientation='horizontal', shrink=0.8) | |
| 457 | + cbar.set_label('s$^{-1}$ MeV$^{-1}$ sr$^{-1}$') | |
| 390 | 458 | |
| 391 | 459 | # Show boundary contours |
| 392 | 460 | contours = sub.contour(logenergies, thetas, image, [0.0], colors=('white')) |
| ... | ... | @@ -454,26 +522,26 @@ def plot_irf(irf, emin, emax, tmin, tmax, plotfile): |
| 454 | 522 | (gammalib.toupper(mission), instrument, response, selection) |
| 455 | 523 | |
| 456 | 524 | # Create figure |
| 457 | - fig = plt.figure(figsize=(12,8)) | |
| 525 | + fig = plt.figure(figsize=(16,8)) | |
| 458 | 526 | |
| 459 | 527 | # Add title |
| 460 | 528 | fig.suptitle(title, fontsize=16) |
| 461 | 529 | |
| 462 | 530 | # Plot Aeff |
| 463 | - ax1 = fig.add_subplot(221) | |
| 531 | + ax1 = fig.add_subplot(231) | |
| 464 | 532 | plot_aeff(ax1, irf.aeff(), emin=emin, emax=emax, tmin=tmin, tmax=tmax) |
| 465 | 533 | |
| 466 | 534 | # Plot Psf |
| 467 | - ax2 = fig.add_subplot(222) | |
| 535 | + ax2 = fig.add_subplot(232) | |
| 468 | 536 | plot_psf(ax2, irf.psf(), emin=emin, emax=emax, tmin=tmin, tmax=tmax) |
| 469 | 537 | |
| 470 | - # Plot Edisp | |
| 471 | - ax3 = fig.add_subplot(223) | |
| 472 | - plot_edisp(ax3, irf.edisp(), emin=emin, emax=emax, tmin=tmin, tmax=tmax) | |
| 473 | - | |
| 474 | 538 | # Plot Background |
| 475 | - ax4 = fig.add_subplot(224) | |
| 476 | - plot_bkg(ax4, irf.background(), emin=emin, emax=emax, tmin=tmin, tmax=tmax) | |
| 539 | + ax3 = fig.add_subplot(233) | |
| 540 | + plot_bkg(ax3, irf.background(), emin=emin, emax=emax, tmin=tmin, tmax=tmax) | |
| 541 | + | |
| 542 | + # Plot Edisp | |
| 543 | + ax4 = fig.add_subplot(234) | |
| 544 | + plot_edisp(ax4, irf.edisp(), emin=emin, emax=emax, tmin=tmin, tmax=tmax) | |
| 477 | 545 | |
| 478 | 546 | # Show plots or save it into file |
| 479 | 547 | if len(plotfile) > 0: | ... | ... |