Skip to content

Instantly share code, notes, and snippets.

@mastbaum
Created July 3, 2013 14:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mastbaum/5918760 to your computer and use it in GitHub Desktop.
Save mastbaum/5918760 to your computer and use it in GitHub Desktop.
Example of fitting in ROOT
ifndef ROOTSYS
$(error ROOTSYS is not defined)
endif
all: minuit_example
minuit_example:
g++ -o minuit_example minuit_example.cpp `root-config --libs --cflags` -lMinuit
clean:
rm minuit_example
/**
* Example of fitting in ROOT
*
* Uses ROOT's TMinuit (via TFitter) to find the minimum of a 1D parabola,
* starting from a uniform random seed. The fit function uses some externally-
* defined global state.
*/
#include <iostream>
#include <TMath.h>
#include <TFitter.h>
#include <TRandom.h>
static float two = 2.0;
void likelihood(int& npar, double* g, double& result, double* par, int flag) {
result = TMath::Power(par[0], two); // function using some external global
}
int main(int argc, char* argv[]) {
gRandom->SetSeed(0);
TFitter minuit(1); // 1 parameter
minuit.SetFCN(likelihood);
float guess = gRandom->Uniform(-5, 5);
minuit.SetParameter(0, "x", guess, 0.5, 0, 0);
minuit.ExecuteCommand("MIGRAD", 0, 0);
float best_fit = minuit.GetParameter(0);
std::cout << "Best fit: " << best_fit << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment