
from hep.aida import IAnalysisFactory
from java.util import Random
from jhplot import *

af   = IAnalysisFactory.create();
tree = af.createTreeFactory().create();
hf   = af.createHistogramFactory(tree);

r = Random()

dpf = af.createDataPointSetFactory(tree)
dataPointSet = dpf.create("dataPointSet","two dimensional IDataPointSet",2)

for i in range(20):
    dataPointSet.addPoint()
    dp = dataPointSet.point(i)
    dp.coordinate(0).setValue(i)
    dp.coordinate(1).setValue(i+r.nextDouble()-0.5)
    dp.coordinate(1).setErrorPlus(1)
    dp.coordinate(1).setErrorMinus(1)

functionfact = af.createFunctionFactory(tree)
line = functionfact.createFunctionByName("line","p1")

ff = af.createFitFactory()
fitter = ff.createFitter("Chi2","jminuit")
result = fitter.fit(dataPointSet,line)
print "Chi2 = ", result.quality()

c1=HPlotter("ComplexFit",700,600)
c1.visible()
c1.draw(dataPointSet)
c1.draw(result.fittedFunction())
