Main Page | Packages | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | Related Pages

ancillaryFile.py

00001 #!/usr/local/bin/python
00002 #
00003 #                               Copyright 2006
00004 #                                     by
00005 #                        The Board of Trustees of the
00006 #                     Leland Stanford Junior University.
00007 #                            All rights reserved.
00008 #
00009 
00010 __facility__ = "Online"
00011 __abstract__ = "Read ancillary event contributions from a file"
00012 __author__   = "R. Claus <Claus@SLAC.Stanford.edu> SLAC - GLAST LAT I&T/Online"
00013 __date__     = "3/30/2006"
00014 __updated__  = ("$Date: 2006/07/19 16:08:33 $").split(' ')[1]
00015 __version__  = "$Revision: 1.2 $"
00016 __release__  = "$Name: R04-12-00 $"
00017 __credits__  = "SLAC"
00018 
00019 import LATTE.copyright_SLAC
00020 
00021 import struct
00022 from   array   import array
00023 import logging as     log
00024 
00025 import LATTE.merger.geb                   as     geb
00026 from   LATTE.merger.ancillaryContribution import AncillaryContribution
00027 
00028 
00029 class AncillaryContributor(geb.GebContributor):
00030   __typeIds = [AncillaryContribution.EventId,
00031                AncillaryContribution.HeaderId,
00032                AncillaryContribution.TrailerId]
00033   def __init__(self, filename, offset = 0):
00034     geb.GebContributor.__init__(self)
00035     self.__filename  = filename
00036     self.__fakeEvent = struct.pack("<3L", AncillaryContribution.EventId, 3*4, 0xdeadbeefL)
00037     self.__offset = offset
00038 
00039   def name(self):
00040     return "Anc"
00041 
00042   def offset(self):
00043     return self.__offset
00044 
00045   def open(self):
00046     self.__file = file(self.__filename, 'rb')
00047     print "AncContrib.open: %s opened" % (self.__filename)
00048 
00049   def close(self):
00050     self.__file.close()
00051 
00052   def read(self):
00053     """!\brief Method to read an ancillary contribution from a file.
00054     """
00055     data = self.__file.read(8)
00056     if len(data) == 0:  raise EOFError, "AncContrib.read: End of file on %s" % (self.__filename)
00057     (vTypeId, length) = struct.unpack('<2L', data)
00058     typeId = vTypeId & 0x000fffff
00059     if typeId not in AncillaryContributor.__typeIds:
00060       raise KeyError, "AncContrib.read: Unrecognized contribution %08x" % (vTypeId)
00061     length &= 0x0fffffff
00062     #print "AncContrib.read:   id = %08x, len = %08x" % (typeId,length)
00063     #data  += self.__file.read(length - 8)
00064     #data = array('I', data)
00065 
00066     #!\todo Improve the swapping if possible, i.e. avoid a copy
00067     data = array('I', (vTypeId, length))
00068     data.fromfile(self.__file, (length - 8) / 4)
00069     data.byteswap()
00070     data = data.tostring()
00071 
00072     status = 0
00073     if typeId == AncillaryContribution.EventId:
00074       return geb.Geb.Build,     status, AncillaryContribution(self, data)
00075     else:
00076       return geb.Geb.DontBuild, status, AncillaryContribution(self, data)
00077 
00078 
00079 
00080 
00081 
00082 
00083 
00084 
00085 
00086 

Generated on Fri Jul 21 13:26:26 2006 for LATTE R04-12-00 by doxygen 1.4.3