Commit d80ad5c6064e5121d43e442f865d90ef224e1e6f

Authored by knodlseder
1 parent 3ef43b0e

renamed to make_pull_at_sensivitity_limit.py

Showing 1 changed file with 0 additions and 208 deletions
examples/sensitivity_pull.py deleted
1   -#! /usr/bin/env python
2   -# ==========================================================================
3   -# This script generates pull distributions for a source at the 5 sigma
4   -# sensitivity limit. The script makes use of the "processing" Python module,
5   -# if available, that can be used for parallel computing on multiple
6   -# cores/CPUs.
7   -#
8   -# Required 3rd party modules:
9   -# - processing (optional)
10   -#
11   -# Copyright (C) 2011 Jurgen Knodlseder
12   -#
13   -# This program is free software: you can redistribute it and/or modify
14   -# it under the terms of the GNU General Public License as published by
15   -# the Free Software Foundation, either version 3 of the License, or
16   -# (at your option) any later version.
17   -#
18   -# This program is distributed in the hope that it will be useful,
19   -# but WITHOUT ANY WARRANTY; without even the implied warranty of
20   -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21   -# GNU General Public License for more details.
22   -#
23   -# You should have received a copy of the GNU General Public License
24   -# along with this program. If not, see <http://www.gnu.org/licenses/>.
25   -#
26   -# ==========================================================================
27   -from ctatools import *
28   -from gammalib import *
29   -from cspull import *
30   -import sys
31   -import time
32   -
33   -# Try importing processing module
34   -has_processing = False
35   -try:
36   - import processing
37   - has_processing = True
38   -except:
39   - pass
40   -
41   -
42   -# ======================== #
43   -# Create pull distribution #
44   -# ======================== #
45   -def create_pull(loge, emin, emax, models, ntrials=100, duration=180000.0, \
46   - enumbins=0, log=False):
47   - """
48   - Create pull distribution
49   -
50   - Parameters:
51   - emin - Minimum energy (TeV)
52   - emax - Maximum energy (TeV)
53   - Keywords:
54   - log - Create log file(s)
55   - """
56   - # Generate output filename
57   - outfile = "pull_"+loge+".dat"
58   -
59   - # Setup cspull tool
60   - pull = cspull()
61   - pull.models(models)
62   - pull["outfile"] = outfile
63   - pull["ntrials"] = ntrials
64   - pull["caldb"] = "$GAMMALIB/share/caldb/cta"
65   - pull["irf"] = "kb_E_50h_v3"
66   - pull["ra"] = 83.6331
67   - pull["dec"] = 22.0145
68   - pull["emin"] = emin
69   - pull["emax"] = emax
70   - pull["enumbins"] = enumbins
71   - pull["duration"] = duration
72   -
73   - # Optionally open the log file
74   - if log:
75   - pull.logFileOpen()
76   -
77   - # Run tool
78   - pull.run()
79   -
80   - # Return
81   - return
82   -
83   -
84   -# ============ #
85   -# Create model #
86   -# ============ #
87   -def create_model(flux, index=-2.48, fitidx=False):
88   - """
89   - Add standard CTA background model to observations container.
90   - We use a simple power law here, scaled to Konrad's E configuration
91   - performance table.
92   -
93   - Parameters:
94   - flux - Flux in Crab units
95   - """
96   - # Define background model
97   - bgd_radial = GCTAModelRadialGauss(3.0)
98   - bgd_spectrum = GModelSpectralPlaw(61.8, -1.85)
99   - bgd_spectrum["Prefactor"].scale(1.0e-6)
100   - bgd_spectrum["PivotEnergy"].value(1.0)
101   - bgd_spectrum["PivotEnergy"].scale(1.0e6)
102   - if fitidx:
103   - bgd_spectrum["Index"].free()
104   - else:
105   - bgd_spectrum["Index"].fix()
106   - bgd_model = GCTAModelRadialAcceptance(bgd_radial, bgd_spectrum)
107   - bgd_model.name("Background")
108   - bgd_model.instruments("CTA")
109   -
110   - # Define source spectrum
111   - location = GSkyDir()
112   - location.radec_deg(83.6331, 22.0145)
113   - src_spatial = GModelSpatialPtsrc(location)
114   - src_spectrum = GModelSpectralPlaw(flux, index)
115   - src_spectrum["Prefactor"].scale(5.7e-16)
116   - src_spectrum["PivotEnergy"].value(0.3)
117   - src_spectrum["PivotEnergy"].scale(1.0e6)
118   - if fitidx:
119   - src_spectrum["Index"].free()
120   - else:
121   - src_spectrum["Index"].fix()
122   - src_model = GModelPointSource(src_spatial, src_spectrum)
123   - src_model.name("Test")
124   -
125   - # Add models to container
126   - models = GModels()
127   - models.append(bgd_model)
128   - models.append(src_model)
129   -
130   - # Return model container
131   - return models
132   -
133   -
134   -# ======================== #
135   -# Main routine entry point #
136   -# ======================== #
137   -if __name__ == '__main__':
138   - """
139   - Create pull distributions for the energy bands and fluxes given in the
140   - file specified in the argument
141   - """
142   - # Get input arguments
143   - usage = "sensitivity_pull filename [max_threads]"
144   - if len(sys.argv) < 2 or len(sys.argv) > 3:
145   - print usage
146   - sys.exit()
147   -
148   - # Get sensitivity file
149   - filename = sys.argv[1]
150   -
151   - # Set maximum number of threads
152   - if len(sys.argv) > 2:
153   - max_threads = int(sys.argv[2])
154   - else:
155   - max_threads = 1
156   -
157   - # Load sensitivity file
158   - file = open(filename, 'r')
159   - reader = csv.DictReader(file)
160   -
161   - # Loop over rows
162   - for row in reader:
163   -
164   - # Get parameters
165   - loge = float(row['loge'])
166   - if loge < 0:
167   - loge = "m"+str(abs(loge))
168   - else:
169   - loge = "p"+str(abs(loge))
170   - emin = float(row['emin'])
171   - emax = float(row['emax'])
172   - flux = float(row['crab'])
173   -
174   - # Generate model
175   - models = create_model(flux)
176   -
177   - # Processing support?
178   - if has_processing:
179   -
180   - # Wait until one thread has finished
181   - while len(processing.activeChildren()) >= max_threads:
182   - time.sleep(60)
183   -
184   - # Set arguments
185   - args = (loge, emin, emax, models)
186   - kwargs = {}
187   -
188   - # Generate pull distribution
189   - p = processing.Process(target=create_pull, args=args, kwargs=kwargs)
190   - p.start()
191   - print "Process emin=%.4f emax=%.4f started." % (emin, emax)
192   -
193   - # Wait some time
194   - time.sleep(1)
195   -
196   - # ... no
197   - else:
198   - create_pull(loge, emin, emax, models)
199   -
200   - # Processing support
201   - if has_processing:
202   -
203   - # Wait until all threads finished
204   - while len(processing.activeChildren()) > 0:
205   - time.sleep(60)
206   -
207   - # Close file
208   - file.close()