3 from wirecell
import units
6 import matplotlib.pyplot
as plt
16 'Return a dictionary of arrays instead of a 2D array.' 19 ret[letter] = depos[:,ind]
24 For some reason sometimes zero steps are taken. This removes them 31 return numpy.asarray(ret)
34 def load(filename, jpath="depos"):
36 Load a CWT JSON depo file and return numpy array of depo info. 39 if filename.endswith(
".json"):
41 elif filename.endswith(
".bz2"):
46 with fopen(filename)
as fp:
47 jdat = json.loads(fp.read())
50 for jdepo
in jdat[
"depos"]:
51 depo = tuple([jdepo.get(key,0.0)
for key
in columns])
53 return numpy.asarray(depos)
56 def apply_units(depos, distance_unit, time_unit, energy_unit, step_unit=None, electrons_unit="1.0"):
57 'Apply units to a deposition array, return a new one' 59 print "dtese=", distance_unit, time_unit, energy_unit, step_unit, electrons_unit,
61 depos = numpy.copy(depos)
63 dunit = eval(distance_unit, units.__dict__)
64 tunit = eval(time_unit, units.__dict__)
65 eunit = eval(energy_unit, units.__dict__)
69 sunit = eval(step_unit, units.__dict__)
70 nunit = eval(electrons_unit, units.__dict__)
72 theunits = [dunit]*3 + [eunit] + [tunit] + [sunit] + [nunit]
79 def dump(output_file, depos, jpath="depos"):
81 Save a deposition array to JSON file 85 jdepo = {c:v
for c,v
in zip(columns, depo)}
92 text = json.dumps(out, indent=4)
94 if output_file.endswith(
".json"):
96 elif output_file.endswith(
".json.bz2"):
99 raise IOError(
'Unknown file extension: "%s"' % filename)
100 with fopen(output_file,
'w')
as fp:
109 Return new set of depos all moved by given vector offset. 111 offset = numpy.asarray(offset)
112 depos = numpy.copy(depos)
113 depos[:,0:3] += offset
119 Shift depositions so that they are centered on the given point 121 point = numpy.asarray(point)
122 tot = numpy.asarray([0.0, 0.0, 0.0])
126 offset = point - tot/n
127 return move(depos, offset)
134 ax = fig.add_subplot(111)
136 ax.semilogy(h[1][:-1], h[0])
142 s = depos[
"s"]/units.cm
143 h = numpy.histogram(s, 1000, (0, 0.05))
147 q = depos[
"q"]/units.MeV
148 h = numpy.histogram(q, 1000, (0, 0.2))
153 h = numpy.histogram(n, 1000, (0, 10000))
154 plot_hist(h,
"number [electrons]", output)
157 q = depos[
"q"]/units.MeV
158 s = depos[
"s"]/units.cm
159 h = numpy.histogram(q/s, 1000, (0,10))
163 s = depos[
"s"]/units.cm
165 h = numpy.histogram(n/s, 500, (0,5.0e5))
172 xmm = (numpy.min(x), numpy.max(x))
173 zmm = (numpy.min(z), numpy.max(z))
176 nx =
int((xmm[1] - xmm[0])/units.mm)
177 nz =
int((zmm[1] - zmm[0])/units.mm)
178 print (
"%d x %d pixels: " % (nx, nz))
180 xedges = numpy.linspace(xmm[0], xmm[1], nx)
181 zedges = numpy.linspace(zmm[0], zmm[1], nz)
184 ax = fig.add_subplot(111)
187 h = ax.hist2d(x,z,bins=(xedges, zedges), weights=w)
188 ax.set_xlabel(
"X [mm]")
189 ax.set_ylabel(
"Z [mm]")
190 plt.colorbar(h[3], ax=ax)
195 'Plot colz q as X vs Z' 199 q = depos[
"q"]/units.MeV
204 'Plot colz q/s as X vs Z' 208 q = depos[
"q"]/units.MeV
209 s = depos[
"s"]/units.cm
214 "Plot colz number as X vs Z (transverse)" 222 'Plot number as scatter plot' 224 x = depos[
"x"]/units.mm
225 z = depos[
"z"]/units.mm
228 nmax =
float(numpy.max(n))
229 cmap = plt.get_cmap(
'seismic')
231 sizes = [20.0*nv/nmax
for nv
in n]
232 colors = [
cmap(nv/nmax)
for nv
in n]
235 ax = fig.add_subplot(111)
236 ax.set_title(
"electrons")
238 ax.set_xlabel(
"X [mm]")
239 ax.set_ylabel(
"Z [mm]")
241 ax.scatter(x, z, c=colors, edgecolor=colors, s=sizes)
def plot_qxz(depos, output)
def apply_units(depos, distance_unit, time_unit, energy_unit, step_unit=None, electrons_unit="1.0")
auto enumerate(Iterables &&...iterables)
Range-for loop helper tracking the number of iteration.
def plot_dnds(depos, output)
def plot_deds(depos, output)
def plot_xz_weighted(x, z, w, title, output)
def plot_hist(h, xlab, output)
def plot_nscat(depos, output)
def plot_qsxz(depos, output)
def dump(output_file, depos, jpath="depos")
def plot_q(depos, output)
def remove_zero_steps(depos)
def plot_nxz(depos, output)
auto zip(Iterables &&...iterables)
Range-for loop helper iterating across many collections at the same time.
def plot_n(depos, output)
def load(filename, jpath="depos")
def plot_s(depos, output)