Changeset 13356 for fact/tools/pyscripts


Ignore:
Timestamp:
04/17/12 09:16:41 (13 years ago)
Author:
neise
Message:
new class RawDataFeeder initial commit
File:
1 edited

Legend:

Unmodified
Added
Removed
  • fact/tools/pyscripts/pyfact/pyfact.py

    r13329 r13356  
    11#!/usr/bin/python -tt
    22#
    3 # Werner Lustermann
    4 # ETH Zurich
     3# Werner Lustermann, Dominik Neise
     4# ETH Zurich, TU Dortmund
    55#
    66from ctypes import *
     
    1010# get the ROOT stuff + my shared libs
    1111from ROOT import gSystem
    12 # fitslib.so is made from fits.h and is used to access the data
    13 gSystem.Load('fits_h.so')
     12# pyfits_h.so is made from pyfits.h and is used to access the data
     13# make sure the location of pyfits_h.so is in LD_LIBRARY_PATH.
     14# having it in PYTHONPATH is *not* sufficient
     15gSystem.Load('pyfits_h.so')
    1416from ROOT import *
     17
     18class RawDataFeeder( object ):
     19    """ Wrapper class for RawData class
     20        capable of iterating over multiple RawData Files
     21    """
     22   
     23    def __init__(self, filelist):
     24        """ *filelist* list of files to iterate over
     25            the list should contain tuples, or sublists of two filenames
     26            the first should be a data file (\*.fits.gz)
     27            the second should be an amplitude calibration file(\*.drs.fits.gz)
     28        """
     29        # sanity check for input
     30        if type(filelist) != type(list()):
     31            raise TypeError('filelist should be a list')
     32        for entry in filelist:
     33            if len(entry) != 2:
     34                raise TypeError('the entries of filelist should have length == 2')
     35            for path in entry:
     36                if type(path) != type(str()):
     37                    raise TypeError('the entries of filelist should be path, i.e. of type str()')
     38                #todo check if 'path' is a valid path
     39                # else: throw an Exception, or Warning?
     40       
     41        self.filelist = filelist
     42        self._current_RawData = RawData(filelist[0][0], filelist[0][1], return_dict=True)
     43        del filelist[0]
     44   
     45    def __iter__(self):
     46        return self
     47   
     48    def next():
     49        """ Method being called by the iterator.
     50            Since the RawData Objects are simply looped over, the event_id from the
     51            RawData object will not be unique.
     52            Each RawData obejct will start with event_id = 1 as usual.
     53        """
     54        try:
     55            return self._current_RawData.next()
     56        except StopIteration:
     57            # current_RawData was completely processed
     58            # delete it (I hope this calls the destructor of the fits file and/or closes it)
     59            del self._current_RawData
     60            # and remake it, if possible
     61            if len(self.filelist) > 0:
     62                self._current_RawData = RawData(filelist[0][0], filelist[0][1], return_dict=True)
     63                del filelist[0]
     64            else:
     65                raise
     66       
    1567
    1668
     
    336388        if run.event_id.value == nevents:
    337389            break
    338        
    339390   
    340391if __name__ == '__main__':
Note: See TracChangeset for help on using the changeset viewer.