00001 #!/usr/local/bin/python
00002 #
00003 # Copyright 2003
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__ = "Glast Online consumer plotter"
00012 __author__ = "Lester Miller"
00013 __date__ = "11/20/2003"
00014 __version__ = "$Revision: 1.10 $"
00015 __credits__ = "SLAC"
00016
00017 import LATTE.copyright_SLAC
00018 import LATTE.consumer.consumer as consumer
00019 import LATTE.consumer.standardPlotter as standardPlotter
00020 import threading
00021 import sys
00022 from qt import *
00023 import Numeric as numarray
00024 import os
00025 import sihippo
00026
00027 class consumerPlotter(consumer.Consumer,standardPlotter.standardPlotter,QMainWindow):
00028 """\brief Consumer class to consume LDF data
00029 This class just handles file open/close or server
00030 open/close
00031 """
00032
00033 def __init__(self,argv):
00034 """consumer constructor
00035 """
00036 QMainWindow.__init__(self,None,None,0)
00037 if 'ONLINE_ROOT' in os.environ:
00038 self.configFile = os.path.join(os.environ['ONLINE_ROOT'],'LATTE/consumer/consumerPlotter.cfg')
00039 else:
00040 self.configFile = ''
00041 consumer.Consumer.__init__(self,argv)
00042 standardPlotter.standardPlotter.__init__(self, self.lci)
00043 # used for status
00044 self.COMPL_STATUS_FAILED = 'FAILED'
00045 self.COMPL_STATUS_PASSED = 'PASSED'
00046 self.COMPL_STATUS_UNDEFINED = 'PASSED'
00047 self.COMPL_STATUS_ABORTED = 'ABORTED'
00048 self.COMPL_STATUS_PASSED_CONDITIONALLY = 'PASSED_CONDITIONALLY'
00049 self.__status = self.COMPL_STATUS_UNDEFINED
00050 self.__exportedFileList = []
00051 self.initializePlotter()
00052 self.guiThread = threading.Thread(None,self.loop,'consumerloop',())
00053 self.guiThread.start()
00054
00055 def getName(self):
00056 return 'consumerPlotter'
00057
00058 def parseEvent(self):
00059 """Analyse an event
00060 """
00061 self.ldbi.iterate(self.eventData, len(self.eventData))
00062 self.checkUDF()
00063 if self.endRun:
00064 self.setCompletionStatus(self.COMPL_STATUS_PASSED)
00065 self.reportResults()
00066 self.exportFiles()
00067 if self.startRun:
00068 self.setCompletionStatus(self.COMPL_STATUS_UNDEFINED)
00069 self.clearPlots()
00070 self.readPedestals()
00071 self.updatePlots()
00072 if self.lci.nEvtSeen>0 and self.lci.nEvtSeen%self.checkInterval==0:
00073 self.checkLimits()
00074
00075 def checkUDF(self):
00076 udfDict = self.lcti.getUdfDict()
00077 self.startRun = False
00078 if 'startRun' in udfDict:
00079 self.setRunId(udfDict['startRun'])
00080 self.startRun = True
00081 self.endRun = False
00082 if 'endRun' in udfDict:
00083 self.endRun = True
00084 if 'configFile' in udfDict:
00085 self.setConfigFile(udfDict['configFile'])
00086 if 'dataType' in udfDict:
00087 self.setDataType(udfDict['dataType'])
00088 self.lcti.clearUdfDict()
00089
00090 def checkLimits(self):
00091 print 'checking limits...'
00092 for plot in self.limits.keys():
00093 cLimit = self.limits[plot]
00094 # check ringBuffer for completion
00095 if self.ntRequests[self.getPlotNtuple(plot)]<=0 or self.lci.ntDict[self.getPlotNtuple(plot)].rows()>=self.ntRequests[self.getPlotNtuple(plot)]:
00096 (limitName,limitLabelList,limitFailList,errorLabelList,errorFailList) = self.checkLimit(plot,cLimit)
00097 if len(limitFailList)>0 or len(errorFailList)>0:
00098 #self.histDict[plot].getDataRep(0).setRepColor(sihippo.Color.orange)
00099 print 'Plot ',plot,' is out of limits!!'
00100
00101 # these methods are needed for reporting, they pretend to be rcTransitions
00102 def setCompletionStatus(self,status):
00103 self.__status = status
00104 def getCompletionStatusStr(self):
00105 return self.__status
00106 def getCompletionStatus(self):
00107 return self.__status
00108
00109 def addExportedFile(self,file,delete=False):
00110 self.__exportedFileList.append((file,delete))
00111
00112 def exportFiles(self):
00113 if self.reportPath is not None and os.path.exists(self.reportPath) and len(self.__exportedFileList)>0:
00114 exportDirName = self.getName()+'_'+self.getRunId()
00115 exportDir = os.path.join(self.reportPath,exportDirName)
00116 if os.access(self.reportPath,os.W_OK): # can we write?
00117 os.mkdir(exportDir)
00118 for (file,delete) in self.__exportedFileList:
00119 import shutil
00120 fileName = os.path.basename(file)
00121 outFile = os.path.join(exportDir,fileName)
00122 # avoid copying to itself
00123 if os.path.exists(file) and not os.path.exists(outFile):
00124 if delete:
00125 shutil.move(file,outFile)
00126 else:
00127 shutil.copy(file,outFile)
00128
00129 if __name__ == '__main__':
00130 a = QApplication(sys.argv)
00131 consumer = consumerPlotter(sys.argv[1:])
00132 QObject.connect(a,SIGNAL("lastWindowClosed()"),a,SLOT("quit()"))
00133 a.setMainWidget(consumer)
00134 a.exec_loop()