test_model_spatial_radial.py
2.88 KB
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
#! /usr/bin/env python
# ==========================================================================
# Compare radial model to MC
# --------------------------------------------------------------------------
#
# Copyright (C) 2020 Juergen Knoedlseder
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# ==========================================================================
import math
import gammalib
import matplotlib.pyplot as plt
# ========================== #
# Compare radial model to MC #
# ========================== #
def compare(radial, rad_max=1.0, nrad=100, nmc=1000000):
"""
Compare radial model to MC.
"""
# Get dummy energy and time
energy = gammalib.GEnergy()
time = gammalib.GTime()
# Generate theta vector
theta_bin = rad_max/nrad
theta = [(i+0.5) * theta_bin for i in range(nrad)]
# Generate model vector
model = [radial.eval(t*gammalib.deg2rad, energy, time) for t in theta]
# Generate MC vector
ran = gammalib.GRan()
mc = [0.0 for t in theta]
for i in range(nmc):
dir = radial.mc(energy, time, ran)
offset = radial.dir().dist_deg(dir)
index = int(offset / rad_max * nrad)
if index >= 0 and index < nrad:
mc[index] += 1.0
# Divide MC by area of ring
for i, t in enumerate(theta):
tmin = t-0.5*theta_bin
tmax = t+0.5*theta_bin
area = gammalib.pi * (tmax*tmax - tmin*tmin)
mc[i] = mc[i] / area
# Normalise model
n_model = sum(model)
n_mc = sum(mc)
if n_model > 0.0:
norm = n_mc/n_model
model = [m*norm for m in model]
# Create figure
plt.figure(1)
plt.title('Model-MC comparison')
# Plot data
plt.plot(theta, model, 'r-')
plt.plot(theta, mc, 'b-')
# Set axes
plt.xlabel('Radius (deg)')
plt.ylabel('Number of photons')
# Notify
print('PLEASE CLOSE WINDOW TO CONTINUE ...')
# Show plot
plt.show()
# Return
return
# ======================== #
# Main routine entry point #
# ======================== #
if __name__ == '__main__':
# Define model centre
centre = gammalib.GSkyDir()
centre.radec_deg(83.6331, 22.0145)
# Define radial model
radial = gammalib.GModelSpatialRadialRing(centre, 0.45, 0.15)
# Compare model to MC
compare(radial)