Fitting a histogram with the Chi2 method
Source code name: "jaida_Chi2Fit.java"
Programming language: Java
Topic: Data fitting/jaida
DMelt Version 1. Last modified: 05/25/2016. License: Pro
https://datamelt.org/code/cache/jaida_Chi2Fit_5771.java
To run this script using the DMelt IDE, copy the above URL link to the menu [File]→[Read script from URL] of the DMelt IDE.


import hep.aida.*;
import java.util.Random;

public class jaida_Chi2Fit
{
   public static void main(String[] args)
   {
      // Create factories
      IAnalysisFactory analysisFactory = IAnalysisFactory.create();
      IHistogramFactory histogramFactory = analysisFactory.createHistogramFactory(analysisFactory.createTreeFactory().create());
      IPlotter plotter = analysisFactory.createPlotterFactory().create("Plot");
      IFitFactory fitFactory = analysisFactory.createFitFactory();

      // Create 1D histogram
      IHistogram1D h1d = histogramFactory.createHistogram1D("Gaussian Distribution",100,-5,5);
       IHistogram1D h2d = histogramFactory.createHistogram1D("Gaussian 2",100,-5,5);

      // Fill 1D histogram with Gaussian
      Random r = new Random();
      for (int i=0; i<5000; i++) {
         h1d.fill(r.nextGaussian());
         h2d.fill(0.4*r.nextGaussian());
      }

      // Do Fit
      IFitter fitter = fitFactory.createFitter("chi2");
      IFitResult result = fitter.fit(h1d,"g");

      // Show results
      plotter.createRegions(1,2,0);
      plotter.region(0).plot(h1d);
      plotter.region(0).plot(result.fittedFunction());
      plotter.region(1).plot(h2d);
      plotter.show();
   }
}



You see the box below because you did not login.