plot_g4tuple.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import numpy
4 import matplotlib.pyplot as plt
5 
6 import json
7 import bz2
8 
9 # 0123456
10 depo_keys="xyzqtsn"
11 
12 def load_depos(filename):
13  if filename.endswith(".bz2"):
14  fp = bz2.BZ2File(filename)
15  else:
16  fp = open(filename);
17  jdat = json.loads(fp.read())
18  depos = list()
19  for jdepo in jdat["depos"]:
20  depo = tuple([jdepo[key] for key in depo_keys])
21  if depo[5] == 0.0:
22  continue
23  depos.append(depo)
24  return numpy.asarray(depos)
25 
26 def plot_dedx(depos):
27  dE = depos[:,3]
28  dX = depos[:,5]
29  dEdX = dE/dX
30  h = numpy.histogram(dEdX, 1000, (0,10))
31  plt.clf(); plt.plot(h[1][:-1], h[0])
32  plt.savefig("g4tuple-dedx.pdf")
33  pass
34 
35 def plot_dndx(depos):
36  dN = depos[:,4]
37  dX = depos[:,5]
38  dNdX = dN/dX
39  print dNdX[0]
40  h = numpy.histogram(dNdX, 150, (5.0e4,2.0e5))
41  plt.clf(); plt.semilogy(h[1][:-1], h[0])
42  plt.savefig("g4tuple-dndx.pdf")
43  pass
44 
45 def plot_nxz(depos):
46  "Plot colz number as X vs Z (transverse)"
47  x = depos[:,0]
48  z = depos[:,2]
49  n = depos[:,4]
50 
51 
52  xedges = numpy.linspace(90, 120.0, 300)
53  zedges = numpy.linspace(10, 70.0, 600)
54 
55  #H, xedg, zedg = numpy.histogram2d(z, x, bins=(xedges, zedges), weights=n)
56  fig = plt.figure()
57  ax = fig.add_subplot(111)
58  ax.set_title("Electrons per mm$^2$")
59  #X, Z = numpy.meshgrid(xedges, zedges)
60  #cax = ax.pcolormesh(X, Z, H)
61  #cax = ax.imshow(H)#, interpolation='nearest', origin='low',
62  # # extent=[xedg[0], xedg[-1], zedg[0], zedg[-1]])
63  h = ax.hist2d(x,z,bins=(xedges, zedges), weights=n)
64  ax.set_xlabel("X [cm]")
65  ax.set_ylabel("Z [cm]")
66  plt.colorbar(h[3], ax=ax)
67  fig.savefig("g4tuple-nxz.pdf")
68 
69 
70 if '__main__' == __name__:
71  import sys
72  depos = load_depos(sys.argv[1])
73  plot_dedx(depos);
74  plot_dndx(depos);
75  plot_nxz(depos);
int open(const char *, int)
Opens a file descriptor.