Commit 6de368966bca1957a75cfa89c9a938bf364b8df6
1 parent
aa19272d
Update example scripts.
Showing
6 changed files
with
36 additions
and
1026 deletions
examples/Makefile.am
| ... | ... | @@ -2,14 +2,21 @@ |
| 2 | 2 | |
| 3 | 3 | # Scripts to be distributed |
| 4 | 4 | scripts = README \ |
| 5 | - pipeline_stacked_mem.py \ | |
| 6 | - make_binned_analysis.py \ | |
| 5 | + benchmark_cta_analysis.py \ | |
| 7 | 6 | make_pull_at_sensitivity_limit.py \ |
| 7 | + make_spectrum.py \ | |
| 8 | 8 | make_ts_distributions.py \ |
| 9 | - make_unbinned_analysis.py \ | |
| 9 | + pipeline_binned_disk.py \ | |
| 10 | + pipeline_binned_mem.py \ | |
| 11 | + pipeline_stacked_disk.py \ | |
| 12 | + pipeline_stacked_mem.py \ | |
| 13 | + pipeline_unbinned_disk.py \ | |
| 14 | + pipeline_unbinned_mem.py \ | |
| 15 | + show_butterfly.py \ | |
| 16 | + show_pha.py \ | |
| 17 | + show_response.py \ | |
| 10 | 18 | show_pull_histogram.py \ |
| 11 | - show_ts_distribution.py \ | |
| 12 | - show_butterfly.py | |
| 19 | + show_ts_distribution.py | |
| 13 | 20 | |
| 14 | 21 | # Set directories |
| 15 | 22 | scriptsdir = $(datadir)/examples/python | ... | ... |
examples/README
| ... | ... | @@ -2,6 +2,28 @@ This folder contains Python example scripts that illustrate the |
| 2 | 2 | usage of the ctools. |
| 3 | 3 | |
| 4 | 4 | benchmark_cta_analysis.py |
| 5 | + This script peforms a benchmark of the various CTA analysis types | |
| 6 | + (unbinned, binned, stacked), and if matplotlib is installed, creates | |
| 7 | + a plot of the benchmark results. | |
| 8 | + | |
| 9 | +make_pull_at_sensitivity_limit.py | |
| 10 | + This script creates pull distributions as function of energy | |
| 11 | + for a test source that is at the 5 sigma sensitivity limit for | |
| 12 | + 50h of effective observing time. It takes as input file the | |
| 13 | + output file generated using cspull. | |
| 14 | + If the Python processing module is installed the script can make | |
| 15 | + use of multiple cores and runs in parallel. | |
| 16 | + | |
| 17 | +make_spectrum.py | |
| 18 | + This script shows how to make a spectrum using obsutils. | |
| 19 | + | |
| 20 | +make_ts_distributions.py | |
| 21 | + This script creates TS distributions as function of energy for | |
| 22 | + an off observation of 50h effective observing time. | |
| 23 | + If the Python processing module is installed the script can make | |
| 24 | + use of multiple cores and runs in parallel. | |
| 25 | + | |
| 26 | +benchmark_cta_analysis.py | |
| 5 | 27 | Performs performance benchmarks of the various CTA analysis types. |
| 6 | 28 | |
| 7 | 29 | pipeline_binned_disk.py |
| ... | ... | @@ -28,39 +50,6 @@ pipeline_unbinned_mem.py |
| 28 | 50 | Example pipeline for simulating events and performing an unbinned |
| 29 | 51 | analysis in memory. No files will be written to disk. |
| 30 | 52 | |
| 31 | - | |
| 32 | - | |
| 33 | - | |
| 34 | -make_binned_analysis.py | |
| 35 | - Example Python pipelines for binned analysis. | |
| 36 | - The first pipeline writes intermediate results to the disk while | |
| 37 | - the second pipeline keeps the results in memory. | |
| 38 | - If matplotlib is installed on the system the fitted spectrum | |
| 39 | - and the observed offset angle distribution is displayed in | |
| 40 | - counts space. | |
| 41 | - | |
| 42 | -make_pull_at_sensitivity_limit.py | |
| 43 | - This script creates pull distributions as function of energy | |
| 44 | - for a test source that is at the 5 sigma sensitivity limit for | |
| 45 | - 50h of effective observing time. It takes as input file the | |
| 46 | - output file generated using cspull. | |
| 47 | - If the Python processing module is installed the script can make | |
| 48 | - use of multiple cores and runs in parallel. | |
| 49 | - | |
| 50 | -make_ts_distributions.py | |
| 51 | - This script creates TS distributions as function of energy for | |
| 52 | - an off observation of 50h effective observing time. | |
| 53 | - If the Python processing module is installed the script can make | |
| 54 | - use of multiple cores and runs in parallel. | |
| 55 | - | |
| 56 | -make_unbinned_analysis.py | |
| 57 | - Example Python pipelines for unbinned analysis. | |
| 58 | - The first pipeline writes intermediate results to the disk while | |
| 59 | - the second pipeline keeps the results in memory. | |
| 60 | - If matplotlib is installed on the system the fitted spectrum | |
| 61 | - and the observed offset angle distribution is displayed in | |
| 62 | - counts space. | |
| 63 | - | |
| 64 | 53 | show_butterfly.py |
| 65 | 54 | Displays a butterfly generated using the ctbutterfly tool. |
| 66 | 55 | ... | ... |
examples/benchmark_cta_analysis.py
| 1 | 1 | #! /usr/bin/env python |
| 2 | 2 | # ========================================================================== |
| 3 | 3 | # This script peforms a benchmark of the various CTA analysis types |
| 4 | -# (unbinned, binned, cube-style), and if matplotlib is installed, creates | |
| 4 | +# (unbinned, binned, stacked), and if matplotlib is installed, creates | |
| 5 | 5 | # a plot of the benchmark results. |
| 6 | 6 | # |
| 7 | -# Copyright (C) 2014 Jurgen Knodlseder | |
| 7 | +# Copyright (C) 2014-2015 Jurgen Knodlseder | |
| 8 | 8 | # |
| 9 | 9 | # This program is free software: you can redistribute it and/or modify |
| 10 | 10 | # it under the terms of the GNU General Public License as published by | ... | ... |
examples/make_binned_analysis.py deleted
| 1 | -#! /usr/bin/env python | |
| 2 | -# ========================================================================== | |
| 3 | -# This script illustrates how to build a binned analysis pipeline using the | |
| 4 | -# ctools. If matplotlib is installed, a counts spectrum and an offset | |
| 5 | -# histogram will be displayed. | |
| 6 | -# | |
| 7 | -# Copyright (C) 2011-2014 Jurgen Knodlseder | |
| 8 | -# | |
| 9 | -# This program is free software: you can redistribute it and/or modify | |
| 10 | -# it under the terms of the GNU General Public License as published by | |
| 11 | -# the Free Software Foundation, either version 3 of the License, or | |
| 12 | -# (at your option) any later version. | |
| 13 | -# | |
| 14 | -# This program is distributed in the hope that it will be useful, | |
| 15 | -# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 16 | -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 17 | -# GNU General Public License for more details. | |
| 18 | -# | |
| 19 | -# You should have received a copy of the GNU General Public License | |
| 20 | -# along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 21 | -# | |
| 22 | -# ========================================================================== | |
| 23 | -import gammalib | |
| 24 | -import ctools | |
| 25 | -import os | |
| 26 | -import glob | |
| 27 | -import sys | |
| 28 | -import math | |
| 29 | -try: | |
| 30 | - import matplotlib.pyplot as plt | |
| 31 | - has_matplotlib = True | |
| 32 | -except: | |
| 33 | - has_matplotlib = False | |
| 34 | - | |
| 35 | - | |
| 36 | -# ============================= # | |
| 37 | -# Analysis pipeline - version 1 # | |
| 38 | -# ============================= # | |
| 39 | -def pipeline_v1(): | |
| 40 | - """ | |
| 41 | - Binned analysis pipeline - save intermediate results. | |
| 42 | - | |
| 43 | - This function implements an analysis pipeline that successively calls | |
| 44 | - ctobssim, ctbin and ctlike by saving the intermediate results as FITS | |
| 45 | - files on disk. This replicates the way how the analysis would be done | |
| 46 | - in a ftools-like approach. | |
| 47 | - """ | |
| 48 | - # Set script parameters | |
| 49 | - model_name = "${CTOOLS}/share/models/crab.xml" | |
| 50 | - events_name = "events.fits" | |
| 51 | - cntmap_name = "cntmap.fits" | |
| 52 | - result_name = "results.xml" | |
| 53 | - caldb = "${CALDB}/data/cta/dummy/bcf" | |
| 54 | - irf = "cta_dummy_irf" | |
| 55 | - ra = 83.63 | |
| 56 | - dec = 22.01 | |
| 57 | - rad_sim = 10.0 | |
| 58 | - tstart = 0.0 | |
| 59 | - tstop = 1800.0 | |
| 60 | - emin = 0.1 | |
| 61 | - emax = 100.0 | |
| 62 | - enumbins = 20 | |
| 63 | - nxpix = 200 | |
| 64 | - nypix = 200 | |
| 65 | - binsz = 0.02 | |
| 66 | - coordsys = "CEL" | |
| 67 | - proj = "CAR" | |
| 68 | - | |
| 69 | - # Initialise timing | |
| 70 | - wall_seconds = 0.0 | |
| 71 | - cpu_seconds = 0.0 | |
| 72 | - | |
| 73 | - # Simulate events | |
| 74 | - sim = ctools.ctobssim() | |
| 75 | - sim.logFileOpen() # We need this to explicitely open the log file in Python mode | |
| 76 | - sim["inmodel"].filename(model_name) | |
| 77 | - sim["outevents"].filename(events_name) | |
| 78 | - sim["caldb"].string(caldb) | |
| 79 | - sim["irf"].string(irf) | |
| 80 | - sim["ra"].real(ra) | |
| 81 | - sim["dec"].real(dec) | |
| 82 | - sim["rad"].real(rad_sim) | |
| 83 | - sim["tmin"].real(tstart) | |
| 84 | - sim["tmax"].real(tstop) | |
| 85 | - sim["emin"].real(emin) | |
| 86 | - sim["emax"].real(emax) | |
| 87 | - sim.execute() | |
| 88 | - sys.stdout.write("Simulated events ("+str(sim.celapse())+" CPU seconds)\n") | |
| 89 | - | |
| 90 | - # Update timing | |
| 91 | - wall_seconds += sim.telapse() | |
| 92 | - cpu_seconds += sim.celapse() | |
| 93 | - | |
| 94 | - # Bin events into counts map | |
| 95 | - bin = ctools.ctbin() | |
| 96 | - bin.logFileOpen() # We need this to explicitely open the log file in Python mode | |
| 97 | - bin["inobs"].filename(events_name) | |
| 98 | - bin["outcube"].filename(cntmap_name) | |
| 99 | - bin["ebinalg"].string("LOG") | |
| 100 | - bin["emin"].real(emin) | |
| 101 | - bin["emax"].real(emax) | |
| 102 | - bin["enumbins"].integer(enumbins) | |
| 103 | - bin["nxpix"].integer(nxpix) | |
| 104 | - bin["nypix"].integer(nypix) | |
| 105 | - bin["binsz"].real(binsz) | |
| 106 | - bin["coordsys"].string(coordsys) | |
| 107 | - bin["xref"].real(ra) | |
| 108 | - bin["yref"].real(dec) | |
| 109 | - bin["proj"].string(proj) | |
| 110 | - bin.execute() | |
| 111 | - sys.stdout.write("Binned events into counts map ("+str(bin.celapse())+" CPU seconds)\n") | |
| 112 | - | |
| 113 | - # Update timing | |
| 114 | - wall_seconds += bin.telapse() | |
| 115 | - cpu_seconds += bin.celapse() | |
| 116 | - | |
| 117 | - # Perform maximum likelihood fitting | |
| 118 | - like = ctools.ctlike() | |
| 119 | - like.logFileOpen() # We need this to explicitely open the log file in Python mode | |
| 120 | - like["inobs"].filename(cntmap_name) | |
| 121 | - like["inmodel"].filename(model_name) | |
| 122 | - like["outmodel"].filename(result_name) | |
| 123 | - like["caldb"].string(caldb) | |
| 124 | - like["irf"].string(irf) | |
| 125 | - like.execute() | |
| 126 | - sys.stdout.write("Maximum likelihood fitting ("+str(like.celapse())+" CPU seconds)\n") | |
| 127 | - | |
| 128 | - # Update timing | |
| 129 | - wall_seconds += like.telapse() | |
| 130 | - cpu_seconds += like.celapse() | |
| 131 | - | |
| 132 | - # Show total times | |
| 133 | - sys.stdout.write("Total wall time elapsed: "+str(wall_seconds)+" seconds\n") | |
| 134 | - sys.stdout.write("Total CPU time used ...: "+str(cpu_seconds)+" seconds\n") | |
| 135 | - | |
| 136 | - # Return | |
| 137 | - return | |
| 138 | - | |
| 139 | - | |
| 140 | -# ============================= # | |
| 141 | -# Analysis pipeline - version 2 # | |
| 142 | -# ============================= # | |
| 143 | -def pipeline_v2(): | |
| 144 | - """ | |
| 145 | - Binned analysis pipeline - keep intermediate results in memory. | |
| 146 | - | |
| 147 | - This function implements an analysis pipeline that successively calls | |
| 148 | - ctobssim, ctbin and ctlike without saving the intermediate results as | |
| 149 | - FITS files on disk. All data are hold in memory. | |
| 150 | - | |
| 151 | - At the end, results are plotted (if matplotlib is installed) | |
| 152 | - """ | |
| 153 | - # Set script parameters | |
| 154 | - model_name = "${CTOOLS}/share/models/crab.xml" | |
| 155 | - caldb = "${CALDB}/data/cta/dummy/bcf" | |
| 156 | - irf = "cta_dummy_irf" | |
| 157 | - ra = 83.63 | |
| 158 | - dec = 22.01 | |
| 159 | - rad_sim = 10.0 | |
| 160 | - tstart = 0.0 | |
| 161 | - tstop = 1800.0 | |
| 162 | - emin = 0.1 | |
| 163 | - emax = 100.0 | |
| 164 | - enumbins = 20 | |
| 165 | - nxpix = 200 | |
| 166 | - nypix = 200 | |
| 167 | - binsz = 0.02 | |
| 168 | - coordsys = "CEL" | |
| 169 | - proj = "CAR" | |
| 170 | - | |
| 171 | - # Initialise timing | |
| 172 | - wall_seconds = 0.0 | |
| 173 | - cpu_seconds = 0.0 | |
| 174 | - | |
| 175 | - # Simulate events | |
| 176 | - sim = ctools.ctobssim() | |
| 177 | - sim["inmodel"].filename(model_name) | |
| 178 | - sim["caldb"].string(caldb) | |
| 179 | - sim["irf"].string(irf) | |
| 180 | - sim["ra"].real(ra) | |
| 181 | - sim["dec"].real(dec) | |
| 182 | - sim["rad"].real(rad_sim) | |
| 183 | - sim["tmin"].real(tstart) | |
| 184 | - sim["tmax"].real(tstop) | |
| 185 | - sim["emin"].real(emin) | |
| 186 | - sim["emax"].real(emax) | |
| 187 | - sim.run() | |
| 188 | - sys.stdout.write("Simulated events ("+str(sim.celapse())+" CPU seconds)\n") | |
| 189 | - | |
| 190 | - # Update timing | |
| 191 | - wall_seconds += sim.telapse() | |
| 192 | - cpu_seconds += sim.celapse() | |
| 193 | - | |
| 194 | - # Bin events into counts map | |
| 195 | - bin = ctools.ctbin(sim.obs()) | |
| 196 | - bin["ebinalg"].string("LOG") | |
| 197 | - bin["emin"].real(emin) | |
| 198 | - bin["emax"].real(emax) | |
| 199 | - bin["enumbins"].integer(enumbins) | |
| 200 | - bin["nxpix"].integer(nxpix) | |
| 201 | - bin["nypix"].integer(nypix) | |
| 202 | - bin["binsz"].real(binsz) | |
| 203 | - bin["coordsys"].string(coordsys) | |
| 204 | - bin["xref"].real(ra) | |
| 205 | - bin["yref"].real(dec) | |
| 206 | - bin["proj"].string(proj) | |
| 207 | - bin.run() | |
| 208 | - sys.stdout.write("Binned events into counts map ("+str(bin.celapse())+" CPU seconds)\n") | |
| 209 | - | |
| 210 | - # Update timing | |
| 211 | - wall_seconds += bin.telapse() | |
| 212 | - cpu_seconds += bin.celapse() | |
| 213 | - | |
| 214 | - # Perform maximum likelihood fitting | |
| 215 | - like = ctools.ctlike(bin.obs()) | |
| 216 | - like.run() | |
| 217 | - sys.stdout.write("Maximum likelihood fitting ("+str(like.celapse())+" CPU seconds)\n") | |
| 218 | - | |
| 219 | - # Update timing | |
| 220 | - wall_seconds += like.telapse() | |
| 221 | - cpu_seconds += like.celapse() | |
| 222 | - | |
| 223 | - # Show total times | |
| 224 | - sys.stdout.write("Total wall time elapsed: "+str(wall_seconds)+" seconds\n") | |
| 225 | - sys.stdout.write("Total CPU time used ...: "+str(cpu_seconds)+" seconds\n") | |
| 226 | - | |
| 227 | - # Show model fitting results | |
| 228 | - #sys.stdout.write(like.obs().models()+"\n") | |
| 229 | - | |
| 230 | - # Plot counts | |
| 231 | - if has_matplotlib: | |
| 232 | - plot_counts(bin.obs()) | |
| 233 | - else: | |
| 234 | - sys.stdout.write("Matplotlib is not (correctly) installed on your system. No counts spectra are shown.\n") | |
| 235 | - | |
| 236 | - # Return | |
| 237 | - return | |
| 238 | - | |
| 239 | - | |
| 240 | -# =========== # | |
| 241 | -# Plot counts # | |
| 242 | -# =========== # | |
| 243 | -def plot_counts(observations): | |
| 244 | - """ | |
| 245 | - Plot counts. | |
| 246 | - """ | |
| 247 | - # Set legend fontsize | |
| 248 | - params = {'legend.fontsize': 10} | |
| 249 | - plt.rcParams.update(params) | |
| 250 | - | |
| 251 | - # Set plot styles | |
| 252 | - styles = ['b-', 'g-', 'y-', 'n-'] | |
| 253 | - | |
| 254 | - # Dump header | |
| 255 | - sys.stdout.write("\n") | |
| 256 | - sys.stdout.write("Make plots (using matplotlib):\n") | |
| 257 | - sys.stdout.write("==============================\n") | |
| 258 | - | |
| 259 | - # Create figure 1 | |
| 260 | - plt.figure(1,figsize=(12,6)) | |
| 261 | - plt.subplots_adjust(hspace=.7) | |
| 262 | - | |
| 263 | - # Create subplot 1 | |
| 264 | - plt.subplot(121) | |
| 265 | - plt.title("Spectrum (summed over all pixels)") | |
| 266 | - | |
| 267 | - # Loop over observations | |
| 268 | - for obs in observations: | |
| 269 | - | |
| 270 | - # Get event cube | |
| 271 | - cube = obs.events() | |
| 272 | - | |
| 273 | - # Create energy axis | |
| 274 | - energy = [] | |
| 275 | - ebounds = cube.ebounds() | |
| 276 | - for i in range(ebounds.size()): | |
| 277 | - energy.append(ebounds.elogmean(i).TeV()) | |
| 278 | - | |
| 279 | - # Create spectrum | |
| 280 | - sys.stdout.write("Extract data:\n") | |
| 281 | - counts = [0.0 for i in range(ebounds.size())] | |
| 282 | - for bin in cube: | |
| 283 | - index = ebounds.index(bin.energy()) | |
| 284 | - counts[index] = counts[index] + bin.counts() | |
| 285 | - | |
| 286 | - # Create error bars | |
| 287 | - error = [math.sqrt(c) for c in counts] | |
| 288 | - | |
| 289 | - # Plot spectrum | |
| 290 | - plt.loglog(energy, counts, 'ro', label='data') | |
| 291 | - plt.errorbar(energy, counts, error, fmt=None, ecolor='r') | |
| 292 | - | |
| 293 | - # Extract models | |
| 294 | - sys.stdout.write("Extract models:\n") | |
| 295 | - sum_model = [0.0 for i in range(ebounds.size())] | |
| 296 | - for k, m in enumerate(observations.models()): | |
| 297 | - sys.stdout.write("- "+m.name()+"\n") | |
| 298 | - model = [0.0 for i in range(ebounds.size())] | |
| 299 | - for bin in cube: | |
| 300 | - index = ebounds.index(bin.energy()) | |
| 301 | - prob = m.eval(bin, obs) | |
| 302 | - model[index] = model[index] + prob * bin.size() | |
| 303 | - for i in range(ebounds.size()): | |
| 304 | - sum_model[i] = sum_model[i] + model[i] | |
| 305 | - plt.loglog(energy, model, styles[k], label=m.name()) | |
| 306 | - plt.loglog(energy, sum_model, 'r-', label='total') | |
| 307 | - | |
| 308 | - # Put labels | |
| 309 | - plt.xlabel("Energy (TeV)") | |
| 310 | - plt.ylabel("Counts") | |
| 311 | - plt.legend(loc="lower left") | |
| 312 | - | |
| 313 | - # Create subplot 2 | |
| 314 | - plt.subplot(122) | |
| 315 | - plt.title("Offset (summed over all energies)") | |
| 316 | - | |
| 317 | - # Set Crab direction | |
| 318 | - crab = gammalib.GSkyDir() | |
| 319 | - crab.radec_deg(83.63, 22.01) | |
| 320 | - | |
| 321 | - # Loop over observations | |
| 322 | - for obs in observations: | |
| 323 | - | |
| 324 | - # Get event cube | |
| 325 | - cube = obs.events() | |
| 326 | - | |
| 327 | - # Create offset histogram | |
| 328 | - sys.stdout.write("Extract data:\n") | |
| 329 | - nx = 30 | |
| 330 | - doffset2 = 0.01 | |
| 331 | - offset2 = [(i+0.5)*doffset2 for i in range(nx)] | |
| 332 | - counts = [0.0 for i in range(nx)] | |
| 333 | - for bin in cube: | |
| 334 | - off = gammalib.GCTAInstDir(bin.dir()).dir().dist_deg(crab) | |
| 335 | - off2 = off*off | |
| 336 | - index = int(off2/doffset2) | |
| 337 | - if index < nx: | |
| 338 | - counts[index] = counts[index] + bin.counts() | |
| 339 | - | |
| 340 | - # Create error bars | |
| 341 | - error = [math.sqrt(c) for c in counts] | |
| 342 | - | |
| 343 | - # Plot distribution | |
| 344 | - plt.semilogy(offset2, counts, 'ro', label='data') | |
| 345 | - | |
| 346 | - # Extract models | |
| 347 | - sys.stdout.write("Extract models:\n") | |
| 348 | - sum_model = [0.0 for i in range(nx)] | |
| 349 | - for k, m in enumerate(observations.models()): | |
| 350 | - sys.stdout.write("- "+m.name()+"\n") | |
| 351 | - model = [0.0 for i in range(nx)] | |
| 352 | - for bin in cube: | |
| 353 | - off = gammalib.GCTAInstDir(bin.dir()).dir().dist_deg(crab) | |
| 354 | - off2 = off*off | |
| 355 | - index = int(off2/doffset2) | |
| 356 | - if index < nx: | |
| 357 | - prob = m.eval(bin, obs) | |
| 358 | - model[index] = model[index] + prob * bin.size() | |
| 359 | - for i in range(nx): | |
| 360 | - sum_model[i] = sum_model[i] + model[i] | |
| 361 | - plt.plot(offset2, model, styles[k], label=m.name()) | |
| 362 | - plt.plot(offset2, sum_model, 'r-', label='total') | |
| 363 | - plt.ylim(ymin=0.1) | |
| 364 | - | |
| 365 | - # Put labels | |
| 366 | - plt.xlabel("Offset (deg^2)") | |
| 367 | - plt.ylabel("Counts") | |
| 368 | - plt.legend(loc="upper right") | |
| 369 | - | |
| 370 | - # Show counts spectra | |
| 371 | - plt.show() | |
| 372 | - | |
| 373 | - # Return | |
| 374 | - return | |
| 375 | - | |
| 376 | - | |
| 377 | -#==========================# | |
| 378 | -# Main routine entry point # | |
| 379 | -#==========================# | |
| 380 | -if __name__ == '__main__': | |
| 381 | - """ | |
| 382 | - CTA binned analysis script. | |
| 383 | - | |
| 384 | - This script implements the analysis steps to perform a binned analysis. | |
| 385 | - The steps include: | |
| 386 | - - Event simulation | |
| 387 | - - Event binning | |
| 388 | - - Binned maximum likelihood fitting | |
| 389 | - | |
| 390 | - Two variations of the script are implemented. One that calls the classes | |
| 391 | - like executables, storing the intermediate results on disk, and a second | |
| 392 | - that uses the results of the precedent class directly without storing | |
| 393 | - any data on disk. | |
| 394 | - | |
| 395 | - The second variation will also show a counts spectrum of the data after | |
| 396 | - fitting (only if matplotlib is available on your system). Note that the | |
| 397 | - counts spectrum is summed over the entire data space, i.e. no event selection | |
| 398 | - around the Crab has been performed. | |
| 399 | - """ | |
| 400 | - # Dump header | |
| 401 | - sys.stdout.write("*************************************\n") | |
| 402 | - sys.stdout.write("* CTA binned analysis scripts *\n") | |
| 403 | - sys.stdout.write("*************************************\n") | |
| 404 | - sys.stdout.write("... this script will take 1-2 minutes\n") | |
| 405 | - | |
| 406 | - # Remove any existing result files | |
| 407 | - list = [glob.glob("*.fits"), glob.glob("*.log"), glob.glob("*.xml")] | |
| 408 | - for files in list: | |
| 409 | - for file in files: | |
| 410 | - os.remove(file) | |
| 411 | - | |
| 412 | - # Save intermediate results on disk | |
| 413 | - sys.stdout.write("\n") | |
| 414 | - sys.stdout.write("Executable analysis pipeline:\n") | |
| 415 | - sys.stdout.write("=============================\n") | |
| 416 | - pipeline_v1() | |
| 417 | - | |
| 418 | - # Do analysis in memory | |
| 419 | - sys.stdout.write("\n") | |
| 420 | - sys.stdout.write("In memory analysis pipeline:\n") | |
| 421 | - sys.stdout.write("============================\n") | |
| 422 | - pipeline_v2() | |
| 423 | - sys.stdout.write("\n") |
examples/make_cube_analysis.py deleted
| 1 | -#! /usr/bin/env python | |
| 2 | -# ========================================================================== | |
| 3 | -# This script illustrates how to perform a stacked cube analysis. | |
| 4 | -# | |
| 5 | -# Copyright (C) 2014 Juergen Knoedlseder | |
| 6 | -# | |
| 7 | -# This program is free software: you can redistribute it and/or modify | |
| 8 | -# it under the terms of the GNU General Public License as published by | |
| 9 | -# the Free Software Foundation, either version 3 of the License, or | |
| 10 | -# (at your option) any later version. | |
| 11 | -# | |
| 12 | -# This program is distributed in the hope that it will be useful, | |
| 13 | -# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 14 | -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 15 | -# GNU General Public License for more details. | |
| 16 | -# | |
| 17 | -# You should have received a copy of the GNU General Public License | |
| 18 | -# along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 19 | -# | |
| 20 | -# ========================================================================== | |
| 21 | -import ctools | |
| 22 | -import gammalib | |
| 23 | -from ctools import obsutils | |
| 24 | - | |
| 25 | - | |
| 26 | -# ================= # | |
| 27 | -# Analysis pipeline # | |
| 28 | -# ================= # | |
| 29 | -def pipeline(caldb = "dummy", \ | |
| 30 | - irf = "cta_dummy_irf", \ | |
| 31 | - model = "$CTOOLS/share/models/crab.xml"): | |
| 32 | - """ | |
| 33 | - Analysis pipeline for a stacked cube analysis. The pipeline writes | |
| 34 | - intermediate results to FITS files on disk. Also log files of all | |
| 35 | - individual ctools will be written. | |
| 36 | - | |
| 37 | - Keywords: | |
| 38 | - caldb - Calibration database repository | |
| 39 | - irf - Name of instrument response | |
| 40 | - model - Source model | |
| 41 | - """ | |
| 42 | - # Set pointing pattern (4 pointings) | |
| 43 | - pointings = obsutils.set_obs_patterns("four", offset=2.0) | |
| 44 | - | |
| 45 | - # Set CTA observations (default: 1800 sec per pointing) | |
| 46 | - obs = obsutils.set_obs_list(pointings, caldb=caldb, irf=irf) | |
| 47 | - | |
| 48 | - # Load models | |
| 49 | - models = gammalib.GModels(model) | |
| 50 | - obs.models(models) | |
| 51 | - | |
| 52 | - # Run ctobssim (simulates 4 event lists) | |
| 53 | - sim = ctools.ctobssim(obs) | |
| 54 | - sim.logFileOpen() | |
| 55 | - sim.run() | |
| 56 | - | |
| 57 | - # Run ctbin (builds stacked event cube) | |
| 58 | - bin = ctools.ctbin(sim.obs()) | |
| 59 | - bin.logFileOpen() | |
| 60 | - bin["outcube"].filename("cntmap.fits") | |
| 61 | - bin["ebinalg"].string("LOG") | |
| 62 | - bin["emin"].real(0.1) | |
| 63 | - bin["emax"].real(100.0) | |
| 64 | - bin["enumbins"].integer(20) | |
| 65 | - bin["usepnt"].boolean(False) | |
| 66 | - bin["nxpix"].integer(300) | |
| 67 | - bin["nypix"].integer(300) | |
| 68 | - bin["binsz"].real(0.02) | |
| 69 | - bin["coordsys"].string("CEL") | |
| 70 | - bin["proj"].string("CAR") | |
| 71 | - bin["xref"].real(83.6331) | |
| 72 | - bin["yref"].real(22.0145) | |
| 73 | - bin.execute() | |
| 74 | - | |
| 75 | - # Run ctexpcube (builds exposure cube) | |
| 76 | - expcube = ctools.ctexpcube(sim.obs()) | |
| 77 | - expcube.logFileOpen() | |
| 78 | - expcube["incube"].filename("cntmap.fits") | |
| 79 | - expcube["outcube"].filename("expcube.fits") | |
| 80 | - expcube["caldb"].string(caldb) | |
| 81 | - expcube["irf"].string(irf) | |
| 82 | - expcube.execute() | |
| 83 | - | |
| 84 | - # Run ctpsfcube (builds PSF cube) | |
| 85 | - psfcube = ctools.ctpsfcube(sim.obs()) | |
| 86 | - psfcube.logFileOpen() | |
| 87 | - psfcube["incube"].filename("NONE") | |
| 88 | - psfcube["outcube"].filename("psfcube.fits") | |
| 89 | - psfcube["caldb"].string(caldb) | |
| 90 | - psfcube["irf"].string(irf) | |
| 91 | - psfcube["ebinalg"].string("LOG") | |
| 92 | - psfcube["emin"].real(0.1) | |
| 93 | - psfcube["emax"].real(100.0) | |
| 94 | - psfcube["enumbins"].integer(20) | |
| 95 | - psfcube["nxpix"].integer(5) | |
| 96 | - psfcube["nypix"].integer(5) | |
| 97 | - psfcube["binsz"].real(2.0) | |
| 98 | - psfcube["coordsys"].string("CEL") | |
| 99 | - psfcube["proj"].string("CAR") | |
| 100 | - psfcube["xref"].real(83.6331) | |
| 101 | - psfcube["yref"].real(22.0145) | |
| 102 | - psfcube["amax"].real(0.3) | |
| 103 | - psfcube["anumbins"].integer(60) | |
| 104 | - psfcube.execute() | |
| 105 | - | |
| 106 | - # Run ctbkgcube (builds background cube) | |
| 107 | - bkgcube = ctools.ctbkgcube(sim.obs()) | |
| 108 | - bkgcube.logFileOpen() | |
| 109 | - bkgcube["incube"].filename("cntmap.fits") | |
| 110 | - bkgcube["inmodel"].filename(model) | |
| 111 | - bkgcube["outcube"].filename("bkgcube.fits") | |
| 112 | - bkgcube["outmodel"].filename("bkgcube.xml") | |
| 113 | - bkgcube.execute() | |
| 114 | - | |
| 115 | - # Create observation container for stacked cube analysis | |
| 116 | - obs.clear() | |
| 117 | - cta = gammalib.GCTAObservation("cntmap.fits", "expcube.fits", "psfcube.fits") | |
| 118 | - obs.append(cta) | |
| 119 | - obs.models(gammalib.GModels("bkgcube.xml")) | |
| 120 | - | |
| 121 | - # Run ctlike | |
| 122 | - like = ctools.ctlike(obs) | |
| 123 | - like.logFileOpen() | |
| 124 | - like["debug"].boolean(True) | |
| 125 | - like["outmodel"].filename("cube_results.xml") | |
| 126 | - like.execute() | |
| 127 | - | |
| 128 | - # Return | |
| 129 | - return | |
| 130 | - | |
| 131 | - | |
| 132 | -#==========================# | |
| 133 | -# Main routine entry point # | |
| 134 | -#==========================# | |
| 135 | -if __name__ == '__main__': | |
| 136 | - """ | |
| 137 | - """ | |
| 138 | - # Dump header | |
| 139 | - print("*************************************") | |
| 140 | - print("* CTA stacked cube analysis *") | |
| 141 | - print("*************************************") | |
| 142 | - | |
| 143 | - # Run pipeline | |
| 144 | - pipeline() | |
| 145 | - | |
| 146 | 0 | \ No newline at end of file |
examples/make_unbinned_analysis.py deleted
| 1 | -#! /usr/bin/env python | |
| 2 | -# ========================================================================== | |
| 3 | -# This script illustrates how to build an unbinned analysis pipeline with | |
| 4 | -# the ctools. If matplotlib is installed, the spectrum will be displayed | |
| 5 | -# on the screen. | |
| 6 | -# | |
| 7 | -# Required 3rd party modules: | |
| 8 | -# None | |
| 9 | -# | |
| 10 | -# Copyright (C) 2011-2015 Jurgen Knodlseder | |
| 11 | -# | |
| 12 | -# This program is free software: you can redistribute it and/or modify | |
| 13 | -# it under the terms of the GNU General Public License as published by | |
| 14 | -# the Free Software Foundation, either version 3 of the License, or | |
| 15 | -# (at your option) any later version. | |
| 16 | -# | |
| 17 | -# This program is distributed in the hope that it will be useful, | |
| 18 | -# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 19 | -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 20 | -# GNU General Public License for more details. | |
| 21 | -# | |
| 22 | -# You should have received a copy of the GNU General Public License | |
| 23 | -# along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 24 | -# | |
| 25 | -# ========================================================================== | |
| 26 | -import gammalib | |
| 27 | -import ctools | |
| 28 | -import math | |
| 29 | -import os | |
| 30 | -import glob | |
| 31 | -import sys | |
| 32 | -try: | |
| 33 | - import matplotlib.pyplot as plt | |
| 34 | - has_matplotlib = True | |
| 35 | -except: | |
| 36 | - has_matplotlib = False | |
| 37 | - | |
| 38 | - | |
| 39 | -# ============================= # | |
| 40 | -# Analysis pipeline - version 1 # | |
| 41 | -# ============================= # | |
| 42 | -def pipeline_v1(): | |
| 43 | - """ | |
| 44 | - Unbinned analysis pipeline - save intermediate results. | |
| 45 | - | |
| 46 | - This function implements an analysis pipeline that successively calls | |
| 47 | - ctobssim, ctselect and ctlike by saving the intermediate results as FITS | |
| 48 | - files on disk. This replicates the way how the analysis would be done | |
| 49 | - in a ftools-like approach. | |
| 50 | - """ | |
| 51 | - # Set script parameters | |
| 52 | - model_name = "${CTOOLS}/share/models/crab.xml" | |
| 53 | - events_name = "events.fits" | |
| 54 | - selected_events_name = "selected_events.fits" | |
| 55 | - result_name = "results.xml" | |
| 56 | - caldb = "${CALDB}/data/cta/dummy/bcf" | |
| 57 | - irf = "cta_dummy_irf" | |
| 58 | - ra = 83.63 | |
| 59 | - dec = 22.01 | |
| 60 | - rad_sim = 10.0 | |
| 61 | - tstart = 0.0 | |
| 62 | - tstop = 1800.0 | |
| 63 | - emin = 0.1 | |
| 64 | - emax = 100.0 | |
| 65 | - rad_select = 3.0 | |
| 66 | - | |
| 67 | - # Initialise timing | |
| 68 | - wall_seconds = 0.0 | |
| 69 | - cpu_seconds = 0.0 | |
| 70 | - | |
| 71 | - # Simulate events | |
| 72 | - sim = ctools.ctobssim() | |
| 73 | - sim.logFileOpen() | |
| 74 | - sim["inmodel"].filename(model_name) | |
| 75 | - sim["outevents"].filename(events_name) | |
| 76 | - sim["caldb"].string(caldb) | |
| 77 | - sim["irf"].string(irf) | |
| 78 | - sim["ra"].real(ra) | |
| 79 | - sim["dec"].real(dec) | |
| 80 | - sim["rad"].real(rad_sim) | |
| 81 | - sim["tmin"].real(tstart) | |
| 82 | - sim["tmax"].real(tstop) | |
| 83 | - sim["emin"].real(emin) | |
| 84 | - sim["emax"].real(emax) | |
| 85 | - sim.execute() | |
| 86 | - print("Simulated events ("+str(sim.celapse())+" CPU seconds)") | |
| 87 | - | |
| 88 | - # Update timing | |
| 89 | - wall_seconds += sim.telapse() | |
| 90 | - cpu_seconds += sim.celapse() | |
| 91 | - | |
| 92 | - # Select events | |
| 93 | - select = ctools.ctselect() | |
| 94 | - select.logFileOpen() | |
| 95 | - select["inobs"].filename(events_name) | |
| 96 | - select["outobs"].filename(selected_events_name) | |
| 97 | - select["ra"].real(ra) | |
| 98 | - select["dec"].real(dec) | |
| 99 | - select["rad"].real(rad_select) | |
| 100 | - select["tmin"].real(tstart) | |
| 101 | - select["tmax"].real(tstop) | |
| 102 | - select["emin"].real(emin) | |
| 103 | - select["emax"].real(emax) | |
| 104 | - select.execute() | |
| 105 | - print("Select event ("+str(select.celapse())+" CPU seconds)") | |
| 106 | - | |
| 107 | - # Update timing | |
| 108 | - wall_seconds += select.telapse() | |
| 109 | - cpu_seconds += select.celapse() | |
| 110 | - | |
| 111 | - # Perform maximum likelihood fitting | |
| 112 | - like = ctools.ctlike() | |
| 113 | - like.logFileOpen() | |
| 114 | - like["inobs"].filename(selected_events_name) | |
| 115 | - like["inmodel"].filename(model_name) | |
| 116 | - like["outmodel"].filename(result_name) | |
| 117 | - like["caldb"].string(caldb) | |
| 118 | - like["irf"].string(irf) | |
| 119 | - like.execute() | |
| 120 | - print("Maximum likelihood fitting ("+str(like.celapse())+" CPU seconds)") | |
| 121 | - | |
| 122 | - # Update timing | |
| 123 | - wall_seconds += like.telapse() | |
| 124 | - cpu_seconds += like.celapse() | |
| 125 | - | |
| 126 | - # Show total times | |
| 127 | - print("Total wall time elapsed: "+str(wall_seconds)+" seconds") | |
| 128 | - print("Total CPU time used ...: "+str(cpu_seconds)+" seconds") | |
| 129 | - | |
| 130 | - # Return | |
| 131 | - return | |
| 132 | - | |
| 133 | - | |
| 134 | -# ============================= # | |
| 135 | -# Analysis pipeline - version 2 # | |
| 136 | -# ============================= # | |
| 137 | -def pipeline_v2(show_data): | |
| 138 | - """ | |
| 139 | - Unbinned analysis pipeline - keep intermediate results in memory. | |
| 140 | - | |
| 141 | - This function implements an analysis pipeline that successively calls | |
| 142 | - ctobssim, ctselect and ctlike without saving the intermediate results as | |
| 143 | - FITS files on disk. All data is only hold in memory. | |
| 144 | - """ | |
| 145 | - # Set script parameters | |
| 146 | - model_name = "${CTOOLS}/share/models/crab.xml" | |
| 147 | - caldb = "${CALDB}/data/cta/dummy/bcf" | |
| 148 | - irf = "cta_dummy_irf" | |
| 149 | - ra = 83.63 | |
| 150 | - dec = 22.01 | |
| 151 | - rad_sim = 10.0 | |
| 152 | - tstart = 0.0 | |
| 153 | - tstop = 1800.0 | |
| 154 | - emin = 0.1 | |
| 155 | - emax = 100.0 | |
| 156 | - rad_select = 3.0 | |
| 157 | - | |
| 158 | - # Initialise timing | |
| 159 | - wall_seconds = 0.0 | |
| 160 | - cpu_seconds = 0.0 | |
| 161 | - | |
| 162 | - # Simulate events | |
| 163 | - sim = ctools.ctobssim() | |
| 164 | - sim["inmodel"].filename(model_name) | |
| 165 | - sim["caldb"].string(caldb) | |
| 166 | - sim["irf"].string(irf) | |
| 167 | - sim["ra"].real(ra) | |
| 168 | - sim["dec"].real(dec) | |
| 169 | - sim["rad"].real(rad_sim) | |
| 170 | - sim["tmin"].real(tstart) | |
| 171 | - sim["tmax"].real(tstop) | |
| 172 | - sim["emin"].real(emin) | |
| 173 | - sim["emax"].real(emax) | |
| 174 | - sim.run() | |
| 175 | - print("Simulated events ("+str(sim.celapse())+" CPU seconds)") | |
| 176 | - | |
| 177 | - # Update timing | |
| 178 | - wall_seconds += sim.telapse() | |
| 179 | - cpu_seconds += sim.celapse() | |
| 180 | - | |
| 181 | - # Select events | |
| 182 | - select = ctools.ctselect(sim.obs()) | |
| 183 | - select["ra"].real(ra) | |
| 184 | - select["dec"].real(dec) | |
| 185 | - select["rad"].real(rad_select) | |
| 186 | - select["tmin"].real(tstart) | |
| 187 | - select["tmax"].real(tstop) | |
| 188 | - select["emin"].real(emin) | |
| 189 | - select["emax"].real(emax) | |
| 190 | - select.run() | |
| 191 | - print("Select event ("+str(select.celapse())+" CPU seconds)") | |
| 192 | - | |
| 193 | - # Update timing | |
| 194 | - wall_seconds += select.telapse() | |
| 195 | - cpu_seconds += select.celapse() | |
| 196 | - | |
| 197 | - # Perform maximum likelihood fitting | |
| 198 | - like = ctools.ctlike(select.obs()) | |
| 199 | - like.run() | |
| 200 | - print("Maximum likelihood fitting ("+str(like.celapse())+" CPU seconds)") | |
| 201 | - | |
| 202 | - # Update timing | |
| 203 | - wall_seconds += like.telapse() | |
| 204 | - cpu_seconds += like.celapse() | |
| 205 | - | |
| 206 | - # Show total times | |
| 207 | - print("Total wall time elapsed: "+str(wall_seconds)+" seconds") | |
| 208 | - print("Total CPU time used ...: "+str(cpu_seconds)+" seconds") | |
| 209 | - | |
| 210 | - # Optionally plot counts | |
| 211 | - if show_data: | |
| 212 | - if has_matplotlib: | |
| 213 | - plot_counts(like.obs()) | |
| 214 | - else: | |
| 215 | - sys.stdout.write("Matplotlib is not (correctly) installed on your system. No counts spectra are shown.\n") | |
| 216 | - | |
| 217 | - # Return | |
| 218 | - return | |
| 219 | - | |
| 220 | - | |
| 221 | -# =========== # | |
| 222 | -# Plot counts # | |
| 223 | -# =========== # | |
| 224 | -def plot_counts(observations): | |
| 225 | - """ | |
| 226 | - Plot counts. | |
| 227 | - """ | |
| 228 | - # Set legend fontsize | |
| 229 | - params = {'legend.fontsize': 10} | |
| 230 | - plt.rcParams.update(params) | |
| 231 | - | |
| 232 | - # Set plot styles | |
| 233 | - styles = ['b-', 'g-', 'y-', 'n-'] | |
| 234 | - | |
| 235 | - # Dump header | |
| 236 | - print("") | |
| 237 | - print("Make plots (using matplotlib):") | |
| 238 | - print("==============================") | |
| 239 | - | |
| 240 | - # Create figure 1 | |
| 241 | - plt.figure(1,figsize=(12,6)) | |
| 242 | - plt.subplots_adjust(hspace=.7) | |
| 243 | - | |
| 244 | - # Create subplot 1 | |
| 245 | - plt.subplot(121) | |
| 246 | - plt.title("Spectrum (summed over all pixels)") | |
| 247 | - | |
| 248 | - # Loop over observations | |
| 249 | - for obs in observations: | |
| 250 | - | |
| 251 | - # Get event list | |
| 252 | - list = obs.events() | |
| 253 | - | |
| 254 | - # Create energy axis | |
| 255 | - ebounds = gammalib.GEbounds(10, gammalib.GEnergy(0.1, "TeV"), gammalib.GEnergy(100.0, "TeV")) | |
| 256 | - energy = [ebounds.elogmean(i).TeV() for i in range(ebounds.size())] | |
| 257 | - | |
| 258 | - # Create spectrum | |
| 259 | - print("Extract data:") | |
| 260 | - counts = [0.0 for i in range(ebounds.size())] | |
| 261 | - print(list.size()) | |
| 262 | - for atom in list: | |
| 263 | - index = ebounds.index(atom.energy()) | |
| 264 | - counts[index] = counts[index] + 1.0 | |
| 265 | - | |
| 266 | - # Create error bars | |
| 267 | - error = [math.sqrt(c) for c in counts] | |
| 268 | - | |
| 269 | - # Plot spectrum | |
| 270 | - plt.loglog(energy, counts, 'ro', label='data') | |
| 271 | - #plt.errorbar(energy, counts, error, fmt=None, ecolor='r') | |
| 272 | - | |
| 273 | - # Extract models | |
| 274 | - #print("Extract models:") | |
| 275 | - #sum_model = [0.0 for i in range(ebounds.size())] | |
| 276 | - #for k, m in enumerate(observations.models()): | |
| 277 | - # print("- "+m.name()) | |
| 278 | - # model = [0.0 for i in range(ebounds.size())] | |
| 279 | - # for atom in list: | |
| 280 | - # index = ebounds.index(atom.energy()) | |
| 281 | - # prob = m.eval(atom, obs) | |
| 282 | - # model[index] = model[index] + prob * atom.size() | |
| 283 | - # for i in range(ebounds.size()): | |
| 284 | - # sum_model[i] = sum_model[i] + model[i] | |
| 285 | - # plt.loglog(energy, model, styles[k], label=m.name()) | |
| 286 | - #plt.loglog(energy, sum_model, 'r-', label='total') | |
| 287 | - | |
| 288 | - # Put labels | |
| 289 | - plt.xlabel("Energy (TeV)") | |
| 290 | - plt.ylabel("Counts") | |
| 291 | - plt.legend(loc="lower left") | |
| 292 | - | |
| 293 | - # Create subplot 2 | |
| 294 | - plt.subplot(122) | |
| 295 | - plt.title("Offset (summed over all energies)") | |
| 296 | - | |
| 297 | - # Set Crab direction | |
| 298 | - crab = gammalib.GSkyDir() | |
| 299 | - crab.radec_deg(83.63, 22.01) | |
| 300 | - | |
| 301 | - # Loop over observations | |
| 302 | - for obs in observations: | |
| 303 | - | |
| 304 | - # Get event cube | |
| 305 | - list = obs.events() | |
| 306 | - | |
| 307 | - # Create offset histogram | |
| 308 | - print "Extract data:" | |
| 309 | - nx = 30 | |
| 310 | - doffset2 = 0.01 | |
| 311 | - offset2 = [(i+0.5)*doffset2 for i in range(nx)] | |
| 312 | - counts = [0.0 for i in range(nx)] | |
| 313 | - for atom in list: | |
| 314 | - off = gammalib.GCTAInstDir(atom.dir()).dir().dist_deg(crab) | |
| 315 | - off2 = off*off | |
| 316 | - index = int(off2/doffset2) | |
| 317 | - if index < nx: | |
| 318 | - counts[index] = counts[index] + 1.0 | |
| 319 | - | |
| 320 | - # Create error bars | |
| 321 | - error = [math.sqrt(c) for c in counts] | |
| 322 | - | |
| 323 | - # Plot distribution | |
| 324 | - plt.semilogy(offset2, counts, 'ro', label='data') | |
| 325 | - | |
| 326 | - # Extract models | |
| 327 | - print "Extract models:" | |
| 328 | - sum_model = [0.0 for i in range(nx)] | |
| 329 | - for k, m in enumerate(observations.models()): | |
| 330 | - print "- "+m.name() | |
| 331 | - model = [0.0 for i in range(nx)] | |
| 332 | - for atom in list: | |
| 333 | - off = gammalib.GCTAInstDir(atom.dir()).dir().dist_deg(crab) | |
| 334 | - off2 = off*off | |
| 335 | - index = int(off2/doffset2) | |
| 336 | - if index < nx: | |
| 337 | - prob = m.eval(atom, obs) | |
| 338 | - model[index] = model[index] + prob * atom.size() | |
| 339 | - for i in range(nx): | |
| 340 | - sum_model[i] = sum_model[i] + model[i] | |
| 341 | - #plt.plot(offset2, model, styles[k], label=m.name()) | |
| 342 | - #plt.plot(offset2, sum_model, 'r-', label='total') | |
| 343 | - #plt.ylim(ymin=0.1) | |
| 344 | - | |
| 345 | - # Put labels | |
| 346 | - plt.xlabel("Offset (deg^2)") | |
| 347 | - plt.ylabel("Counts") | |
| 348 | - plt.legend(loc="upper right") | |
| 349 | - | |
| 350 | - # Show counts spectra | |
| 351 | - plt.show() | |
| 352 | - | |
| 353 | - # Return | |
| 354 | - return | |
| 355 | - | |
| 356 | - | |
| 357 | -#==========================# | |
| 358 | -# Main routine entry point # | |
| 359 | -#==========================# | |
| 360 | -if __name__ == '__main__': | |
| 361 | - """ | |
| 362 | - CTA unbinned analysis script. | |
| 363 | - | |
| 364 | - This script implements the analysis steps to perform an unbinned analysis. | |
| 365 | - The steps include: | |
| 366 | - - Event simulation | |
| 367 | - - Event selection | |
| 368 | - - Binned maximum likelihood fitting | |
| 369 | - | |
| 370 | - Two variations of the script are implemented. One that calls the classes | |
| 371 | - like executables, storing the intermediate results on disk, and a second | |
| 372 | - that uses the results of the precedent class directly without storing | |
| 373 | - any data on disk. | |
| 374 | - """ | |
| 375 | - # Initialise flags | |
| 376 | - need_help = False | |
| 377 | - show_data = True | |
| 378 | - | |
| 379 | - # Test for command line arguments | |
| 380 | - print sys.argv[0] | |
| 381 | - if (len(sys.argv) > 1): | |
| 382 | - if sys.argv[1] == "-h": | |
| 383 | - need_help = True | |
| 384 | - elif sys.argv[1] == "-noshow": | |
| 385 | - show_data = False | |
| 386 | - else: | |
| 387 | - need_help = True | |
| 388 | - | |
| 389 | - # Print help if needed and exit | |
| 390 | - if need_help: | |
| 391 | - print "Usage: example_binned.py [OPTIONS]" | |
| 392 | - print " -h Display this usage message" | |
| 393 | - print " -noshow Do not show data using matplotlib" | |
| 394 | - sys.exit() | |
| 395 | - | |
| 396 | - # Dump header | |
| 397 | - print "*********************************" | |
| 398 | - print "* CTA unbinned analysis scripts *" | |
| 399 | - print "*********************************" | |
| 400 | - | |
| 401 | - # Remove any existing result files | |
| 402 | - list = [glob.glob("*.fits"), glob.glob("*.log"), glob.glob("*.xml")] | |
| 403 | - for files in list: | |
| 404 | - for file in files: | |
| 405 | - os.remove(file) | |
| 406 | - | |
| 407 | - # Save intermediate results on disk | |
| 408 | - print "" | |
| 409 | - print "Executable analysis pipeline:" | |
| 410 | - print "=============================" | |
| 411 | - pipeline_v1() | |
| 412 | - | |
| 413 | - # Do analysis in memory | |
| 414 | - print "" | |
| 415 | - print "In memory analysis pipeline:" | |
| 416 | - print "============================" | |
| 417 | - pipeline_v2(show_data) | |
| 418 | - print "" |