test_diffuse_cube.py
5.2 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
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
#! /usr/bin/env python
# ==========================================================================
# This script tests the simulation of diffuse map cubes.
#
# If matplotlib is installed, the simulation results will be displayed.
#
# --------------------------------------------------------------------------
#
# Copyright (C) 2013 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/>.
#
# ==========================================================================
from gammalib import *
from math import *
# ======================== #
# Simulate CTA observation #
# ======================== #
def simulate(xmlname, e_min, e_max, area, duration, dir, radius):
"""
Simulate CTA observation.
"""
# Allocate MC parameters
emin = GEnergy()
emax = GEnergy()
tmin = GTime(0.0)
tmax = GTime(duration)
# Define MC parameters
emin.TeV(e_min)
emax.TeV(e_max)
# Allocate random number generator
ran = GRan()
# Load models and extract first model
models = GModels(xmlname)
model = models[0]
#print(model)
#print(model.spatial())
#print(model.spatial().spectrum())
# Simulate photons
photons = model.mc(area, dir, radius, emin, emax, tmin, tmax, ran)
# Print photons
print(str(len(photons)) + " photons simulated.")
# Return photons
return photons
# ============ #
# Show photons #
# ============ #
def show_photons(photons, xmlname, e_min, e_max, area, duration, dir, radius, ebins=30):
"""
Show photons using matplotlib (if available).
"""
# Only proceed if matplotlib is available
try:
# Import matplotlib
import matplotlib.pyplot as plt
# Create figure
plt.figure(1)
plt.title("MC simulated photon spectrum (" + str(e_min) + '-' + str(e_max) + " TeV)")
# Setup energy range covered by data
ebds = GEbounds(ebins, GEnergy(e_min, "TeV"), GEnergy(e_max, "TeV"))
# Create energy axis
energy = []
for i in range(ebds.size()):
energy.append(ebds.elogmean(i).TeV())
# Fill histogram
counts = [0.0 for i in range(ebds.size())]
for photon in photons:
index = ebds.index(photon.energy())
counts[index] = counts[index] + 1.0
# Create error bars
error = [sqrt(c) for c in counts]
# Get model values
models = GModels(xmlname)
mod = models[0]
model = []
t = GTime()
mod.spatial().set_mc_cone(dir, radius)
for i in range(ebds.size()):
eng = ebds.elogmean(i)
ewidth = ebds.ewidth(i)
f = mod.spatial().spectrum().eval(eng, t) * \
area * duration * ewidth.MeV()
model.append(f)
# Plot data
plt.loglog(energy, counts, 'ro')
plt.errorbar(energy, counts, error, fmt=None, ecolor='r')
# Plot model
plt.plot(energy, model, 'b-')
# Set axes
plt.xlabel("Energy (TeV)")
plt.ylabel("Number of incident photons")
# Create figure
plt.figure(2)
plt.title("MC simulated photon map")
# Create RA and DEC arrays
ra = []
dec = []
for photon in photons:
ra.append(photon.dir().ra_deg())
dec.append(photon.dir().dec_deg())
# Make scatter plot
plt.scatter(ra, dec, marker=".")
# Set axes
plt.xlabel("Right Ascension (deg)")
plt.ylabel("Declination (deg)")
# Notify
print("PLEASE CLOSE WINDOW TO CONTINUE ...")
# Allocate histogram
# Show plot
plt.show()
except ImportError:
print("Matplotlib is not (correctly) installed on your system.")
# Return
return
#==========================#
# Main routine entry point #
#==========================#
if __name__ == '__main__':
"""
Simulate photons.
"""
# Dump header
print("")
print("********************")
print("* Simulate photons *")
print("********************")
# Set model filename
xmlnames = ["data/model_diffuse_cube.xml"]
# Set simulation parameters
e_min = 0.1 # 0.1 TeV
e_max = 100.0 # 100 TeV
area = 1.0 # 1 cm2
duration = 1.0 # 1 sec
dir = GSkyDir()
dir.radec_deg(84.17263, 22.01444)
radius = 1.0
# Loop over models
for xmlname in xmlnames:
# Perform simulation
photons = simulate(xmlname, e_min, e_max, area, duration, dir, radius)
# Show photons
show_photons(photons, xmlname, e_min, e_max, area, duration, dir, radius)