The following is a set of java examples that illustrate how to perform relatively simple fits. These examples can be compiled and run as standalone programs using JAIDA (see the JAIDA Release Notes) or they can be loaded, compiled and run within JAS3. Just copy and paste the desired example in a file that has the same name as the class file.
Gaussian Fit To An Histogram
Code Block |
---|
title | Chi2GaussianFit.java |
---|
borderStyle | solid |
---|
|
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.createFitter("chi2","minuit");
// Fit the histogram with the gaussian function
IFitResult result = gaussFitfitter.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();
}
}
|
Fit To A Data Point Set