import hep.aida.*;
import org.freehep.application.*;
import org.freehep.application.studio.*;

public class Example2
{
   public static void main(String[] argv) throws java.io.IOException
   {
      IAnalysisFactory  af     = IAnalysisFactory.create();
      // The line below is the AIDA way to open a data set.
      //ITree                     tree = af.createTreeFactory().create("JAS3DataSet.aida");
      // We use instead the following JAS3 specific way to open a tree so that we can access the
      // already opened tree.
      ITree tree = (ITree) ((Studio) Application.getApplication()).getLookup().lookup(ITree.class); 
      tree.cd("/JAS3DataSet.aida");
      IFitter                     fitter = af.createFitFactory().createFitter();
      IFunctionFactory ff       = af.createFunctionFactory(tree);

      IDataPointSet dataPoint = (IDataPointSet) tree.find("dataPointSet parabola");

      IFunction parabola = ff.createFunctionFromScript("parabola",1,"a + b*x[0] + c*x[0]*x[0]","a,b,c","");
      parabola.setParameters( new double[] {10,2,1} );
   
      IFitResult result = fitter.fit(dataPoint,parabola);

      System.out.println("** Chi2 : "+result.quality());

      IPlotter plotter = af.createPlotterFactory().create("Example2 Plot");
      plotter.region(0).plot(dataPoint);
      plotter.region(0).plot(result.fittedFunction());
      plotter.show();
   }
}