How To Write Your Own Function

Introduction

To define a new function that can be displayed on a plot in Java Analysis Studio you must define a class that extends one of the following classes:

jas.hist.Basic1DFunction
For a simple non-fittable function.
jas.hist.Fittable1DFunction
If the function is to be fittable.

In addition your class must implement jas.hist.HasHandles if you want the user to be able to manipulate the function by dragging handles in the histogram window.  Your class must implement the jas.hist.FunctionAdvancedOptions interface if you wish to have an advanced options dialog box available.  This dialog is suitable if your function has options that the user could not otherwise control.  For example, the order of the polynomial function is set by default to 2, but that can be changed in the advanced dialog for jasext.hist.function.PolynomoialFunction to any other valid integer.

Contents

Extending jas.hist.Basic1DFunction

By extending jas.hist.Basic1DFunction you can produce a function which can be displayed on a plot. The following shows a simple implementation of Basic1DFunction.

Testing your Function

To test your function store this test routine in the same directory as your test function. 

Open the both your test function and this routine in JAS and compile them both, then select Job, Load Program, make sure the directory containing the files is in your JAS CLASSPATH, then enter the name of the function (SineTest), and hit OK.

This will register your function with the plotter, and create a histogram for testing your function. To test, view the histogram, right click, and choose Add Function.

If you change your function, you will need to recompile it, choose Job, Unload All Programs, then load the test program again.

Extending jas.hist.Fittable1DFunction

In order to make your function fittable you must change it to extend jas.hist.Fittable1DFunction, and add two extra methods. 

  • valueAt(double x, double[] p)
  • setFit(Fitter fit, double[] p)

In addition you must modify the setParameter() method to clear any fit associated with the function. Here are the modified and added methods.

Once these methods have been implemented the function should be fittable, as shown in this diagram:

Implementing jas.hist.HasHandles

It is sometime useful for the user to be able to move the function interactively, for example to set the approximate parameter values before performing a fit. To enable this you must make your function implement jas.hist.HasHandles. This is the implemention of the getHandles method for the SineFunction: 

This example uses  jas.hist.Handle, an abstract class with three abstract methods that you must implement:

public double getX()
public double getY()

These methods return the current location of the handle.

protected void moveTo(double x, double y)

This method is called when the handle has been moved.  Use the given x and y values to modify the parameters of the function appropriately.

The complete implementation of SineFunction can be found here.

Implementing jas.hist.FunctionAdvancedOptions

You will want to implement this interface if your function has specific options which the user cannot change any other way.  The interface jas.hist.FunctionAdvancedOptions contains only one method:

    public void openAdvancedDialog(Frame f, JASHist hist)

This method is called when the user clicks on the 'Advanced Options' button or selects 'Advanced Options' from one of the menus, and is typically used to display a Dialog asking the user some question.  It passes as an argument a reference to the java.awt.Frame object that you should use as the parent frame for the dialog box.  It also receives a reference to the current plot. 

Automatically Loading your Function at JAS Startup

To automatically load your function at JAS startup time you will need to write a simple extension plugin which registers your function. See for example the implementation of jasext.hist.Register.

If you add a function you think would be generally useful please send it to us so we can add it to tehdefault functions in JAS.


Page last updated: Wednesday, January 14, 2004 by SLAC\tonyj