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

public class Example5
{
   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");
      IHistogramFactory hf     = af.createHistogramFactory(tree);
      IFitFactory                fitf    = af.createFitFactory();
      IFitter                        fitter = fitf.createFitter();
      ITupleFactory          tupf  = af.createTupleFactory(tree);

      ITuple tuple = (ITuple) tree.find("tuple");
      IHistogram1D hist = hf.createHistogram1D("hist","double gaussian Hist",60, 0, 20);
      tuple.project(hist, tupf.createEvaluator("lifetime"),tupf.createFilter("signalAndBkg>1.5 && signalAndBkg<2.5"));

      double[] initialPars = {20, -1./hist.rms()};
      IFitResult result = fitter.fit(hist,"e",initialPars);

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

