Changeset 17979 for fact/tools/pyscripts/new_pyfact/pyfact.py
- Timestamp:
- 10/02/14 20:18:07 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
fact/tools/pyscripts/new_pyfact/pyfact.py
r17978 r17979 10 10 ROOT.gROOT.SetBatch(True) 11 11 12 # ######### BUILDING OF THE SHARED OBJECT FILES ###############################12 #--------- BUILDING OF THE SHARED OBJECT FILES ------------------------------- 13 13 if __name__ == '__main__' and len(sys.argv) > 1 and 'build' in sys.argv[1]: 14 14 ROOT.gSystem.AddLinkedLibs("-lz") 15 15 root_make_string = ROOT.gSystem.GetMakeSharedLib() 16 if not "-std=c++0x"in root_make_string:16 if "-std=c++0x" not in root_make_string: 17 17 make_string = root_make_string.replace('$Opt', '$Opt -std=c++0x -D HAVE_ZLIB') 18 18 ROOT.gSystem.SetMakeSharedLib(make_string) … … 21 21 ROOT.gROOT.ProcessLine(".L extern_Mars_mcore/zfits.h+O") 22 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\"' ") 23 if "-std=c++0x" not in root_make_string: 24 make_string = root_make_string.replace( 25 '$Opt', 26 "$Opt -std=c++0x " 27 "-D HAVE_ZLIB " 28 "-D'PACKAGE_NAME=\"PACKAGE_NAME\"' " 29 "-D'PACKAGE_VERSION=\"PACKAGE_VERSION\"' " 30 "-D'REVISION=\"REVISION\"' " 31 ) 26 32 ROOT.gSystem.SetMakeSharedLib(make_string) 27 33 ROOT.gROOT.ProcessLine(".L extern_Mars_mcore/DrsCalib.h+O") 28 34 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 ############################################################# 35 ROOT.gInterpreter.GenerateDictionary( 36 "map<string,fits::Entry>", 37 "map;string;extern_Mars_mcore/fits.h") 38 ROOT.gInterpreter.GenerateDictionary( 39 "pair<string,fits::Entry>", 40 "map;string;extern_Mars_mcore/fits.h") 41 ROOT.gInterpreter.GenerateDictionary( 42 "map<string,fits::Table::Column>", 43 "map;string;extern_Mars_mcore/fits.h") 44 ROOT.gInterpreter.GenerateDictionary( 45 "pair<string,fits::Table::Column>", 46 "map;string;extern_Mars_mcore/fits.h") 47 ROOT.gInterpreter.GenerateDictionary( 48 "vector<DrsCalibrate::Step>", 49 "vector;extern_Mars_mcore/DrsCalib.h") 50 51 #-------- USAGE ------------------------------------------------------------- 36 52 if __name__ == '__main__' and len(sys.argv) < 3: 37 53 print """ Usage: … … 52 68 sys.exit(1) 53 69 54 # ######## START OF PYFACT MODULE ############################################70 #-------- START OF PYFACT MODULE -------------------------------------------- 55 71 path = os.path.dirname(os.path.realpath(__file__)) 56 72 ROOT.gSystem.Load(path+'/AutoDict_map_string_fits__Entry__cxx.so') … … 66 82 del path 67 83 68 raw_base = '/fact/raw' 69 aux_base = '/fact/aux' 84 env_name = 'FACT_DATA_BASE_DIR' 85 if env_name in os.environ: 86 base_path = os.environ[env_name] 87 raw_base = os.path.join(base_path, 'raw') 88 aux_base = os.path.join(base_path, 'aux') 89 else: 90 raw_base = os.path.join('fact', 'raw') 91 aux_base = os.path.join('fact', 'aux') 70 92 71 93 72 94 def stub_to_auxfile(s, s2=None, basep=aux_base, basen="*"): 73 """ FIXME :-) 95 """ FIXME :-) 74 96 It's not yet clear how to generate aux file names from stubs, or iterables 75 97 """ 76 98 if s2 is not None: 77 s = "{0}_{1:03d}".format(s, s2)99 s = "{0}_{1:03d}".format(s, s2) 78 100 y = s[0:4] 79 101 m = s[4:6] 80 102 d = s[6:8] 81 103 date = s[0:8] 82 r = s[9:12]83 return os.path.join(basep, y,m,d,date+'.'+basen+'.fits')104 #r = s[9:12] 105 return os.path.join(basep, y, m, d, date + '.' + basen + '.fits') 84 106 85 107 86 108 def make_path(s): 87 109 """ make path to fits file 88 110 89 111 s : string or iterable 90 112 91 113 Fits files in pyfact do not need to be specified by full paths 92 114 A so called stub path is sufficient. A valid stub path looks like: … … 96 118 97 119 In addition a file can be specified by a 2-element integer iterable, 98 e.g. a list or a tuple, whose first element is the integer denoting the 120 e.g. a list or a tuple, whose first element is the integer denoting the 99 121 'night' : yyyymmdd 100 122 the 2nd integer is the 'run' : rrr … … 103 125 104 126 the pyfact.Fits class can handle .fits, .fits.gz, and .fits.fz files. 105 stubs dont's specify which one should be used, so we need to check 106 which one exists, if more than one file exists fitting to the stub, 127 stubs dont's specify which one should be used, so we need to check 128 which one exists, if more than one file exists fitting to the stub, 107 129 the order of precendence is: 108 130 * .fits -- since access should be pretty fast … … 116 138 night = int(s[0]) 117 139 run = int(s[1]) 118 _s = "{0:08d}_{1:03d}".format(night, run)140 _s = "{0:08d}_{1:03d}".format(night, run) 119 141 else: 120 142 _s = s … … 122 144 # so now s is some kind of string 123 145 # maybe it is already a path: 124 if os.path.isabs( _s):125 # s is already an absolute path. 146 if os.path.isabs(_s): 147 # s is already an absolute path. 126 148 # maybe the file is not there, but we don't care about that 127 149 return _s 128 150 129 # it was not an absolute path, maybe it's a relative path, let's check if 151 # it was not an absolute path, maybe it's a relative path, let's check if 130 152 # it points to a file. 131 elif os.path.exists( _s):153 elif os.path.exists(_s): 132 154 # s is certainly a path, it even points to a file :-) 133 155 return os.path.abspath(_s) 134 156 135 # okay ... s was not a path, so let's generate a nice absolute path 157 # okay ... s was not a path, so let's generate a nice absolute path 136 158 # from the stub, in case it is a stub .. 137 159 elif re.match('\d{8}_\d{3}', _s): … … 140 162 m = _s[4:6] 141 163 d = _s[6:8] 142 r = _s[9:12]143 start = os.path.join(raw_base, y,m,d,_s[:12]+'.fits')164 #r = _s[9:12] 165 start = os.path.join(raw_base, y, m, d, _s[:12] + '.fits') 144 166 candidates = [start, start+'.fz', start+'.gz'] 145 167 for c in candidates: 146 168 if os.path.exists(c): 147 169 return c 148 # if we reached this point, s was a stub, but we were not 149 # able to find a file on the file system... 150 # let's return our best guess, the '.fz' file path 170 # if we reached this point, s was a stub, but we were not 171 # able to find a file on the file system... 172 # let's return our best guess, the '.fz' file path 151 173 # the Fits.__init__() will complain, if it can't find the file. 152 174 return candidates[1] … … 154 176 raise IOError("{0} can't be used for path generation".format(s)) 155 177 156 class Fits( object ): 178 179 class Fits(object): 157 180 """ General FITS file access 158 181 … … 162 185 """ 163 186 __module__ = 'pyfact' 187 164 188 def __init__(self, path): 165 189 """ Open fits file, parse header and setup table colums … … 178 202 def __repr__(self): 179 203 return '{s.__class__.__name__}(path="{s._path}")'.format(s=self) 180 204 181 205 def __str__(self): 182 s = """Fits object: 206 s = """Fits object: 183 207 path = {s._path} 184 208 row = {s._current_row} … … 187 211 return s 188 212 189 190 213 def __len__(self): 191 214 """ return the number of events, in case it's a FACT physics data file … … 197 220 """ 198 221 """ 199 str_to_bool = { 'T':True, 'F':False}200 type_conversion = { 'I' : int, 'F' : float, 'T' : str, 'B': str_to_bool.__getitem__}222 str_to_bool = {'T': True, 'F': False} 223 type_conversion = {'I': int, 'F': float, 'T': str, 'B': str_to_bool.__getitem__} 201 224 202 225 self.header = {} 203 226 self.header_comments = {} 204 for key, entry in self.f.GetKeys():227 for key, entry in self.f.GetKeys(): 205 228 try: 206 229 self.header[key] = type_conversion[entry.type](entry.value) 207 230 except KeyError: 208 raise IOError("Error: entry type unknown.\n Is %s, but should be one of: [I,F,T,B]" % (entry.type) ) 231 raise IOError( 232 "Error: entry type unknown.\n " 233 "Is %s, but should be one of: [I,F,T,B]" % (entry.type)) 209 234 self.header_comments[key] = entry.comment 210 235 … … 212 237 """ 213 238 """ 214 col_type_to_np_type_map = { 'L' : 'b1', 'A' : 'a1', 'B' : 'i1', 215 'I' : 'i2', 'J' : 'i4', 'K' : 'i8', 'E' : 'f4', 'D' : 'f8'} 239 col_type_to_np_type_map = { 240 'L': 'b1', 241 'A': 'a1', 242 'B': 'i1', 243 'I': 'i2', 244 'J': 'i4', 245 'K': 'i8', 246 'E': 'f4', 247 'D': 'f8'} 216 248 self.cols = {} 217 for key, col in self.f.GetColumns():249 for key, col in self.f.GetColumns(): 218 250 self.cols[key] = np.zeros(col.num, col_type_to_np_type_map[col.type]) 219 251 if col.num != 0: … … 231 263 else: 232 264 self._current_row += 1 233 if self.f.GetNextRow() ==False:265 if self.f.GetNextRow() is False: 234 266 self._current_row = None 235 267 raise StopIteration … … 237 269 row = int(row) 238 270 self._current_row = row 239 if self.f.GetRow(row) ==False:271 if self.f.GetRow(row) is False: 240 272 self._current_row = None 241 273 raise StopIteration 242 274 return self 243 275 244 class AuxFile( Fits ): 276 277 class AuxFile(Fits): 245 278 """ easy(?) access to FACT aux files 246 279 """ 247 280 __module__ = 'pyfact' 281 248 282 def __init__(self, path, verbose=False): 249 283 self._verbose = verbose … … 251 285 252 286 def _setup_columns(self): 253 col_type_to_np_type_map = { 'L' : 'b1', 'A' : 'a1', 'B' : 'i1', 254 'I' : 'i2', 'J' : 'i4', 'K' : 'i8', 'E' : 'f4', 'D' : 'f8'} 287 col_type_to_np_type_map = { 288 'L': 'b1', 289 'A': 'a1', 290 'B': 'i1', 291 'I': 'i2', 292 'J': 'i4', 293 'K': 'i8', 294 'E': 'f4', 295 'D': 'f8'} 255 296 self._cols = {} 256 297 self.cols = {} 257 298 258 299 N = len(self) 259 for key, col in self.f.GetColumns():300 for key, col in self.f.GetColumns(): 260 301 self._cols[key] = np.zeros(col.num, col_type_to_np_type_map[col.type]) 261 302 self.cols[key] = np.zeros((N, col.num), col_type_to_np_type_map[col.type]) … … 263 304 self.f.SetPtrAddress(key, self._cols[key]) 264 305 265 266 for i,row in enumerate(self): 306 for i, row in enumerate(self): 267 307 if self._verbose: 268 308 try: … … 274 314 275 315 for key in self._cols: 276 self.cols[key][i, :] = self._cols[key]277 278 279 class RawData( Fits):316 self.cols[key][i, :] = self._cols[key] 317 318 319 class RawData(Fits): 280 320 """ Special raw data FITS file access (with DRS4 calibration) 281 321 282 322 During iteration the C++ method DrsCalibration::Apply is being called. 283 323 """ 284 __module__='pyfact' 324 __module__ = 'pyfact' 325 285 326 def __init__(self, data_path, calib_path): 286 327 """ -constructor- … … 289 330 """ 290 331 super(RawData, self).__init__(data_path) 291 self.cols['CalibData'] = np.zeros( 292 self.cols['CalibData2D'] = self.cols['CalibData'].reshape( 332 self.cols['CalibData'] = np.zeros(self.cols['Data'].shape, np.float32) 333 self.cols['CalibData2D'] = self.cols['CalibData'].reshape(self.header['NPIX'], -1) 293 334 if not self.cols['CalibData2D'].base is self.cols['CalibData']: 294 335 print "Error seomthing went wrong!" 295 336 self.drs_calibration = ROOT.DrsCalibration() 296 self.drs_calibration.ReadFitsImp( calib_path)337 self.drs_calibration.ReadFitsImp(calib_path) 297 338 298 339 self.drs_calibrate = ROOT.DrsCalibrate() … … 308 349 super(RawData, self).next(row) 309 350 310 self.drs_calibration.Apply( self.cols['CalibData'], 311 self.cols['Data'], 312 self.cols['StartCellData'], 313 self.header['NROI']) 351 self.drs_calibration.Apply( 352 self.cols['CalibData'], 353 self.cols['Data'], 354 self.cols['StartCellData'], 355 self.header['NROI'] 356 ) 314 357 315 358 for previous_start_cells in self.list_of_previous_start_cells: 316 359 self.drs_calibrate.CorrectStep( 317 318 319 320 321 322 360 self.cols['CalibData'], 361 self.header['NPIX'], 362 self.header['NROI'], 363 previous_start_cells, 364 self.cols['StartCellData'], 365 self.header['NROI']+10) 323 366 self.drs_calibrate.CorrectStep( 324 325 326 327 328 329 367 self.cols['CalibData'], 368 self.header['NPIX'], 369 self.header['NROI'], 370 previous_start_cells, 371 self.cols['StartCellData'], 372 3) 330 373 self.list_of_previous_start_cells.append(self.cols['StartCellData']) 331 374 if len(self.list_of_previous_start_cells) > 5: … … 336 379 337 380 return self 338 339 381 340 382 … … 348 390 print "The files has these cols:", f.cols.keys() 349 391 350 for counter, row in enumerate(f):392 for counter, row in enumerate(f): 351 393 print "Event Id:", row.cols['EventNum'] 352 394 print "shape of column 'StartCellData'", row.cols['StartCellData'].shape
Note:
See TracChangeset
for help on using the changeset viewer.