1 | #!/usr/bin/python
|
---|
2 | #
|
---|
3 | # Werner Lustermann
|
---|
4 | # ETH Zurich
|
---|
5 | #
|
---|
6 | from ctypes import *
|
---|
7 | import numpy as np
|
---|
8 | from scipy import signal
|
---|
9 |
|
---|
10 | # get the ROOT stuff + my shared libs
|
---|
11 | from ROOT import gSystem
|
---|
12 | # fitslib.so is made from fits.h and is used to access the data
|
---|
13 | gSystem.Load('~/py/fitslib.so')
|
---|
14 | from ROOT import *
|
---|
15 |
|
---|
16 |
|
---|
17 | class RawData( object ):
|
---|
18 | """ raw data access and calibration
|
---|
19 |
|
---|
20 | - open raw data file and drs calibration file
|
---|
21 | - performs amplitude calibration
|
---|
22 | - performs baseline substraction if wanted
|
---|
23 | - provides all data in an array:
|
---|
24 | row = number of pixel
|
---|
25 | col = length of region of interest
|
---|
26 |
|
---|
27 | """
|
---|
28 |
|
---|
29 | def __init__(self, data_file_name,
|
---|
30 | calib_file_name, baseline_file_name=''):
|
---|
31 | """ initialize object
|
---|
32 |
|
---|
33 | open data file and calibration data file
|
---|
34 | get basic information about the data in data_file_name
|
---|
35 | allocate buffers for data access
|
---|
36 |
|
---|
37 | data_file_name : fits or fits.gz file of the data including the path
|
---|
38 | calib_file_name : fits or fits.gz file containing DRS calibration data
|
---|
39 | baseline_file_name : npy file containing the baseline values
|
---|
40 |
|
---|
41 | """
|
---|
42 |
|
---|
43 | self.data_file_name = data_file_name
|
---|
44 | self.calib_file_name = calib_file_name
|
---|
45 | self.baseline_file_name = baseline_file_name
|
---|
46 |
|
---|
47 | # baseline correction: True / False
|
---|
48 | if len(baseline_file_name) == 0:
|
---|
49 | self.correct_baseline = False
|
---|
50 | else:
|
---|
51 | self.correct_baseline = True
|
---|
52 |
|
---|
53 | # access data file
|
---|
54 | try:
|
---|
55 | data_file = fits(self.data_file_name)
|
---|
56 | except IOError:
|
---|
57 | print 'problem accessing data file: ', data_file_name
|
---|
58 | raise # stop ! no data
|
---|
59 | #: data file (fits object)
|
---|
60 | self.data_file = data_file
|
---|
61 |
|
---|
62 | # get basic information about the data file
|
---|
63 | #: region of interest (number of DRS slices read)
|
---|
64 | self.nroi = data_file.GetUInt('NROI')
|
---|
65 | #: number of pixels (should be 1440)
|
---|
66 | self.npix = data_file.GetUInt('NPIX')
|
---|
67 | #: number of events in the data run
|
---|
68 | self.nevents = data_file.GetNumRows()
|
---|
69 |
|
---|
70 | # allocate the data memories
|
---|
71 | self.event_id = c_ulong()
|
---|
72 | self.trigger_type = c_ushort()
|
---|
73 | #: 1D array with raw data
|
---|
74 | self.data = np.zeros( self.npix * self.nroi, np.int16 )
|
---|
75 | #: slice where drs readout started
|
---|
76 | self.start_cells = np.zeros( self.npix, np.int16 )
|
---|
77 |
|
---|
78 | # set the pointers to the data++
|
---|
79 | data_file.SetPtrAddress('EventNum', self.event_id)
|
---|
80 | data_file.SetPtrAddress('TriggerType', self.trigger_type)
|
---|
81 | data_file.SetPtrAddress('StartCellData', self.start_cells)
|
---|
82 | data_file.SetPtrAddress('Data', self.data)
|
---|
83 |
|
---|
84 | # open the calibration file
|
---|
85 | try:
|
---|
86 | calib_file = fits(self.calib_file_name)
|
---|
87 | except IOError:
|
---|
88 | print 'problem accessing calibration file: ', calib_file_name
|
---|
89 | raise
|
---|
90 | #: drs calibration file
|
---|
91 | self.calib_file = calib_file
|
---|
92 |
|
---|
93 | baseline_mean = calib_file.GetN('BaselineMean')
|
---|
94 | gain_mean = calib_file.GetN('GainMean')
|
---|
95 | trigger_offset_mean = calib_file.GetN('TriggerOffsetMean')
|
---|
96 |
|
---|
97 | self.blm = np.zeros(baseline_mean, np.float32)
|
---|
98 | self.gm = np.zeros(gain_mean, np.float32)
|
---|
99 | self.tom = np.zeros(trigger_offset_mean, np.float32)
|
---|
100 |
|
---|
101 | self.Nblm = baseline_mean / self.npix
|
---|
102 | self.Ngm = gain_mean / self.npix
|
---|
103 | self.Ntom = trigger_offset_mean / self.npix
|
---|
104 |
|
---|
105 | calib_file.SetPtrAddress('BaselineMean', self.blm)
|
---|
106 | calib_file.SetPtrAddress('GainMean', self.gm)
|
---|
107 | calib_file.SetPtrAddress('TriggerOffsetMean', self.tom)
|
---|
108 | calib_file.GetRow(0)
|
---|
109 |
|
---|
110 | self.v_bsl = np.zeros(self.npix) # array of baseline values (all ZERO)
|
---|
111 | self.data_saverage_out = None
|
---|
112 | self.pulse_time_of_maximum = None
|
---|
113 | self.pulse_amplitude = None
|
---|
114 |
|
---|
115 | def __iter__(self):
|
---|
116 | """ iterator """
|
---|
117 | return self
|
---|
118 |
|
---|
119 | def next(self):
|
---|
120 | """ used by __iter__ """
|
---|
121 |
|
---|
122 | if self.data_file.GetNextRow() == False:
|
---|
123 | raise StopIteration
|
---|
124 | else:
|
---|
125 | self.calibrate_drs_amplitude()
|
---|
126 |
|
---|
127 | #print 'nevents = ', self.nevents, 'event_id = ', self.event_id.value
|
---|
128 | return self.acal_data, self.start_cells, self.trigger_type.value
|
---|
129 |
|
---|
130 | def next_event(self):
|
---|
131 | """ load the next event from disk and calibrate it
|
---|
132 |
|
---|
133 | """
|
---|
134 |
|
---|
135 | self.data_file.GetNextRow()
|
---|
136 | self.calibrate_drs_amplitude()
|
---|
137 |
|
---|
138 | def calibrate_drs_amplitude(self):
|
---|
139 | """ perform the drs amplitude calibration of the event data
|
---|
140 |
|
---|
141 | """
|
---|
142 |
|
---|
143 | to_mV = 2000./4096.
|
---|
144 | #: 2D array with amplitude calibrated dat in mV
|
---|
145 | acal_data = self.data * to_mV # convert ADC counts to mV
|
---|
146 |
|
---|
147 | # make 2D arrays: row = pixel, col = drs_slice
|
---|
148 | acal_data = np.reshape(acal_data, (self.npix, self.nroi) )
|
---|
149 | blm = np.reshape(self.blm, (self.npix, 1024) )
|
---|
150 | tom = np.reshape(self.tom, (self.npix, 1024) )
|
---|
151 | gm = np.reshape(self.gm, (self.npix, 1024) )
|
---|
152 |
|
---|
153 | for pixel in range( self.npix ):
|
---|
154 | # rotate the pixel baseline mean to the Data startCell
|
---|
155 | blm_pixel = np.roll( blm[pixel,:], -self.start_cells[pixel] )
|
---|
156 | acal_data[pixel,:] -= blm_pixel[0:self.nroi]
|
---|
157 | acal_data[pixel,:] -= tom[pixel, 0:self.nroi]
|
---|
158 | acal_data[pixel,:] /= gm[pixel, 0:self.nroi]
|
---|
159 |
|
---|
160 | self.acal_data = acal_data * 1907.35
|
---|
161 |
|
---|
162 |
|
---|
163 | def baseline_read_values(self, file, bsl_hist='bsl_sum/hplt_mean'):
|
---|
164 | """
|
---|
165 |
|
---|
166 | open ROOT file with baseline histogram and read baseline values
|
---|
167 | file name of the root file
|
---|
168 | bsl_hist path to the histogram containing the basline values
|
---|
169 |
|
---|
170 | """
|
---|
171 |
|
---|
172 | try:
|
---|
173 | f = TFile(file)
|
---|
174 | except:
|
---|
175 | print 'Baseline data file could not be read: ', file
|
---|
176 | return
|
---|
177 |
|
---|
178 | h = f.Get(bsl_hist)
|
---|
179 |
|
---|
180 | for i in range(self.npix):
|
---|
181 | self.v_bsl[i] = h.GetBinContent(i+1)
|
---|
182 |
|
---|
183 | f.Close()
|
---|
184 |
|
---|
185 | def baseline_correct(self):
|
---|
186 | """ subtract baseline from the data
|
---|
187 |
|
---|
188 | """
|
---|
189 |
|
---|
190 | for pixel in range(self.npix):
|
---|
191 | self.acal_data[pixel,:] -= self.v_bsl[pixel]
|
---|
192 |
|
---|
193 | def info(self):
|
---|
194 | """ print run information
|
---|
195 |
|
---|
196 | """
|
---|
197 |
|
---|
198 | print 'data file: ', data_file_name
|
---|
199 | print 'calib file: ', calib_file_name
|
---|
200 | print 'calibration file'
|
---|
201 | print 'N baseline_mean: ', self.Nblm
|
---|
202 | print 'N gain mean: ', self.Ngm
|
---|
203 | print 'N TriggeroffsetMean: ', self.Ntom
|
---|
204 |
|
---|
205 | # -----------------------------------------------------------------------------
|
---|
206 | class fnames( object ):
|
---|
207 | """ organize file names of a FACT data run
|
---|
208 |
|
---|
209 | """
|
---|
210 |
|
---|
211 | def __init__(self, specifier = ['012', '023', '2011', '11', '24'],
|
---|
212 | rpath = '/scratch_nfs/res/bsl/',
|
---|
213 | zipped = True):
|
---|
214 | """
|
---|
215 | specifier : list of strings defined as:
|
---|
216 | [ 'DRS calibration file', 'Data file', 'YYYY', 'MM', 'DD']
|
---|
217 |
|
---|
218 | rpath : directory path for the results; YYYYMMDD will be appended to rpath
|
---|
219 | zipped : use zipped (True) or unzipped (Data)
|
---|
220 |
|
---|
221 | """
|
---|
222 |
|
---|
223 | self.specifier = specifier
|
---|
224 | self.rpath = rpath
|
---|
225 | self.zipped = zipped
|
---|
226 |
|
---|
227 | self.make( self.specifier, self.rpath, self.zipped )
|
---|
228 |
|
---|
229 |
|
---|
230 | def make( self, specifier, rpath, zipped ):
|
---|
231 | """ create (make) the filenames
|
---|
232 |
|
---|
233 | names : dictionary of filenames, tags { 'data', 'drscal', 'results' }
|
---|
234 | data : name of the data file
|
---|
235 | drscal : name of the drs calibration file
|
---|
236 | results : radikal of file name(s) for results (to be completed by suffixes)
|
---|
237 | """
|
---|
238 |
|
---|
239 | self.specifier = specifier
|
---|
240 |
|
---|
241 | if zipped:
|
---|
242 | dpath = '/data00/fact-construction/raw/'
|
---|
243 | ext = '.fits.gz'
|
---|
244 | else:
|
---|
245 | dpath = '/data03/fact-construction/raw/'
|
---|
246 | ext = '.fits'
|
---|
247 |
|
---|
248 | year = specifier[2]
|
---|
249 | month = specifier[3]
|
---|
250 | day = specifier[4]
|
---|
251 |
|
---|
252 | yyyymmdd = year + month + day
|
---|
253 | dfile = specifier[1]
|
---|
254 | cfile = specifier[0]
|
---|
255 |
|
---|
256 | rpath = rpath + yyyymmdd + '/'
|
---|
257 | self.rpath = rpath
|
---|
258 | self.names = {}
|
---|
259 |
|
---|
260 | tmp = dpath + year + '/' + month + '/' + day + '/' + yyyymmdd + '_'
|
---|
261 | self.names['data'] = tmp + dfile + ext
|
---|
262 | self.names['drscal'] = tmp + cfile + '.drs' + ext
|
---|
263 | self.names['results'] = rpath + yyyymmdd + '_' + dfile + '_' + cfile
|
---|
264 |
|
---|
265 | self.data = self.names['data']
|
---|
266 | self.drscal = self.names['drscal']
|
---|
267 | self.results = self.names['results']
|
---|
268 |
|
---|
269 | def info( self ):
|
---|
270 | """ print complete filenames
|
---|
271 |
|
---|
272 | """
|
---|
273 |
|
---|
274 | print 'file names:'
|
---|
275 | print 'data: ', self.names['data']
|
---|
276 | print 'drs-cal: ', self.names['drscal']
|
---|
277 | print 'results: ', self.names['results']
|
---|
278 |
|
---|
279 | # end of class definition: fnames( object )
|
---|
280 |
|
---|
281 | def _test_iter():
|
---|
282 | """ test for function __iter__ """
|
---|
283 |
|
---|
284 | data_file_name = '/data00/fact-construction/raw/2011/11/24/20111124_111.fits.gz'
|
---|
285 | calib_file_name = '/data00/fact-construction/raw/2011/11/24/20111124_111.drs.fits.gz'
|
---|
286 | run = RawData( data_file_name, calib_file_name )
|
---|
287 |
|
---|
288 | for data, scell, tt in run:
|
---|
289 | print 'data[0,0] = ', data[0,0], "start_cell[0] =", scell[0], "trigger type = ", tt
|
---|
290 |
|
---|
291 |
|
---|
292 | if __name__ == '__main__':
|
---|
293 | """ tests """
|
---|
294 |
|
---|
295 | _test_iter()
|
---|