source: fact/tools/pyscripts/pyfact/pyfact_rename.py@ 13076

Last change on this file since 13076 was 13076, checked in by neise, 13 years ago
added function __add__ to RawData. one can now jump over a certain amout of events, by doing: e.g. run=run+34
  • Property svn:executable set to *
File size: 9.6 KB
Line 
1#!/usr/bin/python
2#
3# Werner Lustermann
4# ETH Zurich
5#
6from ctypes import *
7import numpy as np
8from scipy import signal
9
10# get the ROOT stuff + my shared libs
11from ROOT import gSystem
12# fitslib.so is made from fits.h and is used to access the data
13gSystem.Load('~/py/fitslib.so')
14from ROOT import *
15
16
17class 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 #: time when the FAD was triggered, in some strange units...
78 self.board_times = np.zeros( 40, np.int32 )
79
80 # set the pointers to the data++
81 data_file.SetPtrAddress('EventNum', self.event_id)
82 data_file.SetPtrAddress('TriggerType', self.trigger_type)
83 data_file.SetPtrAddress('StartCellData', self.start_cells)
84 data_file.SetPtrAddress('Data', self.data)
85 data_file.SetPtrAddress('BoardTime', self.board_times)
86
87 # open the calibration file
88 try:
89 calib_file = fits(self.calib_file_name)
90 except IOError:
91 print 'problem accessing calibration file: ', calib_file_name
92 raise
93 #: drs calibration file
94 self.calib_file = calib_file
95
96 baseline_mean = calib_file.GetN('BaselineMean')
97 gain_mean = calib_file.GetN('GainMean')
98 trigger_offset_mean = calib_file.GetN('TriggerOffsetMean')
99
100 self.blm = np.zeros(baseline_mean, np.float32)
101 self.gm = np.zeros(gain_mean, np.float32)
102 self.tom = np.zeros(trigger_offset_mean, np.float32)
103
104 self.Nblm = baseline_mean / self.npix
105 self.Ngm = gain_mean / self.npix
106 self.Ntom = trigger_offset_mean / self.npix
107
108 calib_file.SetPtrAddress('BaselineMean', self.blm)
109 calib_file.SetPtrAddress('GainMean', self.gm)
110 calib_file.SetPtrAddress('TriggerOffsetMean', self.tom)
111 calib_file.GetRow(0)
112
113 self.v_bsl = np.zeros(self.npix) # array of baseline values (all ZERO)
114 self.data_saverage_out = None
115 self.pulse_time_of_maximum = None
116 self.pulse_amplitude = None
117
118 def __iter__(self):
119 """ iterator """
120 return self
121
122 def __add__(self, jump_over):
123 self.data_file.GetRow(jump_over)
124 return self
125
126 def next(self):
127 """ used by __iter__ """
128
129 if self.data_file.GetNextRow() == False:
130 raise StopIteration
131 else:
132 self.calibrate_drs_amplitude()
133
134 #print 'nevents = ', self.nevents, 'event_id = ', self.event_id.value
135 return self.acal_data, self.start_cells, self.trigger_type.value
136
137 def next_event(self):
138 """ load the next event from disk and calibrate it
139
140 """
141
142 self.data_file.GetNextRow()
143 self.calibrate_drs_amplitude()
144
145 def calibrate_drs_amplitude(self):
146 """ perform the drs amplitude calibration of the event data
147
148 """
149
150 to_mV = 2000./4096.
151 #: 2D array with amplitude calibrated dat in mV
152 acal_data = self.data * to_mV # convert ADC counts to mV
153
154 # make 2D arrays: row = pixel, col = drs_slice
155 acal_data = np.reshape(acal_data, (self.npix, self.nroi) )
156 blm = np.reshape(self.blm, (self.npix, self.Nblm) )
157 tom = np.reshape(self.tom, (self.npix, self.Ntom) )
158 gm = np.reshape(self.gm, (self.npix, self.Ngm) )
159
160 for pixel in range( self.npix ):
161 # rotate the pixel baseline mean to the Data startCell
162 blm_pixel = np.roll( blm[pixel,:], -self.start_cells[pixel] )
163 acal_data[pixel,:] -= blm_pixel[0:self.nroi]
164 acal_data[pixel,:] -= tom[pixel, 0:self.nroi]
165 acal_data[pixel,:] /= gm[pixel, 0:self.nroi]
166
167 self.acal_data = acal_data * 1907.35
168
169
170 def baseline_read_values(self, file, bsl_hist='bsl_sum/hplt_mean'):
171 """
172
173 open ROOT file with baseline histogram and read baseline values
174 file name of the root file
175 bsl_hist path to the histogram containing the basline values
176
177 """
178
179 try:
180 f = TFile(file)
181 except:
182 print 'Baseline data file could not be read: ', file
183 return
184
185 h = f.Get(bsl_hist)
186
187 for i in range(self.npix):
188 self.v_bsl[i] = h.GetBinContent(i+1)
189
190 f.Close()
191
192 def baseline_correct(self):
193 """ subtract baseline from the data
194
195 """
196
197 for pixel in range(self.npix):
198 self.acal_data[pixel,:] -= self.v_bsl[pixel]
199
200 def info(self):
201 """ print run information
202
203 """
204
205 print 'data file: ', data_file_name
206 print 'calib file: ', calib_file_name
207 print 'calibration file'
208 print 'N baseline_mean: ', self.Nblm
209 print 'N gain mean: ', self.Ngm
210 print 'N TriggeroffsetMean: ', self.Ntom
211
212# -----------------------------------------------------------------------------
213class fnames( object ):
214 """ organize file names of a FACT data run
215
216 """
217
218 def __init__(self, specifier = ['012', '023', '2011', '11', '24'],
219 rpath = '/scratch_nfs/res/bsl/',
220 zipped = True):
221 """
222 specifier : list of strings defined as:
223 [ 'DRS calibration file', 'Data file', 'YYYY', 'MM', 'DD']
224
225 rpath : directory path for the results; YYYYMMDD will be appended to rpath
226 zipped : use zipped (True) or unzipped (Data)
227
228 """
229
230 self.specifier = specifier
231 self.rpath = rpath
232 self.zipped = zipped
233
234 self.make( self.specifier, self.rpath, self.zipped )
235
236
237 def make( self, specifier, rpath, zipped ):
238 """ create (make) the filenames
239
240 names : dictionary of filenames, tags { 'data', 'drscal', 'results' }
241 data : name of the data file
242 drscal : name of the drs calibration file
243 results : radikal of file name(s) for results (to be completed by suffixes)
244 """
245
246 self.specifier = specifier
247
248 if zipped:
249 dpath = '/data00/fact-construction/raw/'
250 ext = '.fits.gz'
251 else:
252 dpath = '/data03/fact-construction/raw/'
253 ext = '.fits'
254
255 year = specifier[2]
256 month = specifier[3]
257 day = specifier[4]
258
259 yyyymmdd = year + month + day
260 dfile = specifier[1]
261 cfile = specifier[0]
262
263 rpath = rpath + yyyymmdd + '/'
264 self.rpath = rpath
265 self.names = {}
266
267 tmp = dpath + year + '/' + month + '/' + day + '/' + yyyymmdd + '_'
268 self.names['data'] = tmp + dfile + ext
269 self.names['drscal'] = tmp + cfile + '.drs' + ext
270 self.names['results'] = rpath + yyyymmdd + '_' + dfile + '_' + cfile
271
272 self.data = self.names['data']
273 self.drscal = self.names['drscal']
274 self.results = self.names['results']
275
276 def info( self ):
277 """ print complete filenames
278
279 """
280
281 print 'file names:'
282 print 'data: ', self.names['data']
283 print 'drs-cal: ', self.names['drscal']
284 print 'results: ', self.names['results']
285
286# end of class definition: fnames( object )
287
288def _test_iter():
289 """ test for function __iter__ """
290
291 data_file_name = '/data00/fact-construction/raw/2011/11/24/20111124_111.fits.gz'
292 calib_file_name = '/data00/fact-construction/raw/2011/11/24/20111124_111.drs.fits.gz'
293 run = RawData( data_file_name, calib_file_name )
294
295 for data, scell, tt in run:
296 print 'data[0,0] = ', data[0,0], "start_cell[0] =", scell[0], "trigger type = ", tt
297
298
299if __name__ == '__main__':
300 """ tests """
301
302 _test_iter()
Note: See TracBrowser for help on using the repository browser.