source: fact/tools/pyscripts/new_pyfact/pyfact.py@ 17893

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