``` 1 # Fitting. Fit a 2D histogram with a Gaussian 2 3 from jhplot import * 4 from java.util import Random 5 6 # creating 2D data 7 h1 = H2D('Data', 40, -10, 10, 40, -10,10) 8 r = Random() 9 for i in range(10000): 10 h1.fill(2*r.nextGaussian(),2*r.nextGaussian()+3) 11 12 c1 = HPlot3D('Canvas',800,400,2,1) 13 c1.visible() 14 15 # fitting with 2D Gaussian 16 f=HFitter() 17 f.setFunc('g2D',2, 'N*(exp( -0.5*((mu0-x[0])*(mu0-x[0])+0.5*(mu1-x[1])*(mu1-x[1])) /(s0*s0) ))','N,s0,mu0,mu1') 18 f.setPar("N",100) 19 f.setPar("s0",1.0) 20 f.setPar("mu0",0.0) 21 f.setPar("mu1",1.0) 22 f.fit(h1) 23 24 ff=f.getFittedFunc() 25 r=f.getResult() 26 Pars = r.fittedParameters() 27 Errors = r.errors() 28 Names = r.fittedParameterNames() 29 print "Fit results:" 30 for i in range(ff.numberOfParameters()): 31 print Names[i]+" : "+str(Pars[i])+" +- "+str(Errors[i]) 32 print 'chi**2/ndf='+str(round(r.quality()*r.ndf()))+"/",r.ndf() 33 34 35 f1 = F2D(ff,-10,10,-10,10) 36 c1.setGTitle("2D data fitted with a 2D Gaussian") 37 c1.cd(1,1) 38 c1.draw(h1) 39 40 c1.cd(2,1) 41 c1.draw(f1) ### © jHepWork. S.Chekanov ### ```