| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

In This Package:

TestHeaderObjects.py

Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 '''
00003 Test header objects are correctly sent through I/O
00004 
00005 usage: nuwa.py -m "RootIOTest.TestHeaderObjects [filebase]"
00006 
00007 filebase.txt will be filled with a text representation of the header
00008 objects
00009 
00010 filebase.pkl will be filled with a Python Pickled representation of
00011 the header objects
00012 
00013 The latter can be compared by running this module directly like:
00014 
00015 python TestHeaderObjects.py statefile1.pkl statefiel2.pkl
00016 
00017 '''
00018 
00019 
00020 from GaudiPython.GaudiAlgs import GaudiAlgo
00021 from GaudiPython import SUCCESS, FAILURE, Bindings
00022 import os, pickle
00023 from DybPython.Util import irange
00024 
00025 
00026 class PythonHeaderObject(object):
00027     '''
00028     A purely Python header object
00029     '''
00030 
00031     def __init__(self,ho):
00032         self.defloc = ho.defLoc()
00033 
00034 
00035         ctx = ho.context()
00036         self.site = ctx.GetSite()
00037         self.flag = ctx.GetSimFlag()
00038         self.detid = ctx.GetDetId()
00039 
00040         #print 'Getting timestamp'
00041         ts = ctx.GetTimeStamp()
00042         self.seconds = ts.GetSec()
00043         self.nanosec = ts.GetNanoSec()
00044 
00045         self.execNumber = ho.execNumber()
00046         self.randomState = []
00047 
00048         # print 'Getting random state'
00049         # rs = ho.randomState()
00050         # for rn in irange(rs.begin(), rs.end()):
00051         #     print rn
00052         #     self.randomState.append(rn)
00053         #     continue
00054 
00055         self.inputHeaders = []
00056         ihs = ho.inputHeaders()
00057         siz = ihs.size()
00058         #print 'Recursing on %d input headers'%siz
00059         for ind in range(siz):
00060             ih = ihs[ind]
00061             self.inputHeaders.append(PythonHeaderObject(ih))
00062             continue
00063         return
00064 
00065     def __cmp__(self,other):
00066         return cmp(self.__dict__,other.__dict__)
00067 
00068     def __str__(self):
00069         s = '%s exec=%d sec.ns=%d.%09d site=%d mcflag=%d detid=%d with %d inputHeaders:'%\
00070             (self.defloc,
00071              self.execNumber, self.seconds, self.nanosec,
00072              self.site, self.flag, self.detid, 
00073              len(self.inputHeaders))
00074         if self.inputHeaders:
00075             l = []
00076             for ih in self.inputHeaders:
00077                 l.append('\t%s'%ih)
00078             s += '\n' + '\n'.join(l)
00079 
00080         return s
00081         
00082 
00083 class HeaderDump(GaudiAlgo):
00084     '''
00085     A gaudi alg that dumps header objects into a pickle file
00086     '''
00087     def __init__(self,name='InputHeaderDump',filebase='input_header_dump',headers=None):
00088         GaudiAlgo.__init__(self,name)
00089         self.pkl = open(filebase+'.pkl',"w")
00090         self.txt = open(filebase+'.txt',"w")
00091         if headers:
00092             self.paths = headers
00093         else:
00094             self.paths = ['/Event/Sim/SimHeader','/Event/Gen/GenHeader']
00095         return
00096 
00097     def execute(self):
00098         evt = self.evtSvc()
00099         for path in self.paths:
00100             hdr = evt[path]
00101             if not hdr:
00102                 print 'Failed to get HeaderObject from "%s"'%path
00103                 return FAILURE
00104             self.txt.write("%s"%hdr)
00105             phdr = PythonHeaderObject(hdr)
00106             pickle.dump(phdr,self.pkl)
00107             continue
00108         return SUCCESS
00109 
00110     def finalize(self):
00111         self.pkl.close()
00112         self.txt.close()
00113         return SUCCESS
00114         
00115 
00116 filebase = None
00117 
00118 def configure(argv=None):
00119     global filebase
00120     try:
00121         filebase = argv[0]
00122     except IndexError:
00123         filebase = "test_input_headers.pkl"
00124 
00125 def run(app):
00126     global filebase
00127     alg = HeaderDump(filebase=filebase)
00128     app.addAlgorithm(alg)
00129     return
00130 
00131 def compare(file1,file2):
00132 
00133     print 'Comparing "%s" and "%s"'%(file1,file2)
00134     
00135     file1 = open(file1,"r")
00136     file2 = open(file2,"r")
00137 
00138     count = 0
00139     ndiffer = 0
00140     while True:
00141         count +=1 
00142         try:
00143             o1 = pickle.load(file1)
00144             o2 = pickle.load(file2)
00145         except EOFError:
00146             break
00147 
00148         if o1 == o2: 
00149             print 'Same:'
00150             print 'First: ',o1
00151             print 'Second:',o2
00152             continue
00153 
00154         ndiffer += 1
00155 
00156         print 'Objects #%d differ'%count
00157         print 'Object1:%s'%o1
00158         print 'Object2:%s'%o2
00159         continue
00160     print "Number of differing objects: %d"%ndiffer
00161     return ndiffer
00162 
00163 if __name__ == '__main__':
00164     import sys
00165     compare(sys.argv[1],sys.argv[2])
| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

Generated on Mon Apr 11 20:58:40 2011 for RootIOTest by doxygen 1.4.7