#------------------------------------------------------------------------ # ThisCommandFile = 'DAQ96_Collect_Main.cmd' # # This is the main Command file implementing the data collection actions # for the DAQ96 GUI # # This file contains all the run control infrasctrucure, # while all IOs (except for polling for new event capture) # are handled in other dependent command files, cf: # DAQ96_Collect_ArmAllAdfs.cmd # DAQ96_Collect_OneAdf.cmd # # Created 18-Dec-2006 # #------------------------------------------------------------------------ ############################################################################# # This is the function that will be run as a subprocesss # (so that the GUI can remain interactive) ############################################################################# def DAQ96_collect_event ( # The following arguments pass to the function/subprocess # all the handles to the GUI actions that we will/may use. # The following list must exactly match the one in start_new_thread below Vme_Read , Vme_Write , Rio_Read , Rio_Write , Hst_Histogram , Config_Fpga , Write_Dac , Find_Dac , Adc_Capture , Send_COOR_Msg , Gui_Exec_CmdFile , Tcs_Exec_VioFile , Tcs_Exec_RioFile , Tcs_Exec_CfgFile , Tcs_Exec_TtiFile , Tcs_Exec_MsgFile , Tcs_Exec_McfFile , SetRunIsUnderway , GetAdfIncluded , GetEventTotal , SetRunNumber , SetEventNumber , SetRunStatus , SetEventStatus , GetCoolDownSec , HandleAutoPause , CheckIfPaused , CheckIfStopRequested ) : # We will tag the data files with the version of this collector command file DAQ96_Collect_Version = "0.1" #------------------------------------------------------------------------ # import statements needed within this subprocess thread from Site_Dependent import RunDataDir from Site_Dependent import RunNumberFileName from Site_Dependent import AdfCardTot from Site_Dependent import AdfSlotVsCardNum from Site_Dependent import AdfSlotMaestro #from Site_Dependent import CommentFlag from Site_Dependent import HeaderBeginStr #------------------------------------------------------------------------ # Register Address for Raw ADC Memory Write Enable RegAddr_RawAdcMemory_WriteEnable = 519 #------------------------------------------------------------------------ # This process is autonomous, and will use its own argument dictionary ArgDict = {} # These coordinates initialize the Master and Slave which are not explictely used # but still made available to other dependent command files ArgDict['MasterNum'] = 0 ArgDict['SlaveNum'] = 0 #------------------------------------------------------------------------ # declare the beginning of the run to the GUI # as this will be used to avoid multiple simultaneous runs SetRunIsUnderway ( 1 ) # wait a bit for command file thread to return control to GUI # and thus avoid mixing screen messages time.sleep (1) printLine ( "CollectRun", "------------------------------------------" ) #------------------------------------------------------------------------ # determine new run number by incrementing the number found in a "memory file" try : RunNumberFile = open ( CommandFileDir + RunNumberFileName, 'r' ) RunNum = eval ( RunNumberFile.readline () ) + 1 RunNumberFile.close() except : RunNum = 0 if ( globals().has_key( 'RunNumberFile' ) ) : RunNumberFile.close() # now overwrite the file, i.e. get it ready for next time RunNumberFile = open ( CommandFileDir + RunNumberFileName, 'w' ) RunNumberFile.write ( "%d\n" % RunNum ) RunNumberFile.close() # update the GUI field to show the current run number RunNumStr = "%#03d" % RunNum SetRunNumber ( RunNumStr ) # make the run number available to dependent command files ArgDict['RunNumStr'] = RunNumStr #------------------------------------------------------------------------ # Events are collected in a dedicated Directory OutputDir = RunDataDir # create this mother directory if it doesn't exist try : os.listdir ( OutputDir ) except : os.mkdir ( OutputDir ) # create a SubDirectory; one for each run RunTime = time.localtime( time.time() ) RunTimeStr = time.strftime ( "%H%M%S", RunTime ) # e.g. "202423" RunDateStr = time.strftime ( "%Y%m%d", RunTime ) # e.g. "20070104" OutputDir = OutputDir + "R%s_D%s_T%s\\" % ( RunNumStr, RunDateStr, RunTimeStr ) try : os.listdir ( OutputDir ) # note: it should not exist... should we catch this as an error? except : os.mkdir ( OutputDir ) # set path to output directory to make it easier to create files os.chdir ( OutputDir ) #------------------------------------------------------------------------ # create a logfile specific for this run where # we may print details of execution information LogFileName = "Run_%s.log" % RunNumStr LogFile = open ( OutputDir + LogFileName, 'a' ) LogFile.write ( "\n" ) LogFile.write ( "Run #%s " % RunNumStr ) LogFile.write ( time.strftime ( "%a-%d-%b-%Y-%X", time.localtime( time.time() ) ) ) LogFile.write ( " Version V%s " % DAQ96_Collect_Version ) LogFile.write ( "\n------------------------------------\n" ) LogFile.write ( "\n" ) #------------------------------------------------------------------------ # now we are ready to start the run printLine ( "CollectRun", "Starting Run #%s" % RunNumStr ) LogFile.write ( "Start Run #%s\n" % RunNumStr ) #################### # setup main event loop #################### Run_Paused = 0 # where we will record the request to pause Run_Abort = 0 # where we will record the request to stop TotalEvent = GetEventTotal() # retrieved from GUI EvtCaptNum = 0 # retrieve the set of Adf Cards in this run AdfIncluded = GetAdfIncluded() AdfIncludedLine = "This run will include ADF #" for AdfNum in range(AdfCardTot) : if AdfIncluded[AdfNum] : AdfIncludedLine = "%s %d &" % ( AdfIncludedLine, AdfNum ) printLine ( "CollectRun", AdfIncludedLine[:-1] ) LogFile.write ( "%s\n" % AdfIncludedLine[:-1] ) for EvtLoopNum in range( 1, TotalEvent+1) : # update the run status on the GUI SetRunStatus ( "Running" ) # update the event number on the GUI EvtLoopNumStr = "%#03d" % EvtLoopNum SetEventNumber ( EvtLoopNumStr ) #################### # Prepare for next event #################### # retrieve data for this Adf Gui_Exec_CmdFile ( ComFile = CommandFileDir + 'DAQ96_Collect_ArmAllAdfs.cmd', ArgDict = ArgDict ) #################### # Waiting for new event #################### printLine ( "CollectRun", "Waiting for Event #%s" % EvtLoopNumStr ) # update the event status on the GUI SetEventStatus ( "Waiting" ) # Poll and watch for event capture waiting_sec = 0 while (1) : # wait for ever, or until run cancelled # We check if the GUI wants to interrupt waiting if CheckIfStopRequested() : # do nothing now but remember we are aborting Run_Abort = 1 break # break out of the polling loop # this is where we poll to see if an event has been captured WriteEnableMask, Reply_Status \ = Rio_Read ( SlotNum = AdfSlotMaestro, ChipNum = 0, # either FPGA would be fine RegAddr = RegAddr_RawAdcMemory_WriteEnable ) if ( WriteEnableMask == 0x0000 ) : # i.e. an event was captured break # break out of the polling loop # wait before polling again waiting_sec = waiting_sec + 1 time.sleep (1) # update the event status on the GUI SetEventStatus ( "Waiting %d" % waiting_sec ) # if we were told to abort while waiting # break out of the whole event-do-loop if ( Run_Abort == 1 ) : break #################### # Event has been captured #################### # New Event: the loop number is the captured event number EvtCaptNum = EvtLoopNum EvtCaptNumStr = "%#03d" % EvtCaptNum # make the event number available to dependent command files ArgDict['EvtCaptNumStr'] = EvtCaptNumStr # Remember event capture time EventTime = time.localtime( time.time() ) EventTimeStr = time.strftime ( "%H%M%S", EventTime ) # e.g. "202423" EventDateTimeStr = time.strftime ( "%a-%d-%b-%Y-%X", EventTime ) # e.g. 'Wed-14-Jul-1999-00:24:23' # Open a New Event output File EventFileName = "R%s_E%s_T%s.dat" % ( RunNumStr, EvtCaptNumStr, EventTimeStr ) EventFile = open ( OutputDir + EventFileName, 'w' ) # make this event file available to dependent command files ArgDict['EventFile'] = EventFile #################### # data collection #################### SetEventStatus ( "Reading" ) printLine ( "CollectRun", "Reading data for Event #%s" % EvtLoopNumStr ) LogFile.write ( "Event #%s @ %s\n" % ( EvtCaptNumStr, EventDateTimeStr[-8:] ) ) EventFile.write ( "%s V%s Run #%s Event #%s Time %s\n" % ( HeaderBeginStr , DAQ96_Collect_Version , RunNumStr, EvtCaptNumStr, EventDateTimeStr ) ) # Collect the data from this event # from all the Adf cards in this run for AdfNum in range(AdfCardTot) : if AdfIncluded[AdfNum] : printLine ( "CollectRun", "Collecting Data from ADF #%d at Slot#%d" % ( AdfNum, AdfSlotVsCardNum[AdfNum] ) ) # make the card number available to dependent command files ArgDict['AdfNum'] = AdfNum # retrieve data for this Adf Gui_Exec_CmdFile ( ComFile = CommandFileDir + 'DAQ96_Collect_OneAdf.cmd', ArgDict = ArgDict ) # Done with Event File EventFile.close ( ) #################### # run control #################### # set pause flag, if auto-pause currently requested HandleAutoPause () # find out *now* how long we will need to cool down, # so that pause/resume can cancel its effect CoolDownSec = GetCoolDownSec () # See if we need to pause if CheckIfPaused() : Run_Paused = 1 printLine ( "CollectRun", "Paused after Event #%s" % EvtCaptNumStr ) LogFile.write ( "Run Paused @ %s\n" % time.strftime ( "%H:%M:%S", time.localtime( time.time() ) ) ) SetRunStatus ( "Paused" ) # avoid cool down when we will be coming out of pause CoolDownSec = 0 # keep checking until run is resumed # note: stopping the run will implicitely resume and get us out of here while (Run_Paused) : if CheckIfPaused() : time.sleep (1) # wait and check again... else : Run_Paused = 0 LogFile.write ( "Run Resumed @ %s\n" % time.strftime ( "%H:%M:%S", time.localtime( time.time() ) ) ) # Handle Cool down while also watching for stop run request if CoolDownSec != 0 : printLine ( "CollectRun", "Cool down %d sec..." % CoolDownSec ) while (1) : #note: we go through at least once e.g. to notice stop # This is where we check if the GUI wants to interrupt the run if CheckIfStopRequested() : # do nothing now but remember we are aborting Run_Abort = 1 # and cancel the rest of any cool down CoolDownSec = 0 # Handle cooldown if ( CoolDownSec > 0 ) : SetEventStatus ( "Cooling %d" % CoolDownSec ) time.sleep (1) CoolDownSec = CoolDownSec - 1 else : break # break out of the while-cooldown-loop # break out of the event-do-loop when we are told to abort if ( Run_Abort == 1 ) : break # we can already declare the end of the run to the GUI SetRunIsUnderway ( 0 ) #------------------------------------------------------------------------ # done with event loop if ( Run_Abort == 1 ) : SetRunStatus ( "Aborted" ) LogFile.write ( "Run Aborted @ %s\n" % time.strftime ( "%H:%M:%S", time.localtime( time.time() ) ) ) else : SetRunStatus ( "Done" ) printLine ( "CollectRun", "Stopping Run #%s after collecting %d events" % ( RunNumStr, EvtCaptNum ) ) LogFile.write ( "Stop Run #%s after collecting %d events\n" % ( RunNumStr, EvtCaptNum ) ) #------------------------------------------------------------------------ # clean up last try closing any open file if ( globals().has_key( 'EventFile' ) ) : EventFile.close() if ( globals().has_key( 'LogFile' ) ) : LogFile.close() #------------------------------------------------------------------------ printLine ( "CollectRun", "------------------------------------------" ) return ############################################################################# # This is where we start the subprocess and immediately return control to GUI # The code for the subprocess is the function above ############################################################################# from thread import start_new_thread printLine ( "CollctStart", "Starting DAQ96 Collection SubProcess" ) start_new_thread ( DAQ96_collect_event , ( # make sure the following arguments match the above list Vme_Read , Vme_Write , Rio_Read , Rio_Write , Hst_Histogram , Config_Fpga , Write_Dac , Find_Dac , Adc_Capture , Send_COOR_Msg , Gui_Exec_CmdFile , Tcs_Exec_VioFile , Tcs_Exec_RioFile , Tcs_Exec_CfgFile , Tcs_Exec_TtiFile , Tcs_Exec_MsgFile , Tcs_Exec_McfFile , SetRunIsUnderway , GetAdfIncluded , GetEventTotal , SetRunNumber , SetEventNumber , SetRunStatus , SetEventStatus , GetCoolDownSec , HandleAutoPause , CheckIfPaused , CheckIfStopRequested ) )