How to write a pseudo counter controller

The basics

An example of a X-ray beam position monitor (XBPM) pseudo counter controller will be build incrementally from scratch to aid in the explanation. Its purpose is to provide an easy feedback about the beam position in the vertical and horizontal axes as well as the total intensity of the beam.

By now you should have read the general controller basics chapter. Let’s start from writing a PseudoCounterController subclass with a proper constructor and the roles defined.

from sardana.pool.controller import PseudoCounterController

class XBPMPseudoCounterController(PseudoCounterController):

    counter_roles = ('top', 'bottom', 'right', 'left')
    pseudo_counter_roles = ('vertical', 'horizontal', 'total')

    def __init__(self, inst, props, *args, **kwargs):
        super(XBPMPseudoCounterController, self).__init__(inst, props, *args, **kwargs)

The counter_roles and pseudo_counter_roles tuples contains names of the counter and pseudo counter roles respectively. These names are used when creating the controller instance and their order is important when writing the controller itself. Each controller will define its own roles.


It is possible to omit the pseudo_counter_roles definition if the controller provides only one axis. The controller class name will be assumed as the pseudo counter role.

The constructor does nothing apart of calling the parent class constructor but could be used to implement any necessary initialization.

The pseudo counter calculations are implemented in the calc() method:

def calc(self, index, counter_values):
    top, bottom, right, left = counter_values

    if index == 1: # vertical
        vertical = (top - bottom)/(top + bottom)
        return vertical
    elif index == 2: # horizontal
        horizontal = (right - left)/(right + left)
        return horizontal
    elif index == 3: # total
        total = (top + bottom + right + left) / 4
        return total

From the implementation we can conclude that the vertical pseudo counter will give values from -1 to 1 depending on the beam position in the vertical dimension. If the beam passes closer to the top sensor, the value will be more positive. If the beam passes closer to the bottom sensor the value will be more negative. The value close to the zero indicates the beam centered in the middle. Similarly behaves the horizontal pseudo counter. The total pseudo counter is the mean value of all the four sensors and indicates the beam intensity.

What to do when pseudo value can not be determined?

Some controllers won’t be able to provide the pseudo calculation result because for some combinations of the physical values it may not exist.

In such cases as the controller developer you should raise an exception from Calc() or CalcAll() with a descriptive message why the value could not be calculated. Sardana will take care of propagating this exception message to the user giving him feedback.

Changing default interface

Pseudo counters instantiated from your controller will have a default interface, which among others, comprises the value attribute. This attribute is feed with the result of the calc() method and by default it expects values of float type and scalar format. You can easily change the default interface. This way you could program a pseudo counter to obtain an image RoI of a 2D experimental channel.

Here is an example of how to change value attribute’s format to an image and specify its maximum dimension of 1024 x 1024 pixels:

def GetAxisAttributes(self, axis):
    axis_attrs = PseudoCounterController.GetAxisAttributes(self, axis)
    axis_attrs = dict(axis_attrs)
    axis_attrs['Value'][Type] = ((float, ), )
    axis_attrs['Value'][MaxDimSize] = (1024, 1024)
    return axis_attrs

Get pseudo counter shape

If you change the pseudo counter format to spectrum or image then you controller should provide the shape of the calculation result in either of the formats:

  • one-element sequence with the length of the spectrum

  • two-element sequence with the horizontal and vertical dimensions of the image

using the GetAxisPar() method.

Here is an example of the possible implementation of GetAxisPar():

def GetAxisPar(self, axis, par):
    if par == "shape":
        return [1024, 1024]

For backwards compatibility, in case of not implementing the shape axis parameter, shape will be determined from the MaxDimSize of the Value attribute as defined in Changing default interface.

Including external variables in the calculation

The pseudo counter calculation may require an arbitrary variable which is not a counter value. One can use Taurus or PyTango libraries in order to read their attributes and use them in the calculation. It is even possible to write pseudo counters not based at all on the counters. In this case it is enough to define an empty counter_roles tuple.