#------------------------------------------------------------------------ # ThisCommandFile = 'DAQ96_Plot_Main.cmd' # # This is the main Command file for implementing the raw data display actions # for the DAQ96 GUI. # Given # # Created 10-Jan-2007 # #------------------------------------------------------------------------ import string from Site_Dependent import CommandFileDir global AdfCardTot ; from Site_Dependent import AdfCardTot global AdfChannelTot ; from Site_Dependent import AdfChannelTot global CommentFlag ; from Site_Dependent import CommentFlag global HeaderBeginStr ; from Site_Dependent import HeaderBeginStr global TitleBeginStr ; from Site_Dependent import TitleBeginStr global ChannelBeginStr ; from Site_Dependent import ChannelBeginStr global SamplesPerChannel ; from Site_Dependent import SamplesPerChannel global Zeresp ; from Site_Dependent import Zeresp global PlotScale ; from Site_Dependent import PlotScale global TopDrawerExe ; from Site_Dependent import TopDrawerExe global GhostViewExe ; from Site_Dependent import GhostViewExe #------------------------------------------------------------------------ # Retrieve the implicit arguments from the argument dictionary # this argument must have been filled in by the GUI EvtFile = ArgDict['EvtFile'] #------------------------------------------------------------------------ #------------------------------------------------------------------------ global ExtractChannelFromLine def ExtractChannelFromLine ( EvtFileLine ) : ChannelNumStartChar = len(ChannelBeginStr) ChannelNumStr = EvtFileLine[ChannelNumStartChar:ChannelNumStartChar+3] # convert string to integer # while avoiding leading zeroes, which would infer octal format for python if ( ChannelNumStr == '000' ) : ChannelNumStr = '0' # special case as it would get stripped down to nothing. else : # drop leading zeroes that would mean octal format ChannelNumStr = string.lstrip( ChannelNumStr, '0' ) return int ( ChannelNumStr ) #------------------------------------------------------------------------ global InsertTopDrawerPlotBegin def InsertTopDrawerPlotBegin ( TopDrawerFile, RunEventTitle, ChannelNum ) : TopDrawerFile.write ( "\n" ) TopDrawerFile.write ( " SET DEVICE POSTSCR ORIENT=2\n" ) TopDrawerFile.write ( "\n" ) TopDrawerFile.write ( " TITLE TOP 'LArTPC %s'\n" % RunEventTitle ) TopDrawerFile.write ( " TITLE LEFT 'System Channel (scaled 1:%s)'\n" % PlotScale ) TopDrawerFile.write ( " TITLE Bottom 'Sample Number 198 nsec/sample 405 usec total'\n" ) TopDrawerFile.write ( "\n" ) TopDrawerFile.write ( " SET LIMITS X 1 TO 2048\n" ) TopDrawerFile.write ( " SET LIMITS Y %d TO %d\n" % ( ChannelNum-2, ChannelNum+AdfChannelTot+2 ) ) TopDrawerFile.write ( " SET ORDER X Y\n" ) TopDrawerFile.write ( "\n" ) # TopDrawerFile.write ( " SET SYMBOL %do\n" % Freqs_Tested.index(Freq_MHz) ) #------------------------------------------------------------------------ global InsertTopDrawerEntryPoint def InsertTopDrawerEntryPoint ( TopDrawerFile, ChannelNumDec, SampleNum, SampleVal ) : SampleRescaled = ChannelNumDec + ( SampleVal - Zeresp ) / PlotScale TopDrawerFile.write ( "%d %s\n" % ( SampleNum, SampleRescaled ) ) #------------------------------------------------------------------------ global InsertTopDrawerChannelEnd def InsertTopDrawerChannelEnd ( TopDrawerFile ) : TopDrawerFile.write ( "\n" ) TopDrawerFile.write ( " PLOT\n" ) #------------------------------------------------------------------------ global InsertTopDrawerPlotEnd def InsertTopDrawerPlotEnd ( TopDrawerFile ) : TopDrawerFile.write ( "\n" ) TopDrawerFile.write ( " EXIT\n" ) #------------------------------------------------------------------------ global RunTopDrawer def RunTopDrawer ( TopDrawerFileName, PostscriptFileName ) : if os.path.isfile ( PostscriptFileName ) : # delete any previous file of that name os.remove ( PostscriptFileName ) TopDrawerCmd = 'cmd/c %s %s ' % ( TopDrawerExe, TopDrawerFileName ) printLine ( "PlotEvt", "Running TopDrawer..." ) os.system ( TopDrawerCmd ) RenFileCmd = 'ren %s %s ' % ( TopDrawerFileName[:string.find(TopDrawerFileName,'.')], PostscriptFileName ) printLine ( "PlotEvt", "Renaming TD output file" ) os.system ( RenFileCmd ) printLine ( "PlotEvt", 'Postsript File is %s' % PostscriptFileName ) #------------------------------------------------------------------------ global RunGhostView def RunGhostView ( PostscriptFileName ) : GhostviewCmd = 'start "%s" "%s" ' % ( GhostViewExe, PostscriptFileName ) printLine ( "PlotEvt", "Running Ghostview..." ) os.system ( GhostviewCmd ) #------------------------------------------------------------------------ def PlotOneAdf ( EventFile, RunEventTitle, OutputNameBaseStr ) : ReachedEof = 0 # read the next line, i.e. the first channel of a new ADF card EvtFileLine = EventFile.readline() # be ready to skip extra comment lines until we find the channel label while ( EvtFileLine[0:len(ChannelBeginStr)] != ChannelBeginStr ) : # read the next line EvtFileLine = EventFile.readline() # be prepared for truncated file if ( EvtFileLine == '' ) : ReachedEof = 1 break # There should only be comments at this point if ( EvtFileLine[0] != CommentFlag ) : printLine ( "**Error**", "Expected a comment before new ADF, but found <%s>" % EvtFileLine[:-1] ) if ( ReachedEof == 1 ) : printLine ( "PlotEvt", "Done with Event File" ) else : # retrieve a Channel Number from this comment line FirstChannelNumDec = ExtractChannelFromLine ( EvtFileLine ) # This first channel should be the first from a new ADF card if ( ( FirstChannelNumDec % AdfChannelTot ) != 0 ) : printLine ( "**Error**", "Next Channel is #%d but a new ADF card was expected" % FirstChannelNumDec ) else : #deduce which new ADF card this is AdfNum = FirstChannelNumDec / AdfChannelTot if ( AdfNum >= AdfCardTot ) : printLine ( "**Error**", "Next Channel is #%d, i.e. out of expected range" % FirstChannelNumDec ) #printLine ( "PlotEvt", "Found Channel #%d, i.e. beginning of ADF#%d" % ( FirstChannelNumDec, AdfNum ) ) # derive file names for output files TopDrawerFileName = "%s_Adf%d.top" % ( OutputNameBaseStr, AdfNum ) PostscriptFileName = "%s_Adf%d.ps" % ( OutputNameBaseStr, AdfNum ) # create a new file for topdrawer commands printLine ( "PlotEvt", "Creating TopDrawer File %s" % TopDrawerFileName ) TopDrawerFile = open ( TopDrawerFileName, 'w' ) # fill in title, scale, etc. InsertTopDrawerPlotBegin ( TopDrawerFile, RunEventTitle, FirstChannelNumDec ) # Read data from all 32 channels on this card for ChannelNumDec in range ( FirstChannelNumDec, FirstChannelNumDec+AdfChannelTot ) : # read next line(s), until we find the next channel of this ADF card while ( EvtFileLine[0:len(ChannelBeginStr)] != ChannelBeginStr ) : # read the next line, i.e. the next channel of this ADF card EvtFileLine = EventFile.readline() # We may reach the EndOfFile if ( EvtFileLine == '' ) : ReachedEof = 1 break # out of while # There should only be comments at this point if ( EvtFileLine[0] != CommentFlag ) : printLine ( "**Error**", "Expected a comment before channel #%d, but found <%s>" % ( ChannelNumDec, EvtFileLine[:-1] ) ) # we should not reach the end of file within a card if ( ReachedEof == 1 ) : printLine ( "PlotEvt", "reached EOF before data for Channel #%d" % ChannelNumDec ) break # retrieve the channel number from this line NextChannelNumDec = ExtractChannelFromLine ( EvtFileLine ) # verify this next channel number matches expectation if ( NextChannelNumDec != ChannelNumDec ) : printLine ( "**Error**", "Next channel is #%d instead of #%d" % ( NextChannelNumDec, ChannelNumDec ) ) break # read all samples for this channel for SampleNum in range ( SamplesPerChannel ) : EvtFileLine = EventFile.readline() try : while ( EvtFileLine[0] == CommentFlag ) : # skip comment lines EvtFileLine = EventFile.readline() # convert to a decimal value SampleVal = float ( EvtFileLine ) InsertTopDrawerEntryPoint ( TopDrawerFile, ChannelNumDec, SampleNum, SampleVal ) except : printLine ( "**Error**", "Couldn't extract Sample #%d for Channel #%d from raw data line <%s>" % ( SampleNum, ChannelNumDec, EvtFileLine ) ) break # end this channel InsertTopDrawerChannelEnd ( TopDrawerFile ) # done with all channels of this ADF InsertTopDrawerPlotEnd( TopDrawerFile ) TopDrawerFile.close ( ) # create postscript file RunTopDrawer ( TopDrawerFileName, PostscriptFileName ) # view postscript file RunGhostView ( PostscriptFileName ) # done with this ADF # clean up and close any open file if ( globals().has_key( 'TopDrawerFile' ) ) : TopDrawerFile.close() return EvtFileLine #------------------------------------------------------------------------ #------------------------------------------------------------------------ printLine ( "PlotEvt", "------------------------------------------" ) # extract the file path and file name EvtFile_Split = os.path.split ( EvtFile ) EvtFile_path = EvtFile_Split[0] + '/' EvtFile_Name = EvtFile_Split[1] EvtFile_NameNoExt = EvtFile_Name[:string.find(EvtFile_Name,'.')] EvtFile_Ext = EvtFile_Name[string.find(EvtFile_Name,'.'):] # open the event file printLine ( "PlotEvt", "Plotting raw data from <%s>" % EvtFile_Name ) EventFile = open ( EvtFile, 'r' ) # read the first line, i.e. "header" EvtFileLine = EventFile.readline() # make sure this looks like an event file if ( EvtFile_Ext != ".dat" ) : printLine ( "**Error**", "Event file extension should be <.dat>" ) elif ( EvtFileLine[0:len(HeaderBeginStr)] != HeaderBeginStr ) : printLine ( "**Error**", "First line doesn't start with <%s>" % HeaderBeginStr ) # ready to start else : # set the default directory to this path printLine ( "PlotEvt", "Path set to <%s>" % EvtFile_path ) os.chdir ( EvtFile_path ) # the first line contains the header line to use as plot title RunEventTitle = EvtFileLine[ string.find( EvtFileLine, TitleBeginStr ) :-1 ] printLine ( "PlotEvt", RunEventTitle ) #plot each ADF card one at a time while ( PlotOneAdf ( EventFile = EventFile, RunEventTitle = RunEventTitle, OutputNameBaseStr = EvtFile_NameNoExt ) != '' ) : printLine ( "PlotEvt", "..." ) # clean up and close any open file if ( globals().has_key( 'EventFile' ) ) : EventFile.close() printLine ( "PlotEvt", "------------------------------------------" )