import hep.aida.*;
import hep.aida.ref.function.*;
import java.util.Random;
import sk.uniba.fmph.pocprak.ioutils.*;

public class pFit {
  static double[] p= new double[1000];
  static int nevent = 0;


  static void ReadData() throws Exception{
    double[] values;
    InTextFile in = InTextFile.open("termalizacia.txt");
    while (true){
      values = in.readlnDoubles();
      if(values==null) break;
      if(values.length==2){
        p[nevent]=values[1];
        nevent++;
      }
    }
    in.close();
  }




  public static void main(String[] args) throws Exception{
    Killer.createKiller();
    ReadData();

    IAnalysisFactory af = IAnalysisFactory.create();
    IHistogramFactory hf = af.createHistogramFactory(null);
    IHistogram1D h = hf.createHistogram1D("HYBNOST",20, -0.2,0.2);


    for (int i = 0; i < nevent; i++) {
      h.fill(p[i]);
    }

    IFunction f = new AbstractIFunction("normal", 1, 3) {
            public double value(double[] v)
               { return p[0]*
                      Math.exp(- (v[0]-p[1])*(v[0]-p[1])/(2*p[2]*p[2])); }
    };
    //f.setParameters(new double[] {100.,0.,0.07} );

    IFitFactory fitFactory = af.createFitFactory();
    IFitter fitter = fitFactory.createFitter("chi2","jminuit","noClone=\"true\"");

    //IFitResult result = fitter.fit(h,f);

    IPlotterFactory pf = af.createPlotterFactory();
    IPlotterStyle style = pf.createPlotterStyle();
    style.dataStyle().setParameter("fillHistogramBars","false");
    style.dataStyle().setParameter("functionLineColor","red");

    IPlotter plotter = pf.create();
    plotter.currentRegion().plot(h,style);
    //plotter.currentRegion().plot(result.fittedFunction(),style);

    plotter.show();

  }
}
