| 1 | #!/usr/bin/python -tti | 
|---|
| 2 |  | 
|---|
| 3 | from pylab import * | 
|---|
| 4 | import numpy as np | 
|---|
| 5 | from scipy import * | 
|---|
| 6 | from matplotlib.patches import RegularPolygon | 
|---|
| 7 | from factdimserver import * | 
|---|
| 8 |  | 
|---|
| 9 |  | 
|---|
| 10 | class EventBrowser: | 
|---|
| 11 |  | 
|---|
| 12 | def __init__(self, run, filename): | 
|---|
| 13 |  | 
|---|
| 14 | self.LoadDetector("") | 
|---|
| 15 | self.run = run | 
|---|
| 16 | self.event = run.next() | 
|---|
| 17 | self.nevents = run.nevents | 
|---|
| 18 | self.fig = figure(figsize =(10, 8)) | 
|---|
| 19 | self.ax = self.fig.add_subplot(111) | 
|---|
| 20 | self.ax.set_axis_off() | 
|---|
| 21 | self.axz, kk = mpl.colorbar.make_axes(self.ax, orientation='vertical') | 
|---|
| 22 | self.Plot() | 
|---|
| 23 | # Create a new timer object. Set the interval 1000 milliseconds (1000 is default) | 
|---|
| 24 | # and tell the timer what function should be called. | 
|---|
| 25 | self.timer = fig.canvas.new_timer(interval=1000) | 
|---|
| 26 | self.timer.add_callback(self.timer_callback) | 
|---|
| 27 | self.timer.start() | 
|---|
| 28 | self.fig.canvas.mpl_connect('pick_event', self.onpick) | 
|---|
| 29 | self.fig.canvas.mpl_connect('key_press_event', self.onpress) | 
|---|
| 30 |  | 
|---|
| 31 | def timer_callback(self): | 
|---|
| 32 | self.adc_data = get_fad_adc() | 
|---|
| 33 | self.cam_data = get_fad_cams() | 
|---|
| 34 |  | 
|---|
| 35 | def LoadDetector(self,filename): | 
|---|
| 36 | if filename == "": | 
|---|
| 37 | filename  = "FACTmap111030.txt" | 
|---|
| 38 | file = open(filename) | 
|---|
| 39 | xpixel = [] | 
|---|
| 40 | ypixel = [] | 
|---|
| 41 | idpixel = [] | 
|---|
| 42 |  | 
|---|
| 43 | for lines in file: | 
|---|
| 44 | if lines[0] != '#': | 
|---|
| 45 | softid = int(lines.split()[0]) | 
|---|
| 46 | idpixel.append(softid) | 
|---|
| 47 | x = -1*float(lines.split()[9]) | 
|---|
| 48 | y = float(lines.split()[8]) | 
|---|
| 49 | if softid == 1438 or softid == 1439: | 
|---|
| 50 | x = -1 * x | 
|---|
| 51 | xpixel.append(x) | 
|---|
| 52 | ypixel.append(y) | 
|---|
| 53 |  | 
|---|
| 54 |  | 
|---|
| 55 | self.xpixel = xpixel | 
|---|
| 56 | self.ypixel = ypixel | 
|---|
| 57 | self.idpixel = idpixel | 
|---|
| 58 |  | 
|---|
| 59 | def Plot(self): | 
|---|
| 60 | ww, dummy = self.max_finder(self.despiker(self.event['data'])) | 
|---|
| 61 | print ww | 
|---|
| 62 | self.ww = ww | 
|---|
| 63 | wmax = ww.max() | 
|---|
| 64 | cmap = plt.cm.spectral | 
|---|
| 65 | for x, y, w in zip(self.xpixel, self.ypixel, ww): | 
|---|
| 66 | self.ax.add_artist(RegularPolygon([x, y], 6, 0.6, 0, facecolor=cmap(w),edgecolor='black',linewidth=0.2)) | 
|---|
| 67 | self.ax.plot(self.xpixel, self.ypixel, 'h', color="white",  ms=12, visible=False, picker=10) | 
|---|
| 68 | self.ax.set_xlim(-21, 21) | 
|---|
| 69 | self.ax.set_ylim(-21, 21) | 
|---|
| 70 | self.axz.cla() | 
|---|
| 71 |  | 
|---|
| 72 | #Create axes for the color bar | 
|---|
| 73 | norm = mpl.colors.Normalize(vmin=0, vmax=wmax) | 
|---|
| 74 | cb1 = mpl.colorbar.ColorbarBase(self.axz, cmap=cmap, | 
|---|
| 75 | norm=norm, | 
|---|
| 76 | orientation='vertical') | 
|---|
| 77 | text = "Event Browser, press 'n' in this window to iterate over the events." | 
|---|
| 78 | text = text + "\nEvent id: " + str(self.event["event_id"]) | 
|---|
| 79 | text = text + "\nTrigger type: " + str(self.event["trigger_type"]) | 
|---|
| 80 | self.ax.annotate(text, (0.05, 0.05), | 
|---|
| 81 | xycoords="figure fraction", va="center", ha="left", | 
|---|
| 82 | bbox=dict(boxstyle="round, pad=1", fc="w")) | 
|---|
| 83 |  | 
|---|
| 84 | def onpress(self, event): | 
|---|
| 85 | 'define some key press events' | 
|---|
| 86 | if event.key in ('q','Q'): sys.exit() | 
|---|
| 87 | if event.key not in ('n', 'p'): return | 
|---|
| 88 | if event.key=='n': | 
|---|
| 89 | self.event = self.run.next() | 
|---|
| 90 | else: | 
|---|
| 91 | print "Sorry I cannot go back!" | 
|---|
| 92 | return | 
|---|
| 93 | self.ax.cla() | 
|---|
| 94 | self.ax.set_axis_off() | 
|---|
| 95 | self.Plot() | 
|---|
| 96 | self.update() | 
|---|
| 97 |  | 
|---|
| 98 | def onpick(self, event): | 
|---|
| 99 |  | 
|---|
| 100 | N = len(event.ind) | 
|---|
| 101 | if not N: return True | 
|---|
| 102 |  | 
|---|
| 103 | if N > 1: | 
|---|
| 104 | print '%i points found!' % N | 
|---|
| 105 | self.pixelID = event.ind[0] | 
|---|
| 106 |  | 
|---|
| 107 | # the click locations | 
|---|
| 108 | x = event.mouseevent.xdata | 
|---|
| 109 | y = event.mouseevent.ydata | 
|---|
| 110 | print x, y | 
|---|
| 111 | self.update() | 
|---|
| 112 |  | 
|---|
| 113 |  | 
|---|
| 114 | def update(self): | 
|---|
| 115 |  | 
|---|
| 116 | dataind = self.pixelID | 
|---|
| 117 |  | 
|---|
| 118 | # put a user function in here! | 
|---|
| 119 | self.userfunc(dataind) | 
|---|
| 120 |  | 
|---|
| 121 | self.fig.canvas.draw() | 
|---|
| 122 | show() | 
|---|
| 123 |  | 
|---|
| 124 | def userfunc(self,dataind): | 
|---|
| 125 | print 'No userfunc defined' | 
|---|
| 126 | pass | 
|---|
| 127 |  | 
|---|
| 128 |  | 
|---|
| 129 | # *blocking* function to get the FAD ADC data in a nice format | 
|---|
| 130 | def 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 |  | 
|---|
| 146 | def 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(1440,4) | 
|---|
| 154 |  | 
|---|
| 155 | return cd | 
|---|
| 156 |  | 
|---|
| 157 | if __name__ == '__main__': | 
|---|
| 158 |  | 
|---|
| 159 |  | 
|---|
| 160 | eb = EventBrowser(run, "") | 
|---|
| 161 |  | 
|---|
| 162 | def PlotFFT(pixelID): | 
|---|
| 163 |  | 
|---|
| 164 | fig2 = figure(2, figsize=(10, 8)) | 
|---|
| 165 |  | 
|---|
| 166 | ax1= fig2.add_subplot(311) | 
|---|
| 167 | ax2 = fig2.add_subplot(312) | 
|---|
| 168 | ax3 = fig2.add_subplot(313, sharey=ax1, sharex=ax1) | 
|---|
| 169 |  | 
|---|
| 170 | ax1.cla() | 
|---|
| 171 | ax2.cla() | 
|---|
| 172 | ax3.cla() | 
|---|
| 173 |  | 
|---|
| 174 | ax1.set_title("PixelID "+str(pixelID)) | 
|---|
| 175 |  | 
|---|
| 176 | ax1.grid() | 
|---|
| 177 | ax2.grid() | 
|---|
| 178 | ax3.grid() | 
|---|
| 179 |  | 
|---|
| 180 | x_drs = np.arange(0, 300, 1) | 
|---|
| 181 |  | 
|---|
| 182 | truncated_x_drs = x_drs[20:-10] | 
|---|
| 183 |  | 
|---|
| 184 | data = eb.event['data'] | 
|---|
| 185 |  | 
|---|
| 186 | drsdata = data[pixelID] | 
|---|
| 187 | ax1.plot(x_drs, drsdata, '.:') | 
|---|
| 188 |  | 
|---|
| 189 | step = zeros(300) | 
|---|
| 190 | step[:100] = 1 | 
|---|
| 191 | ft = fft(drsdata) | 
|---|
| 192 |  | 
|---|
| 193 | new_drs = np.arange(-150, 150, 1) | 
|---|
| 194 |  | 
|---|
| 195 | ftdom = ft.copy() | 
|---|
| 196 | for i in range(10,290): | 
|---|
| 197 | ftdom[i]=0 | 
|---|
| 198 | ax2.plot(new_drs, abs(concatenate((ftdom[150:],ftdom[:150]))), marker='o') | 
|---|
| 199 | invdata = ifft(ftdom) | 
|---|
| 200 | ax3.plot(x_drs, invdata, '.:') | 
|---|
| 201 | ax3.plot(x_drs, drsdata, '.:') | 
|---|
| 202 | ax3.set_xlabel("DRS counts") | 
|---|
| 203 | fig2.canvas.draw() | 
|---|
| 204 |  | 
|---|
| 205 |  | 
|---|
| 206 | eb.userfunc = PlotFFT | 
|---|
| 207 |  | 
|---|
| 208 | show() | 
|---|
| 209 |  | 
|---|
| 210 |  | 
|---|
| 211 |  | 
|---|
| 212 |  | 
|---|
| 213 |  | 
|---|
| 214 |  | 
|---|