1 # Fitting. A fit of a histogram filled with Gaussian random numbers 2 3 """

Code example

In this example we generate a Gaussian distribution and fit it with a Gaussian function. $$P(x) = \frac{1}{\sigma \sqrt {2\pi} } e^{(x - \mu)^2 / 2\sigma ^2}$$ Look at the Wikipedia definition """ 18 19 from java.awt import Color 20 from java.util import Random 21 from jhplot import HPlot,H1D,F1D 22 23 24 c1 = HPlot("Canvas",600,400,1, 1) 25 c1.setGTitle("Fit example"); #put title 26 c1.visible(1) 27 c1.setAutoRange() 28 29 min=-3.0 30 max=3.0 31 h1 = H1D("Simple1",50, min, max) 32 rand = Random() 33 34 # fill a histogram 35 for i in range(5000): 36 h1.fill(rand.nextGaussian()) 37 38 h1.setPenWidthErr(2) 39 h1.setStyle("p") 40 h1.setSymbol(4) 41 h1.setDrawLine(0) 42 c1.setNameX("Xaxis") 43 c1.setNameY("Yaxis"); 44 c1.setName("Canvas title") 45 c1.drawStatBox(h1) 46 47 # create all factories 48 c1.factories(); 49 50 # access IAnalysisFactory 51 af = c1.analF() 52 53 # access ITreeFactory 54 tf=c1.treeF() 55 56 57 # access IFitFactory 58 fitf=c1.fitF() 59 60 # access IFunctionFactory 61 funcF=c1.funcF() 62 63 64 # make fit using JAIDA 65 fitter = fitf.createFitter( "chi2", "jminuit" ) 66 result = fitter.fit(h1.get(), "g" ) 67 fresult=result.fittedFunction(); 68 fPars = result.fittedParameters() 69 fParErrs = result.errors() 70 fParNames = result.fittedParameterNames() 71 print "Fit results:" 72 for i in range(fresult.numberOfParameters()): 73 print(fParNames[i]+" : "+str(fPars[i])+" +- "+str(fParErrs[i])) 74 75 # make F1D function from IFunction in the same range as histogram 76 f2 = F1D("Gaussian",fresult,min,max) 77 f2.setColor(Color.blue) 78 f2.setPenWidth(3); 79 80 # draw fit line 81 c1.draw(f2) 82 # draw histogram on top 83 c1.draw(h1) 84 85 # export to png 86 # c1.export(Editor.DocMasterName()+".png") ### © jHepWork. S.Chekanov ###