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

public class Example1
{
   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();

      IDataPointSet dataPointSet = (IDataPointSet) tree.find("dataPointSet line");

      IFitResult result = fitter.fit(dataPointSet,"p1");

      IFunction f = result.fittedFunction();
      String[] pars = result.fittedParameterNames();
      System.out.println("*** Chi2 "+result.quality());
      for ( int i = 0; i < pars.length; i++ ) 
           System.out.println(" -"+i+"- "+pars[i]+"  "+result.fittedParameter(pars[i])+" "+Math.sqrt( result.covMatrixElement(i,i)));

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