make_ts_distributions.py
5.28 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
#! /usr/bin/env python
# ==========================================================================
# Generate TS distribution
#
# Copyright (C) 2011-2016 Jurgen Knodlseder
#
# 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 sys
import time
import cscripts
# Optionally import processing module
try:
import processing
has_processing = True
except:
has_processing = False
# ====================== #
# Create TS distribution #
# ====================== #
def create_ts(loge, emin, emax, ntrials=100, duration=180000.0,
enumbins=0, log=False):
"""
Create TS distribution.
Parameters
----------
loge : float
Logarithm of mean energy (TeV)
emin : float
Minimum energy (TeV)
emax : float
Maximum energy (TeV)
ntrials : int, optional
Number of MC samples
durations : float, optional
Observation duration (s)
enumbins : int, optional
Number of energy bins
log : bool, optional
Create log file(s)
"""
# Generate output filename
outfile = 'ts_'+str(loge)+'.dat'
# Setup cstsdist tool
tsdist = cscripts.cstsdist()
tsdist['inmodel'] = 'data/crab.xml'
tsdist['srcname'] = 'Crab'
tsdist['outfile'] = outfile
tsdist['ntrials'] = ntrials
tsdist['caldb'] = 'prod2'
tsdist['irf'] = 'South_0.5h'
tsdist['ra'] = 83.63
tsdist['dec'] = 22.01
tsdist['emin'] = emin
tsdist['emax'] = emax
tsdist['enumbins'] = enumbins
tsdist['tmin'] = 0
tsdist['tmax'] = duration
tsdist['rad'] = 5.0
tsdist['npix'] = 200
tsdist['binsz'] = 0.05
#tsdist['debug'] = True
# Optionally open the log file
if log:
tsdist.logFileOpen()
# Run tool
tsdist.run()
# Return
return
# ======================== #
# Main routine entry point #
# ======================== #
if __name__ == '__main__':
# Get input arguments
usage = 'make_ts_distributions [-n ntrials] [-e enumbins] [-m max_threads]'
if len(sys.argv) < 1:
print(usage)
sys.exit()
# Set default parameters
ntrials = 100
enumbins = 0
duration = 180000.0
max_threads = 1
# Parameter dictionnary
pars = [{'option': '-n', 'value': ntrials},
{'option': '-e', 'value': enumbins},
{'option': '-d', 'value': duration},
{'option': '-m', 'value': max_threads}]
# Gather parameters from command line
i = 1
while i < len(sys.argv):
# Search for options
for par in pars:
if sys.argv[i] == par['option']:
if len(sys.argv) > i+1:
i += 1
try:
par['value'] = int(sys.argv[i])
except:
print(usage)
sys.exit()
else:
print(usage)
sys.exit()
# Next item
i += 1
# Recover parameters
ntrials = pars[0]['value']
enumbins = pars[1]['value']
duration = pars[2]['value']
max_threads = pars[3]['value']
# Loop over energy bands. The energy bands are those that are also
# used for sensitivity computation.
for ieng in range(20):
# Set energies
loge = -1.7 + ieng * 0.2
emean = pow(10.0, loge)
emin = pow(10.0, loge-0.1)
emax = pow(10.0, loge+0.1)
if loge < 0:
loge = 'm'+str(abs(loge))
else:
loge = 'p'+str(abs(loge))
# If processing support is available then start max_threads processes
# in parallel
if has_processing:
# Wait until one thread has finished
while len(processing.activeChildren()) >= max_threads:
time.sleep(10)
# Set arguments
args = (loge, emin, emax)
kwargs = {'ntrials': ntrials, 'enumbins': enumbins,
'duration': duration}
# Generate pull distribution
p = processing.Process(target=create_ts, args=args, kwargs=kwargs)
p.start()
print('Process emin=%.4f emax=%.4f started.' % (emin, emax))
# Wait a short time to allow process to start
time.sleep(1)
# ... otherwise simply start a single process
else:
create_ts(loge, emin, emax, ntrials=ntrials, enumbins=enumbins,
duration=duration)
# If processing support is available then wait until all threads finished
if has_processing:
while len(processing.activeChildren()) > 0:
time.sleep(10)