1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
17 ///////////////////////////////////////////////////////////////////////////
20 // Subset of matheamtical functions not included in the TMath
23 /////////////////////////////////////////////////////////////////////////
25 #include "Riostream.h"
32 #include "TObjString.h"
33 #include "TLinearFitter.h"
36 #include "TGraphErrors.h"
37 #include "TMultiGraph.h"
42 // includes neccessary for test functions
46 #include "TStopwatch.h"
47 #include "TTreeStream.h"
49 #include "TStatToolkit.h"
52 ClassImp(TStatToolkit) // Class implementation to enable ROOT I/O
54 TStatToolkit::TStatToolkit() : TObject()
57 // Default constructor
60 ///////////////////////////////////////////////////////////////////////////
61 TStatToolkit::~TStatToolkit()
69 //_____________________________________________________________________________
70 void TStatToolkit::EvaluateUni(Int_t nvectors, Double_t *data, Double_t &mean
71 , Double_t &sigma, Int_t hh)
74 // Robust estimator in 1D case MI version - (faster than ROOT version)
76 // For the univariate case
77 // estimates of location and scatter are returned in mean and sigma parameters
78 // the algorithm works on the same principle as in multivariate case -
79 // it finds a subset of size hh with smallest sigma, and then returns mean and
80 // sigma of this subset
85 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};
86 Int_t *index=new Int_t[nvectors];
87 TMath::Sort(nvectors, data, index, kFALSE);
89 Int_t nquant = TMath::Min(Int_t(Double_t(((hh*1./nvectors)-0.5)*40))+1, 11);
90 Double_t factor = faclts[TMath::Max(0,nquant-1)];
95 Double_t bestmean = 0;
96 Double_t bestsigma = (data[index[nvectors-1]]-data[index[0]]+1.); // maximal possible sigma
97 bestsigma *=bestsigma;
99 for (Int_t i=0; i<hh; i++){
100 sumx += data[index[i]];
101 sumx2 += data[index[i]]*data[index[i]];
104 Double_t norm = 1./Double_t(hh);
105 Double_t norm2 = (hh-1)>0 ? 1./Double_t(hh-1):1;
106 for (Int_t i=hh; i<nvectors; i++){
107 Double_t cmean = sumx*norm;
108 Double_t csigma = (sumx2 - hh*cmean*cmean)*norm2;
109 if (csigma<bestsigma){
115 sumx += data[index[i]]-data[index[i-hh]];
116 sumx2 += data[index[i]]*data[index[i]]-data[index[i-hh]]*data[index[i-hh]];
119 Double_t bstd=factor*TMath::Sqrt(TMath::Abs(bestsigma));
128 void TStatToolkit::EvaluateUniExternal(Int_t nvectors, Double_t *data, Double_t &mean, Double_t &sigma, Int_t hh, Float_t externalfactor)
130 // Modified version of ROOT robust EvaluateUni
131 // robust estimator in 1D case MI version
132 // added external factor to include precision of external measurement
137 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};
138 Int_t *index=new Int_t[nvectors];
139 TMath::Sort(nvectors, data, index, kFALSE);
141 Int_t nquant = TMath::Min(Int_t(Double_t(((hh*1./nvectors)-0.5)*40))+1, 11);
142 Double_t factor = faclts[0];
144 // fix proper normalization - Anja
145 factor = faclts[nquant-1];
152 Int_t bestindex = -1;
153 Double_t bestmean = 0;
154 Double_t bestsigma = -1;
155 for (Int_t i=0; i<hh; i++){
156 sumx += data[index[i]];
157 sumx2 += data[index[i]]*data[index[i]];
160 Double_t kfactor = 2.*externalfactor - externalfactor*externalfactor;
161 Double_t norm = 1./Double_t(hh);
162 for (Int_t i=hh; i<nvectors; i++){
163 Double_t cmean = sumx*norm;
164 Double_t csigma = (sumx2*norm - cmean*cmean*kfactor);
165 if (csigma<bestsigma || bestsigma<0){
172 sumx += data[index[i]]-data[index[i-hh]];
173 sumx2 += data[index[i]]*data[index[i]]-data[index[i-hh]]*data[index[i-hh]];
176 Double_t bstd=factor*TMath::Sqrt(TMath::Abs(bestsigma));
183 //_____________________________________________________________________________
184 Int_t TStatToolkit::Freq(Int_t n, const Int_t *inlist
185 , Int_t *outlist, Bool_t down)
188 // Sort eleements according occurancy
189 // The size of output array has is 2*n
192 Int_t * sindexS = new Int_t[n]; // temp array for sorting
193 Int_t * sindexF = new Int_t[2*n];
194 for (Int_t i=0;i<n;i++) sindexS[i]=0;
195 for (Int_t i=0;i<2*n;i++) sindexF[i]=0;
197 TMath::Sort(n,inlist, sindexS, down);
198 Int_t last = inlist[sindexS[0]];
205 for(Int_t i=1;i<n; i++){
206 val = inlist[sindexS[i]];
207 if (last == val) sindexF[countPos]++;
210 sindexF[countPos+n] = val;
215 if (last==val) countPos++;
216 // sort according frequency
217 TMath::Sort(countPos, sindexF, sindexS, kTRUE);
218 for (Int_t i=0;i<countPos;i++){
219 outlist[2*i ] = sindexF[sindexS[i]+n];
220 outlist[2*i+1] = sindexF[sindexS[i]];
229 //___TStatToolkit__________________________________________________________________________
230 void TStatToolkit::TruncatedMean(const TH1 * his, TVectorD *param, Float_t down, Float_t up, Bool_t verbose){
234 Int_t nbins = his->GetNbinsX();
235 Float_t nentries = his->GetEntries();
240 for (Int_t ibin=1;ibin<nbins; ibin++){
241 ncumul+= his->GetBinContent(ibin);
242 Float_t fraction = Float_t(ncumul)/Float_t(nentries);
243 if (fraction>down && fraction<up){
244 sum+=his->GetBinContent(ibin);
245 mean+=his->GetBinCenter(ibin)*his->GetBinContent(ibin);
246 sigma2+=his->GetBinCenter(ibin)*his->GetBinCenter(ibin)*his->GetBinContent(ibin);
250 sigma2= TMath::Sqrt(TMath::Abs(sigma2/sum-mean*mean));
252 (*param)[0] = his->GetMaximum();
254 (*param)[2] = sigma2;
257 if (verbose) printf("Mean\t%f\t Sigma2\t%f\n", mean,sigma2);
260 void TStatToolkit::LTM(TH1 * his, TVectorD *param , Float_t fraction, Bool_t verbose){
262 // LTM : Trimmed mean on histogram - Modified version for binned data
264 // Robust statistic to estimate properties of the distribution
265 // See http://en.wikipedia.org/w/index.php?title=Trimmed_estimator&oldid=582847999
267 // New faster version is under preparation
273 Int_t nbins = his->GetNbinsX();
274 Int_t nentries = (Int_t)his->GetEntries();
275 if (nentries<=0) return;
276 Double_t *data = new Double_t[nentries];
278 for (Int_t ibin=1;ibin<nbins; ibin++){
279 Double_t entriesI = his->GetBinContent(ibin);
280 //Double_t xcenter= his->GetBinCenter(ibin);
281 Double_t x0 = his->GetXaxis()->GetBinLowEdge(ibin);
282 Double_t w = his->GetXaxis()->GetBinWidth(ibin);
283 for (Int_t ic=0; ic<entriesI; ic++){
284 if (npoints<nentries){
285 data[npoints]= x0+w*Double_t((ic+0.5)/entriesI);
290 Double_t mean, sigma;
291 Int_t npoints2=TMath::Min(Int_t(fraction*Float_t(npoints)),npoints-1);
292 npoints2=TMath::Max(Int_t(0.5*Float_t(npoints)),npoints2);
293 TStatToolkit::EvaluateUni(npoints, data, mean,sigma,npoints2);
295 if (verbose) printf("Mean\t%f\t Sigma2\t%f\n", mean,sigma);if (param){
296 (*param)[0] = his->GetMaximum();
303 void TStatToolkit::MedianFilter(TH1 * his1D, Int_t nmedian){
305 // Algorithm to filter histogram
306 // author: marian.ivanov@cern.ch
307 // Details of algorithm:
308 // http://en.wikipedia.org/w/index.php?title=Median_filter&oldid=582191524
310 // his1D - input histogam - to be modiefied by Medianfilter
311 // nmendian - number of bins in median filter
313 Int_t nbins = his1D->GetNbinsX();
314 TVectorD vectorH(nbins);
315 for (Int_t ibin=0; ibin<nbins; ibin++) vectorH[ibin]=his1D->GetBinContent(ibin+1);
316 for (Int_t ibin=0; ibin<nbins; ibin++) {
317 Int_t index0=ibin-nmedian;
318 Int_t index1=ibin+nmedian;
319 if (index0<0) {index1+=-index0; index0=0;}
320 if (index1>=nbins) {index0-=index1-nbins+1; index1=nbins-1;}
321 Double_t value= TMath::Median(index1-index0,&(vectorH.GetMatrixArray()[index0]));
322 his1D->SetBinContent(ibin+1, value);
326 Bool_t TStatToolkit::LTMHisto(TH1 *his1D, TVectorD ¶ms , Float_t fraction){
328 // LTM : Trimmed mean on histogram - Modified version for binned data
330 // Robust statistic to estimate properties of the distribution
331 // To handle binning error special treatment
332 // for definition of unbinned data see:
333 // http://en.wikipedia.org/w/index.php?title=Trimmed_estimator&oldid=582847999
335 // Function parameters:
336 // his1D - input histogram
337 // params - vector with parameters
341 // - 3 - error estimate of mean
342 // - 4 - error estimate of RMS
343 // - 5 - first accepted bin position
344 // - 6 - last accepted bin position
346 Int_t nbins = his1D->GetNbinsX();
347 Int_t nentries = (Int_t)his1D->GetEntries();
348 const Double_t kEpsilon=0.0000000001;
350 if (nentries<=0) return 0;
351 if (fraction>1) fraction=0;
352 if (fraction<0) return 0;
353 TVectorD vectorX(nbins);
354 TVectorD vectorMean(nbins);
355 TVectorD vectorRMS(nbins);
357 for (Int_t ibin0=1; ibin0<=nbins; ibin0++) sumCont+=his1D->GetBinContent(ibin0);
359 Double_t minRMS=his1D->GetRMS()*10000;
362 for (Int_t ibin0=1; ibin0<nbins; ibin0++){
363 Double_t sum0=0, sum1=0, sum2=0;
365 for ( ibin1=ibin0; ibin1<nbins; ibin1++){
366 Double_t cont=his1D->GetBinContent(ibin1);
367 Double_t x= his1D->GetBinCenter(ibin1);
371 if ( (ibin0!=ibin1) && sum0>=fraction*sumCont) break;
373 vectorX[ibin0]=his1D->GetBinCenter(ibin0);
374 if (sum0<fraction*sumCont) continue;
376 // substract fractions of bin0 and bin1 to keep sum0=fration*sumCont
378 Double_t diff = sum0-fraction*sumCont;
379 Double_t mean = sum1/sum0;
381 Double_t x0=his1D->GetBinCenter(ibin0);
382 Double_t x1=his1D->GetBinCenter(ibin1);
383 Double_t y0=his1D->GetBinContent(ibin0);
384 Double_t y1=his1D->GetBinContent(ibin1);
386 Double_t d = y0+y1-diff; //enties to keep
388 if (y0<=kEpsilon&&y1>kEpsilon){
391 if (y1<=kEpsilon&&y0>kEpsilon){
394 if (y0>kEpsilon && y1>kEpsilon && x1>x0 ){
395 w0 = (d*(x1-mean))/((x1-x0)*y0);
398 if (w0>1) {w1+=(w0-1)*y0/y1; w0=1;}
399 if (w1>1) {w0+=(w1-1)*y1/y0; w1=1;}
401 if ( (x1>x0) &&TMath::Abs(y0*w0+y1*w1-d)>kEpsilon*sum0){
402 printf(" TStatToolkit::LTMHisto error\n");
410 Double_t xx0=his1D->GetXaxis()->GetBinUpEdge(ibin0)-0.5*w0*his1D->GetBinWidth(ibin0);
411 Double_t xx1=his1D->GetXaxis()->GetBinLowEdge(ibin1)+0.5*w1*his1D->GetBinWidth(ibin1);
419 // choose the bin with smallest rms
422 vectorMean[ibin0]=sum1/sum0;
423 vectorRMS[ibin0]=TMath::Sqrt(TMath::Abs(sum2/sum0-vectorMean[ibin0]*vectorMean[ibin0]));
424 if (vectorRMS[ibin0]<minRMS){
425 minRMS=vectorRMS[ibin0];
427 params[1]=vectorMean[ibin0];
428 params[2]=vectorRMS[ibin0];
429 params[3]=vectorRMS[ibin0]/TMath::Sqrt(sumCont*fraction);
430 params[4]=0; // what is the formula for error of RMS???
433 params[7]=his1D->GetBinCenter(ibin0);
434 params[8]=his1D->GetBinCenter(ibin1);
445 Double_t TStatToolkit::FitGaus(TH1* his, TVectorD *param, TMatrixD */*matrix*/, Float_t xmin, Float_t xmax, Bool_t verbose){
447 // Fit histogram with gaussian function
450 // return value- chi2 - if negative ( not enough points)
451 // his - input histogram
452 // param - vector with parameters
453 // xmin, xmax - range to fit - if xmin=xmax=0 - the full histogram range used
455 // 1. Step - make logarithm
456 // 2. Linear fit (parabola) - more robust - always converge
457 // 3. In case of small statistic bins are averaged
459 static TLinearFitter fitter(3,"pol2");
463 if (his->GetMaximum()<4) return -1;
464 if (his->GetEntries()<12) return -1;
465 if (his->GetRMS()<mat.GetTol()) return -1;
466 Float_t maxEstimate = his->GetEntries()*his->GetBinWidth(1)/TMath::Sqrt((TMath::TwoPi()*his->GetRMS()));
467 Int_t dsmooth = TMath::Nint(6./TMath::Sqrt(maxEstimate));
469 if (maxEstimate<1) return -1;
470 Int_t nbins = his->GetNbinsX();
476 xmin = his->GetXaxis()->GetXmin();
477 xmax = his->GetXaxis()->GetXmax();
479 for (Int_t iter=0; iter<2; iter++){
480 fitter.ClearPoints();
482 for (Int_t ibin=1;ibin<nbins+1; ibin++){
484 Float_t entriesI = his->GetBinContent(ibin);
485 for (Int_t delta = -dsmooth; delta<=dsmooth; delta++){
486 if (ibin+delta>1 &&ibin+delta<nbins-1){
487 entriesI += his->GetBinContent(ibin+delta);
492 Double_t xcenter= his->GetBinCenter(ibin);
493 if (xcenter<xmin || xcenter>xmax) continue;
494 Double_t error=1./TMath::Sqrt(countB);
497 if (par[0]+par[1]*xcenter+par[2]*xcenter*xcenter>20) return 0;
498 cont = TMath::Exp(par[0]+par[1]*xcenter+par[2]*xcenter*xcenter);
499 if (cont>1.) error = 1./TMath::Sqrt(cont*Float_t(countB));
501 if (entriesI>1&&cont>1){
502 fitter.AddPoint(&xcenter,TMath::Log(Float_t(entriesI)),error);
508 fitter.GetParameters(par);
516 fitter.GetParameters(par);
517 fitter.GetCovarianceMatrix(mat);
518 if (TMath::Abs(par[1])<mat.GetTol()) return -1;
519 if (TMath::Abs(par[2])<mat.GetTol()) return -1;
520 Double_t chi2 = fitter.GetChisquare()/Float_t(npoints);
521 //fitter.GetParameters();
522 if (!param) param = new TVectorD(3);
523 // if (!matrix) matrix = new TMatrixD(3,3); // Covariance matrix to be implemented
524 (*param)[1] = par[1]/(-2.*par[2]);
525 (*param)[2] = 1./TMath::Sqrt(TMath::Abs(-2.*par[2]));
526 (*param)[0] = TMath::Exp(par[0]+ par[1]* (*param)[1] + par[2]*(*param)[1]*(*param)[1]);
531 printf("Chi2=%f\n",chi2);
532 TF1 * f1= new TF1("f1","[0]*exp(-(x-[1])^2/(2*[2]*[2]))",his->GetXaxis()->GetXmin(),his->GetXaxis()->GetXmax());
533 f1->SetParameter(0, (*param)[0]);
534 f1->SetParameter(1, (*param)[1]);
535 f1->SetParameter(2, (*param)[2]);
541 Double_t TStatToolkit::FitGaus(Float_t *arr, Int_t nBins, Float_t xMin, Float_t xMax, TVectorD *param, TMatrixD */*matrix*/, Bool_t verbose){
543 // Fit histogram with gaussian function
546 // nbins: size of the array and number of histogram bins
547 // xMin, xMax: histogram range
548 // param: paramters of the fit (0-Constant, 1-Mean, 2-Sigma)
549 // matrix: covariance matrix -- not implemented yet, pass dummy matrix!!!
552 // >0: the chi2 returned by TLinearFitter
553 // -3: only three points have been used for the calculation - no fitter was used
554 // -2: only two points have been used for the calculation - center of gravity was uesed for calculation
555 // -1: only one point has been used for the calculation - center of gravity was uesed for calculation
556 // -4: invalid result!!
559 // 1. Step - make logarithm
560 // 2. Linear fit (parabola) - more robust - always converge
562 static TLinearFitter fitter(3,"pol2");
563 static TMatrixD mat(3,3);
564 static Double_t kTol = mat.GetTol();
565 fitter.StoreData(kFALSE);
566 fitter.ClearPoints();
571 Float_t rms = TMath::RMS(nBins,arr);
572 Float_t max = TMath::MaxElement(nBins,arr);
573 Float_t binWidth = (xMax-xMin)/(Float_t)nBins;
582 for (Int_t i=0; i<nBins; i++){
584 if (arr[i]>0) nfilled++;
587 if (max<4) return -4;
588 if (entries<12) return -4;
589 if (rms<kTol) return -4;
595 for (Int_t ibin=0;ibin<nBins; ibin++){
596 Float_t entriesI = arr[ibin];
598 Double_t xcenter = xMin+(ibin+0.5)*binWidth;
600 Float_t error = 1./TMath::Sqrt(entriesI);
601 Float_t val = TMath::Log(Float_t(entriesI));
602 fitter.AddPoint(&xcenter,val,error);
605 matA(npoints,1)=xcenter;
606 matA(npoints,2)=xcenter*xcenter;
608 meanCOG+=xcenter*entriesI;
609 rms2COG +=xcenter*entriesI*xcenter;
620 //analytic calculation of the parameters for three points
629 // use fitter for more than three points
631 fitter.GetParameters(par);
632 fitter.GetCovarianceMatrix(mat);
633 chi2 = fitter.GetChisquare()/Float_t(npoints);
635 if (TMath::Abs(par[1])<kTol) return -4;
636 if (TMath::Abs(par[2])<kTol) return -4;
638 if (!param) param = new TVectorD(3);
639 //if (!matrix) matrix = new TMatrixD(3,3); // !!!!might be a memory leek. use dummy matrix pointer to call this function! // Covariance matrix to be implemented
641 (*param)[1] = par[1]/(-2.*par[2]);
642 (*param)[2] = 1./TMath::Sqrt(TMath::Abs(-2.*par[2]));
643 Double_t lnparam0 = par[0]+ par[1]* (*param)[1] + par[2]*(*param)[1]*(*param)[1];
644 if ( lnparam0>307 ) return -4;
645 (*param)[0] = TMath::Exp(lnparam0);
650 printf("Chi2=%f\n",chi2);
651 TF1 * f1= new TF1("f1","[0]*exp(-(x-[1])^2/(2*[2]*[2]))",xMin,xMax);
652 f1->SetParameter(0, (*param)[0]);
653 f1->SetParameter(1, (*param)[1]);
654 f1->SetParameter(2, (*param)[2]);
661 //use center of gravity for 2 points
665 (*param)[1] = meanCOG;
666 (*param)[2] = TMath::Sqrt(TMath::Abs(meanCOG*meanCOG-rms2COG));
672 (*param)[1] = meanCOG;
673 (*param)[2] = binWidth/TMath::Sqrt(12);
681 Float_t TStatToolkit::GetCOG(const Short_t *arr, Int_t nBins, Float_t xMin, Float_t xMax, Float_t *rms, Float_t *sum)
684 // calculate center of gravity rms and sum for array 'arr' with nBins an a x range xMin to xMax
685 // return COG; in case of failure return xMin
692 Float_t binWidth = (xMax-xMin)/(Float_t)nBins;
694 for (Int_t ibin=0; ibin<nBins; ibin++){
695 Float_t entriesI = (Float_t)arr[ibin];
696 Double_t xcenter = xMin+(ibin+0.5)*binWidth;
698 meanCOG += xcenter*entriesI;
699 rms2COG += xcenter*entriesI*xcenter;
704 if ( sumCOG == 0 ) return xMin;
709 (*rms) = TMath::Sqrt(TMath::Abs(meanCOG*meanCOG-rms2COG));
710 if ( npoints == 1 ) (*rms) = binWidth/TMath::Sqrt(12);
721 ///////////////////////////////////////////////////////////////
722 ////////////// TEST functions /////////////////////////
723 ///////////////////////////////////////////////////////////////
729 void TStatToolkit::TestGausFit(Int_t nhistos){
731 // Test performance of the parabolic - gaussian fit - compare it with
733 // nhistos - number of histograms to be used for test
735 TTreeSRedirector *pcstream = new TTreeSRedirector("fitdebug.root","recreate");
737 Float_t *xTrue = new Float_t[nhistos];
738 Float_t *sTrue = new Float_t[nhistos];
739 TVectorD **par1 = new TVectorD*[nhistos];
740 TVectorD **par2 = new TVectorD*[nhistos];
744 TH1F **h1f = new TH1F*[nhistos];
745 TF1 *myg = new TF1("myg","gaus");
746 TF1 *fit = new TF1("fit","gaus");
750 for (Int_t i=0;i<nhistos; i++){
751 par1[i] = new TVectorD(3);
752 par2[i] = new TVectorD(3);
753 h1f[i] = new TH1F(Form("h1f%d",i),Form("h1f%d",i),20,-10,10);
754 xTrue[i]= gRandom->Rndm();
756 sTrue[i]= .75+gRandom->Rndm()*.5;
757 myg->SetParameters(1,xTrue[i],sTrue[i]);
758 h1f[i]->FillRandom("myg");
764 for (Int_t i=0; i<nhistos; i++){
765 h1f[i]->Fit(fit,"0q");
766 (*par1[i])(0) = fit->GetParameter(0);
767 (*par1[i])(1) = fit->GetParameter(1);
768 (*par1[i])(2) = fit->GetParameter(2);
771 printf("Gaussian fit\t");
775 //TStatToolkit gaus fit
776 for (Int_t i=0; i<nhistos; i++){
777 TStatToolkit::FitGaus(h1f[i]->GetArray()+1,h1f[i]->GetNbinsX(),h1f[i]->GetXaxis()->GetXmin(),h1f[i]->GetXaxis()->GetXmax(),par2[i],&dummy);
781 printf("Parabolic fit\t");
784 for (Int_t i=0;i<nhistos; i++){
785 Float_t xt = xTrue[i];
786 Float_t st = sTrue[i];
795 for (Int_t i=0;i<nhistos; i++){
812 TGraph2D * TStatToolkit::MakeStat2D(TH3 * his, Int_t delta0, Int_t delta1, Int_t type){
816 // delta - number of bins to integrate
817 // type - 0 - mean value
819 TAxis * xaxis = his->GetXaxis();
820 TAxis * yaxis = his->GetYaxis();
821 // TAxis * zaxis = his->GetZaxis();
822 Int_t nbinx = xaxis->GetNbins();
823 Int_t nbiny = yaxis->GetNbins();
826 TGraph2D *graph = new TGraph2D(nbinx*nbiny);
828 for (Int_t ix=0; ix<nbinx;ix++)
829 for (Int_t iy=0; iy<nbiny;iy++){
830 Float_t xcenter = xaxis->GetBinCenter(ix);
831 Float_t ycenter = yaxis->GetBinCenter(iy);
832 snprintf(name,1000,"%s_%d_%d",his->GetName(), ix,iy);
833 TH1 *projection = his->ProjectionZ(name,ix-delta0,ix+delta0,iy-delta1,iy+delta1);
835 if (type==0) stat = projection->GetMean();
836 if (type==1) stat = projection->GetRMS();
837 if (type==2 || type==3){
839 TStatToolkit::LTM((TH1F*)projection,&vec,0.7);
840 if (type==2) stat= vec[1];
841 if (type==3) stat= vec[0];
843 if (type==4|| type==5){
844 projection->Fit(&f1);
845 if (type==4) stat= f1.GetParameter(1);
846 if (type==5) stat= f1.GetParameter(2);
848 //printf("%d\t%f\t%f\t%f\n", icount,xcenter, ycenter, stat);
849 graph->SetPoint(icount,xcenter, ycenter, stat);
855 TGraphErrors * TStatToolkit::MakeStat1D(TH2 * his, Int_t deltaBin, Double_t fraction, Int_t returnType, Int_t markerStyle, Int_t markerColor){
857 // function to retrieve the "mean and RMS estimate" of 2D histograms
859 // Robust statistic to estimate properties of the distribution
860 // See http://en.wikipedia.org/wiki/Trimmed_estimator
862 // deltaBin - number of bins to integrate (bin+-deltaBin)
863 // fraction - fraction of values for the LTM and for the gauss fit
869 // 4 - Gaus fit mean - on LTM range
870 // 5 - Gaus fit sigma - on LTM range
872 TAxis * xaxis = his->GetXaxis();
873 Int_t nbinx = xaxis->GetNbins();
877 TVectorD vecX(nbinx);
878 TVectorD vecXErr(nbinx);
879 TVectorD vecY(nbinx);
880 TVectorD vecYErr(nbinx);
885 for (Int_t jx=1; jx<=nbinx;jx++){
887 Float_t xcenter = xaxis->GetBinCenter(jx);
888 snprintf(name,1000,"%s_%d",his->GetName(), ix);
889 TH1 *projection = his->ProjectionY(name,TMath::Max(jx-deltaBin,1),TMath::Min(jx+deltaBin,nbinx));
892 TStatToolkit::LTMHisto((TH1F*)projection,vecLTM,fraction);
895 stat = projection->GetMean();
896 err = projection->GetMeanError();
899 stat = projection->GetRMS();
900 err = projection->GetRMSError();
902 if (returnType==2 || returnType==3){
903 if (returnType==2) {stat= vecLTM[1]; err =projection->GetRMSError();}
904 if (returnType==3) {stat= vecLTM[2]; err =projection->GetRMSError();}
906 if (returnType==4|| returnType==5){
907 projection->Fit(&f1,"QN","QN", vecLTM[7], vecLTM[8]);
909 stat= f1.GetParameter(1);
910 err=f1.GetParError(1);
913 stat= f1.GetParameter(2);
914 err=f1.GetParError(2);
917 vecX[icount]=xcenter;
923 TGraphErrors *graph = new TGraphErrors(icount,vecX.GetMatrixArray(), vecY.GetMatrixArray(),0, vecYErr.GetMatrixArray());
924 graph->SetMarkerStyle(markerStyle);
925 graph->SetMarkerColor(markerColor);
933 TString* TStatToolkit::FitPlane(TTree *tree, const char* drawCommand, const char* formula, const char* cuts, Double_t & chi2, Int_t &npoints, TVectorD &fitParam, TMatrixD &covMatrix, Float_t frac, Int_t start, Int_t stop,Bool_t fix0){
935 // fit an arbitrary function, specified by formula into the data, specified by drawCommand and cuts
936 // returns chi2, fitParam and covMatrix
937 // returns TString with fitted formula
940 TString formulaStr(formula);
941 TString drawStr(drawCommand);
942 TString cutStr(cuts);
945 TString strVal(drawCommand);
946 if (strVal.Contains(":")){
947 TObjArray* valTokens = strVal.Tokenize(":");
948 drawStr = valTokens->At(0)->GetName();
949 ferr = valTokens->At(1)->GetName();
954 formulaStr.ReplaceAll("++", "~");
955 TObjArray* formulaTokens = formulaStr.Tokenize("~");
956 Int_t dim = formulaTokens->GetEntriesFast();
958 fitParam.ResizeTo(dim);
959 covMatrix.ResizeTo(dim,dim);
961 TLinearFitter* fitter = new TLinearFitter(dim+1, Form("hyp%d",dim));
962 fitter->StoreData(kTRUE);
963 fitter->ClearPoints();
965 Int_t entries = tree->Draw(drawStr.Data(), cutStr.Data(), "goff", stop-start, start);
967 delete formulaTokens;
968 return new TString("An ERROR has occured during fitting!");
970 Double_t **values = new Double_t*[dim+1] ;
971 for (Int_t i=0; i<dim+1; i++) values[i]=NULL;
973 entries = tree->Draw(ferr.Data(), cutStr.Data(), "goff", stop-start, start);
975 delete formulaTokens;
977 return new TString("An ERROR has occured during fitting!");
979 Double_t *errors = new Double_t[entries];
980 memcpy(errors, tree->GetV1(), entries*sizeof(Double_t));
982 for (Int_t i = 0; i < dim + 1; i++){
984 if (i < dim) centries = tree->Draw(((TObjString*)formulaTokens->At(i))->GetName(), cutStr.Data(), "goff", stop-start,start);
985 else centries = tree->Draw(drawStr.Data(), cutStr.Data(), "goff", stop-start,start);
987 if (entries != centries) {
990 return new TString("An ERROR has occured during fitting!");
992 values[i] = new Double_t[entries];
993 memcpy(values[i], tree->GetV1(), entries*sizeof(Double_t));
996 // add points to the fitter
997 for (Int_t i = 0; i < entries; i++){
999 for (Int_t j=0; j<dim;j++) x[j]=values[j][i];
1000 fitter->AddPoint(x, values[dim][i], errors[i]);
1004 if (frac>0.5 && frac<1){
1005 fitter->EvalRobust(frac);
1008 fitter->FixParameter(0,0);
1012 fitter->GetParameters(fitParam);
1013 fitter->GetCovarianceMatrix(covMatrix);
1014 chi2 = fitter->GetChisquare();
1016 TString *preturnFormula = new TString(Form("( %f+",fitParam[0])), &returnFormula = *preturnFormula;
1018 for (Int_t iparam = 0; iparam < dim; iparam++) {
1019 returnFormula.Append(Form("%s*(%f)",((TObjString*)formulaTokens->At(iparam))->GetName(),fitParam[iparam+1]));
1020 if (iparam < dim-1) returnFormula.Append("+");
1022 returnFormula.Append(" )");
1025 for (Int_t j=0; j<dim+1;j++) delete [] values[j];
1028 delete formulaTokens;
1032 return preturnFormula;
1035 TString* TStatToolkit::FitPlaneConstrain(TTree *tree, const char* drawCommand, const char* formula, const char* cuts, Double_t & chi2, Int_t &npoints, TVectorD &fitParam, TMatrixD &covMatrix, Float_t frac, Int_t start, Int_t stop,Double_t constrain){
1037 // fit an arbitrary function, specified by formula into the data, specified by drawCommand and cuts
1038 // returns chi2, fitParam and covMatrix
1039 // returns TString with fitted formula
1042 TString formulaStr(formula);
1043 TString drawStr(drawCommand);
1044 TString cutStr(cuts);
1047 TString strVal(drawCommand);
1048 if (strVal.Contains(":")){
1049 TObjArray* valTokens = strVal.Tokenize(":");
1050 drawStr = valTokens->At(0)->GetName();
1051 ferr = valTokens->At(1)->GetName();
1056 formulaStr.ReplaceAll("++", "~");
1057 TObjArray* formulaTokens = formulaStr.Tokenize("~");
1058 Int_t dim = formulaTokens->GetEntriesFast();
1060 fitParam.ResizeTo(dim);
1061 covMatrix.ResizeTo(dim,dim);
1063 TLinearFitter* fitter = new TLinearFitter(dim+1, Form("hyp%d",dim));
1064 fitter->StoreData(kTRUE);
1065 fitter->ClearPoints();
1067 Int_t entries = tree->Draw(drawStr.Data(), cutStr.Data(), "goff", stop-start, start);
1068 if (entries == -1) {
1069 delete formulaTokens;
1070 return new TString("An ERROR has occured during fitting!");
1072 Double_t **values = new Double_t*[dim+1] ;
1073 for (Int_t i=0; i<dim+1; i++) values[i]=NULL;
1075 entries = tree->Draw(ferr.Data(), cutStr.Data(), "goff", stop-start, start);
1076 if (entries == -1) {
1077 delete formulaTokens;
1079 return new TString("An ERROR has occured during fitting!");
1081 Double_t *errors = new Double_t[entries];
1082 memcpy(errors, tree->GetV1(), entries*sizeof(Double_t));
1084 for (Int_t i = 0; i < dim + 1; i++){
1086 if (i < dim) centries = tree->Draw(((TObjString*)formulaTokens->At(i))->GetName(), cutStr.Data(), "goff", stop-start,start);
1087 else centries = tree->Draw(drawStr.Data(), cutStr.Data(), "goff", stop-start,start);
1089 if (entries != centries) {
1092 delete formulaTokens;
1093 return new TString("An ERROR has occured during fitting!");
1095 values[i] = new Double_t[entries];
1096 memcpy(values[i], tree->GetV1(), entries*sizeof(Double_t));
1099 // add points to the fitter
1100 for (Int_t i = 0; i < entries; i++){
1102 for (Int_t j=0; j<dim;j++) x[j]=values[j][i];
1103 fitter->AddPoint(x, values[dim][i], errors[i]);
1106 for (Int_t i = 0; i < dim; i++){
1108 for (Int_t j=0; j<dim;j++) if (i!=j) x[j]=0;
1110 fitter->AddPoint(x, 0, constrain);
1116 if (frac>0.5 && frac<1){
1117 fitter->EvalRobust(frac);
1119 fitter->GetParameters(fitParam);
1120 fitter->GetCovarianceMatrix(covMatrix);
1121 chi2 = fitter->GetChisquare();
1124 TString *preturnFormula = new TString(Form("( %f+",fitParam[0])), &returnFormula = *preturnFormula;
1126 for (Int_t iparam = 0; iparam < dim; iparam++) {
1127 returnFormula.Append(Form("%s*(%f)",((TObjString*)formulaTokens->At(iparam))->GetName(),fitParam[iparam+1]));
1128 if (iparam < dim-1) returnFormula.Append("+");
1130 returnFormula.Append(" )");
1132 for (Int_t j=0; j<dim+1;j++) delete [] values[j];
1136 delete formulaTokens;
1140 return preturnFormula;
1145 TString* TStatToolkit::FitPlaneFixed(TTree *tree, const char* drawCommand, const char* formula, const char* cuts, Double_t & chi2, Int_t &npoints, TVectorD &fitParam, TMatrixD &covMatrix, Float_t frac, Int_t start, Int_t stop){
1147 // fit an arbitrary function, specified by formula into the data, specified by drawCommand and cuts
1148 // returns chi2, fitParam and covMatrix
1149 // returns TString with fitted formula
1152 TString formulaStr(formula);
1153 TString drawStr(drawCommand);
1154 TString cutStr(cuts);
1157 TString strVal(drawCommand);
1158 if (strVal.Contains(":")){
1159 TObjArray* valTokens = strVal.Tokenize(":");
1160 drawStr = valTokens->At(0)->GetName();
1161 ferr = valTokens->At(1)->GetName();
1166 formulaStr.ReplaceAll("++", "~");
1167 TObjArray* formulaTokens = formulaStr.Tokenize("~");
1168 Int_t dim = formulaTokens->GetEntriesFast();
1170 fitParam.ResizeTo(dim);
1171 covMatrix.ResizeTo(dim,dim);
1172 TString fitString="x0";
1173 for (Int_t i=1; i<dim; i++) fitString+=Form("++x%d",i);
1174 TLinearFitter* fitter = new TLinearFitter(dim, fitString.Data());
1175 fitter->StoreData(kTRUE);
1176 fitter->ClearPoints();
1178 Int_t entries = tree->Draw(drawStr.Data(), cutStr.Data(), "goff", stop-start, start);
1179 if (entries == -1) {
1180 delete formulaTokens;
1181 return new TString("An ERROR has occured during fitting!");
1183 Double_t **values = new Double_t*[dim+1] ;
1184 for (Int_t i=0; i<dim+1; i++) values[i]=NULL;
1186 entries = tree->Draw(ferr.Data(), cutStr.Data(), "goff", stop-start, start);
1187 if (entries == -1) {
1189 delete formulaTokens;
1190 return new TString("An ERROR has occured during fitting!");
1192 Double_t *errors = new Double_t[entries];
1193 memcpy(errors, tree->GetV1(), entries*sizeof(Double_t));
1195 for (Int_t i = 0; i < dim + 1; i++){
1197 if (i < dim) centries = tree->Draw(((TObjString*)formulaTokens->At(i))->GetName(), cutStr.Data(), "goff", stop-start,start);
1198 else centries = tree->Draw(drawStr.Data(), cutStr.Data(), "goff", stop-start,start);
1200 if (entries != centries) {
1203 delete formulaTokens;
1204 return new TString("An ERROR has occured during fitting!");
1206 values[i] = new Double_t[entries];
1207 memcpy(values[i], tree->GetV1(), entries*sizeof(Double_t));
1210 // add points to the fitter
1211 for (Int_t i = 0; i < entries; i++){
1213 for (Int_t j=0; j<dim;j++) x[j]=values[j][i];
1214 fitter->AddPoint(x, values[dim][i], errors[i]);
1218 if (frac>0.5 && frac<1){
1219 fitter->EvalRobust(frac);
1221 fitter->GetParameters(fitParam);
1222 fitter->GetCovarianceMatrix(covMatrix);
1223 chi2 = fitter->GetChisquare();
1226 TString *preturnFormula = new TString("("), &returnFormula = *preturnFormula;
1228 for (Int_t iparam = 0; iparam < dim; iparam++) {
1229 returnFormula.Append(Form("%s*(%f)",((TObjString*)formulaTokens->At(iparam))->GetName(),fitParam[iparam]));
1230 if (iparam < dim-1) returnFormula.Append("+");
1232 returnFormula.Append(" )");
1235 for (Int_t j=0; j<dim+1;j++) delete [] values[j];
1237 delete formulaTokens;
1241 return preturnFormula;
1248 Int_t TStatToolkit::GetFitIndex(const TString fString, const TString subString){
1250 // fitString - ++ separated list of fits
1251 // substring - ++ separated list of the requiered substrings
1253 // return the last occurance of substring in fit string
1255 TObjArray *arrFit = fString.Tokenize("++");
1256 TObjArray *arrSub = subString.Tokenize("++");
1258 for (Int_t i=0; i<arrFit->GetEntries(); i++){
1260 TString str =arrFit->At(i)->GetName();
1261 for (Int_t isub=0; isub<arrSub->GetEntries(); isub++){
1262 if (str.Contains(arrSub->At(isub)->GetName())==0) isOK=kFALSE;
1272 TString TStatToolkit::FilterFit(const TString &input, const TString filter, TVectorD ¶m, TMatrixD & covar){
1274 // Filter fit expression make sub-fit
1276 TObjArray *array0= input.Tokenize("++");
1277 TObjArray *array1= filter.Tokenize("++");
1278 //TString *presult=new TString("(0");
1279 TString result="(0.0";
1280 for (Int_t i=0; i<array0->GetEntries(); i++){
1282 TString str(array0->At(i)->GetName());
1283 for (Int_t j=0; j<array1->GetEntries(); j++){
1284 if (str.Contains(array1->At(j)->GetName())==0) isOK=kFALSE;
1288 result+=Form("*(%f)",param[i+1]);
1289 printf("%f\t%f\t%s\n",param[i+1], TMath::Sqrt(covar(i+1,i+1)),str.Data());
1298 void TStatToolkit::Update1D(Double_t delta, Double_t sigma, Int_t s1, TMatrixD &vecXk, TMatrixD &covXk){
1300 // Update parameters and covariance - with one measurement
1302 // vecXk - input vector - Updated in function
1303 // covXk - covariance matrix - Updated in function
1304 // delta, sigma, s1 - new measurement, rms of new measurement and the index of measurement
1305 const Int_t knMeas=1;
1306 Int_t knElem=vecXk.GetNrows();
1308 TMatrixD mat1(knElem,knElem); // update covariance matrix
1309 TMatrixD matHk(1,knElem); // vector to mesurement
1310 TMatrixD vecYk(knMeas,1); // Innovation or measurement residual
1311 TMatrixD matHkT(knElem,knMeas); // helper matrix Hk transpose
1312 TMatrixD matSk(knMeas,knMeas); // Innovation (or residual) covariance
1313 TMatrixD matKk(knElem,knMeas); // Optimal Kalman gain
1314 TMatrixD covXk2(knElem,knElem); // helper matrix
1315 TMatrixD covXk3(knElem,knElem); // helper matrix
1316 TMatrixD vecZk(1,1);
1317 TMatrixD measR(1,1);
1319 measR(0,0)=sigma*sigma;
1322 for (Int_t iel=0;iel<knElem;iel++)
1323 for (Int_t ip=0;ip<knMeas;ip++) matHk(ip,iel)=0;
1325 for (Int_t iel=0;iel<knElem;iel++) {
1326 for (Int_t jel=0;jel<knElem;jel++) mat1(iel,jel)=0;
1331 vecYk = vecZk-matHk*vecXk; // Innovation or measurement residual
1332 matHkT=matHk.T(); matHk.T();
1333 matSk = (matHk*(covXk*matHkT))+measR; // Innovation (or residual) covariance
1335 matKk = (covXk*matHkT)*matSk; // Optimal Kalman gain
1336 vecXk += matKk*vecYk; // updated vector
1337 covXk2= (mat1-(matKk*matHk));
1338 covXk3 = covXk2*covXk;
1340 Int_t nrows=covXk3.GetNrows();
1342 for (Int_t irow=0; irow<nrows; irow++)
1343 for (Int_t icol=0; icol<nrows; icol++){
1344 // rounding problems - make matrix again symteric
1345 covXk(irow,icol)=(covXk3(irow,icol)+covXk3(icol,irow))*0.5;
1351 void TStatToolkit::Constrain1D(const TString &input, const TString filter, TVectorD ¶m, TMatrixD & covar, Double_t mean, Double_t sigma){
1353 // constrain linear fit
1354 // input - string description of fit function
1355 // filter - string filter to select sub fits
1356 // param,covar - parameters and covariance matrix of the fit
1357 // mean,sigma - new measurement uning which the fit is updated
1360 TObjArray *array0= input.Tokenize("++");
1361 TObjArray *array1= filter.Tokenize("++");
1362 TMatrixD paramM(param.GetNrows(),1);
1363 for (Int_t i=0; i<=array0->GetEntries(); i++){paramM(i,0)=param(i);}
1365 if (filter.Length()==0){
1366 TStatToolkit::Update1D(mean, sigma, 0, paramM, covar);//
1368 for (Int_t i=0; i<array0->GetEntries(); i++){
1370 TString str(array0->At(i)->GetName());
1371 for (Int_t j=0; j<array1->GetEntries(); j++){
1372 if (str.Contains(array1->At(j)->GetName())==0) isOK=kFALSE;
1375 TStatToolkit::Update1D(mean, sigma, i+1, paramM, covar);//
1379 for (Int_t i=0; i<=array0->GetEntries(); i++){
1380 param(i)=paramM(i,0);
1386 TString TStatToolkit::MakeFitString(const TString &input, const TVectorD ¶m, const TMatrixD & covar, Bool_t verbose){
1390 TObjArray *array0= input.Tokenize("++");
1391 TString result=Form("(%f",param[0]);
1392 printf("%f\t%f\t\n", param[0], TMath::Sqrt(covar(0,0)));
1393 for (Int_t i=0; i<array0->GetEntries(); i++){
1394 TString str(array0->At(i)->GetName());
1396 result+=Form("*(%f)",param[i+1]);
1397 if (verbose) printf("%f\t%f\t%s\n", param[i+1], TMath::Sqrt(covar(i+1,i+1)),str.Data());
1404 TGraphErrors * TStatToolkit::MakeGraphErrors(TTree * tree, const char * expr, const char * cut, Int_t mstyle, Int_t mcolor, Float_t msize, Float_t offset){
1406 // Query a graph errors
1407 // return TGraphErrors specified by expr and cut
1408 // Example usage TStatToolkit::MakeGraphError(tree,"Y:X:ErrY","X>0", 25,2,0.4)
1409 // tree - tree with variable
1411 const Int_t entries = tree->Draw(expr,cut,"goff");
1414 t.Error("TStatToolkit::MakeGraphError",Form("Empty or Not valid expression (%s) or cut *%s)", expr,cut));
1417 if ( tree->GetV2()==0){
1419 t.Error("TStatToolkit::MakeGraphError",Form("Not valid expression (%s) ", expr));
1422 TGraphErrors * graph=0;
1423 if ( tree->GetV3()!=0){
1424 graph = new TGraphErrors (entries, tree->GetV2(),tree->GetV1(),0,tree->GetV3());
1426 graph = new TGraphErrors (entries, tree->GetV2(),tree->GetV1(),0,0);
1428 graph->SetMarkerStyle(mstyle);
1429 graph->SetMarkerColor(mcolor);
1430 graph->SetLineColor(mcolor);
1431 if (msize>0) graph->SetMarkerSize(msize);
1432 for(Int_t i=0;i<graph->GetN();i++) graph->GetX()[i]+=offset;
1438 TGraph * TStatToolkit::MakeGraphSparse(TTree * tree, const char * expr, const char * cut, Int_t mstyle, Int_t mcolor, Float_t msize, Float_t offset){
1440 // Make a sparse draw of the variables
1441 // Format of expr : Var:Run or Var:Run:ErrorY or Var:Run:ErrorY:ErrorX
1442 // offset : points can slightly be shifted in x for better visibility with more graphs
1444 // Written by Weilin.Yu
1445 // updated & merged with QA-code by Patrick Reichelt
1447 const Int_t entries = tree->Draw(expr,cut,"goff");
1450 t.Error("TStatToolkit::MakeGraphSparse",Form("Empty or Not valid expression (%s) or cut (%s)", expr, cut));
1453 // TGraph * graph = (TGraph*)gPad->GetPrimitive("Graph"); // 2D
1455 Double_t *graphY, *graphX;
1456 graphY = tree->GetV1();
1457 graphX = tree->GetV2();
1459 // sort according to run number
1460 Int_t *index = new Int_t[entries*4];
1461 TMath::Sort(entries,graphX,index,kFALSE);
1463 // define arrays for the new graph
1464 Double_t *unsortedX = new Double_t[entries];
1465 Int_t *runNumber = new Int_t[entries];
1466 Double_t count = 0.5;
1468 // evaluate arrays for the new graph according to the run-number
1471 unsortedX[index[0]] = count;
1472 runNumber[0] = graphX[index[0]];
1473 // loop the rest of entries
1474 for(Int_t i=1;i<entries;i++)
1476 if(graphX[index[i]]==graphX[index[i-1]])
1477 unsortedX[index[i]] = count;
1478 else if(graphX[index[i]]!=graphX[index[i-1]]){
1481 unsortedX[index[i]] = count;
1482 runNumber[icount]=graphX[index[i]];
1486 // count the number of xbins (run-wise) for the new graph
1487 const Int_t newNbins = int(count+0.5);
1488 Double_t *newBins = new Double_t[newNbins+1];
1489 for(Int_t i=0; i<=count+1;i++){
1493 // define and fill the new graph
1494 TGraph *graphNew = 0;
1495 if (tree->GetV3()) {
1496 if (tree->GetV4()) {
1497 graphNew = new TGraphErrors(entries,unsortedX,graphY,tree->GetV4(),tree->GetV3());
1499 else { graphNew = new TGraphErrors(entries,unsortedX,graphY,0,tree->GetV3()); }
1501 else { graphNew = new TGraphErrors(entries,unsortedX,graphY,0,0); }
1502 // with "Set(...)", the x-axis is being sorted
1503 graphNew->GetXaxis()->Set(newNbins,newBins);
1505 // set the bins for the x-axis, apply shifting of points
1507 for(Int_t i=0;i<count;i++){
1508 snprintf(xName,50,"%d",runNumber[i]);
1509 graphNew->GetXaxis()->SetBinLabel(i+1,xName);
1510 graphNew->GetX()[i]+=offset;
1513 graphNew->GetHistogram()->SetTitle("");
1514 graphNew->SetMarkerStyle(mstyle);
1515 graphNew->SetMarkerColor(mcolor);
1516 if (msize>0) graphNew->SetMarkerSize(msize);
1517 delete [] unsortedX;
1518 delete [] runNumber;
1528 // functions used for the trending
1531 Int_t TStatToolkit::MakeStatAlias(TTree * tree, const char * expr, const char * cut, const char * alias)
1534 // Add alias using statistical values of a given variable.
1535 // (by MI, Patrick Reichelt)
1537 // tree - input tree
1538 // expr - variable expression
1539 // cut - selection criteria
1540 // Output - return number of entries used to define variable
1541 // In addition mean, rms, median, and robust mean and rms (choosing fraction of data with smallest RMS)
1544 1.) create the robust estimators for variable expr="QA.TPC.CPass1.meanTPCncl" and create a corresponding
1545 aliases with the prefix alias[0]="ncl", calculated using fraction alias[1]="0.90"
1547 TStatToolkit::MakeStatAlias(tree,"QA.TPC.CPass1.meanTPCncl","QA.TPC.CPass1.status>0","ncl:0.9");
1548 root [4] tree->GetListOfAliases().Print()
1549 OBJ: TNamed ncl_Median (130.964333+0)
1550 OBJ: TNamed ncl_Mean (122.120387+0)
1551 OBJ: TNamed ncl_RMS (33.509623+0)
1552 OBJ: TNamed ncl_Mean90 (131.503862+0)
1553 OBJ: TNamed ncl_RMS90 (3.738260+0)
1556 Int_t entries = tree->Draw(expr,cut,"goff");
1558 printf("Expression or cut not valid:\t%s\t%s\n", expr, cut);
1562 TObjArray* oaAlias = TString(alias).Tokenize(":");
1563 if (oaAlias->GetEntries()<2) return 0;
1564 Float_t entryFraction = atof( oaAlias->At(1)->GetName() );
1566 Double_t median = TMath::Median(entries,tree->GetV1());
1567 Double_t mean = TMath::Mean(entries,tree->GetV1());
1568 Double_t rms = TMath::RMS(entries,tree->GetV1());
1569 Double_t meanEF=0, rmsEF=0;
1570 TStatToolkit::EvaluateUni(entries, tree->GetV1(), meanEF, rmsEF, entries*entryFraction);
1572 tree->SetAlias(Form("%s_Median",oaAlias->At(0)->GetName()), Form("(%f+0)",median));
1573 tree->SetAlias(Form("%s_Mean",oaAlias->At(0)->GetName()), Form("(%f+0)",mean));
1574 tree->SetAlias(Form("%s_RMS",oaAlias->At(0)->GetName()), Form("(%f+0)",rms));
1575 tree->SetAlias(Form("%s_Mean%d",oaAlias->At(0)->GetName(),Int_t(entryFraction*100)), Form("(%f+0)",meanEF));
1576 tree->SetAlias(Form("%s_RMS%d",oaAlias->At(0)->GetName(),Int_t(entryFraction*100)), Form("(%f+0)",rmsEF));
1581 Int_t TStatToolkit::SetStatusAlias(TTree * tree, const char * expr, const char * cut, const char * alias)
1584 // Add alias to trending tree using statistical values of a given variable.
1585 // (by MI, Patrick Reichelt)
1587 // format of expr : varname (e.g. meanTPCncl)
1588 // format of cut : char like in TCut
1589 // format of alias: alias:query:entryFraction(EF) (fraction of entries used for uniformity evaluation)
1590 // e.g.: varname_Out:(abs(varname-meanEF)>6.*rmsEF):0.8
1591 // available internal variables are: 'varname, Median, Mean, MeanEF, RMS, RMSEF'
1592 // in the alias, 'varname' will be replaced by its content, and 'EF' by the percentage (e.g. MeanEF -> Mean80)
1595 1.) Define robust mean (possible, but easier done with TStatToolkit::MakeStatAlias(...))
1596 TStatToolkit::SetStatusAlias(tree, "meanTPCnclF", "meanTPCnclF>0", "meanTPCnclF_MeanEF:MeanEF:0.80") ;
1597 root [10] tree->GetListOfAliases()->Print()
1598 Collection name='TList', class='TList', size=1
1599 OBJ: TNamed meanTPCnclF_Mean80 0.899308
1600 2.) create alias outlyers - 6 sigma cut
1601 TStatToolkit::SetStatusAlias(tree, "meanTPCnclF", "meanTPCnclF>0", "meanTPCnclF_Out:(abs(meanTPCnclF-MeanEF)>6.*RMSEF):0.8")
1602 meanTPCnclF_Out ==> (abs(meanTPCnclF-0.899308)>6.*0.016590)
1603 3.) the same functionality as in 2.)
1604 TStatToolkit::SetStatusAlias(tree, "meanTPCnclF", "meanTPCnclF>0", "varname_Out2:(abs(varname-MeanEF)>6.*RMSEF):0.8")
1605 meanTPCnclF_Out2 ==> (abs(meanTPCnclF-0.899308)>6.*0.016590)
1608 Int_t entries = tree->Draw(expr,cut,"goff");
1610 printf("Expression or cut not valid:\t%s\t%s\n", expr, cut);
1614 TObjArray* oaVar = TString(expr).Tokenize(":");
1616 snprintf(varname,50,"%s", oaVar->At(0)->GetName());
1618 TObjArray* oaAlias = TString(alias).Tokenize(":");
1619 if (oaAlias->GetEntries()<3) return 0;
1620 Float_t entryFraction = atof( oaAlias->At(2)->GetName() );
1622 Double_t median = TMath::Median(entries,tree->GetV1());
1623 Double_t mean = TMath::Mean(entries,tree->GetV1());
1624 Double_t rms = TMath::RMS(entries,tree->GetV1());
1625 Double_t meanEF=0, rmsEF=0;
1626 TStatToolkit::EvaluateUni(entries, tree->GetV1(), meanEF, rmsEF, entries*entryFraction);
1628 TString sAlias( oaAlias->At(0)->GetName() );
1629 sAlias.ReplaceAll("varname",varname);
1630 sAlias.ReplaceAll("MeanEF", Form("Mean%1.0f",entryFraction*100) );
1631 sAlias.ReplaceAll("RMSEF", Form("RMS%1.0f",entryFraction*100) );
1632 TString sQuery( oaAlias->At(1)->GetName() );
1633 sQuery.ReplaceAll("varname",varname);
1634 sQuery.ReplaceAll("MeanEF", Form("%f",meanEF) );
1635 sQuery.ReplaceAll("RMSEF", Form("%f",rmsEF) ); //make sure to replace 'RMSEF' before 'RMS'...
1636 sQuery.ReplaceAll("Median", Form("%f",median) );
1637 sQuery.ReplaceAll("Mean", Form("%f",mean) );
1638 sQuery.ReplaceAll("RMS", Form("%f",rms) );
1639 printf("define alias:\t%s = %s\n", sAlias.Data(), sQuery.Data());
1643 snprintf(query,200,"%s", sQuery.Data());
1644 snprintf(aname,200,"%s", sAlias.Data());
1645 tree->SetAlias(aname, query);
1651 TMultiGraph* TStatToolkit::MakeStatusMultGr(TTree * tree, const char * expr, const char * cut, const char * alias, Int_t igr)
1654 // Compute a trending multigraph that shows for which runs a variable has outliers.
1655 // (by MI, Patrick Reichelt)
1657 // format of expr : varname:xaxis (e.g. meanTPCncl:run)
1658 // format of cut : char like in TCut
1659 // format of alias: (1):(varname_Out==0):(varname_Out)[:(varname_Warning):...]
1660 // in the alias, 'varname' will be replaced by its content (e.g. varname_Out -> meanTPCncl_Out)
1661 // note: the aliases 'varname_Out' etc have to be defined by function TStatToolkit::SetStatusAlias(...)
1662 // counter igr is used to shift the multigraph in y when filling a TObjArray.
1664 TObjArray* oaVar = TString(expr).Tokenize(":");
1665 if (oaVar->GetEntries()<2) return 0;
1668 snprintf(varname,50,"%s", oaVar->At(0)->GetName());
1669 snprintf(var_x ,50,"%s", oaVar->At(1)->GetName());
1671 TString sAlias(alias);
1672 sAlias.ReplaceAll("varname",varname);
1673 TObjArray* oaAlias = TString(sAlias.Data()).Tokenize(":");
1674 if (oaAlias->GetEntries()<3) return 0;
1677 TMultiGraph* multGr = new TMultiGraph();
1678 Int_t marArr[6] = {24+igr%2, 20+igr%2, 20+igr%2, 20+igr%2, 22, 23};
1679 Int_t colArr[6] = {kBlack, kBlack, kRed, kOrange, kMagenta, kViolet};
1680 Double_t sizArr[6] = {1.2, 1.1, 1.0, 1.0, 1, 1};
1681 const Int_t ngr = oaAlias->GetEntriesFast();
1682 for (Int_t i=0; i<ngr; i++){
1683 if (i==2) continue; // the Fatal(Out) graph will be added in the end to be plotted on top!
1684 snprintf(query,200, "%f*(%s-0.5):%s", 1.+igr, oaAlias->At(i)->GetName(), var_x);
1685 multGr->Add( (TGraphErrors*) TStatToolkit::MakeGraphSparse(tree,query,cut,marArr[i],colArr[i],sizArr[i]) );
1687 snprintf(query,200, "%f*(%s-0.5):%s", 1.+igr, oaAlias->At(2)->GetName(), var_x);
1688 multGr->Add( (TGraphErrors*) TStatToolkit::MakeGraphSparse(tree,query,cut,marArr[2],colArr[2],sizArr[2]) );
1690 multGr->SetName(varname);
1691 multGr->SetTitle(varname); // used for y-axis labels. // details to be included!
1698 void TStatToolkit::AddStatusPad(TCanvas* c1, Float_t padratio, Float_t bottommargin)
1701 // add pad to bottom of canvas for Status graphs (by Patrick Reichelt)
1702 // call function "DrawStatusGraphs(...)" afterwards
1704 TCanvas* c1_clone = (TCanvas*) c1->Clone("c1_clone");
1708 TPad* pad1 = new TPad("pad1", "pad1", 0., padratio, 1., 1.);
1710 pad1->SetNumber(1); // so it can be called via "c1->cd(1);"
1712 TPad* pad2 = new TPad("pad2", "pad2", 0., 0., 1., padratio);
1715 // draw original canvas into first pad
1717 c1_clone->DrawClonePad();
1718 pad1->SetBottomMargin(0.001);
1719 pad1->SetRightMargin(0.01);
1720 // set up second pad
1723 pad2->SetTopMargin(0);
1724 pad2->SetBottomMargin(bottommargin); // for the long x-axis labels (runnumbers)
1725 pad2->SetRightMargin(0.01);
1729 void TStatToolkit::DrawStatusGraphs(TObjArray* oaMultGr)
1732 // draw Status graphs into active pad of canvas (by MI, Patrick Reichelt)
1733 // ...into bottom pad, if called after "AddStatusPad(...)"
1735 const Int_t nvars = oaMultGr->GetEntriesFast();
1736 TGraph* grAxis = (TGraph*) ((TMultiGraph*) oaMultGr->At(0))->GetListOfGraphs()->At(0);
1737 grAxis->SetMaximum(0.5*nvars+0.5);
1738 grAxis->SetMinimum(0);
1739 grAxis->GetYaxis()->SetLabelSize(0);
1740 Int_t entries = grAxis->GetN();
1741 printf("entries (via GetN()) = %d\n",entries);
1742 grAxis->GetXaxis()->SetLabelSize(5.7*TMath::Min(TMath::Max(5./entries,0.01),0.03));
1743 grAxis->GetXaxis()->LabelsOption("v");
1746 // draw multigraphs & names of status variables on the y axis
1747 for (Int_t i=0; i<nvars; i++){
1748 ((TMultiGraph*) oaMultGr->At(i))->Draw("p");
1749 TLatex* ylabel = new TLatex(-0.1, 0.5*i+0.5, ((TMultiGraph*) oaMultGr->At(i))->GetTitle());
1750 ylabel->SetTextAlign(32); //hor:right & vert:centered
1751 ylabel->SetTextSize(0.025/gPad->GetHNDC());
1757 void TStatToolkit::DrawHistogram(TTree * tree, const char* drawCommand, const char* cuts, const char* histoname, const char* histotitle, Int_t nsigma, Float_t fraction )
1760 // Draw histogram from TTree with robust range
1761 // Only for 1D so far!
1764 // - histoname: name of histogram
1765 // - histotitle: title of histgram
1766 // - fraction: fraction of data to define the robust mean
1767 // - nsigma: nsigma value for range
1770 TString drawStr(drawCommand);
1771 TString cutStr(cuts);
1775 cerr<<" Tree pointer is NULL!"<<endl;
1780 Int_t entries = tree->Draw(drawStr.Data(), cutStr.Data(), "goff");
1781 if (entries == -1) {
1782 cerr<<"TTree draw returns -1"<<endl;
1787 if(tree->GetV1()) dim = 1;
1788 if(tree->GetV2()) dim = 2;
1789 if(tree->GetV3()) dim = 3;
1791 cerr<<"TTree has more than 2 dimensions (not yet supported)"<<endl;
1796 Double_t meanX, rmsX=0;
1797 Double_t meanY, rmsY=0;
1798 TStatToolkit::EvaluateUni(entries, tree->GetV1(),meanX,rmsX, fraction*entries);
1800 TStatToolkit::EvaluateUni(entries, tree->GetV1(),meanY,rmsY, fraction*entries);
1801 TStatToolkit::EvaluateUni(entries, tree->GetV2(),meanX,rmsX, fraction*entries);
1805 hOut = new TH1F(histoname, histotitle, 200, meanX-nsigma*rmsX, meanX+nsigma*rmsX);
1806 for (Int_t i=0; i<entries; i++) hOut->Fill(tree->GetV1()[i]);
1807 hOut->GetXaxis()->SetTitle(tree->GetHistogram()->GetXaxis()->GetTitle());
1811 hOut = new TH2F(histoname, histotitle, 200, meanX-nsigma*rmsX, meanX+nsigma*rmsX,200, meanY-nsigma*rmsY, meanY+nsigma*rmsY);
1812 for (Int_t i=0; i<entries; i++) hOut->Fill(tree->GetV2()[i],tree->GetV1()[i]);
1813 hOut->GetXaxis()->SetTitle(tree->GetHistogram()->GetXaxis()->GetTitle());
1814 hOut->GetYaxis()->SetTitle(tree->GetHistogram()->GetYaxis()->GetTitle());