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

public class Example3
{
   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");
      IFitFactory             fitf    = af.createFitFactory();
      IFitter                     fitter = fitf.createFitter();
      IFitData                 data = fitf.createFitData();

      IHistogram1D hist = (IHistogram1D) tree.find("gauss Sum Hist");

      data.create1DConnection(hist);

      data.range(0).excludeAll();;
      data.range(0).include(0,1.5);
      double[] g1InitPars = new double[] {20,1,0.2};
      IFitResult result1 = fitter.fit(data,"g",g1InitPars);

      data.range(0).excludeAll();;
      data.range(0).include(1.5,5);
      double[] g2InitPars = new double[] {45,3,1};
      IFitResult result2 = fitter.fit(data,"g",g2InitPars);

      IPlotter plotter = af.createPlotterFactory().create("Example3 Plot");
      plotter.region(0).plot(hist);
      plotter.region(0).plot(result1.fittedFunction());
      plotter.region(0).plot(result2.fittedFunction());
      plotter.show();
   }
}

