]> git.uio.no Git - u/mrichter/AliRoot.git/blame - STAT/AliTMinuitToolkit.cxx
Added AliTMinuitToolkit (A.Kalweit)
[u/mrichter/AliRoot.git] / STAT / AliTMinuitToolkit.cxx
CommitLineData
4b23a517 1
2#include "AliTMinuitToolkit.h"
3#include <TNamed.h>
4#include <TVirtualFitter.h>
5#include <TH1F.h>
6#include <TH2F.h>
7#include <TF1.h>
8#include <TFormula.h>
9#include <TVectorD.h>
10#include <TMatrixD.h>
11#include <TMath.h>
12#include <TString.h>
13#include <TROOT.h>
14#include <TCanvas.h>
15#include <TRandom.h>
16
17
18//--------------------------------------------------------------------------
19//
20// The AliTMinuitToolkit serves as an easy to use interface for the TMinuit
21// package:
22//
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.
27//
28//
29// 1. Setting the formula:
30//
31// The formula is simply set via "void SetFitFunction(TFormula * formula)".
32//
33//
34// 2. Adding the data points
35//
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()".
42//
43//
44// 3. Accessing the fit results
45//
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.
50//
51//
52// 4. Non-linear robust fitting:
53//
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.
58//
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].
62//
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)"
68//
69//-------------------------------------------------------------------------
70
71
72ClassImp(AliTMinuitToolkit)
73
74AliTMinuitToolkit::AliTMinuitToolkit() :
75 TNamed(),
76 fFormula(0),
77 fWeightFunction(0),
78 fFitAlgorithm(0),
79 fPoints(0),
80 fParam(0),
81 fParamLimits(0),
82 fCovar(0),
83 fChi2(0)
84{
85 //
86 // standard constructor
87 //
88 fMaxCalls = 500;
89 fPrecision = 1;
90}
91
92
93
94AliTMinuitToolkit::~AliTMinuitToolkit(){
95 //
96 // destructor
97 //
98 delete fPoints;
99 delete fWeightFunction;
100 delete fParamLimits;
101 delete fFormula;
102 delete fParam;
103 delete fCovar;
104 delete fChi2;
105}
106
107void AliTMinuitToolkit::FitHistogram(TH1F * his) {
108 //
109 // Fit a one dimensional histogram
110 //
111 fPoints = new TMatrixD(his->GetNbinsX(), 2);
112
113 for(Int_t ibin=0; ibin < his->GetNbinsX(); ibin++) {
114 Double_t x = his->GetXaxis()->GetBinCenter(ibin+1);
115 Double_t y = his->GetBinContent(ibin+1);
116
117 (*fPoints)(ibin, 0) = x;
118 (*fPoints)(ibin, 1) = y;
119 }
120
121 Fit();
122}
123
124
125void AliTMinuitToolkit::FitHistogram(TH2F * his) {
126 //
127 // Fit a two dimensional histogram
128 //
129 fPoints = new TMatrixD((Long64_t)his->GetEntries(), 2);
130 Long64_t entry = 0;
131
132 for(Int_t ibin=0; ibin < his->GetNbinsX(); ibin++) {
133 Double_t x = his->GetXaxis()->GetBinCenter(ibin);
134 for(Int_t jbin=0; jbin < his->GetNbinsY(); jbin++) {
135 Long64_t n = his->GetBin(ibin, jbin);
136 Double_t y = his->GetYaxis()->GetBinCenter(jbin);
137 for(Int_t ientries=0; ientries < his->GetBinContent(n); ientries++) {
138 (*fPoints)(entry,0) = x;
139 (*fPoints)(entry,1) = y;
140 entry++;
141 }
142
143 }
144 }
145
146 Fit();
147}
148
149
150void AliTMinuitToolkit::SetWeightFunction(Char_t * name, Float_t param1, Float_t param2) {
151 //
152 // Set the weight function which must be defined on the interval [0,1].
153 //
154 TString FuncType(name);
155 FuncType.ToUpper();
156
157 if (FuncType == "EXPONENTIAL") fWeightFunction = new TFormula("exp", Form("TMath::Exp(-TMath::Log(%f)*x)", param1));
158 if (FuncType == "BOX") fWeightFunction = new TFormula("box", Form("TMath::Erfc((x-%f)/0.0001)", param1));
159 if (FuncType == "ERRORFUNCTION") fWeightFunction = new TFormula("err", Form("TMath::Erfc((x-%f)/%f)", param1, param2));// !!!!!!!!!!!!!!!!!
160
161}
162
163
164void AliTMinuitToolkit::FitterFCN(int &npar, double *dummy, double &fchisq, double *gin, int iflag){
165 //
166 // internal function which gives the specified function to the TMinuit function
167 //
168
169 // suppress warnings for unused variables:
170 dummy = dummy;
171 iflag = iflag;
172 npar = npar;
173 //
174 AliTMinuitToolkit * fitter = (AliTMinuitToolkit*)TVirtualFitter::GetFitter()->GetObjectFit();
175 fchisq = 0;
176 Int_t nvar = fitter->GetPoints()->GetNcols()-1;
177 Int_t npoints = fitter->GetPoints()->GetNrows();
178
179 // sort points for weighting
180 Double_t sortList[npoints];
181 Int_t indexList[npoints];
182
183 TVectorD *fWeight = new TVectorD(npoints);
184
185 for (Int_t ipoint=0; ipoint<npoints; ipoint++){
186 Double_t x[100];
187 for (Int_t ivar=0; ivar<nvar; ivar++){
188 x[ivar] = (*fitter->GetPoints())(ipoint, ivar);
189 }
190 Float_t funx = fitter->GetFormula()->EvalPar(x,gin);
191 sortList[ipoint] = TMath::Abs((*fitter->GetPoints())(ipoint, nvar) - funx);
192 }
193
194 TMath::Sort(npoints, sortList, indexList, false);
195
196 Double_t t;
197 for (Int_t ipoint=0; ipoint<npoints; ipoint++){
198 t = indexList[ipoint]/(Double_t)npoints;
199 (*fWeight)(ipoint) = fitter->GetWeightFunction()->Eval(t);
200 }
201 //
202 // calculate chisquare
203 for (Int_t ipoint=0; ipoint<npoints; ipoint++){
204 Double_t x[100];
205 for (Int_t ivar=0; ivar<nvar; ivar++){
206 x[ivar] = (*fitter->GetPoints())(ipoint, ivar);
207 }
208 Float_t funx = fitter->GetFormula()->EvalPar(x,gin);
209
210 Double_t delta = (*fitter->GetPoints())(ipoint, nvar) - funx;
211 fchisq+= delta*delta*(*fWeight)(ipoint);
212
213 }
214 delete fWeight;
215}
216
217
218void AliTMinuitToolkit::Fit() {
219 //
220 // internal function that calls the fitter
221 //
222 Int_t nparam = fParam->GetNrows();
223
224 // set all paramter limits to infinity as default
225 if (fParamLimits == 0) {
226 fParamLimits = new TMatrixD(nparam ,2);
227 for (Int_t iparam=0; iparam<nparam; iparam++){
228 (*fParamLimits)(iparam, 0) = 0;
229 (*fParamLimits)(iparam, 1) = 0;
230 }
231 }
232
233 // set all weights to 1 as default
234 if (fWeightFunction == 0) {
235 fWeightFunction = new TFormula("constant", "1");
236 }
237
238 // migrad fit algorithm as default
239 if (fFitAlgorithm == 0) {
240 fFitAlgorithm = "migrad";
241 }
242
243 // set up the fitter
244 TVirtualFitter *minuit = TVirtualFitter::Fitter(0, nparam);
245 minuit->SetObjectFit(this);
246 minuit->SetFCN((void*)(AliTMinuitToolkit::FitterFCN));
247
248 // initialize paramters (step size???)
249 for (Int_t iparam=0; iparam<nparam; iparam++){
250 minuit->SetParameter(iparam, Form("p[%d]",iparam), (*fParam)(iparam), (*fParam)(iparam)/10, (*fParamLimits)(iparam, 0), (*fParamLimits)(iparam, 1));
251 }
252
253 Double_t argList[2];
254 argList[0] = fMaxCalls; //maximal number of calls
255 argList[1] = fPrecision; //tolerance normalized to 0.001
256 if (fMaxCalls == 500 && fPrecision == 1) minuit->ExecuteCommand(fFitAlgorithm, 0, 0);
257 if (fMaxCalls != 500 || fPrecision != 1) minuit->ExecuteCommand(fFitAlgorithm, argList, 2);
258 // two additional arguments can be specified ExecuteCommand("migrad", argList, 2) - use 0,0 for default
259
260 // fill parameter vector
261 for (Int_t ivar=0; ivar<nparam; ivar++){
262 (*fParam)(ivar) = minuit->GetParameter(ivar);
263 }
264
265 // fill covariance matrix
266 fCovar = new TMatrixD(nparam, nparam);
267 //TVirtualFitter *fitCov = TVirtualFitter::GetFitter();
268 for(Int_t i=0; i < nparam; i++) {
269 for(Int_t j=0; j < nparam; j++) {
270 (*fCovar)(i,j) = minuit->GetCovarianceMatrixElement(i,j);
271 }
272 }
273
274}
275
276
277
278void AliTMinuitToolkit::Test() {
279 //
280 // This test function shows the basic working principles of this class
281 // and illustrates how a robust fit can improve the results
282 //
283 TFormula *FormExp = new TFormula("formExp", "[0]*TMath::Exp(-[1]*x)");
284 SetFitFunction(FormExp);
285 SetFitAlgorithm("simplex");
286 // Set initial values
287 TVectorD *vec1 = new TVectorD(2);
288 (*vec1)(0) = 1800;
289 (*vec1)(1) = 1;
290 SetInitialParam(vec1);
291 //provide some example histogram
292 TH1F * hist = new TH1F("bla", "with (red) and without (black) robust option", 20,0,4);
293 TRandom * rand = new TRandom();
294 for (Int_t i = 0; i < 10000; i++) {
295 hist->Fill(rand->Exp(1));
296 if (i < 1000) hist->Fill(3); //"outliers"
297 if (i < 1070) hist->Fill(3.5);
298 if (i < 670) hist->Fill(2);
299 if (i < 770) hist->Fill(1.5);//"outliers"
300 if (i < 740) hist->Fill(1);
301 }
302 TCanvas * canv = new TCanvas();
303 canv->cd(1);
304 hist->Draw();
305 // fit it with the exponential decay
306 FitHistogram(hist);
307 // draw fit function
308 TF1 *func = new TF1("test", "[0]*TMath::Exp(-[1]*x)", 0, 6);
309 func->SetParameter(0, (*GetParameters())(0));
310 func->SetParameter(1, (*GetParameters())(1));
311 func->Draw("same");
312 // robust fit
313 TVectorD *vec2 = new TVectorD(2);
314 (*vec2)(0) = 1800;
315 (*vec2)(1) = 1;
316 SetInitialParam(vec2);
317 SetWeightFunction("Box", 0.7);
318 FitHistogram(hist);
319 TF1 *func2 = new TF1("test2", "[0]*TMath::Exp(-[1]*x)", 0, 6);
320 func2->SetParameter(0, (*GetParameters())(0));
321 func2->SetParameter(1, (*GetParameters())(1));
322 func2->SetLineColor(kRed);
323 func2->Draw("same");
324
325}
326