Commit 01e99588e101aa3629f455681093045833f7b6a3
1 parent
c5df4e57
initial version
Showing
1 changed file
with
115 additions
and
0 deletions
test/pull_histogram.py
0 → 100755
| 1 | +#! /usr/bin/env python | ||
| 2 | +# ========================================================================== | ||
| 3 | +# This script displays pull histograms of fit parameters. | ||
| 4 | +# | ||
| 5 | +# Copyright (C) 2011 Jurgen Knodlseder | ||
| 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 matplotlib.mlab as mlab | ||
| 22 | +import matplotlib.pyplot as plt | ||
| 23 | +import numpy as np | ||
| 24 | +import sys | ||
| 25 | +import csv | ||
| 26 | + | ||
| 27 | + | ||
| 28 | +# ====================== # | ||
| 29 | +# Read pull distribution # | ||
| 30 | +# ====================== # | ||
| 31 | +def read_pull(filename, parname): | ||
| 32 | + """ | ||
| 33 | + Read pull distribution. | ||
| 34 | + """ | ||
| 35 | + # Initialise list | ||
| 36 | + values = [] | ||
| 37 | + | ||
| 38 | + # Open reader | ||
| 39 | + reader = csv.reader(open(filename, 'r'), delimiter=',') | ||
| 40 | + | ||
| 41 | + # Read rows | ||
| 42 | + first = True | ||
| 43 | + index = -1 | ||
| 44 | + for row in reader: | ||
| 45 | + | ||
| 46 | + # Get column index if first row | ||
| 47 | + if first: | ||
| 48 | + try: | ||
| 49 | + index = row.index(parname) | ||
| 50 | + except: | ||
| 51 | + print 'ERROR: Parameter "'+parname+'" not found in list:' | ||
| 52 | + for p in row: | ||
| 53 | + print ' "'+p+'"' | ||
| 54 | + raise NameError(parname) | ||
| 55 | + #print index | ||
| 56 | + | ||
| 57 | + # Handle data rows | ||
| 58 | + else: | ||
| 59 | + values.append(float(row[index])) | ||
| 60 | + | ||
| 61 | + # Flag that first row has been passed | ||
| 62 | + first = False | ||
| 63 | + | ||
| 64 | + # Create numpy array | ||
| 65 | + a = np.array(values) | ||
| 66 | + #print a | ||
| 67 | + | ||
| 68 | + # Return array | ||
| 69 | + return a | ||
| 70 | + | ||
| 71 | + | ||
| 72 | +#==========================# | ||
| 73 | +# Main routine entry point # | ||
| 74 | +#==========================# | ||
| 75 | +if __name__ == '__main__': | ||
| 76 | + """ | ||
| 77 | + Display pull histograms using matplotlib. | ||
| 78 | + """ | ||
| 79 | + # Print usage information | ||
| 80 | + usage = "Usage: pull_histogram filename parname [bins]" | ||
| 81 | + if len(sys.argv) < 3: | ||
| 82 | + print usage | ||
| 83 | + sys.exit() | ||
| 84 | + | ||
| 85 | + # Extract parameters | ||
| 86 | + filename = sys.argv[1] | ||
| 87 | + parname = sys.argv[2] | ||
| 88 | + if len(sys.argv) > 3: | ||
| 89 | + nbins = int(sys.argv[3]) | ||
| 90 | + else: | ||
| 91 | + nbins = 50 | ||
| 92 | + | ||
| 93 | + # Read values from CSV file | ||
| 94 | + values = read_pull(filename, parname) | ||
| 95 | + | ||
| 96 | + # Create histogram | ||
| 97 | + n, bins, patches = plt.hist(values, nbins, range=[-4.0,4.0], | ||
| 98 | + normed=True, facecolor='green') | ||
| 99 | + | ||
| 100 | + # Create expected distribution | ||
| 101 | + y = mlab.normpdf(bins, 0.0, 1.0) | ||
| 102 | + l = plt.plot(bins, y, 'r-', linewidth=2) | ||
| 103 | + | ||
| 104 | + # Set parname for plotting | ||
| 105 | + name = parname[:-1] | ||
| 106 | + | ||
| 107 | + # Set plot | ||
| 108 | + plt.xlabel('Pull ('+name+')') | ||
| 109 | + plt.ylabel('Probability') | ||
| 110 | + plt.title(name) | ||
| 111 | + plt.grid(True) | ||
| 112 | + | ||
| 113 | + # Show histogram | ||
| 114 | + plt.show() | ||
| 115 | + | ||
| 0 | \ No newline at end of file | 116 | \ No newline at end of file |