1 | #!/usr/bin/python -tt
|
---|
2 | #
|
---|
3 | # Werner Lustermann, Dominik Neise
|
---|
4 | # ETH Zurich, TU Dortmund
|
---|
5 | #
|
---|
6 | import os
|
---|
7 | import sys
|
---|
8 | import numpy as np
|
---|
9 | import ROOT
|
---|
10 |
|
---|
11 |
|
---|
12 | ########## BUILDING OF THE SHARED OBJECT FILES ###############################
|
---|
13 | if __name__ == '__main__' and len(sys.argv) > 1 and 'build' in sys.argv[1]:
|
---|
14 | hostname = ROOT.gSystem.HostName()
|
---|
15 | libz_path_dict = {
|
---|
16 | # hostname : /path/to/libz.so
|
---|
17 | 'isdc' : "/usr/lib64/libz.so",
|
---|
18 | 'neiseLenovo' : "/usr/lib/libz.so",
|
---|
19 | 'factcontrol' : "/usr/lib/libz.so",
|
---|
20 | "max-K50AB" : "/usr/lib/x86_64-linux-gnu/libz.so",
|
---|
21 | "watz" : "/usr/lib/x86_64-linux-gnu/libz.so",
|
---|
22 | "grolsch" : "/usr/lib/i386-linux-gnu/libz.so",
|
---|
23 | }
|
---|
24 | libz_loaded = False
|
---|
25 | for my_hostname in libz_path_dict:
|
---|
26 | if my_hostname in hostname:
|
---|
27 | ROOT.gSystem.Load(libz_path_dict[my_hostname])
|
---|
28 | libz_loaded = True
|
---|
29 | if not libz_loaded:
|
---|
30 | print """Warning - Warning - Warning - Warning - Warning - Warning - Warning
|
---|
31 | I most probably need to load libz.so but I don't know where it is.
|
---|
32 |
|
---|
33 | Please edit pyfact.py around line 16-24 and insert your hostname and your
|
---|
34 | path to your libz.so
|
---|
35 | Sorry for the inconvenience.
|
---|
36 | """
|
---|
37 | sys.exit(-1)
|
---|
38 |
|
---|
39 |
|
---|
40 | root_make_string = ROOT.gSystem.GetMakeSharedLib()
|
---|
41 | if not "-std=c++0x" in root_make_string:
|
---|
42 | make_string = root_make_string.replace('$Opt', '$Opt -std=c++0x -D HAVE_ZLIB')
|
---|
43 | ROOT.gSystem.SetMakeSharedLib(make_string)
|
---|
44 | ROOT.gROOT.ProcessLine(".L extern_Mars_mcore/izstream.h+O")
|
---|
45 | ROOT.gROOT.ProcessLine(".L extern_Mars_mcore/fits.h+O")
|
---|
46 | ROOT.gROOT.ProcessLine(".L extern_Mars_mcore/zfits.h+O")
|
---|
47 | ROOT.gROOT.ProcessLine(".L extern_Mars_mcore/factfits.h+O")
|
---|
48 | if not "-std=c++0x" in root_make_string:
|
---|
49 | make_string = root_make_string.replace('$Opt', "$Opt -std=c++0x -D HAVE_ZLIB -D'PACKAGE_NAME=\"PACKAGE_NAME\"' "
|
---|
50 | "-D'PACKAGE_VERSION=\"PACKAGE_VERSION\"' -D'REVISION=\"REVISION\"' ")
|
---|
51 | ROOT.gSystem.SetMakeSharedLib(make_string)
|
---|
52 | ROOT.gROOT.ProcessLine(".L extern_Mars_mcore/DrsCalib.h+O")
|
---|
53 |
|
---|
54 | ROOT.gInterpreter.GenerateDictionary("map<string,fits::Entry>","map;string;extern_Mars_mcore/fits.h")
|
---|
55 | ROOT.gInterpreter.GenerateDictionary("pair<string,fits::Entry>","map;string;extern_Mars_mcore/fits.h")
|
---|
56 | ROOT.gInterpreter.GenerateDictionary("map<string,fits::Table::Column>","map;string;extern_Mars_mcore/fits.h")
|
---|
57 | ROOT.gInterpreter.GenerateDictionary("pair<string,fits::Table::Column>","map;string;extern_Mars_mcore/fits.h")
|
---|
58 | ROOT.gInterpreter.GenerateDictionary("vector<DrsCalibrate::Step>","vector;extern_Mars_mcore/DrsCalib.h")
|
---|
59 |
|
---|
60 | ########## USAGE #############################################################
|
---|
61 | if __name__ == '__main__' and len(sys.argv) < 3:
|
---|
62 | print """ Usage:
|
---|
63 | ----------------------------------------------------------------------
|
---|
64 | To just build the shared object libs call:
|
---|
65 | python pyfact.py build
|
---|
66 |
|
---|
67 | To build (if necessary) and open an example file
|
---|
68 | python -i pyfact.py /path/to/data_file.zfits /path/to/calib_file.drs.fits.gz
|
---|
69 |
|
---|
70 | Any of the 3 file 'types': fits.gz zfits fits
|
---|
71 | should be supported.
|
---|
72 |
|
---|
73 | To clean all of automatically built files, do something like:
|
---|
74 | rm *.so *.d AutoDict_* extern_Mars_mcore/*.so extern_Mars_mcore/*.d
|
---|
75 | ----------------------------------------------------------------------
|
---|
76 | """
|
---|
77 | sys.exit(1)
|
---|
78 |
|
---|
79 | ######### START OF PYFACT MODULE ############################################
|
---|
80 | path = os.path.dirname(os.path.realpath(__file__))
|
---|
81 | ROOT.gSystem.Load(path+'/AutoDict_map_string_fits__Entry__cxx.so')
|
---|
82 | ROOT.gSystem.Load(path+'/AutoDict_map_string_fits__Table__Column__cxx.so')
|
---|
83 | ROOT.gSystem.Load(path+'/AutoDict_pair_string_fits__Entry__cxx.so')
|
---|
84 | ROOT.gSystem.Load(path+'/AutoDict_pair_string_fits__Table__Column__cxx.so')
|
---|
85 | ROOT.gSystem.Load(path+'/AutoDict_vector_DrsCalibrate__Step__cxx.so')
|
---|
86 | ROOT.gSystem.Load(path+'/extern_Mars_mcore/fits_h.so')
|
---|
87 | ROOT.gSystem.Load(path+'/extern_Mars_mcore/izstream_h.so')
|
---|
88 | ROOT.gSystem.Load(path+'/extern_Mars_mcore/zfits_h.so')
|
---|
89 | ROOT.gSystem.Load(path+'/extern_Mars_mcore/factfits_h.so')
|
---|
90 | ROOT.gSystem.Load(path+'/extern_Mars_mcore/DrsCalib_h.so')
|
---|
91 | del path
|
---|
92 |
|
---|
93 | class Fits( object ):
|
---|
94 | """ General FITS file access
|
---|
95 |
|
---|
96 | Wrapper for factfits class from Mars/mcore/factfits.h
|
---|
97 | (factfits might not be suited to read any FITS file out there, but certainly
|
---|
98 | all FACT FITS files can be read using this class)
|
---|
99 | """
|
---|
100 | __module__ = 'pyfact'
|
---|
101 | def __init__(self, path):
|
---|
102 | """
|
---|
103 | """
|
---|
104 | if not os.path.exists(path):
|
---|
105 | raise IOError(path+' was not found')
|
---|
106 | self.f = ROOT.factfits(path)
|
---|
107 | self._make_header()
|
---|
108 | self._setup_columns()
|
---|
109 |
|
---|
110 | def _make_header(self):
|
---|
111 | """
|
---|
112 | """
|
---|
113 | str_to_bool = { 'T':True, 'F':False}
|
---|
114 | type_conversion = { 'I' : int, 'F' : float, 'T' : str, 'B' : str_to_bool.__getitem__}
|
---|
115 |
|
---|
116 | self.header = {}
|
---|
117 | self.header_comments = {}
|
---|
118 | for key,entry in self.f.GetKeys():
|
---|
119 | try:
|
---|
120 | self.header[key] = type_conversion[entry.type](entry.value)
|
---|
121 | except KeyError:
|
---|
122 | raise IOError("Error: entry type unknown.\n Is %s, but should be one of: [I,F,T,B]" % (entry.type) )
|
---|
123 | self.header_comments[key] = entry.comment
|
---|
124 |
|
---|
125 | def _setup_columns(self):
|
---|
126 | """
|
---|
127 | """
|
---|
128 | col_type_to_np_type_map = { 'L' : 'b1', 'A' : 'a1', 'B' : 'i1',
|
---|
129 | 'I' : 'i2', 'J' : 'i4', 'K' : 'i8', 'E' : 'f4', 'D' : 'f8'}
|
---|
130 | self.cols = {}
|
---|
131 | for key,col in self.f.GetColumns():
|
---|
132 | self.cols[key] = np.zeros(col.num, col_type_to_np_type_map[col.type])
|
---|
133 | if col.num != 0:
|
---|
134 | self.f.SetPtrAddress(key, self.cols[key])
|
---|
135 |
|
---|
136 | def __iter__(self):
|
---|
137 | return self
|
---|
138 |
|
---|
139 | def next(self, row=None):
|
---|
140 | """
|
---|
141 | """
|
---|
142 | if row is None:
|
---|
143 | if self.f.GetNextRow() == False:
|
---|
144 | raise StopIteration
|
---|
145 | else:
|
---|
146 | row = int(row)
|
---|
147 | if self.f.GetRow(row) == False:
|
---|
148 | raise StopIteration
|
---|
149 | return self
|
---|
150 |
|
---|
151 |
|
---|
152 |
|
---|
153 | class RawData( Fits ):
|
---|
154 | """ Special raw data FITS file access (with DRS4 calibration)
|
---|
155 |
|
---|
156 | During iteration the C++ method DrsCalibration::Apply is being called.
|
---|
157 | """
|
---|
158 | __module__='pyfact'
|
---|
159 | def __init__(self, data_path, calib_path):
|
---|
160 | """ -constructor-
|
---|
161 | *data_path* : fits or fits.gz file of the data including the path
|
---|
162 | *calib_path* : fits or fits.gz file containing DRS calibration data
|
---|
163 | """
|
---|
164 | super(RawData, self).__init__(data_path)
|
---|
165 | self.cols['CalibData'] = np.zeros( self.cols['Data'].shape, np.float32)
|
---|
166 | self.cols['CalibData2D'] = self.cols['CalibData'].reshape( self.header['NPIX'], -1)
|
---|
167 | if not self.cols['CalibData2D'].base is self.cols['CalibData']:
|
---|
168 | print "Error seomthing went wrong!"
|
---|
169 | self.drs_calibration = ROOT.DrsCalibration()
|
---|
170 | self.drs_calibration.ReadFitsImp( calib_path )
|
---|
171 |
|
---|
172 | self.drs_calibrate = ROOT.DrsCalibrate()
|
---|
173 | self.list_of_previous_start_cells = []
|
---|
174 |
|
---|
175 | def __iter__(self):
|
---|
176 | """ iterator """
|
---|
177 | return self
|
---|
178 |
|
---|
179 | def next(self, row=None):
|
---|
180 | """
|
---|
181 | """
|
---|
182 | super(RawData, self).next(row)
|
---|
183 |
|
---|
184 | self.drs_calibration.Apply( self.cols['CalibData'],
|
---|
185 | self.cols['Data'],
|
---|
186 | self.cols['StartCellData'],
|
---|
187 | self.header['NROI'])
|
---|
188 |
|
---|
189 | for previous_start_cells in self.list_of_previous_start_cells:
|
---|
190 | self.drs_calibrate.CorrectStep(
|
---|
191 | self.cols['CalibData'],
|
---|
192 | self.header['NPIX'],
|
---|
193 | self.header['NROI'],
|
---|
194 | previous_start_cells,
|
---|
195 | self.cols['StartCellData'],
|
---|
196 | self.header['NROI']+10)
|
---|
197 | self.drs_calibrate.CorrectStep(
|
---|
198 | self.cols['CalibData'],
|
---|
199 | self.header['NPIX'],
|
---|
200 | self.header['NROI'],
|
---|
201 | previous_start_cells,
|
---|
202 | self.cols['StartCellData'],
|
---|
203 | 3)
|
---|
204 | self.list_of_previous_start_cells.append(self.cols['StartCellData'])
|
---|
205 | if len(self.list_of_previous_start_cells) > 5:
|
---|
206 | self.list_of_previous_start_cells.pop(0)
|
---|
207 |
|
---|
208 | for ch in range(self.header['NPIX']):
|
---|
209 | self.drs_calibrate.RemoveSpikes3(self.cols['CalibData2D'][ch], self.header['NROI'])
|
---|
210 |
|
---|
211 | return self
|
---|
212 |
|
---|
213 |
|
---|
214 |
|
---|
215 | if __name__ == '__main__':
|
---|
216 | """ Example """
|
---|
217 | print "Example for calibrated raw-file"
|
---|
218 | f = RawData(sys.argv[1], sys.argv[2])
|
---|
219 |
|
---|
220 | print "number of events:", f.header['NAXIS2']
|
---|
221 | print "date of observation:", f.header['DATE']
|
---|
222 | print "The files has these cols:", f.cols.keys()
|
---|
223 |
|
---|
224 | for counter,row in enumerate(f):
|
---|
225 | print "Event Id:", row.cols['EventNum']
|
---|
226 | print "shape of column 'StartCellData'", row.cols['StartCellData'].shape
|
---|
227 | print "dtype of column 'Data'", row.cols['StartCellData'].dtype
|
---|
228 | if counter > 3:
|
---|
229 | break
|
---|
230 | # get next row
|
---|
231 | f.next()
|
---|
232 | print "Event Id:", f.cols['EventNum']
|
---|
233 | # get another row
|
---|
234 | f.next(10)
|
---|
235 | print "Event Id:", f.cols['EventNum']
|
---|
236 | # Go back again
|
---|
237 | f.next(3)
|
---|
238 | print "Event Id:", f.cols['EventNum']
|
---|
239 |
|
---|
240 | import matplotlib.pyplot as plt
|
---|
241 | plt.ion()
|
---|
242 | for i in range(f.header['NPIX']):
|
---|
243 | plt.cla()
|
---|
244 | plt.title("Event %d"%(f.cols['EventNum']))
|
---|
245 | plt.plot(f.cols['CalibData2D'][i], '.:', label='pixel %d'%i)
|
---|
246 | plt.legend()
|
---|
247 | answer = raw_input('anykey for next pixel; "q" to quit. :')
|
---|
248 | if 'q' in answer.lower():
|
---|
249 | break
|
---|
250 |
|
---|