Commit 6480558d92ecf2bdaf85a23dbe5e3cd5ea8e68c8

Authored by Jürgen Knödlseder
1 parent cbe8c747

Add show_sensitivity.py script.

examples/README
... ... @@ -69,7 +69,10 @@ show_pull_histogram.py
69 69 Displays pull histograms generated by the cspull script.
70 70  
71 71 show_response.py
72   - Display one or several CTA response functions.
  72 + Displays one or several CTA response functions.
  73 +
  74 +show_sensitivity.py
  75 + Displays sensitivity information generated by the cssens script.
73 76  
74 77 show_ts_distribution.py
75 78 Displays Test Statistic histograms generated using the cstsdist
... ...
examples/show_sensitivity.py 0 → 100644
  1 +#! /usr/bin/env python
  2 +# ==========================================================================
  3 +# This script displays a differential sensitivity plot generated by cssens.
  4 +#
  5 +# Copyright (C) 2015 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 sys
  22 +import math
  23 +import gammalib
  24 +try:
  25 + import matplotlib.pyplot as plt
  26 +except:
  27 + sys.exit("This script needs matplotlib")
  28 +
  29 +
  30 +# ===================== #
  31 +# Read sensitivity data #
  32 +# ===================== #
  33 +def read_sensitivity(filename):
  34 + """
  35 + Read sensitivity information from CSV file.
  36 + """
  37 + # Read filename
  38 + csv = gammalib.GCsv(filename,',')
  39 +
  40 + # Create dictionnary
  41 + sensitivity = {}
  42 + for column in range(csv.ncols()):
  43 + name = csv[0,column]
  44 + values = []
  45 + for row in range(csv.nrows()-1):
  46 + values.append(float(csv[row+1,column]))
  47 + sensitivity[name] = values
  48 +
  49 + # Add linear energy values
  50 + if sensitivity.has_key("loge"):
  51 + name = 'energy'
  52 + values = []
  53 + for value in sensitivity["loge"]:
  54 + values.append(math.pow(10.0, value))
  55 + sensitivity[name] = values
  56 +
  57 + # Return
  58 + return sensitivity
  59 +
  60 +
  61 +# ===================== #
  62 +# Show sensitivity data #
  63 +# ===================== #
  64 +def show_sensitivity(sensitivity, title="Sensitivity"):
  65 + """
  66 + Plot sensitivity.
  67 + """
  68 + # Create figure
  69 + plt.figure()
  70 +
  71 + # Set plot attributes
  72 + plt.title(title)
  73 + plt.loglog()
  74 + plt.grid()
  75 +
  76 + # Show differential sensitivity
  77 + plt.plot(sensitivity['energy'], sensitivity['eflux'], 'ro-')
  78 +
  79 + # Set labels
  80 + plt.xlabel("Energy (TeV)")
  81 + plt.ylabel(r"Sensitivity (erg cm$^{-2}$ s$^{-1}$)")
  82 +
  83 + # Show plot
  84 + plt.show()
  85 +
  86 + # Return
  87 + return
  88 +
  89 +
  90 +# ======================== #
  91 +# Main routine entry point #
  92 +# ======================== #
  93 +if __name__ == '__main__':
  94 + """
  95 + """
  96 + # Print usage information
  97 + usage = "Usage: show_sensitivity [filename]"
  98 + if len(sys.argv) < 1:
  99 + sys.stdout.write(usage+"\n")
  100 + sys.exit()
  101 +
  102 + # Extract parameters
  103 + if len(sys.argv) == 1:
  104 + filename = 'sensitivity.dat'
  105 + else:
  106 + filename = sys.argv[1]
  107 +
  108 + # Set title
  109 + title = 'Differential sensitivity ('+filename+')'
  110 +
  111 + # Read sensitivity data
  112 + sensitivity = read_sensitivity(filename)
  113 +
  114 + # Show sensitivity data
  115 + show_sensitivity(sensitivity, title=title)
... ...