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

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