Experiment

 

class Experiment( Device):
    """
    All detectors are class variables of this class. The detectors
    to appear in the document are selected during the instantiation: 
    det = Experiment( read_attrs = ['eh_c01', 'eh_c02']) 
    """
    if len( allDevices) == 0:
        allDevices = HasyUtils.getOnlineXML()
    #
    # her we execute statements of this kind:
    #  eh_c01 = Cpt( Signal, name='Counts', kind='normal')
    # this way, calss variables are created. Later they will be
    # referenced as instance variables: selg.eh_c01
    #
    for elm in allDevices:
        if elm[ 'module'] != 'sis3820' and \
           elm[ 'module'] != 'mca_8701':
            continue
        cmd = "%s = Cpt(Signal, name='Counts', kind='hinted')" % elm['name']
        exec( cmd)

    def __init__(self, *args, **kwargs):

        if 'name' in kwargs:
            raise ValueError( "TangoIfc.Experiment: 'name' is taken from the ActiveMntGrp")
        
        env = blueskyDESY.getEnv()
        try: 
            self.name = env[ 'ActiveMntGrp'] # e.g. 'MG1'
            self.activeMntGrp = env[ self.name]
        except Exception as e:
            raise ValueError( "TangoIfc.Experiment: no ActiveMntGrp")

        super().__init__( name = self.name, *args, **kwargs)
        self._acquiring_lock = threading.Lock()
        
        self.counters = []
        for elm in self.activeMntGrp[ 'counters']:
            hsh = {}
            hsh[ 'name'] = elm
            p = tango.DeviceProxy( elm)
            hsh[ 'TangoDevice'] = p.TangoDevice
            hsh[ 'proxy'] = tango.DeviceProxy( hsh[ 'TangoDevice'])
            self.counters.append( hsh)
        
        self.mcas = []
        for elm in self.activeMntGrp[ 'mcas']:
            hsh = {}
            hsh[ 'name'] = elm
            p = tango.DeviceProxy( elm)
            hsh[ 'TangoDevice'] = p.TangoDevice
            hsh[ 'proxy'] = tango.DeviceProxy( hsh[ 'TangoDevice'])
            self.mcas.append( hsh)

        self.timers = []
        for elm in self.activeMntGrp[ 'timers']:
            hsh = {}
            hsh[ 'name'] = elm
            p = tango.DeviceProxy( elm)
            hsh[ 'TangoDevice'] = p.TangoDevice
            hsh[ 'proxy'] = tango.DeviceProxy( hsh[ 'TangoDevice'])
            self.timers.append( hsh)

        self.sampleTime = self.activeMntGrp[ 'sampleTime']

        return
    
    def _capture(self, status):
        try:
            if not self._acquiring_lock.acquire(timeout=0):
                raise RuntimeError("Cannot trigger, currently triggering!")

            #lst = [ elm[ 'name'] for elm in self.counters]
            #print( "TangoIfc.Experiment._capture: counters %s" % repr( lst))
            #lst = [ elm[ 'name'] for elm in self.timers] 
            #print( "TangoIfc.Experiment._capture: timers %s, sample time %g" % ( repr( lst), self.sampleTime))

            #
            # resetting counters
            #
            for elm in self.counters:
                elm[ 'proxy'].Reset()
            #
            # start MCAs
            #
            for elm in self.mcas:
                elm[ 'proxy'].Clear()
                elm[ 'proxy'].Start()
            #
            # prepare timers: sampleTime and start()
            #
            for elm in self.timers:
                elm[ 'proxy'].SampleTime = self.sampleTime
            for elm in self.timers:
                elm[ 'proxy'].start()
            #
            # check, if on of the imers is MOVING
            #
            isMoving = False
            for elm in self.timers:
                if elm[ 'proxy'].state() == tango.DevState.MOVING:
                    isMoving = True
                    break
            while isMoving:
                isMoving = False
                for elm in self.timers:
                    if elm[ 'proxy'].state() == tango.DevState.MOVING:
                        isMoving = True
                        break
                ttime.sleep( 0.1)
            #
            # read the counters
            #
            for elm in self.counters:
                cmd = "self.%s.set( %g)" % ( elm[ 'name'], elm[ 'proxy'].Counts)
                exec( cmd)
            #
            # read the mcas
            #
            for elm in self.mcas:
                #dataLength = elm[ 'proxy'].DataLength
                #cmd = "self.%s.set( [1, 2, 3])" % ( elm[ 'name'])
                #exec( cmd)

                elm[ 'proxy'].Stop()
                elm[ 'proxy'].Read()
                self.eh_mca01.set( elm[ 'proxy'].Data)
                
        except Exception as exc:
            #
            # set_exception() generates a lot of output but stops the plan
            #
            status.set_exception(exc)
            raise Exception( "TangoIfc.MG._capture: caught exception, %s" % repr( exc))
        else:
            status.set_finished()
        finally:
            self._acquiring_lock.release()
            
    def trigger(self):
        status = DeviceStatus(self)
    
        # Start a background thread to capture an image and write it to disk.
        thread = threading.Thread(target=self._capture, args=(status,))
        thread.start()
        # Promptly return a status object, which will be marked "done" when the
        # capture completes.
        return status