# The Peak Optimiser

In instrument control the need may arise to optimise a peak with respect to several variables. Optimising means finding the maximum of the peak with respect to several variables. This is useful during instrument calibration, for example. Four circle diffractometers use this facility on a day to day basis for finding and verifying the exact position of reflections. In order to support both usages a more general module has been implemented. The algorithm is like this:

    while errors gt precision and cycles lt maxcycles
      for all variables
          do a scan
          Try find the maximum, two halfwidth points and the peak center.
          if failure extend the scan.
          if success shift the variable, remember last shift.
          If shift lt precicison mark this variable as done
       end for
    end while

Possible outcomes of this procedure are: success, the peak was lost or the maximum number of cycles was reached. This routine requires that the instrument is currently placed somewhere on the peak and not miles away.

The Peak Optimiser is implemented as an object with the name opti. It understand the following commands:

opti clear
  clears the optimiser.
opti addvar name step nStep precision
  This command adds a variable to optimise to the optimiser. The user has to specify the name of the variable, the step width to use for scanning, the number of steps needed to cover the full peak when scanning and the precision which should be achieved when optimising the peak. The step width and number of steps parameters should cover the whole peak. However, the Optimiser will extend the scan is the specified range is not sufficient.
opti run
  Starts the optimiser. It will then optimise the peak. This may take some time.

The behaviour of the optimiser can be configured by modifying some parameters. The synatx is easy: opti parameter prints the value of the parameter, opti parameter newval sets a new value for the parameter. The following parameters are supported:

maxcycles
  The maximum number of cycles the optimiser will run when trying to optimise a peak. The default is 7.
threshold
  When a peak cannot be identified after a scan on a variable, the optimiser will check if there is a peak at all. In order to do that it searches for a count rate higher then the threshold parameter. If such a rate cannot be found the optimiser will abort and complain that he lost the peak.
channel
  The counter channel to use for scanning. The default is to use the counter. By modifying this parameter, the optimiser can optimise on a monitor instead.
countmode
  The counting mode to use when scanning. Possible values are timer or monitor.
preset
  The preset value to use for counting in the scan. Depending on the status of the countmode parameter this is either a preset time or a preset monitor.