package jas.hist; import java.io.Serializable; import java.util.Observable; import java.util.Observer; public abstract class Fitter extends Observable implements Observer, Runnable, Serializable { public abstract double getChiSquared(); public abstract double[] getParameterSigmas(); protected abstract void fit(Fittable1DFunction fa, double[] x, double[] y, double[] sigmaY) throws FitFailed; public void fit() throws FitFailed { if (state != READYTOFIT) throw new FitFailed("Not ready to fit"); internalFit(); observeData(); } public synchronized void start() { if (state == READYTOFIT) { thread = new Thread(this,"Fit thread"); thread.start(); } } public synchronized void stop() { if (thread != null) thread.stop(); } public void run() { try { internalFit(); observeData(); } catch (FitFailed x) {} finally { synchronized (this) { thread = null; } } } public synchronized void update(Observable obs, Object arg) { if (obs == m_data) { if (state == FIT) setState(READYTOFIT); start(); } } public int getState() { return state; } private synchronized void observeData() { if (!observing && m_data instanceof Observable) ((Observable) m_data).addObserver(this); } private synchronized void setState(int state) { this.state = state; setChanged(); notifyObservers(new FitUpdate(state)); } private synchronized void setState(int state, FitFailed x) { this.state = state; setChanged(); notifyObservers(new FitUpdate(state,x)); } protected void setPercentComplete(int percent) { setChanged(); notifyObservers(new FitUpdate(state,percent)); } private void internalFit() throws FitFailed { setState(FITTING); try { int n = m_data.getNPoints(); double[] x = new double[n]; double[] y = new double[n]; double[] yerr = new double[n]; for (int i=0; i