2 #include "AliTMinuitToolkit.h"
4 #include <TVirtualFitter.h>
18 //--------------------------------------------------------------------------
20 // The AliTMinuitToolkit serves as an easy to use interface for the TMinuit
23 // - It allows to fit a curve to one and two dimensional histograms
24 // (TH2F::Fit() only allows to fit a hyperplane).
25 // - Or n points can be specified directly via a n x 2 matrix.
26 // - An option for robust fitting of non-linear functions is implemented.
29 // 1. Setting the formula:
31 // The formula is simply set via "void SetFitFunction(TFormula * formula)".
34 // 2. Adding the data points
36 // - In order to fit a histogram, use "void FitHistogram(TH1F * his)" or
37 // "void FitHistogram(TH2F * his)". The fitter is started automatically
38 // - Alternatively, the direct specification of the points is possible via
39 // "void SetPoints(TMatrixD * points)". Note, that the each point
40 // corresponds to one row in the matrix. The fitter is then started with
41 // the command "void Fit()".
44 // 3. Accessing the fit results
46 // The N parameters of the formula are stored in a N-dimensional vector which
47 // is returned by "TVectorD * GetParameters()". In a similar the covariance
48 // matrix of the fit is returned via "TMatrixD * GetCovarianceMatrix()" which
49 // is of the type N x N.
52 // 4. Non-linear robust fitting:
54 // Even a few outliers can lead to wrong results of a least-squares fitting
55 // procedure. In this case the use of robust(resistant) methods can be
56 // helpful, but a stronger dependence on starting values or convergence to
57 // local minima can occur.
59 // The robust option becomes active if a weighting function is specified.
60 // All points are sorted according to their distance to the curve and
61 // weighted. The weighting function must be defined on the interval [0,1].
63 // Some standard weighting functions are predefined in
64 // "SetWeightFunction(Char_t * name, Float_t param1, Float_t param2 = 0)":
65 // - "BOX" equals to 1 if x < param1 and to 0 if x > param1.
66 // - "EXPONENTIAL" corresponds to "Math::Exp(-TMath::Log(param1)*x)"
67 // - "ERRORFUNCTION" corresponds to "TMath::Erfc((x-param1)/param2)"
69 //-------------------------------------------------------------------------
72 ClassImp(AliTMinuitToolkit)
74 AliTMinuitToolkit::AliTMinuitToolkit() :
88 // standard constructor
95 AliTMinuitToolkit::AliTMinuitToolkit(const AliTMinuitToolkit&) :
113 AliTMinuitToolkit& AliTMinuitToolkit::operator=(const AliTMinuitToolkit&) {
120 AliTMinuitToolkit::~AliTMinuitToolkit(){
125 delete fWeightFunction;
133 void AliTMinuitToolkit::FitHistogram(TH1F * his) {
135 // Fit a one dimensional histogram
137 fPoints = new TMatrixD(his->GetNbinsX(), 2);
139 for(Int_t ibin=0; ibin < his->GetNbinsX(); ibin++) {
140 Double_t x = his->GetXaxis()->GetBinCenter(ibin+1);
141 Double_t y = his->GetBinContent(ibin+1);
143 (*fPoints)(ibin, 0) = x;
144 (*fPoints)(ibin, 1) = y;
151 void AliTMinuitToolkit::FitHistogram(TH2F * his) {
153 // Fit a two dimensional histogram
155 fPoints = new TMatrixD((Long64_t)his->GetEntries(), 2);
158 for(Int_t ibin=0; ibin < his->GetNbinsX(); ibin++) {
159 Double_t x = his->GetXaxis()->GetBinCenter(ibin);
160 for(Int_t jbin=0; jbin < his->GetNbinsY(); jbin++) {
161 Long64_t n = his->GetBin(ibin, jbin);
162 Double_t y = his->GetYaxis()->GetBinCenter(jbin);
163 for(Int_t ientries=0; ientries < his->GetBinContent(n); ientries++) {
164 (*fPoints)(entry,0) = x;
165 (*fPoints)(entry,1) = y;
176 void AliTMinuitToolkit::SetWeightFunction(Char_t * name, Float_t param1, Float_t param2) {
178 // Set the weight function which must be defined on the interval [0,1].
180 TString FuncType(name);
183 if (FuncType == "EXPONENTIAL") fWeightFunction = new TFormula("exp", Form("TMath::Exp(-TMath::Log(%f)*x)", param1));
184 if (FuncType == "BOX") fWeightFunction = new TFormula("box", Form("TMath::Erfc((x-%f)/0.0001)", param1));
185 if (FuncType == "ERRORFUNCTION") fWeightFunction = new TFormula("err", Form("TMath::Erfc((x-%f)/%f)", param1, param2));// !!!!!!!!!!!!!!!!!
190 void AliTMinuitToolkit::FitterFCN(int &npar, double *dummy, double &fchisq, double *gin, int iflag){
192 // internal function which gives the specified function to the TMinuit function
195 // suppress warnings for unused variables:
200 AliTMinuitToolkit * fitter = (AliTMinuitToolkit*)TVirtualFitter::GetFitter()->GetObjectFit();
202 Int_t nvar = fitter->GetPoints()->GetNcols()-1;
203 Int_t npoints = fitter->GetPoints()->GetNrows();
205 // sort points for weighting
206 Double_t *sortList = new Double_t[npoints];
207 Int_t *indexList = new Int_t[npoints];
209 TVectorD *Weight = new TVectorD(npoints);
211 for (Int_t ipoint=0; ipoint<npoints; ipoint++){
213 for (Int_t ivar=0; ivar<nvar; ivar++){
214 x[ivar] = (*fitter->GetPoints())(ipoint, ivar);
216 Float_t funx = fitter->GetFormula()->EvalPar(x,gin);
217 sortList[ipoint] = TMath::Abs((*fitter->GetPoints())(ipoint, nvar) - funx);
220 TMath::Sort(npoints, sortList, indexList, false);
223 for (Int_t ipoint=0; ipoint<npoints; ipoint++){
224 t = indexList[ipoint]/(Double_t)npoints;
225 (*Weight)(ipoint) = fitter->GetWeightFunction()->Eval(t);
228 // calculate chisquare
229 for (Int_t ipoint=0; ipoint<npoints; ipoint++){
231 for (Int_t ivar=0; ivar<nvar; ivar++){
232 x[ivar] = (*fitter->GetPoints())(ipoint, ivar);
234 Float_t funx = fitter->GetFormula()->EvalPar(x,gin);
236 Double_t delta = (*fitter->GetPoints())(ipoint, nvar) - funx;
237 fchisq+= delta*delta*(*Weight)(ipoint);
246 void AliTMinuitToolkit::Fit() {
248 // internal function that calls the fitter
250 Int_t nparam = fParam->GetNrows();
252 // set all paramter limits to infinity as default
253 if (fParamLimits == 0) {
254 fParamLimits = new TMatrixD(nparam ,2);
255 for (Int_t iparam=0; iparam<nparam; iparam++){
256 (*fParamLimits)(iparam, 0) = 0;
257 (*fParamLimits)(iparam, 1) = 0;
261 // set all weights to 1 as default
262 if (fWeightFunction == 0) {
263 fWeightFunction = new TFormula("constant", "1");
266 // migrad fit algorithm as default
267 if (fFitAlgorithm == 0) {
268 fFitAlgorithm = "migrad";
272 TVirtualFitter *minuit = TVirtualFitter::Fitter(0, nparam);
273 minuit->SetObjectFit(this);
274 minuit->SetFCN((void*)(AliTMinuitToolkit::FitterFCN));
276 // initialize paramters (step size???)
277 for (Int_t iparam=0; iparam<nparam; iparam++){
278 minuit->SetParameter(iparam, Form("p[%d]",iparam), (*fParam)(iparam), (*fParam)(iparam)/10, (*fParamLimits)(iparam, 0), (*fParamLimits)(iparam, 1));
282 argList[0] = fMaxCalls; //maximal number of calls
283 argList[1] = fPrecision; //tolerance normalized to 0.001
284 if (fMaxCalls == 500 && fPrecision == 1) minuit->ExecuteCommand(fFitAlgorithm, 0, 0);
285 if (fMaxCalls != 500 || fPrecision != 1) minuit->ExecuteCommand(fFitAlgorithm, argList, 2);
286 // two additional arguments can be specified ExecuteCommand("migrad", argList, 2) - use 0,0 for default
288 // fill parameter vector
289 for (Int_t ivar=0; ivar<nparam; ivar++){
290 (*fParam)(ivar) = minuit->GetParameter(ivar);
293 // fill covariance matrix
294 fCovar = new TMatrixD(nparam, nparam);
295 //TVirtualFitter *fitCov = TVirtualFitter::GetFitter();
296 for(Int_t i=0; i < nparam; i++) {
297 for(Int_t j=0; j < nparam; j++) {
298 (*fCovar)(i,j) = minuit->GetCovarianceMatrixElement(i,j);
306 void AliTMinuitToolkit::Test() {
308 // This test function shows the basic working principles of this class
309 // and illustrates how a robust fit can improve the results
311 TFormula *FormExp = new TFormula("formExp", "[0]*TMath::Exp(-[1]*x)");
312 SetFitFunction(FormExp);
313 SetFitAlgorithm("simplex");
314 // Set initial values
315 TVectorD *vec1 = new TVectorD(2);
318 SetInitialParam(vec1);
319 //provide some example histogram
320 TH1F * hist = new TH1F("bla", "with (red) and without (black) robust option", 20,0,4);
321 TRandom * rand = new TRandom();
322 for (Int_t i = 0; i < 10000; i++) {
323 hist->Fill(rand->Exp(1));
324 if (i < 1000) hist->Fill(3); //"outliers"
325 if (i < 1070) hist->Fill(3.5);
326 if (i < 670) hist->Fill(2);
327 if (i < 770) hist->Fill(1.5);//"outliers"
328 if (i < 740) hist->Fill(1);
330 TCanvas * canv = new TCanvas();
333 // fit it with the exponential decay
336 TF1 *func = new TF1("test", "[0]*TMath::Exp(-[1]*x)", 0, 6);
337 func->SetParameter(0, (*GetParameters())(0));
338 func->SetParameter(1, (*GetParameters())(1));
341 TVectorD *vec2 = new TVectorD(2);
344 SetInitialParam(vec2);
345 SetWeightFunction("Box", 0.7);
347 TF1 *func2 = new TF1("test2", "[0]*TMath::Exp(-[1]*x)", 0, 6);
348 func2->SetParameter(0, (*GetParameters())(0));
349 func2->SetParameter(1, (*GetParameters())(1));
350 func2->SetLineColor(kRed);