Changeset 14454 for fact/tools/PyDimCtrl


Ignore:
Timestamp:
10/03/12 19:25:45 (12 years ago)
Author:
neise
Message:
evolvoing
File:
1 edited

Legend:

Unmodified
Added
Removed
  • fact/tools/PyDimCtrl/ScriptsForPyDimCtrl.py

    r14453 r14454  
    22
    33import time
     4from factdimserver import *
     5import numpy as np
    46
    57last_drive_kwargs = {}
    68last_drive_method = None
    79
     10bias_calibration = {}
     11
    812def FadConnectCrate( crate ):
    913  cratenum = None
    1014
    11   if cate == 'all':
     15  if crate == 'all':
    1216    print "connecting to all crates"
    1317    for i in range( 40 ):
     
    1822    try:
    1923      cratenum = int(crate)
    20     except ValueError e:
     24    except ValueError as e:
    2125      print "cannot convert crate parameter to integer. crate=", crate
    2226      print e
     
    3438  cratenum = None
    3539
    36   if cate == 'all':
     40  if crate == 'all':
    3741    print "connecting to all crates"
    3842    for i in range( 40 ):
     
    4246    try:
    4347      cratenum = int(crate)
    44     except ValueError e:
     48    except ValueError as e:
    4549      print "cannot convert crate parameter to integer. crate=", crate
    4650      print e
     
    5054    print "connecting to crate", cratenum
    5155    for i in range(cratenum*10, (cratenum+1)*10 ):
     56      print 'disconnecting from i', time.time()
    5257      fad_control.disconnect(i)
    5358    print "... done"
     
    5661
    5762def IsReadyForDataTaking():
    58 """ Checking the system statuses if they are ready for data taking
    59 """
     63  """ Checking the system statuses if they are ready for data taking
     64  """
    6065
    6166  print "--------------------------------------"
     
    7984  print "--------------------------------------"
    8085
    81 def NotReadyForDataTaking( servers_n_targets = [
    82       (feedback, 12),
     86def NotReadyForDataTaking( servers_n_targets = [ (feedback, 12),
    8387      (bias_control, 9),
    8488      (fad_control, 4) ] ):
    85 """ Checking the system statuses if they are ready for data taking
     89  """ Checking the system statuses if they are ready for data taking
    8690    return list of servers, which are NOT ready for Data Taking
    8791    so one can use this function like this
     
    9094    else:
    9195      # start data taking
    92 """
     96  """
    9397  not_ready = []
    9498 
     
    110114
    111115def IsTracking():
    112 """ Name is misleading, it actually *waits* until Drive is in Tracking
     116  """ Name is misleading, it actually *waits* until Drive is in Tracking
    113117  original comment: Checking if Drive is in Status Tracking
    114 """
     118  """
    115119 
    116120  #return drive_control.stn == 8
     
    119123
    120124def PrepareBiasForDataTaking():
    121 """ should have the original behaviour, no new fancy features
     125  """ should have the original behaviour, no new fancy features
    122126    check feedback state before switching BIAS ON and ramping up to nominal Voltage
    123 """
     127  """
    124128
    125129  start = time()
     
    165169
    166170def StopTracking():
    167 """ should have the original behaviour, no new fancy features
     171  """ should have the original behaviour, no new fancy features
    168172    stop drivectrl tracking the current source
    169 """
     173  """
    170174  drive_control.stop()
    171175  drive_control.wait(6) #Armed
     
    176180
    177181def SwitchOnBias():
    178 """ should have the original behaviour, no new fancy features
     182  """ should have the original behaviour, no new fancy features
    179183    bring Feedback to state CurrentControIdle and switch on Bias
    180 """
     184  """
    181185  print ' switching on current controll feedback'
    182186  feedback.stop()
     
    219223
    220224
    221 def waitForTracking()
    222 """ Wait for drivectrl to reply that its tracking the given source
    223 """
     225def waitForTracking():
     226  """ Wait for drivectrl to reply that its tracking the given source
     227  """
    224228 
    225229  print "...waiting for DRIVE_CONTROL"
     
    239243
    240244def TakeDataRun():
    241 """ Take a 5min Data Run
    242 """
     245  """ Take a 5min Data Run
     246  """
    243247  # check if all subsystems are in the correct state 
    244248  IsReadyForDataTaking()
    245249
    246250  print ' taking Data:FullTriggerArea 5min Run ...'
    247   mcp.start( 300, -1, 'data')
     251  mcp.start( 300, -1, 'data\0')
    248252
    249253  print '...waiting for FAD to be in state 8: Writing Data'
     
    254258
    255259def TakeExtLpRun():
    256 """ Take a external Lightpulser Run
    257 """
     260  """ Take a external Lightpulser Run
     261  """
    258262  # check if all subsystems are in the correct state
    259263  IsReadyForDataTaking()
    260264
    261265  print 'taking External Light Pulser with BIAS on 1000 ...'
    262   mcp.start(-1, 1000, 'light-pulser-ext')
     266  mcp.start(-1, 1000, 'light-pulser-ext\0')
    263267
    264268  print '...waiting for FAD to be in state 8: Writing Data'
     
    269273 
    270274def TakePedestalOnRun():
    271 """ Take a Pedestal 1000 run with Bias ON
    272 """
     275  """ Take a Pedestal 1000 run with Bias ON
     276  """
    273277  # check if all subsystems are in the correct state
    274278  IsReadyForDataTaking()
    275279
    276280  print 'taking External Light Pulser with BIAS on 1000 ...'
    277   mcp.start(-1, 1000, 'pedestal')
     281  mcp.start(-1, 1000, 'pedestal\0')
    278282
    279283  print '...waiting for FAD to be in state 8: Writing Data'
     
    285289
    286290def Take( time, num_events, runtype):
    287 """ more general version of e.g. TakePedestalOnRun
     291  """ more general version of e.g. TakePedestalOnRun
    288292    Note: One has to check, if Ready for *Take* by oneself
    289 """
     293  """
     294  runtype += '\0'
    290295  # check if all subsystems are in the correct state
    291296  #IsReadyForDataTaking()
     
    302307
    303308def TakeData():
    304 """ taking a Data Set (1x Pedestal On, 1x LPext, 4x5min DataRun)
    305 """
     309  """ taking a Data Set (1x Pedestal On, 1x LPext, 4x5min DataRun)
     310  """
    306311  # take a Pedestal run
    307312  IsTracking()
     
    319324
    320325def TakeDrsCalibration():
    321 """ script for DRS-Calibration before Data taking
    322 """
     326  """ script for DRS-Calibration before Data taking
     327  """
    323328  print 'script for DRS-Calibration before Data taking'
    324329  print 'starting up...'
     
    388393
    389394def DataTaking1():
    390 """ Script for taking data when you are tracking wobble position 1
     395  """ Script for taking data when you are tracking wobble position 1
    391396    take a DRS CaLibration and physics Data afterwards
    392 """
     397  """
    393398  # Move Telescope to Wobble Position 1
    394399  print '--------------------------------------'
     
    415420
    416421def DataTaking2():
    417 """ Script for taking data when you are tracking wobble position 2
    418 """
     422  """ Script for taking data when you are tracking wobble position 2
     423  """
    419424  # Move Telescope to Wobble Position 2
    420425  print '--------------------------------------'
     
    438443
    439444def TakeCrab():
    440 """ Data taking and tracking script for Crab
    441 """
     445  """ Data taking and tracking script for Crab
     446  """
    442447  print '======================================'
    443448  print 'Data taking and tracking script for'
     
    473478
    474479def TrackCrabWobble1():
    475 """ changing tracking to "Crab" Wobble 1
    476 """
     480  """ changing tracking to "Crab" Wobble 1
     481  """
    477482  print 'moving telescope to wobble position 1'
    478483  print '...waiting for DRIVE_CONTROL'
     
    483488  time.sleep(5.)
    484489
    485   drive_control.track_source( 0.6, 50, 'Crab')
     490  drive_control.track_source( 0.6, 50, 'Crab\0')
    486491  # we store this command in global vars, so it is increadibly easy, to
    487492  # do this call again, when needed
     
    489494  last_drive_kwargs = { 'wobble_offset' : 0.6,
    490495                          'wobble_angle' : 50,
    491                           'source_name' : 'Crab' }
     496                          'source_name' : 'Crab\0' }
    492497  # so in case, this call needs to be repeated, just do
    493498  # last_drive_method(**last_drive_kwargs)
     
    497502
    498503def TrackCrabWobble2():
    499 """ changing tracking to "Crab" Wobble 2
    500 """
     504  """ changing tracking to "Crab" Wobble 2
     505  """
    501506  print 'moving telescope to wobble position 2'
    502507  print '...waiting for DRIVE_CONTROL'
     
    512517  last_drive_kwargs = { 'wobble_offset' : 0.6,
    513518                          'wobble_angle' : -130,
    514                           'source_name' : 'Crab' }
     519                          'source_name' : 'Crab\0' }
    515520  # so in case, this call needs to be repeated, just do
    516521  last_drive_method(**last_drive_kwargs)
     
    521526
    522527
    523 def FirstDrsCalib():
    524 """ performs the everything, which is done in the FirstDrsCalib Script as well .
    525 """
     528def FirstDrsCalib( SkipCurrentCalib=False ):
     529  """ performs the everything, which is done in the FirstDrsCalib Script as well .
     530  """
    526531  # As a First step we want to calibrate the current, which are read from the bias crate,
    527532  # and not take a DRS calibration, as it is mentioned in the data taking page...
     
    553558  print " ...done"
    554559
    555   # in case we reach this line, the voltages are all off, and the feedback does not do anything
    556   # So lets do the current calibration, therefor we tell the bias crate to ramp up just 1 single DAC count(~22mV)
    557   # the result of this action is, to get bias_ctrl into the state 'VoltageOn'(9), but since we only go one DAC count it shouldn't take long
    558   print " setting bias globally to 1 DAC"
    559   bias_control.set_global_dac(1)
    560 
    561   time.sleep(2)
    562   print " ...waiting for BIAS to be in state 9: VoltageOn"
    563   bias_control.wait(9)
    564   print " ...done"
    565 
    566   # now we may tell the feedback program to calibrate the currents ...
    567   # I do not understand, if I have to explicitely allow the feedback program to generate output,
    568   # or if it just produces output...
    569   # As far as I understand, the feedback output enable status is the same,
    570   # as it was before I send the STOP command... so it is unknown at this point.
    571   # and in addition enabling or disabling the output, when STOPed is not possible as far as I know...
    572   # I try to enable it anyway.
    573   print " enabling output for feedback"
    574   feedback.enable_output(1)
    575   time.sleep(2)
    576   print " ...done"
    577 
    578   print " calibrating bias crate current readings..."
    579   feedback.calibrate_currents()
    580   time.sleep(5)
    581   # in order to find out when the calibration ends, we have to wait for the transistion from state
    582   # 'Calibrating'(13) back to 'Connected'(6)
    583   print " ...waiting for FEEDBACK to be in state 13: Calibrating"
    584   feedback.wait(13)
    585   print " ...waiting for FEEDBACK to be in state 6: Connected"
    586   feedback.wait(6)
    587 
    588   # Thomas Bretz told me, that the feedback, after this is step has disabled its output
    589   # and is in the mode, we might call 'temperature control' even there is no temerature beeing controlled.
    590   # I don't know where the voltage is ... in order to perform the calibration, the feedback had to
    591   # ramp up to 2V below the operational voltage, i.e. about 1V below the breakdown voltage
    592 
    593   # We want to take a DRS amplitude calibration so we have to ramp down the bias voltage.
    594   # this 10sec wait is needed in order for the bias not to disconect all the time...
    595   print " ... current calibration done"
    596   time.sleep(10)
    597 
    598   print " switching off bias"
    599   bias_control.set_zero_voltage()
    600   time.sleep(5)
    601   print " ...waiting for BIAS to be in state 7: VoltageOff"
    602   bias_control.wait(7)
    603   print " ...done"
     560  if not SkipCurrentCalib:
     561    # in case we reach this line, the voltages are all off, and the feedback does not do anything
     562    # So lets do the current calibration, therefor we tell the bias crate to ramp up just 1 single DAC count(~22mV)
     563    # the result of this action is, to get bias_ctrl into the state 'VoltageOn'(9), but since we only go one DAC count it shouldn't take long
     564    print " setting bias globally to 1 DAC"
     565    bias_control.set_global_dac(1)
     566
     567    time.sleep(2)
     568    print " ...waiting for BIAS to be in state 9: VoltageOn"
     569    bias_control.wait(9)
     570    print " ...done"
     571
     572    # now we may tell the feedback program to calibrate the currents ...
     573    # I do not understand, if I have to explicitely allow the feedback program to generate output,
     574    # or if it just produces output...
     575    # As far as I understand, the feedback output enable status is the same,
     576    # as it was before I send the STOP command... so it is unknown at this point.
     577    # and in addition enabling or disabling the output, when STOPed is not possible as far as I know...
     578    # I try to enable it anyway.
     579    print " enabling output for feedback"
     580    feedback.enable_output(1)
     581    time.sleep(2)
     582    print " ...done"
     583
     584    print " calibrating bias crate current readings..."
     585    feedback.calibrate_currents()
     586    time.sleep(5)
     587    # in order to find out when the calibration ends, we have to wait for the transistion from state
     588    # 'Calibrating'(13) back to 'Connected'(6)
     589    print " ...waiting for FEEDBACK to be in state 13: Calibrating"
     590    feedback.wait(13)
     591    print " ...waiting for FEEDBACK to be in state 6: Connected"
     592    feedback.wait(6)
     593
     594    # Thomas Bretz told me, that the feedback, after this is step has disabled its output
     595    # and is in the mode, we might call 'temperature control' even there is no temerature beeing controlled.
     596    # I don't know where the voltage is ... in order to perform the calibration, the feedback had to
     597    # ramp up to 2V below the operational voltage, i.e. about 1V below the breakdown voltage
     598
     599    # We want to take a DRS amplitude calibration so we have to ramp down the bias voltage.
     600    # this 10sec wait is needed in order for the bias not to disconect all the time...
     601    print " ... current calibration done"
     602    time.sleep(10)
     603
     604    print " switching off bias"
     605    bias_control.set_zero_voltage()
     606    time.sleep(5)
     607    print " ...waiting for BIAS to be in state 7: VoltageOff"
     608    bias_control.wait(7)
     609    print " ...done"
    604610
    605611  # So now we can take the 3 runs, which are called DRS amplitude calibration:
     
    616622
    617623  fad_control.start_drs_calibration()
    618   mcp.start(-1, 1000, "drs-pedestal")
    619 
     624  time.sleep(0.5)
     625  mcp.start(-1, 1000, "drs-pedestal\0")
     626
     627  print "...waiting for FAD to be in state 8: Writing Data"
     628  fad_control.wait(8)
     629  print "...waiting for FAD to be in state 4: Connected"
     630  fad_control.wait_nice(4)
     631  print "... done"
     632
     633  print "taking DRS:Gain 1000 ..."
     634  mcp.start(-1, 1000, 'drs-gain\0')
     635  print "...waiting for FAD to be in state 8: Writing Data"
     636  fad_control.wait(8)
     637  print "...waiting for FAD to be in state 4: Connected"
     638  fad_control.wait_nice(4)
     639  print "... done"
     640
     641  time.sleep(2)
     642  if GetDrsCalibGainRms():
     643    print
     644    print 'First DRS Calib Script will be aborted'
     645    print 'operator, please power cycle FACT'
     646    return False
     647
     648  print "taking Pedestal 1000 ..."
     649  mcp.start( -1, 1000, 'pedestal\0')
    620650  print "...waiting for FAD to be in state 8: Writing Data"
    621651  fad_control.wait(8)
    622652  print "...waiting for FAD to be in state 4: Connected"
    623653  fad_control.wait(4)
    624   print "... done"
    625 
    626   print "taking DRS:Gain 1000 ..."
    627   mcp.start(-1, 1000, 'drs-gain')
    628   print "...waiting for FAD to be in state 8: Writing Data"
    629   fad_control.wait(8)
    630   print "...waiting for FAD to be in state 4: Connected"
    631   fad_control.wait(4)
    632   print "... done"
    633 
    634   print "taking Pedestal 1000 ..."
    635   mcp.start( -1, 1000, 'pedestal')
    636   print "...waiting for FAD to be in state 8: Writing Data"
    637   fad_control.wait(8)
    638   print "...waiting for FAD to be in state 4: Connected"
    639   fad_control(4)
    640654  print "... done"
    641655
     
    647661  fad_control.set_file_format(2)
    648662
    649   mcp.start(-1, 1000, 'pedestal')
     663  mcp.start(-1, 1000, 'pedestal\0')
    650664  print "...waiting for FAD to be in state 8: Writing Data"
    651665  fad_control.wait(8)
     
    680694  print "...ramping to nominal voltage"
    681695  print "...waiting for BIAS to be in state 9: VoltageOn"
    682   bias_control(9)
     696  bias_control.wait(9)
    683697  print "...bias on"
    684698  # here we should wait 45 sec in order for the current control to get enough current readings and temp readings to stabilize..
     
    691705  # then the DRS calibration above, and the pedestal run in between have to be changed as well.
    692706  print "taking Pedestal with BIAS on 3000 ..."
    693   mcp.start(-1, 3000, 'pedestal')
     707  mcp.start(-1, 3000, 'pedestal\0')
    694708  print "...waiting for FAD to be in state 8: Writing Data"
    695709  fad_control.wait(8)
     
    710724
    711725def Ratescan( ra=None, dec=None, sourcename=None):
    712 """
     726  """
    713727# call it by: .x ScriptsForDimCtrl/Ratescan.dim mode=<trackmode> ra=<Right ascension> dec=<Declination> source=<source_name>
    714728# mode=0: Manual tracking Mode: set tracking in drivectrl manually
     
    716730# mode=2: source Mode: scripts sends tracking command to drivectrl with the given source_name
    717731
    718 """
     732  """
    719733  print '======================================'
    720734  print 'RATESCAN'
     
    730744    print 'script will wait for drive'
    731745    print 'to be in state tracking'
    732   else if None != ra or None != dec:
     746  elif None != ra or None != dec:
    733747    try:
    734748      ra = float(ra)
     
    743757    print '...Dec = ', dec
    744758    drive_control.track( ra, dec)
    745   else if None != sourcename:
     759  elif None != sourcename:
    746760    print '...stop tracking'
    747761    StopTracking()
    748762    print '...change tracking of telescope to:', sourcename
     763    sourcename += '\0'
    749764    drive_control.track_source( 0, 0, sourcename)
    750765  else:
     
    792807
    793808def ResetCrate( crate_num ):
    794 """ Reset Crate
     809  """ Reset Crate
    795810    crate_num = 0,1,2 or 3  the number of the crate to reset.
    796811    crate_num = 'all' is NOT YET SUPPORTED
    797 """
     812  """
    798813  c = int(crate_num)
    799814 
     
    832847
    833848  print '...connecting FAD boards of crate', c
    834   FadConnectCrate(C)
     849  FadConnectCrate(c)
    835850  print '======================================'
    836   print 'Crate-Reset for crate'c'finished'
     851  print 'Crate-Reset for crate', c, 'finished'
    837852  print '======================================'
     853
     854def GetDrsCalibGainRms():
     855#drs_calibration(self) method of factdimserver.FAD_CONTROL instance
     856#DESC in SERVICE_DESC is empty ?!
     857#I:1;I:3;F:1474560;F:1474560;F:1474560;F:1474560;F:1474560;F:1474560;F:163840;F:163840
     858  data = fad_control.drs_calibration()
     859  N1 = data[0]
     860  N3 = data[1:4]
     861  OffsetMean = np.array(data[4:4+1024*1440]).reshape(1440,1024)
     862  OffsetRms = np.array(data[4+1024*1440:4+1024*1440*2]).reshape(1440,1024)
     863  GainMean = np.array(data[4+1024*1440*2:4+1024*1440*3]).reshape(1440,1024)
     864  GainRms = np.array(data[4+1024*1440*3:4+1024*1440*4]).reshape(1440,1024)
     865
     866  gr = GainRms.mean(axis=1)
     867  lala = np.zeros(len(gr)/9)
     868  for i,v in enumerate(lala):
     869    lala[i] = gr[i*9:(i+1)*9].mean()
     870
     871  # outliers
     872  mean = lala.mean()
     873  std = lala.std()
     874
     875  print 'Mean DRS GainRms value:', mean
     876  print 'std:', std
     877  outs = np.where( lala > mean+7*std)[0]
     878  if len(outs) > 0:
     879    print 'WARNING possible DRS underflow detected!!!'
     880    for out in outs:
     881      out = int(out)
     882      crate= out/40
     883      board = (out-40*crate)/4
     884      chip = out-40*crate-4*board
     885      print 'possible DRS underflow in DRS:', crate, board, chip, '--> Mean-GainRms:', lala[out]
     886      return outs
     887  else:
     888    return False
     889
     890import types
     891
     892def wait_nice(self, state_num, timeout=None):                           
     893  if not hasattr(self, 'stn'):
     894    raise TypeError(self.name+' has no CMD called STATE')
     895  if timeout == None:
     896    timeout = float('inf')
     897  else:
     898    timeout = float(timeout)
     899  start = time.time()
     900  intermed = time.time()-1.
     901  while not self.stn == state_num:
     902    time.sleep(0.1)
     903    if time.time() - intermed >= 1.:
     904      print fad_control.events()[0], 'events @', ftm_control.trigger_rates()[3], 'Hz'
     905      intermed = time.time()
     906   
     907    if time.time() >= start+timeout:
     908      return False
     909  return True
     910
     911fad_control.wait_nice = types.MethodType( wait_nice, fad_control)
     912
     913
     914def GetBiasCalibration():
     915  cali = feedback.calibration()
     916  bias_calibration['Time'] = time.time()
     917  bias_calibration['Calibration'] = cali
     918
     919def GetBiasCurrent(verbose = False):
     920  """ return median, std, max and min current
     921  """
     922  if 'Time' in bias_calibration:
     923    cali = bias_calibration['Calibration']
     924  else:
     925    GetBiasCalibration()
     926    cali = bias_calibration['Calibration']
     927
     928  r = np.array(cali[2*416:2*416+320])
     929
     930  bias = bias_control
     931
     932  I = np.array(bias.current()[0:320], dtype=float)
     933  II = I/4096. * 5000
     934  V = np.array(bias.voltage()[0:320])
     935  if len(sys.argv) > 1:
     936      i = int(sys.argv[1])
     937  else: i=0
     938#  print 'I:', I[i], 'dac\t', II[i], 'uA'
     939#  print 'V:', V[i]
     940#  print ''
     941#  print 'GUI offset:', V[i]/r[i]*1e6
     942#  print 'GUI feedback:', II[0] - V[0]/r[0]*1e6
     943
     944  GUII = II-V/r*1e6
     945  if verbose:
     946    print 'median', np.median(GUII)
     947    print 'mean', GUII.mean()
     948   # print 'rms', ((GUII- GUII.mean())**2).sum()
     949    print 'std', GUII.std()
     950    print 'max', GUII.max()
     951    print 'min', GUII.min()
     952
     953  return GUII.mean(), GUII.std(), GUII.max(), GUII.min()
Note: See TracChangeset for help on using the changeset viewer.