Created
July 3, 2013 14:50
-
-
Save mastbaum/5918760 to your computer and use it in GitHub Desktop.
Example of fitting in ROOT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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