source: fact/tools/PyDimCtrl/event_viewer_test.py@ 16466

Last change on this file since 16466 was 14257, checked in by neise, 12 years ago
evolving
  • Property svn:executable set to *
File size: 4.5 KB
Line 
1#!/usr/bin/python -tti
2
3from pylab import *
4import numpy as np
5from scipy import *
6from matplotlib.patches import RegularPolygon
7import time
8
9from factdimserver import *
10
11
12class EventBrowser:
13
14 def __init__(self, filename):
15
16 self.LoadDetector("")
17
18 self.fig = figure(figsize =(12, 5))
19 self.ax = self.fig.add_subplot(122)
20 self.ax2 = self.fig.add_subplot(121)
21 self.ax.set_axis_off()
22 self.axz, kk = mpl.colorbar.make_axes(self.ax, orientation='vertical')
23 self.fig.canvas.mpl_connect('pick_event', self.onpick)
24 self.ww = np.random.rand(1440)
25
26
27 self.Plot()
28 self.timer = self.fig.canvas.new_timer(interval=10000)
29 self.timer.add_callback(self.timer_callback)
30 self.timer.start()
31
32 def timer_callback(self):
33 self.adc_data = get_fad_adc()
34 self.cam_data = get_fad_cams()
35 self.ww = cam_data[0]
36 self.Plot()
37
38
39 def LoadDetector(self,filename):
40 if filename == "":
41 filename = "FACTmap111030.txt"
42 file = open(filename)
43 xpixel = []
44 xpixel = []
45 ypixel = []
46 idpixel = []
47
48 for lines in file:
49 if lines[0] != '#':
50 softid = int(lines.split()[0])
51 idpixel.append(softid)
52 x = -1*float(lines.split()[9])
53 y = float(lines.split()[8])
54 if softid == 1438 or softid == 1439:
55 x = -1 * x
56 xpixel.append(x)
57 ypixel.append(y)
58
59
60 self.xpixel = xpixel
61 self.ypixel = ypixel
62 self.idpixel = idpixel
63
64 def Plot(self):
65 ww = self.ww
66 wmax = ww.max()
67 ww /= ww.max()
68 cmap = plt.cm.spectral
69
70 for x, y, w in zip(self.xpixel, self.ypixel, ww):
71 self.ax.add_artist(RegularPolygon([x, y], 6, 0.6, 0, facecolor=cmap(w),edgecolor='black',linewidth=0.2))
72 self.ax.plot(self.xpixel, self.ypixel, 'h', color="white", ms=12, visible=False, picker=5)
73 self.ax.set_xlim(-21, 21)
74 self.ax.set_ylim(-21, 21)
75 self.axz.cla()
76
77 #Create axes for the color bar
78 norm = mpl.colors.Normalize(vmin=0, vmax=wmax)
79 cb1 = mpl.colorbar.ColorbarBase(self.axz, cmap=cmap,
80 norm=norm,
81 orientation='vertical')
82# text = "Event Browser"
83# self.ax.annotate(text, (0.05, 0.05),
84# xycoords="figure fraction", va="center", ha="left",
85# bbox=dict(boxstyle="round, pad=1", fc="w"))
86
87
88 def onpress(self, event):
89 'define some key press events'
90 if event.key in ('q','Q'): sys.exit()
91 if event.key not in ('n', 'p'): return
92 if event.key=='n':
93 self.event = self.run.next()
94 else:
95 print "Sorry I cannot go back!"
96 return
97 self.ax.cla()
98 self.ax.set_axis_off()
99 self.Plot()
100
101 def onpick(self, event):
102 xpix = self.xpixel
103 ypix = self.ypixel
104 # the click locations
105 x = event.mouseevent.xdata
106 y = event.mouseevent.ydata
107 ind = event.ind
108
109 N = len(event.ind)
110 if not N: return True
111
112 if N == 1:
113 self.pixelID = event.ind[0]
114 if N > 1:
115 # find nearest point to click
116 distances = np.zeros(N)
117 for ii,i in enumerate(ind):
118 distances[ii] = np.sqrt((x-xpix[i])**2 + (y-ypix[i])**2)
119 self.pixelID = ind[distances.argmin()]
120
121 print self.pixelID
122
123 self.ax2.plot(self.adc_data[self.pixelID], hold=False)
124
125 self.fig.canvas.draw()
126
127
128
129# *blocking* function to get the FAD ADC data in a nice format
130def get_fad_adc():
131 # a large large tuple:
132 # 1650 elements carrying header information
133 offset = 1650
134 # (1440+160)*Roi floats of real ADC data (DRS calibrated,
135 # if fad_control has the constants)
136 raw_data = fad_control.raw_data()
137 if raw_data == None:
138 print "fad_control.raw_data() .. timed out"
139 return None
140 Roi = int(raw_data[0])
141 rd = np.array(raw_data[offset:offset+1440*Roi])
142 rd = raw_data.reshape(1440,Roi)
143 return rd
144
145
146def get_fad_cams():
147
148 cd = fad_control.event_data()
149 if cd == None:
150 print "fad_control.event_data() ... timed out"
151 return None
152 cd = np.array(cd)
153 cd = cd.reshape(4,1440)
154
155 return cd
156
157
158
159if __name__ == '__main__':
160 eb = EventBrowser("")
161 show()
162
163
164
165
166
167
168
169
Note: See TracBrowser for help on using the repository browser.