-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreconstruct.py
172 lines (147 loc) · 5.16 KB
/
reconstruct.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
172
#!/usr/bin/python
from __future__ import print_function
import sys
import numpy as np
import matplotlib.pyplot as plt
from H5file import H5file
import analyze
import math
import pickle
XSTRIPS = 256
YSTRIPS = 256
def dist((tx,ty),(x,y)):
return math.sqrt((tx-x)**2 + (ty-y)**2)
class Reconstruct:
h5file = None
images = {}
stats = {}
analysis = {}
def __init__(self, h5file):
self.h5file = h5file
self.h5file.readPedestal()
def getEvent(self, i):
event = self.h5file.readEvent(i).astype(np.int16)
event = analyze.addBorder(event,self.h5file.pedestal['avg'])
# Apply pedistal
event -= self.h5file.pedestal['cut'][:,None,:]
event *= event > 0
return event
@staticmethod
def strips(tb_strips):
return map(lambda (x,y): (x[1],y[1]), tb_strips)
def simpleAll(self,i):
event = self.getEvent(i)
ep = analyze.findEntrySimple(analyze.findAllPeaks(event))
return self.strips(ep)
def simple200(self,i):
event = self.getEvent(i)
ep = analyze.findEntrySimple(analyze.deadTime(analyze.findAllPeaks(event),time=200))
return self.strips(ep)
def nolow200(self,i):
event = self.getEvent(i)
if event.max() < 300:
return []
ep = analyze.findEntrySimple(analyze.deadTime(analyze.findAllPeaks(event),time=200))
return self.strips(ep)
def simple400(self,i):
event = self.getEvent(i)
ep = analyze.findEntrySimple(analyze.deadTime(analyze.findAllPeaks(event),time=400))
return self.strips(ep)
def entry(self,i):
return self.h5file.readEntry(i,astype='strips')
def setup(self, methods):
for key in methods.keys():
self.images[key] = np.zeros((XSTRIPS,YSTRIPS),dtype=np.uint32)
self.stats[key] = []
self.analysis[key] = []
def reconstruct(self, nevents=None):
# methods = {'entry': self.entry,
# 'all peaks': self.simpleAll,
# 'dead time 200': self.simple200,
# 'dead time 400': self.simple400
# }
methods = {'dead time 200': self.simple200,
'No low 200': self.nolow200
}
self.methods = methods
self.setup(methods)
if nevents is None:
nevents = self.h5file.nevents
self.nevents = nevents
for i in xrange(nevents):
try:
origin = self.h5file.readOrigin(i,astype='strips')
except:
origin = None
for key in methods.keys():
try:
strips = methods[key](i)
except:
print ('No result', i)
continue
for s in strips:
self.images[key][s] += 1
if origin is not None:
self.stats[key].append(dist(s,origin))
print ('{:5.1f}%'.format(float(i)*100/float(nevents)),end='\r')
sys.stdout.flush()
def analyse(self, nevents=None):
# methods = {'entry': self.entry,
# 'all peaks': self.simpleAll,
# 'dead time 200': self.simple200,
# 'dead time 400': self.simple400
# }
methods = {'entry': self.entry,
'dead time 200': self.simple200,
'No low 200': self.nolow200
}
self.methods = methods
self.setup(methods)
if nevents is None:
nevents = self.h5file.nevents
self.nevents = nevents
for i in xrange(nevents):
try:
origin = self.h5file.readOrigin(i,astype='strips')
except:
continue
for key in methods.keys():
try:
strips = methods[key](i)
except:
continue
for s in strips:
self.analysis[key].append((origin,s))
print ('{:5.1f}%'.format(float(i)*100/float(nevents)),end='\r')
sys.stdout.flush()
def save(self, filename):
pfile = open(filename+'.pkl', 'wb')
pickle.dump(self.analysis, pfile)
pfile.close()
if __name__ == '__main__':
plt.rcParams['keymap.xscale'] = ''
plt.rcParams['keymap.yscale'] = ''
plt.rcParams['keymap.pan'] = ''
plt.rcParams['keymap.save'] = ''
plt.rcParams['keymap.fullscreen'] = ''
h5file = H5file(sys.argv[1])
R = Reconstruct(h5file)
R.reconstruct(1000)
# R.analyse()
# R.save('analysis_nolow')
# exit
for key in R.stats.keys():
print ('Found', len(R.stats[key]), 'entry points in for', key)
print ('Out of', R.nevents, 'data sets')
nkey = 0
bins = np.linspace(0, 185, 100)
for key in R.stats.keys():
nkey += 1
plt.subplot(2,len( R.stats.keys()),nkey)
plt.title(key)
plt.imshow(R.images[key].T,interpolation='none')
plt.subplot(2,len( R.stats.keys()),nkey+len( R.stats.keys()))
plt.title(key)
plt.hist(R.stats[key], bins)
plt.yscale('log', nonposy='clip')
plt.show()