-
Notifications
You must be signed in to change notification settings - Fork 1
/
flukaplot.py
171 lines (151 loc) · 6.89 KB
/
flukaplot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# -*- coding: utf-8 -*-
#!/usr/bin/env python
#########################################################################
# Copyright (C) 2010 David Hansen - [email protected]
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 3 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, see <http://www.gnu.org/licenses/>.
#
#
# Plotting functions for FLUKA usrbins.
# Note: For use with ipython, you must call ipython with -pylab
#
#
#
#
#
#
#
# TODO:
# - import the Data.py from flair in a better way
# - Document code better
import sys
import subprocess
import os
import math
import struct
from pylab import *
from numpy import *
from matplotlib.colors import LogNorm
from mayavi import mlab
from flair.Data import *
class AUUsrbin(Usrbin):
def arrayRead(self,det=0):
"""Reads detector data to nparray"""
bin = self.detector[det]
data = self.readData(det)
data = asarray(unpackArray(data)).reshape((bin.nx,bin.ny,bin.nz), order='F')
#Add data to the detector
bin.start = [bin.xlow,bin.ylow,bin.zlow]
bin.stop = [bin.xhigh,bin.yhigh,bin.zhigh]
bin.delta = [bin.dx,bin.dy,bin.dz]
bin.num = [bin.nx,bin.ny,bin.nz]
#Compensate for normalization when not region binning
if (bin.type in [1,7,11,17]): #Cylinder bins
r = linspace(bin.xlow,bin.xhigh, num=bin.nx, endpoint=False)
normalization = (((2*bin.dx)*r+bin.dx**2)*bin.dz*bin.dy/2).reshape(bin.nx,1,1)
data = data*normalization
elif (bin.type in [0,3,4,5,10,13,14,15,16]): #Cartesian bins
normalization = bin.dx*bin.dy*bin.dz
data = data*normalization
bin.data = data
return data
def plot(self,det,axis,start=None,stop=None):
"""plots data of the detector det, along the axis. Data is averaged
over the data range.
det: detector to use
axis: axis that will be the x-axis in the plot2D
start and stop: start and stop of the range (in cm, kept for theta) to plot. Example
start= [0,0,0], stop = [2.5,3.8,10]
"""
bin = self.detector[det]
try:
bin.data
except AttributeError:
self.arrayRead(det)
sumaxis = [2,1,0]
sumaxis.remove(axis)
low,high = self.__ranges(det,start,stop)
#Get requested data
subdata = (slice(low[0],high[0]),slice(low[1],high[1]),slice(low[2],high[2]))
plotdata = bin.data[subdata]
for x in sumaxis:
plotdata = sum(plotdata,x)
#Renormalize data (renormalization theory not required)
print str(low) + " " +str(high)
if (bin.type in [1,7,11,17]): #Cylinder bins
plotdata = plotdata/(bin.dx**2*bin.dz*bin.dy*(high[0]**2*high[2]*high[1]-low[0]**2*low[2]*low[1])/4)
elif (bin.type in [0,3,4,5,10,13,14,15,16]): #Cartesian bins
plotdata = plotdata/(bin.dx*bin.dy*bin.dz*(high[0]-low[0])*(high[1]-low[1])*(high[1]-low[1]))
xdata = linspace(bin.start[axis]+bin.delta[axis]/2,bin.stop[axis]+bin.delta[axis]/2, num=bin.num[axis], endpoint=False)[slice(low[axis],high[axis])]
plot(xdata,plotdata,label=bin.name)
def plot2D(self,det,axisX,axisY,start=None,stop=None,logz=False):
bin = self.detector[det]
try:
bin.data
except AttributeError:
self.arrayRead(det)
sumaxis = [2,1,0]
sumaxis.remove(axisX)
sumaxis.remove(axisY)
low,high = self.__ranges(det,start,stop)
subdata = (slice(low[0],high[0]),slice(low[1],high[1]),slice(low[2],high[2]))
plotdata = bin.data[subdata]
plotdata = sum(plotdata,sumaxis[0])
#Renormalize data
if (bin.type in [1,7,11,17]): #Cylinder bins
plotdata = plotdata/((pi*high[0]**2*high[2]*high[1]/(2*pi))-(pi*low[0]**2*low[2]*low[1]/(2*pi)))
elif (bin.type in [0,3,4,5,10,13,14,15,16]): #Cartesian bins
plotdata = plotdata/((high[0]-low[0])*(high[1]-low[1])*(high[1]-low[1]))
xdata = linspace(bin.start[axisX]+bin.delta[axisX]/2,bin.stop[axisX]+bin.delta[axisX]/2, num=bin.num[axisX], endpoint=False)[slice(low[axisX],high[axisX])]
ydata = linspace(bin.start[axisY]+bin.delta[axisY]/2,bin.stop[axisY]+bin.delta[axisY]/2, num=bin.num[axisY], endpoint=False)[slice(low[axisY],high[axisY])]
X,Y = meshgrid(ydata,xdata)
if log:
pcolormesh(X,Y,plotdata,norm=LogNorm(vmin=plotdata[plotdata.nonzero()].min(), vmax=plotdata.max()))
else:
pcolormesh(X,Y,plotdata)
#Plots 2D data
def plotSurf(self,det,axisX,axisY,start=None,stop=None):
bin = self.detector[det]
try:
bin.data
except AttributeError:
self.arrayRead(det)
sumaxis = [2,1,0]
sumaxis.remove(axisX)
sumaxis.remove(axisY)
low,high = self.__ranges(det,start,stop)
subdata = (slice(low[0],high[0]),slice(low[1],high[1]),slice(low[2],high[2]))
plotdata = bin.data[subdata]
plotdata = sum(plotdata,sumaxis[0])
#Renormalize data
if (bin.type in [1,7,11,17]): #Cylinder bins
plotdata = plotdata/((pi*high[0]**2*high[2]*high[1]/(2*pi))-(pi*low[0]**2*low[2]*low[1]/(2*pi)))
elif (bin.type in [0,3,4,5,10,13,14,15,16]): #Cartesian bins
plotdata = plotdata/((high[0]-low[0])*(high[1]-low[1])*(high[1]-low[1]))
x = linspace(bin.start[axisX]+bin.delta[axisX]/2,bin.stop[axisX]+bin.delta[axisX]/2, num=bin.num[axisX], endpoint=False)[slice(low[axisX],high[axisX])]
y = linspace(bin.start[axisY]+bin.delta[axisY]/2,bin.stop[axisY]+bin.delta[axisY]/2, num=bin.num[axisY], endpoint=False)[slice(low[axisY],high[axisY])]
mlab.figure(1, fgcolor=(0, 0, 0), bgcolor=(1, 1, 1))
z =plotdata
# Visualize the points
mlab.surf(x,y,z)
mlab.show()
def __ranges(self,det,start=None,stop=None):
bin = self.detector[det]
if start==None : start = bin.start
if stop ==None : stop = bin.stop
low = map(floor,[(x-xlow)/d for x,xlow,d in zip(start,bin.start,bin.delta)])
low = [int(max(x,0)) for x in low]
high = map(ceil,[(x-xlow)/d for x,xlow,d in zip(stop,bin.start,bin.delta)])
high = [int(min(x,y)) for x,y in zip(high,bin.num)]
return low,high