-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyzeResults.py
343 lines (317 loc) · 13.1 KB
/
analyzeResults.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from OOFlex.files.BINReader import BINReader
from TCTData import TCTData
import math
import argparse
import numpy as np
import matplotlib
matplotlib.use( "Agg" )
import pylab
import os.path
def plot_parameters( plots = 0 ):
"""Plot format
"""
pylab.rcParams[ 'lines.linewidth' ] = 1
pylab.rcParams[ 'lines.markeredgewidth' ] = 1
pylab.rcParams[ 'lines.markersize' ] = 7
pylab.rcParams[ 'font.size' ] = 10 - plots / 2
# pylab.rcParams[ 'font.weight' ] = 'semibold'
pylab.rcParams[ 'axes.linewidth' ] = 2
pylab.rcParams[ 'axes.titlesize' ] = 35 - plots / 2
pylab.rcParams[ 'axes.labelsize' ] = 15 - plots / 2
# pylab.rcParams[ 'axes.labelweight' ] = 'semibold'
pylab.rcParams[ 'ytick.major.pad' ] = 9 - plots / 4
pylab.rcParams[ 'xtick.major.pad' ] = 9 - plots / 4
pylab.rcParams[ 'xtick.labelsize' ] = 13
pylab.rcParams[ 'ytick.labelsize' ] = 13
pylab.rcParams[ 'legend.fontsize' ] = 10 - plots / 1.5
pylab.rcParams[ 'grid.linewidth' ] = 1
def getCharge( dataTCT ):
"""It calculates the charge taking into account the gain if the amplifiar from Particulars
www.particulars.si
"""
voltage = 8
max_gain = 53
rel_gain = { 6 : 0.15, 7 : 0.4, 8 : 0.6, 9 : 0.8, 10 : 0.9, 11 : 1, 12 : 1, 13 : 1, 14 : 1, 15 : 1 }
gain = max_gain * rel_gain[ voltage ]
charge = sum( dataTCT.cuttedData ) * dataTCT.header[ "xincr" ] / ( 50 * gain )
return abs( charge )
def getCurrent( dataTCT ):
"""It calculates the charge taking into account the gain if the amplifiar from Part$
www.particulars.si
"""
voltage = 8
max_gain = 53
rel_gain = { 6 : 0.15, 7 : 0.4, 8 : 0.6, 9 : 0.8, 10 : 0.9, 11 : 1, 12 : 1, 13 : 1, 14 : 1, 15 : 1}
gain = max_gain * rel_gain[ voltage ]
i = dataTCT.initPoint
current = []
corr_curr = []
trap_time = 30e-8
print dataTCT.endPoint
print len(dataTCT.averageData)
while i < dataTCT.endPoint:
current.append(dataTCT.averageData[i] / ( 50 * gain ))
i += 1
i = 0
while i < len(current):
corr_curr.append(current[i] * math.exp(i * dataTCT.header[ "xincr" ]/trap_time))
i += 1
return current, corr_curr
def convertAndShiftData( dataTCT ):
"""Converts binary to ascii and turns positive signals from p-type
sensors to negative. The algorithm looks for minimums, not maximums.
"""
for wave in dataTCT.rawData:
ywave = dataTCT.data2wave( wave )
if dataTCT.bulk == "p":
ywave = list( np.array( ywave ) * -1 )
dataTCT.pedestalInit, dataTCT.pedestalEnd, pdt = pedestal( ywave )
dataTCT.ydata = list( np.array( ywave ) - pdt )
def removeElectronicNoise( dataTCT, noiseTCT ):
for wave in dataTCT.ydata:
noiseless = np.array( wave ) - np.array( noiseTCT.ydata[ 0 ] )
dataTCT.noiselessData = list( noiseless )
def averageTheData( dataTCT ):
"""It averages all the waveforms in the file.
"""
average = np.array( len( dataTCT.noiselessData[ 0 ] ) * [ 0.0 ] )
for wave in dataTCT.noiselessData:
average += np.array( wave )
average /= float( len( dataTCT.noiselessData ) )
dataTCT.averageData = list( average )
def cutData( dataTCT, cut = "signal" ):
""" Function that looks for the minimum in the signal and it cuts a 50ns window.
The window has to begin 1ns before the beginning of the signal and extends
for 50ns.
If the argument cut is equal to peak the cut will take only the peak, not 50ns
"""
window = [ -1e-9, 49e-9 ]
minimum = dataTCT.averageData.index( min( dataTCT.averageData ) )
for k in range( len( dataTCT.averageData[ : minimum + 1 ] ) - 1, -1, -1 ):
if dataTCT.averageData[ k ] > 0.0:
dataTCT.initSignal = k
break
for k in range( minimum, len( dataTCT.averageData )):
if dataTCT.averageData[ k ] > 0.0:
dataTCT.endSignal = k
break
dataTCT.calculateWindow( window )
if cut == "peak":
dataTCT.initPoint = dataTCT.initSignal
dataTCT.endPoint = dataTCT.endSignal
elif cut == "signal":
dataTCT.initPoint = dataTCT.initWindow
dataTCT.endPoint = dataTCT.endWindow
elif cut == "whole":
dataTCT.initPoint = 0
dataTCT.endPoint = len( dataTCT.average)
dataTCT.cuttedData = dataTCT.initPoint * [ 0 ] + dataTCT.averageData[ dataTCT.initPoint : dataTCT.endPoint ] + ( len( dataTCT.averageData ) - dataTCT.endPoint ) * [ 0 ]
def pedestal( data ):
"""It calculates the pedestal to eliminate any possible DC in the measure.
"""
minimum = data.index( min( data ) )
if minimum / 2 < 3000:
initPoint = minimum + 2000
endPoint = minimum + 5000
else :
initPoint = 0
endPoint = minimum / 2
pedestal = sum( data[ initPoint : endPoint ] ) / float( endPoint - initPoint )
return initPoint, endPoint, pedestal
def plotAnalysis( dataTCT, current, corr_curr ):
"""Plot the different steps I make to the waveforms to get the signal and
integrate it and calculate the charge later. These plots is my check to
know I am doing the things right.
"""
i = 0
diff = []
while i < len(current):
diff.append(corr_curr[i] - current[i])
i += 1
#plot_parameters()
fig = pylab.figure( 1, ( 8, 7 ) )
fig.add_subplot( 221 )
#pylab.plot( dataTCT.xdata, dataTCT.ydata[ 0 ], "k", label = "Raw data" )
#ftemp = open( dataTCT.filename.replace( ".bin", ".txt" ), "w" )
#for i in range(len(dataTCT.xdata)):
# ftemp.write( str(dataTCT.xdata[i]) + "\t" + str(dataTCT.noiselessData[ 0 ][ i ]) + "\n" )
#ftemp.close()
#pylab.plot( dataTCT.xdata, dataTCT.noiselessData[ 0 ], "b", label = "Noiseless data" )
pylab.plot( dataTCT.xdata[dataTCT.initPoint : dataTCT.endPoint - 700], current[0:len(current)-700], "b", label = "Current")
pylab.ticklabel_format( axis = 'both', style = 'sci', scilimits = ( 0, 0 ) )
pylab.legend( loc = 0 )
pylab.grid( True )
pylab.xlabel( "Time(s)" )
pylab.ylabel( "Voltage(V)" )
fig.add_subplot( 222 )
#pylab.plot( dataTCT.xdata[ dataTCT.pedestalInit : dataTCT.pedestalEnd ], dataTCT.ydata[ 0 ][ dataTCT.pedestalInit : dataTCT.pedestalEnd ], "k", label = "Pedestal Example" )
pylab.plot( dataTCT.xdata[dataTCT.initPoint : dataTCT.endPoint - 700], corr_curr[0:len(corr_curr)-700], "b", label = "Corrected Current")
pylab.ticklabel_format( axis = 'both', style = 'sci', scilimits = ( 0, 0 ) )
pylab.legend( loc = 0 )
pylab.grid( True )
pylab.xlabel( "Time(s)" )
pylab.ylabel( "Voltage(V)" )
fig.add_subplot( 223 )
pylab.plot( dataTCT.xdata[ dataTCT.initPoint : dataTCT.endPoint ], dataTCT.averageData[ dataTCT.initPoint : dataTCT.endPoint ], "k", label = "50ns window" )
pylab.ticklabel_format( axis = 'both', style = 'sci', scilimits = ( 0, 0 ) )
pylab.legend( loc = 0 )
pylab.grid( True )
pylab.legend( loc = 0 )
pylab.xlabel( "Time(s)" )
pylab.ylabel( "Voltage(V)" )
fig.add_subplot( 224 )
#pylab.plot( dataTCT.xdata[ dataTCT.initPoint - 100 : dataTCT.endPoint + 100 ], dataTCT.cuttedData[ dataTCT.initPoint - 100 : dataTCT.endPoint + 100 ], "k", label = "Integrating\nwindow" )
pylab.plot( dataTCT.xdata[dataTCT.initPoint : dataTCT.endPoint - 700], diff[0:len(diff)-700], "b", label = "Difference")
pylab.ticklabel_format( axis = 'both', style = 'sci', scilimits = ( 0, 0 ) )
pylab.legend( loc = 0 )
pylab.grid( True )
pylab.xlabel( "Time(s)" )
pylab.ylabel( "Voltage(V) " )
pylab.tight_layout()
pylab.savefig( dataTCT.filename.replace( ".bin", ".png" ) )
pylab.close()
def plotResults( dataTCT, current, corr_curr ):
i = 0
diff = []
while i < len(current):
diff.append(corr_curr[i] - current[i])
i += 1
fig = pylab.figure( 1, ( 8, 7 ) )
pylab.plot( dataTCT.xdata[dataTCT.initPoint : dataTCT.endPoint - 700], current[0:len(current)-700], "r", label = "Current")
pylab.plot( dataTCT.xdata[dataTCT.initPoint : dataTCT.endPoint - 700], corr_curr[0:len(corr_curr)-700], "k", label = "Corrected Current")
pylab.plot( dataTCT.xdata[dataTCT.initPoint : dataTCT.endPoint - 700], diff[0:len(diff)-700], "b", label = "Difference")
pylab.ticklabel_format( axis = 'both', style = 'sci', scilimits = ( 0, 0 ) )
pylab.legend( loc = 0 )
pylab.grid( True )
pylab.xlabel( "Time(s)" )
pylab.ylabel( "Voltage(V) " )
pylab.tight_layout()
pylab.savefig( dataTCT.filename.replace( ".bin", ".png" ) )
pylab.close()
def plotTotResults(filename_tot, xdata_tot, current_tot, init_tot, end_tot,):
fig = pylab.figure( 1, ( 8, 7 ) )
n = 0
voltage = []
while n <=100:
voltage.append(n * 10)
n += 1
i = 1
while i < len(current_tot):
print str(init_tot[i] - end_tot[i])
print len(current_tot[i])
pylab.plot( xdata_tot[i][init_tot[i]:end_tot[i]-800], current_tot[i][0:len(current_tot[i]) - 800], label = str(voltage[i]) + " V")
i += 10
pylab.ticklabel_format( axis = 'both', style = 'sci', scilimits = ( 0, 0 ) )
pylab.legend( loc = 0 )
pylab.grid( True )
pylab.xlabel( "Time" )
pylab.ylabel( "Current" )
pylab.tight_layout()
pylab.savefig( "CurrentCombine.png" )
pylab.close()
def processData( dataTCT, noiseTCT, verbosity, cut = "signal" ):
"""Different steps to finally get the charge from the waveforms
"""
convertAndShiftData( dataTCT )
removeElectronicNoise( dataTCT, noiseTCT )
averageTheData( dataTCT )
cutData( dataTCT, cut )
charge = getCharge( dataTCT )
corr_charge = []
current, corr_curr = getCurrent(dataTCT)
#if verbosity:
#plotAnalysis( dataTCT, current, corr_curr )
#plotResults( dataTCT, current, corr_curr )
return charge, current, corr_curr
def natSorting( results ):
"""Natural sorting to save the data in the results file
"""
indexes = {}
sorted_data = []
for line in results:
indexes[ int( line.split( "\t" )[ 0 ] ) ] = line
for key in sorted( indexes.keys() )[ : : -1 ]:
sorted_data.append( indexes[ key ] )
return sorted_data
def parseador():
parser = argparse.ArgumentParser( description = "TCT analysis" )
parser.add_argument( "-f", "--files", type = str, nargs = "+", help = "binary data files" )
parser.add_argument( "-n", "--noise_file", type = str, help = "electronic noise file" )
parser.add_argument( "-b", "--bulk", type = str, choices = [ "p", "n" ], default = "n", help = "bulk of the sensor. Choices = n, p. Default = %(default)s", metavar = "" )
parser.add_argument( "-i", "--integration_window", type = str, choices = [ "signal", "peak", "whole" ], default = "signal", help = "definiton of the integration window, 50ns since the beginning of the peak or only the peak. Choices = signal, peak. Default = %(default)s", metavar = "" )
parser.add_argument( "-v", "--verbosity", type = int, required = False, default = 1, help = "Verbosity, plots for individual scans are produced only when the verbosity is on. Choices = 0, 1", metavar = "" )
args = parser.parse_args()
return args
def getData( dataTCT ):
"""Binary reader and converter to ascii data.
"""
binreader = BINReader( dataTCT.filename )
binreader.readData()
dataTCT.header = binreader.header
dataTCT.rawData = binreader.data
binreader.close()
def strmResults( charge ):
"""Plot the results in the screen to monitor while
the program is running.
"""
#print "Charge: %eC" % charge
pairs = charge / ( 1.602176e-19 * 2 )
#print "Pairs: %d" % pairs
mips = charge / ( 1.602176e-19 * 2 * 80 * 300 )
#print "MIPS(300um): %.2f" % mips
return pairs, mips
def writeResults( filename, header, results ):
"""Function to write the results in a txt file.
"""
if os.path.isfile( filename ):
fd = open( filename, "a" )
else:
fd = open( filename, "w" )
fd.write( header )
fd.writelines( results )
fd.close()
def main( files, noiseFile, bulk, integrationWindow, verbosity ):
results = []
dataTCT = []
noiseTCT = TCTData( noiseFile )
noiseTCT.bulk = bulk
getData( noiseTCT )
convertAndShiftData( noiseTCT )
count = 0
current_tot = []
corr_curr_tot = []
xdata_tot = []
init_tot = []
end_tot = []
charge_tot = []
filename_tot = []
for filename in files:
dataTCT.append( TCTData( filename ) )
dataTCT[ -1 ].bulk = bulk
voltage = [ value for value in dataTCT[ -1 ].filename.split( "_" ) if value[ 0 ] == "V" and value[ -1 ] == "V" ][ 0 ].replace( "V", "" )
getData( dataTCT[ -1 ] )
charge, current, corr_curr = processData( dataTCT[ -1 ], noiseTCT, verbosity )
pairs, mips = strmResults( charge )
if (verbosity or count % 100 == 0):
print "File: %s" % dataTCT[ -1 ].filename
print "Charge: %eC" % charge
print "Pairs: %d" % pairs
print "MIPS(300um): %.2f" % mips
results.append( voltage + "\t" + str( charge ) + "\t" + str( pairs ) + "\t" + str( mips ) + "\n" )
count = count + 1
current_tot.append(current)
corr_curr_tot.append(corr_curr)
xdata_tot.append(dataTCT[-1].xdata)
init_tot.append(dataTCT[-1].initPoint)
end_tot.append(dataTCT[-1].endPoint)
# corr_charge_tot.append(corr_charge / ( 1.602176e-19 * 2 ))
charge_tot.append(charge / ( 1.602176e-19 * 2 ))
filename_tot.append(dataTCT[-1].filename)
plotTotResults(filename_tot, xdata_tot, charge_tot, current_tot, corr_curr_tot, init_tot, end_tot)
results = natSorting( results )
writeResults( "results.txt", "Voltage(V)\tCharge(C)\tPairs\tMIPS\n", results )
if __name__ == "__main__":
args = parseador()
main( args.files, args.noise_file, args.bulk, args.integration_window, args.verbosity )