]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliMathBase.cxx
Removing the circular dependency between the ESD and STEER libraries by moving the...
[u/mrichter/AliRoot.git] / STEER / AliMathBase.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16
17 ///////////////////////////////////////////////////////////////////////////
18 // Class AliMathBase
19 // 
20 // Subset of  matheamtical functions  not included in the TMath
21 //
22
23 ///////////////////////////////////////////////////////////////////////////
24 #include "TMath.h"
25 #include "AliMathBase.h"
26 #include "Riostream.h"
27 #include "TH1F.h"
28 #include "TH3.h"
29 #include "TF1.h"
30 #include "TLinearFitter.h"
31
32 #include "AliExternalTrackParam.h"
33
34 //
35 // includes neccessary for test functions
36 //
37
38 #include "TSystem.h"
39 #include "TRandom.h"
40 #include "TStopwatch.h"
41 #include "TTreeStream.h"
42  
43 ClassImp(AliMathBase) // Class implementation to enable ROOT I/O
44  
45 AliMathBase::AliMathBase() : TObject()
46 {
47   //
48   // Default constructor
49   //
50 }
51 ///////////////////////////////////////////////////////////////////////////
52 AliMathBase::~AliMathBase()
53 {
54   //
55   // Destructor
56   //
57 }
58
59
60 //_____________________________________________________________________________
61 void AliMathBase::EvaluateUni(Int_t nvectors, Double_t *data, Double_t &mean
62                            , Double_t &sigma, Int_t hh)
63 {
64   //
65   // Robust estimator in 1D case MI version - (faster than ROOT version)
66   //
67   // For the univariate case
68   // estimates of location and scatter are returned in mean and sigma parameters
69   // the algorithm works on the same principle as in multivariate case -
70   // it finds a subset of size hh with smallest sigma, and then returns mean and
71   // sigma of this subset
72   //
73
74   if (hh==0)
75     hh=(nvectors+2)/2;
76   Double_t faclts[]={2.6477,2.5092,2.3826,2.2662,2.1587,2.0589,1.9660,1.879,1.7973,1.7203,1.6473};
77   Int_t *index=new Int_t[nvectors];
78   TMath::Sort(nvectors, data, index, kFALSE);
79   
80   Int_t    nquant = TMath::Min(Int_t(Double_t(((hh*1./nvectors)-0.5)*40))+1, 11);
81   Double_t factor = faclts[TMath::Max(0,nquant-1)];
82   
83   Double_t sumx  =0;
84   Double_t sumx2 =0;
85   Int_t    bestindex = -1;
86   Double_t bestmean  = 0; 
87   Double_t bestsigma = (data[index[nvectors-1]]-data[index[0]]+1.);   // maximal possible sigma
88   bestsigma *=bestsigma;
89
90   for (Int_t i=0; i<hh; i++){
91     sumx  += data[index[i]];
92     sumx2 += data[index[i]]*data[index[i]];
93   }
94   
95   Double_t norm = 1./Double_t(hh);
96   Double_t norm2 = 1./Double_t(hh-1);
97   for (Int_t i=hh; i<nvectors; i++){
98     Double_t cmean  = sumx*norm;
99     Double_t csigma = (sumx2 - hh*cmean*cmean)*norm2;
100     if (csigma<bestsigma){
101       bestmean  = cmean;
102       bestsigma = csigma;
103       bestindex = i-hh;
104     }
105     
106     sumx  += data[index[i]]-data[index[i-hh]];
107     sumx2 += data[index[i]]*data[index[i]]-data[index[i-hh]]*data[index[i-hh]];
108   }
109   
110   Double_t bstd=factor*TMath::Sqrt(TMath::Abs(bestsigma));
111   mean  = bestmean;
112   sigma = bstd;
113   delete [] index;
114
115 }
116
117
118
119 void AliMathBase::EvaluateUniExternal(Int_t nvectors, Double_t *data, Double_t &mean, Double_t &sigma, Int_t hh,  Float_t externalfactor)
120 {
121   // Modified version of ROOT robust EvaluateUni
122   // robust estimator in 1D case MI version
123   // added external factor to include precision of external measurement
124   // 
125
126   if (hh==0)
127     hh=(nvectors+2)/2;
128   Double_t faclts[]={2.6477,2.5092,2.3826,2.2662,2.1587,2.0589,1.9660,1.879,1.7973,1.7203,1.6473};
129   Int_t *index=new Int_t[nvectors];
130   TMath::Sort(nvectors, data, index, kFALSE);
131   //
132   Int_t    nquant = TMath::Min(Int_t(Double_t(((hh*1./nvectors)-0.5)*40))+1, 11);
133   Double_t factor = faclts[0];
134   if (nquant>0){
135     // fix proper normalization - Anja
136     factor = faclts[nquant-1];
137   }
138
139   //
140   //
141   Double_t sumx  =0;
142   Double_t sumx2 =0;
143   Int_t    bestindex = -1;
144   Double_t bestmean  = 0; 
145   Double_t bestsigma = -1;
146   for (Int_t i=0; i<hh; i++){
147     sumx  += data[index[i]];
148     sumx2 += data[index[i]]*data[index[i]];
149   }
150   //   
151   Double_t kfactor = 2.*externalfactor - externalfactor*externalfactor;
152   Double_t norm = 1./Double_t(hh);
153   for (Int_t i=hh; i<nvectors; i++){
154     Double_t cmean  = sumx*norm;
155     Double_t csigma = (sumx2*norm - cmean*cmean*kfactor);
156     if (csigma<bestsigma ||  bestsigma<0){
157       bestmean  = cmean;
158       bestsigma = csigma;
159       bestindex = i-hh;
160     }
161     //
162     //
163     sumx  += data[index[i]]-data[index[i-hh]];
164     sumx2 += data[index[i]]*data[index[i]]-data[index[i-hh]]*data[index[i-hh]];
165   }
166   
167   Double_t bstd=factor*TMath::Sqrt(TMath::Abs(bestsigma));
168   mean  = bestmean;
169   sigma = bstd;
170   delete [] index;
171 }
172
173
174 //_____________________________________________________________________________
175 Int_t AliMathBase::Freq(Int_t n, const Int_t *inlist
176                         , Int_t *outlist, Bool_t down)
177 {    
178   //
179   //  Sort eleements according occurancy 
180   //  The size of output array has is 2*n 
181   //
182
183   Int_t * sindexS = new Int_t[n];     // temp array for sorting
184   Int_t * sindexF = new Int_t[2*n];   
185   for (Int_t i=0;i<n;i++) sindexF[i]=0;
186   //
187   TMath::Sort(n,inlist, sindexS, down);  
188   Int_t last      = inlist[sindexS[0]];
189   Int_t val       = last;
190   sindexF[0]      = 1;
191   sindexF[0+n]    = last;
192   Int_t countPos  = 0;
193   //
194   //  find frequency
195   for(Int_t i=1;i<n; i++){
196     val = inlist[sindexS[i]];
197     if (last == val)   sindexF[countPos]++;
198     else{      
199       countPos++;
200       sindexF[countPos+n] = val;
201       sindexF[countPos]++;
202       last =val;
203     }
204   }
205   if (last==val) countPos++;
206   // sort according frequency
207   TMath::Sort(countPos, sindexF, sindexS, kTRUE);
208   for (Int_t i=0;i<countPos;i++){
209     outlist[2*i  ] = sindexF[sindexS[i]+n];
210     outlist[2*i+1] = sindexF[sindexS[i]];
211   }
212   delete [] sindexS;
213   delete [] sindexF;
214   
215   return countPos;
216
217 }
218
219 //___AliMathBase__________________________________________________________________________
220 void AliMathBase::TruncatedMean(TH1F * his, TVectorD *param, Float_t down, Float_t up, Bool_t verbose){
221   //
222   //
223   //
224   Int_t nbins    = his->GetNbinsX();
225   Float_t nentries = his->GetEntries();
226   Float_t sum      =0;
227   Float_t mean   = 0;
228   Float_t sigma2 = 0;
229   Float_t ncumul=0;  
230   for (Int_t ibin=1;ibin<nbins; ibin++){
231     ncumul+= his->GetBinContent(ibin);
232     Float_t fraction = Float_t(ncumul)/Float_t(nentries);
233     if (fraction>down && fraction<up){
234       sum+=his->GetBinContent(ibin);
235       mean+=his->GetBinCenter(ibin)*his->GetBinContent(ibin);
236       sigma2+=his->GetBinCenter(ibin)*his->GetBinCenter(ibin)*his->GetBinContent(ibin);      
237     }
238   }
239   mean/=sum;
240   sigma2= TMath::Sqrt(TMath::Abs(sigma2/sum-mean*mean));
241   if (param){
242     (*param)[0] = his->GetMaximum();
243     (*param)[1] = mean;
244     (*param)[2] = sigma2;
245     
246   }
247   if (verbose)  printf("Mean\t%f\t Sigma2\t%f\n", mean,sigma2);
248 }
249
250 void AliMathBase::LTM(TH1F * his, TVectorD *param , Float_t fraction,  Bool_t verbose){
251   //
252   // LTM
253   //
254   Int_t nbins    = his->GetNbinsX();
255   Int_t nentries = (Int_t)his->GetEntries();
256   Double_t *data  = new Double_t[nentries];
257   Int_t npoints=0;
258   for (Int_t ibin=1;ibin<nbins; ibin++){
259     Float_t entriesI = his->GetBinContent(ibin);
260     Float_t xcenter= his->GetBinCenter(ibin);
261     for (Int_t ic=0; ic<entriesI; ic++){
262       if (npoints<nentries){
263         data[npoints]= xcenter;
264         npoints++;
265       }
266     }
267   }
268   Double_t mean, sigma;
269   Int_t npoints2=TMath::Min(Int_t(fraction*Float_t(npoints)),npoints-1);
270   npoints2=TMath::Max(Int_t(0.5*Float_t(npoints)),npoints2);
271   AliMathBase::EvaluateUni(npoints, data, mean,sigma,npoints2);
272   delete [] data;
273   if (verbose)  printf("Mean\t%f\t Sigma2\t%f\n", mean,sigma);if (param){
274     (*param)[0] = his->GetMaximum();
275     (*param)[1] = mean;
276     (*param)[2] = sigma;    
277   }
278 }
279
280 Double_t  AliMathBase::FitGaus(TH1F* his, TVectorD *param, TMatrixD *matrix, Float_t xmin, Float_t xmax, Bool_t verbose){
281   //
282   //  Fit histogram with gaussian function
283   //  
284   //  Prameters:
285   //       return value- chi2 - if negative ( not enough points)
286   //       his        -  input histogram
287   //       param      -  vector with parameters 
288   //       xmin, xmax -  range to fit - if xmin=xmax=0 - the full histogram range used
289   //  Fitting:
290   //  1. Step - make logarithm
291   //  2. Linear  fit (parabola) - more robust - always converge
292   //  3. In case of small statistic bins are averaged
293   //  
294   static TLinearFitter fitter(3,"pol2");
295   TVectorD  par(3);
296   TVectorD  sigma(3);
297   TMatrixD mat(3,3);
298   if (his->GetMaximum()<4) return -1;  
299   if (his->GetEntries()<12) return -1;  
300   if (his->GetRMS()<mat.GetTol()) return -1;
301   Float_t maxEstimate   = his->GetEntries()*his->GetBinWidth(1)/TMath::Sqrt((TMath::TwoPi()*his->GetRMS()));
302   Int_t dsmooth = TMath::Nint(6./TMath::Sqrt(maxEstimate));
303
304   if (maxEstimate<1) return -1;
305   Int_t nbins    = his->GetNbinsX();
306   Int_t npoints=0;
307   //
308
309
310   if (xmin>=xmax){
311     xmin = his->GetXaxis()->GetXmin();
312     xmax = his->GetXaxis()->GetXmax();
313   }
314   for (Int_t iter=0; iter<2; iter++){
315     fitter.ClearPoints();
316     npoints=0;
317     for (Int_t ibin=1;ibin<nbins+1; ibin++){
318       Int_t countB=1;
319       Float_t entriesI =  his->GetBinContent(ibin);
320       for (Int_t delta = -dsmooth; delta<=dsmooth; delta++){
321         if (ibin+delta>1 &&ibin+delta<nbins-1){
322           entriesI +=  his->GetBinContent(ibin+delta);
323           countB++;
324         }
325       }
326       entriesI/=countB;
327       Double_t xcenter= his->GetBinCenter(ibin);
328       if (xcenter<xmin || xcenter>xmax) continue;
329       Double_t error=1./TMath::Sqrt(countB);
330       Float_t   cont=2;
331       if (iter>0){
332         if (par[0]+par[1]*xcenter+par[2]*xcenter*xcenter>20) return 0;
333         cont = TMath::Exp(par[0]+par[1]*xcenter+par[2]*xcenter*xcenter);
334         if (cont>1.) error = 1./TMath::Sqrt(cont*Float_t(countB));
335       }
336       if (entriesI>1&&cont>1){
337         fitter.AddPoint(&xcenter,TMath::Log(Float_t(entriesI)),error);
338         npoints++;
339       }
340     }  
341     if (npoints>3){
342       fitter.Eval();
343       fitter.GetParameters(par);
344     }else{
345       break;
346     }
347   }
348   if (npoints<=3){
349     return -1;
350   }
351   fitter.GetParameters(par);
352   fitter.GetCovarianceMatrix(mat);
353   if (TMath::Abs(par[1])<mat.GetTol()) return -1;
354   if (TMath::Abs(par[2])<mat.GetTol()) return -1;
355   Double_t chi2 = fitter.GetChisquare()/Float_t(npoints);
356   //fitter.GetParameters();
357   if (!param)  param  = new TVectorD(3);
358   if (!matrix) matrix = new TMatrixD(3,3);
359   (*param)[1] = par[1]/(-2.*par[2]);
360   (*param)[2] = 1./TMath::Sqrt(TMath::Abs(-2.*par[2]));
361   (*param)[0] = TMath::Exp(par[0]+ par[1]* (*param)[1] +  par[2]*(*param)[1]*(*param)[1]);
362   if (verbose){
363     par.Print();
364     mat.Print();
365     param->Print();
366     printf("Chi2=%f\n",chi2);
367     TF1 * f1= new TF1("f1","[0]*exp(-(x-[1])^2/(2*[2]*[2]))",his->GetXaxis()->GetXmin(),his->GetXaxis()->GetXmax());
368     f1->SetParameter(0, (*param)[0]);
369     f1->SetParameter(1, (*param)[1]);
370     f1->SetParameter(2, (*param)[2]);    
371     f1->Draw("same");
372   }
373   return chi2;
374 }
375
376 Double_t  AliMathBase::FitGaus(Float_t *arr, Int_t nBins, Float_t xMin, Float_t xMax, TVectorD *param, TMatrixD *matrix, Bool_t verbose){
377   //
378   //  Fit histogram with gaussian function
379   //  
380   //  Prameters:
381   //     nbins: size of the array and number of histogram bins
382   //     xMin, xMax: histogram range
383   //     param: paramters of the fit (0-Constant, 1-Mean, 2-Sigma, 3-Sum)
384   //     matrix: covariance matrix -- not implemented yet, pass dummy matrix!!!
385   //
386   //  Return values:
387   //    >0: the chi2 returned by TLinearFitter
388   //    -3: only three points have been used for the calculation - no fitter was used
389   //    -2: only two points have been used for the calculation - center of gravity was uesed for calculation
390   //    -1: only one point has been used for the calculation - center of gravity was uesed for calculation
391   //    -4: invalid result!!
392   //
393   //  Fitting:
394   //  1. Step - make logarithm
395   //  2. Linear  fit (parabola) - more robust - always converge
396   //  
397   static TLinearFitter fitter(3,"pol2");
398   static TMatrixD mat(3,3);
399   static Double_t kTol = mat.GetTol();
400   fitter.StoreData(kFALSE);
401   fitter.ClearPoints();
402   TVectorD  par(3);
403   TVectorD  sigma(3);
404   TMatrixD A(3,3);
405   TMatrixD b(3,1);
406   Float_t rms = TMath::RMS(nBins,arr);
407   Float_t max = TMath::MaxElement(nBins,arr);
408   Float_t binWidth = (xMax-xMin)/(Float_t)nBins;
409
410   Float_t meanCOG = 0;
411   Float_t rms2COG = 0;
412   Float_t sumCOG  = 0;
413
414   Float_t entries = 0;
415   Int_t nfilled=0;
416
417   for (Int_t i=0; i<nBins; i++){
418       entries+=arr[i];
419       if (arr[i]>0) nfilled++;
420   }
421   (*param)[0] = 0;
422   (*param)[1] = 0;
423   (*param)[2] = 0;
424   (*param)[3] = 0;
425
426   if (max<4) return -4;
427   if (entries<12) return -4;
428   if (rms<kTol) return -4;
429
430   (*param)[3] = entries;
431
432   Int_t npoints=0;
433   for (Int_t ibin=0;ibin<nBins; ibin++){
434       Float_t entriesI = arr[ibin];
435     if (entriesI>1){
436       Double_t xcenter = xMin+(ibin+0.5)*binWidth;
437       Float_t error    = 1./TMath::Sqrt(entriesI);
438       Float_t val = TMath::Log(Float_t(entriesI));
439       fitter.AddPoint(&xcenter,val,error);
440       if (npoints<3){
441           A(npoints,0)=1;
442           A(npoints,1)=xcenter;
443           A(npoints,2)=xcenter*xcenter;
444           b(npoints,0)=val;
445           meanCOG+=xcenter*entriesI;
446           rms2COG +=xcenter*entriesI*xcenter;
447           sumCOG +=entriesI;
448       }
449       npoints++;
450     }
451   }
452   
453   Double_t chi2 = 0;
454   if (npoints>=3){
455       if ( npoints == 3 ){
456           //analytic calculation of the parameters for three points
457           A.Invert();
458           TMatrixD res(1,3);
459           res.Mult(A,b);
460           par[0]=res(0,0);
461           par[1]=res(0,1);
462           par[2]=res(0,2);
463           chi2 = -3.;
464       } else {
465           // use fitter for more than three points
466           fitter.Eval();
467           fitter.GetParameters(par);
468           fitter.GetCovarianceMatrix(mat);
469           chi2 = fitter.GetChisquare()/Float_t(npoints);
470       }
471       if (TMath::Abs(par[1])<kTol) return -4;
472       if (TMath::Abs(par[2])<kTol) return -4;
473
474       if (!param)  param  = new TVectorD(4);
475       if ( param->GetNrows()<4 ) param->ResizeTo(4);
476       if (!matrix) matrix = new TMatrixD(3,3);  // !!!!might be a memory leek. use dummy matrix pointer to call this function!
477
478       (*param)[1] = par[1]/(-2.*par[2]);
479       (*param)[2] = 1./TMath::Sqrt(TMath::Abs(-2.*par[2]));
480       Double_t lnparam0 = par[0]+ par[1]* (*param)[1] +  par[2]*(*param)[1]*(*param)[1];
481       if ( lnparam0>307 ) return -4;
482       (*param)[0] = TMath::Exp(lnparam0);
483       if (verbose){
484           par.Print();
485           mat.Print();
486           param->Print();
487           printf("Chi2=%f\n",chi2);
488           TF1 * f1= new TF1("f1","[0]*exp(-(x-[1])^2/(2*[2]*[2]))",xMin,xMax);
489           f1->SetParameter(0, (*param)[0]);
490           f1->SetParameter(1, (*param)[1]);
491           f1->SetParameter(2, (*param)[2]);
492           f1->Draw("same");
493       }
494       return chi2;
495   }
496
497   if (npoints == 2){
498       //use center of gravity for 2 points
499       meanCOG/=sumCOG;
500       rms2COG /=sumCOG;
501       (*param)[0] = max;
502       (*param)[1] = meanCOG;
503       (*param)[2] = TMath::Sqrt(TMath::Abs(meanCOG*meanCOG-rms2COG));
504       chi2=-2.;
505   }
506   if ( npoints == 1 ){
507       meanCOG/=sumCOG;
508       (*param)[0] = max;
509       (*param)[1] = meanCOG;
510       (*param)[2] = binWidth/TMath::Sqrt(12);
511       chi2=-1.;
512   }
513   return chi2;
514
515 }
516
517
518 Float_t AliMathBase::GetCOG(Short_t *arr, Int_t nBins, Float_t xMin, Float_t xMax, Float_t *rms, Float_t *sum)
519 {
520     //
521     //  calculate center of gravity rms and sum for array 'arr' with nBins an a x range xMin to xMax
522     //  return COG; in case of failure return xMin
523     //
524     Float_t meanCOG = 0;
525     Float_t rms2COG = 0;
526     Float_t sumCOG  = 0;
527     Int_t npoints   = 0;
528
529     Float_t binWidth = (xMax-xMin)/(Float_t)nBins;
530
531     for (Int_t ibin=0; ibin<nBins; ibin++){
532         Float_t entriesI = (Float_t)arr[ibin];
533         Double_t xcenter = xMin+(ibin+0.5)*binWidth;
534         if ( entriesI>0 ){
535             meanCOG += xcenter*entriesI;
536             rms2COG += xcenter*entriesI*xcenter;
537             sumCOG  += entriesI;
538             npoints++;
539         }
540     }
541     if ( sumCOG == 0 ) return xMin;
542     meanCOG/=sumCOG;
543
544     if ( rms ){
545         rms2COG /=sumCOG;
546         (*rms) = TMath::Sqrt(TMath::Abs(meanCOG*meanCOG-rms2COG));
547         if ( npoints == 1 ) (*rms) = binWidth/TMath::Sqrt(12);
548     }
549
550     if ( sum )
551         (*sum) = sumCOG;
552
553     return meanCOG;
554 }
555
556
557
558 ///////////////////////////////////////////////////////////////
559 //////////////         TEST functions /////////////////////////
560 ///////////////////////////////////////////////////////////////
561
562
563
564
565
566 void AliMathBase::TestGausFit(Int_t nhistos){
567   //
568   // Test performance of the parabolic - gaussian fit - compare it with 
569   // ROOT gauss fit
570   //  nhistos - number of histograms to be used for test
571   //
572   TTreeSRedirector *pcstream = new TTreeSRedirector("fitdebug.root");
573   
574   Float_t  *xTrue = new Float_t[nhistos];
575   Float_t  *sTrue = new Float_t[nhistos];
576   TVectorD **par1  = new TVectorD*[nhistos];
577   TVectorD **par2  = new TVectorD*[nhistos];
578   TMatrixD dummy(3,3);
579   
580   
581   TH1F **h1f = new TH1F*[nhistos];
582   TF1  *myg = new TF1("myg","gaus");
583   TF1  *fit = new TF1("fit","gaus");
584   gRandom->SetSeed(0);
585   
586   //init
587   for (Int_t i=0;i<nhistos; i++){
588     par1[i] = new TVectorD(3);
589     par2[i] = new TVectorD(3);
590     h1f[i]  = new TH1F(Form("h1f%d",i),Form("h1f%d",i),20,-10,10);
591     xTrue[i]= gRandom->Rndm();
592     gSystem->Sleep(2);
593     sTrue[i]= .75+gRandom->Rndm()*.5;
594     myg->SetParameters(1,xTrue[i],sTrue[i]);
595     h1f[i]->FillRandom("myg");
596   }
597   
598   TStopwatch s;
599   s.Start();
600   //standard gaus fit
601   for (Int_t i=0; i<nhistos; i++){
602     h1f[i]->Fit(fit,"0q");
603     (*par1[i])(0) = fit->GetParameter(0);
604     (*par1[i])(1) = fit->GetParameter(1);
605     (*par1[i])(2) = fit->GetParameter(2);
606   }
607   s.Stop();
608   printf("Gaussian fit\t");
609   s.Print();
610   
611   s.Start();
612   //AliMathBase gaus fit
613   for (Int_t i=0; i<nhistos; i++){
614     AliMathBase::FitGaus(h1f[i]->GetArray()+1,h1f[i]->GetNbinsX(),h1f[i]->GetXaxis()->GetXmin(),h1f[i]->GetXaxis()->GetXmax(),par2[i],&dummy);
615   }
616   
617   s.Stop();
618   printf("Parabolic fit\t");
619   s.Print();
620   //write stream
621   for (Int_t i=0;i<nhistos; i++){
622     Float_t xt  = xTrue[i];
623     Float_t st  = sTrue[i];
624     (*pcstream)<<"data"
625                <<"xTrue="<<xt
626                <<"sTrue="<<st
627                <<"pg.="<<(par1[i])
628                <<"pa.="<<(par2[i])
629                <<"\n";
630   }    
631   //delete pointers
632   for (Int_t i=0;i<nhistos; i++){
633     delete par1[i];
634     delete par2[i];
635     delete h1f[i];
636   }
637   delete pcstream;
638   delete []h1f;
639   delete []xTrue;
640   delete []sTrue;
641   //
642   delete []par1;
643   delete []par2;
644
645 }
646
647
648
649 TGraph2D * AliMathBase::MakeStat2D(TH3 * his, Int_t delta0, Int_t delta1, Int_t type){
650   //
651   //
652   //
653   // delta - number of bins to integrate
654   // type - 0 - mean value
655
656   TAxis * xaxis  = his->GetXaxis();
657   TAxis * yaxis  = his->GetYaxis();
658   //  TAxis * zaxis  = his->GetZaxis();
659   Int_t   nbinx  = xaxis->GetNbins();
660   Int_t   nbiny  = yaxis->GetNbins();
661   char name[1000];
662   Int_t icount=0;
663   TGraph2D  *graph = new TGraph2D(nbinx*nbiny);
664   TF1 f1("f1","gaus");
665   for (Int_t ix=0; ix<nbinx;ix++)
666     for (Int_t iy=0; iy<nbiny;iy++){
667       Float_t xcenter = xaxis->GetBinCenter(ix); 
668       Float_t ycenter = yaxis->GetBinCenter(iy); 
669       sprintf(name,"%s_%d_%d",his->GetName(), ix,iy);
670       TH1 *projection = his->ProjectionZ(name,ix-delta0,ix+delta0,iy-delta1,iy+delta1);
671       Float_t stat= 0;
672       if (type==0) stat = projection->GetMean();
673       if (type==1) stat = projection->GetRMS();
674       if (type==2 || type==3){
675         TVectorD vec(3);
676         AliMathBase::LTM((TH1F*)projection,&vec,0.7);
677         if (type==2) stat= vec[1];
678         if (type==3) stat= vec[0];      
679       }
680       if (type==4|| type==5){
681         projection->Fit(&f1);
682         if (type==4) stat= f1.GetParameter(1);
683         if (type==5) stat= f1.GetParameter(2);
684       }
685       //printf("%d\t%f\t%f\t%f\n", icount,xcenter, ycenter, stat);
686       graph->SetPoint(icount,xcenter, ycenter, stat);
687       icount++;
688     }
689   return graph;
690 }
691
692 TGraph * AliMathBase::MakeStat1D(TH3 * his, Int_t delta1, Int_t type){
693   //
694   //
695   //
696   // delta - number of bins to integrate
697   // type - 0 - mean value
698
699   TAxis * xaxis  = his->GetXaxis();
700   TAxis * yaxis  = his->GetYaxis();
701   //  TAxis * zaxis  = his->GetZaxis();
702   Int_t   nbinx  = xaxis->GetNbins();
703   Int_t   nbiny  = yaxis->GetNbins();
704   char name[1000];
705   Int_t icount=0;
706   TGraph  *graph = new TGraph(nbinx);
707   TF1 f1("f1","gaus");
708   for (Int_t ix=0; ix<nbinx;ix++){
709     Float_t xcenter = xaxis->GetBinCenter(ix); 
710     //    Float_t ycenter = yaxis->GetBinCenter(iy); 
711     sprintf(name,"%s_%d",his->GetName(), ix);
712     TH1 *projection = his->ProjectionZ(name,ix-delta1,ix+delta1,0,nbiny);
713     Float_t stat= 0;
714     if (type==0) stat = projection->GetMean();
715     if (type==1) stat = projection->GetRMS();
716     if (type==2 || type==3){
717       TVectorD vec(3);
718         AliMathBase::LTM((TH1F*)projection,&vec,0.7);
719         if (type==2) stat= vec[1];
720         if (type==3) stat= vec[0];      
721     }
722     if (type==4|| type==5){
723       projection->Fit(&f1);
724       if (type==4) stat= f1.GetParameter(1);
725       if (type==5) stat= f1.GetParameter(2);
726     }
727       //printf("%d\t%f\t%f\t%f\n", icount,xcenter, ycenter, stat);
728     graph->SetPoint(icount,xcenter, stat);
729     icount++;
730   }
731   return graph;
732 }
733
734 Double_t AliMathBase::TruncatedGaus(Double_t mean, Double_t sigma, Double_t cutat)
735 {
736   // return number generated according to a gaussian distribution N(mean,sigma) truncated at cutat
737   //
738   Double_t value;
739   do{
740     value=gRandom->Gaus(mean,sigma);
741   }while(TMath::Abs(value-mean)>cutat);
742   return value;
743 }
744
745 Double_t AliMathBase::TruncatedGaus(Double_t mean, Double_t sigma, Double_t leftCut, Double_t rightCut)
746 {
747   // return number generated according to a gaussian distribution N(mean,sigma)
748   // truncated at leftCut and rightCut
749   //
750   Double_t value;
751   do{
752     value=gRandom->Gaus(mean,sigma);
753   }while((value-mean)<-leftCut || (value-mean)>rightCut);
754   return value;
755 }
756
757 Double_t AliMathBase::BetheBlochAleph(Double_t bg,
758          Double_t kp1,
759          Double_t kp2,
760          Double_t kp3,
761          Double_t kp4,
762          Double_t kp5) {
763   //
764   // This is the empirical ALEPH parameterization of the Bethe-Bloch formula.
765   // It is normalized to 1 at the minimum.
766   //
767   // bg - beta*gamma
768   // 
769   // The default values for the kp* parameters are for ALICE TPC.
770   // The returned value is in MIP units
771   //
772
773   return AliExternalTrackParam::BetheBlochAleph(bg,kp1,kp2,kp3,kp4,kp5);
774 }