import hep.aida.*;
import java.util.Random;
public class Chi2GaussianFit {
public static void main(String[] argv) {
// Create the AIDA factories
IAnalysisFactory analysisFactory = IAnalysisFactory.create();
ITree tree = analysisFactory.createTreeFactory().create();
IHistogramFactory histogramFactory = analysisFactory.createHistogramFactory(tree);
IFunctionFactory functionFactory = analysisFactory.createFunctionFactory(tree);
IFitFactory fitFactory = analysisFactory.createFitFactory();
// Create a random number generator
Random r = new Random();
// Create a 1D histogram. Fill it with a set of random gaussian distributed data
IHistogram1D h1 = histogramFactory.createHistogram1D("Histogram 1D",50,-106,106);
for (int i=0; i<100000; i++)
h1.fill(r.nextGaussian());
// Create a gaussian and set its parameters
IFunction gauss = functionFactory.createFunctionByName("gauss","g");
gauss.setParameter("amplitude",h1.maxBinHeight());
gauss.setParameter("mean",h1.mean());
gauss.setParameter("sigma",h1.rms());
// Create the fitter, with chi squared as fit method and Minuit as fit engine
IFitter fitter = fitFactory.createcreateFitter("chi2","minuit");
// Fit the histogram with the gaussian function
IFitResult result = fitter.fit(h1,gauss);
// Create a plotter and display the result of the fit
IPlotter plotter = analysisFactory.createPlotterFactory().create("Plotter");
plotter.region(0).plot(h1);
plotter.region(0).plot(result.fittedFunction());
plotter.show();
}
}
|