]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - STAT/TStatToolkit.cxx
Fix for coverity (AdC)
[u/mrichter/AliRoot.git] / STAT / TStatToolkit.cxx
index b403172820c8a9694830ba374c8e6919f52e0f47..4a5fc4480fee4ad34e86aaa6da7e14010e90e28c 100644 (file)
 // 
 // Subset of  matheamtical functions  not included in the TMath
 //
-
-///////////////////////////////////////////////////////////////////////////
+//
+/////////////////////////////////////////////////////////////////////////
 #include "TMath.h"
 #include "Riostream.h"
 #include "TH1F.h"
+#include "TH2F.h"
 #include "TH3.h"
 #include "TF1.h"
 #include "TTree.h"
@@ -37,7 +38,6 @@
 #include "TCanvas.h"
 #include "TLatex.h"
 #include "TCut.h"
-
 //
 // includes neccessary for test functions
 //
 
 #include "TStatToolkit.h"
 
+
+using std::cout;
+using std::cerr;
+using std::endl;
+
  
 ClassImp(TStatToolkit) // Class implementation to enable ROOT I/O
  
@@ -257,25 +262,37 @@ void TStatToolkit::TruncatedMean(const TH1 * his, TVectorD *param, Float_t down,
   if (verbose)  printf("Mean\t%f\t Sigma2\t%f\n", mean,sigma2);
 }
 
-void TStatToolkit::LTM(TH1F * his, TVectorD *param , Float_t fraction,  Bool_t verbose){
+void TStatToolkit::LTM(TH1 * his, TVectorD *param , Float_t fraction,  Bool_t verbose){
+  //
+  // LTM : Trimmed mean on histogram - Modified version for binned data
+  //
+  // Robust statistic to estimate properties of the distribution
+  // See http://en.wikipedia.org/w/index.php?title=Trimmed_estimator&oldid=582847999
   //
-  // LTM
+  // New faster version is under preparation
   //
+  if (!param) return;
+  (*param)[0]=0;
+  (*param)[1]=0;
+  (*param)[2]=0;  
   Int_t nbins    = his->GetNbinsX();
   Int_t nentries = (Int_t)his->GetEntries();
+  if (nentries<=0) return;
   Double_t *data  = new Double_t[nentries];
   Int_t npoints=0;
   for (Int_t ibin=1;ibin<nbins; ibin++){
-    Float_t entriesI = his->GetBinContent(ibin);
-    Float_t xcenter= his->GetBinCenter(ibin);
+    Double_t entriesI = his->GetBinContent(ibin);
+    //Double_t xcenter= his->GetBinCenter(ibin);
+    Double_t x0 =  his->GetXaxis()->GetBinLowEdge(ibin);
+    Double_t w  =  his->GetXaxis()->GetBinWidth(ibin);
     for (Int_t ic=0; ic<entriesI; ic++){
       if (npoints<nentries){
-       data[npoints]= xcenter;
+       data[npoints]= x0+w*Double_t((ic+0.5)/entriesI);
        npoints++;
       }
     }
   }
-  Double_t mean, sigma;
+  Double_t mean, sigma;  
   Int_t npoints2=TMath::Min(Int_t(fraction*Float_t(npoints)),npoints-1);
   npoints2=TMath::Max(Int_t(0.5*Float_t(npoints)),npoints2);
   TStatToolkit::EvaluateUni(npoints, data, mean,sigma,npoints2);
@@ -287,6 +304,149 @@ void TStatToolkit::LTM(TH1F * his, TVectorD *param , Float_t fraction,  Bool_t v
   }
 }
 
+
+void TStatToolkit::MedianFilter(TH1 * his1D, Int_t nmedian){
+  //
+  // Algorithm to filter  histogram
+  // author:  marian.ivanov@cern.ch
+  // Details of algorithm:
+  // http://en.wikipedia.org/w/index.php?title=Median_filter&oldid=582191524
+  // Input parameters:
+  //    his1D - input histogam - to be modiefied by Medianfilter
+  //    nmendian - number of bins in median filter
+  //
+  Int_t nbins    = his1D->GetNbinsX();
+  TVectorD vectorH(nbins);
+  for (Int_t ibin=0; ibin<nbins; ibin++) vectorH[ibin]=his1D->GetBinContent(ibin+1);
+  for (Int_t ibin=0; ibin<nbins; ibin++) {
+    Int_t index0=ibin-nmedian;
+    Int_t index1=ibin+nmedian;
+    if (index0<0) {index1+=-index0; index0=0;}
+    if (index1>=nbins) {index0-=index1-nbins+1; index1=nbins-1;}    
+    Double_t value= TMath::Median(index1-index0,&(vectorH.GetMatrixArray()[index0]));
+    his1D->SetBinContent(ibin+1, value);
+  }  
+}
+
+Bool_t TStatToolkit::LTMHisto(TH1 *his1D, TVectorD &params , Float_t fraction){
+  //
+  // LTM : Trimmed mean on histogram - Modified version for binned data
+  // 
+  // Robust statistic to estimate properties of the distribution
+  // To handle binning error special treatment
+  // for definition of unbinned data see:
+  //     http://en.wikipedia.org/w/index.php?title=Trimmed_estimator&oldid=582847999
+  //
+  // Function parameters:
+  //     his1D   - input histogram
+  //     params  - vector with parameters
+  //             - 0 - area
+  //             - 1 - mean
+  //             - 2 - rms 
+  //             - 3 - error estimate of mean
+  //             - 4 - error estimate of RMS
+  //             - 5 - first accepted bin position
+  //             - 6 - last accepted  bin position
+  //
+  Int_t nbins    = his1D->GetNbinsX();
+  Int_t nentries = (Int_t)his1D->GetEntries();
+  const Double_t kEpsilon=0.0000000001;
+
+  if (nentries<=0) return 0;
+  if (fraction>1) fraction=0;
+  if (fraction<0) return 0;
+  TVectorD vectorX(nbins);
+  TVectorD vectorMean(nbins);
+  TVectorD vectorRMS(nbins);
+  Double_t sumCont=0;
+  for (Int_t ibin0=1; ibin0<=nbins; ibin0++) sumCont+=his1D->GetBinContent(ibin0);
+  //
+  Double_t minRMS=his1D->GetRMS()*10000;
+  Int_t maxBin=0;
+  //
+  for (Int_t ibin0=1; ibin0<nbins; ibin0++){
+    Double_t sum0=0, sum1=0, sum2=0;
+    Int_t ibin1=ibin0;
+    for ( ibin1=ibin0; ibin1<nbins; ibin1++){
+      Double_t cont=his1D->GetBinContent(ibin1);
+      Double_t x= his1D->GetBinCenter(ibin1);
+      sum0+=cont;
+      sum1+=cont*x;
+      sum2+=cont*x*x;
+      if ( (ibin0!=ibin1) && sum0>=fraction*sumCont) break;
+    }
+    vectorX[ibin0]=his1D->GetBinCenter(ibin0);
+    if (sum0<fraction*sumCont) continue;
+    //
+    // substract fractions of bin0 and bin1 to keep sum0=fration*sumCont
+    //
+    Double_t diff = sum0-fraction*sumCont;
+    Double_t mean = sum1/sum0;
+    //
+    Double_t x0=his1D->GetBinCenter(ibin0);
+    Double_t x1=his1D->GetBinCenter(ibin1);
+    Double_t y0=his1D->GetBinContent(ibin0);
+    Double_t y1=his1D->GetBinContent(ibin1);
+    //
+    Double_t d = y0+y1-diff;    //enties to keep 
+    Double_t w0=0,w1=0;
+    if (y0<=kEpsilon&&y1>kEpsilon){
+      w1=d/y1;
+    } 
+    if (y1<=kEpsilon&&y0>kEpsilon){
+      w0=d/y0;
+    }
+    if (y0>kEpsilon && y1>kEpsilon && x1>x0  ){
+      w0 = (d*(x1-mean))/((x1-x0)*y0);
+      w1 = (d-y0*w0)/y1;
+      //
+      if (w0>1) {w1+=(w0-1)*y0/y1; w0=1;}
+      if (w1>1) {w0+=(w1-1)*y1/y0; w1=1;}
+    }  
+    if ( (x1>x0) &&TMath::Abs(y0*w0+y1*w1-d)>kEpsilon*sum0){
+      printf(" TStatToolkit::LTMHisto error\n");
+    }
+    sum0-=y0+y1;
+    sum1-=y0*x0;
+    sum1-=y1*x1;
+    sum2-=y0*x0*x0;
+    sum2-=y1*x1*x1;
+    //
+    Double_t xx0=his1D->GetXaxis()->GetBinUpEdge(ibin0)-0.5*w0*his1D->GetBinWidth(ibin0);
+    Double_t xx1=his1D->GetXaxis()->GetBinLowEdge(ibin1)+0.5*w1*his1D->GetBinWidth(ibin1);
+    sum0+=y0*w0+y1*w1;
+    sum1+=y0*w0*xx0;
+    sum1+=y1*w1*xx1;
+    sum2+=y0*w0*xx0*xx0;
+    sum2+=y1*w1*xx1*xx1;
+
+    //
+    // choose the bin with smallest rms
+    //
+    if (sum0>0){
+      vectorMean[ibin0]=sum1/sum0;
+      vectorRMS[ibin0]=TMath::Sqrt(TMath::Abs(sum2/sum0-vectorMean[ibin0]*vectorMean[ibin0]));
+      if (vectorRMS[ibin0]<minRMS){
+       minRMS=vectorRMS[ibin0];
+       params[0]=sum0;
+       params[1]=vectorMean[ibin0];
+       params[2]=vectorRMS[ibin0];
+       params[3]=vectorRMS[ibin0]/TMath::Sqrt(sumCont*fraction);
+       params[4]=0; // what is the formula for error of RMS???
+       params[5]=ibin0;
+       params[6]=ibin1;
+       params[7]=his1D->GetBinCenter(ibin0);
+       params[8]=his1D->GetBinCenter(ibin1);
+       maxBin=ibin0;
+      }
+    }else{
+      break;
+    }
+  }
+  return kTRUE;
+}
+
+
 Double_t  TStatToolkit::FitGaus(TH1* his, TVectorD *param, TMatrixD */*matrix*/, Float_t xmin, Float_t xmax, Bool_t verbose){
   //
   //  Fit histogram with gaussian function
@@ -577,7 +737,7 @@ void TStatToolkit::TestGausFit(Int_t nhistos){
   // ROOT gauss fit
   //  nhistos - number of histograms to be used for test
   //
-  TTreeSRedirector *pcstream = new TTreeSRedirector("fitdebug.root");
+  TTreeSRedirector *pcstream = new TTreeSRedirector("fitdebug.root","recreate");
   
   Float_t  *xTrue = new Float_t[nhistos];
   Float_t  *sTrue = new Float_t[nhistos];
@@ -603,7 +763,7 @@ void TStatToolkit::TestGausFit(Int_t nhistos){
     h1f[i]->FillRandom("myg");
   }
   
-  TStopwatch s;
+  TStopwatch s; 
   s.Start();
   //standard gaus fit
   for (Int_t i=0; i<nhistos; i++){
@@ -680,7 +840,7 @@ TGraph2D * TStatToolkit::MakeStat2D(TH3 * his, Int_t delta0, Int_t delta1, Int_t
       if (type==0) stat = projection->GetMean();
       if (type==1) stat = projection->GetRMS();
       if (type==2 || type==3){
-       TVectorD vec(3);
+       TVectorD vec(10);
        TStatToolkit::LTM((TH1F*)projection,&vec,0.7);
        if (type==2) stat= vec[1];
        if (type==3) stat= vec[0];      
@@ -697,45 +857,77 @@ TGraph2D * TStatToolkit::MakeStat2D(TH3 * his, Int_t delta0, Int_t delta1, Int_t
   return graph;
 }
 
-TGraph * TStatToolkit::MakeStat1D(TH3 * his, Int_t delta1, Int_t type){
-  //
-  //
-  //
-  // delta - number of bins to integrate
-  // type - 0 - mean value
-
+TGraphErrors * TStatToolkit::MakeStat1D(TH2 * his, Int_t deltaBin, Double_t fraction, Int_t returnType, Int_t markerStyle, Int_t markerColor){
+  //
+  // function to retrieve the "mean and RMS estimate" of 2D histograms
+  //     
+  // Robust statistic to estimate properties of the distribution
+  // See http://en.wikipedia.org/wiki/Trimmed_estimator
+  //
+  // deltaBin - number of bins to integrate (bin+-deltaBin)
+  // fraction - fraction of values for the LTM and for the gauss fit
+  // returnType - 
+  //        0 - mean value
+  //        1 - RMS
+  //        2 - LTM mean
+  //        3 - LTM sigma
+  //        4 - Gaus fit mean  - on LTM range
+  //        5 - Gaus fit sigma - on LTM  range
+  // 
   TAxis * xaxis  = his->GetXaxis();
-  TAxis * yaxis  = his->GetYaxis();
-  //  TAxis * zaxis  = his->GetZaxis();
   Int_t   nbinx  = xaxis->GetNbins();
-  Int_t   nbiny  = yaxis->GetNbins();
   char name[1000];
   Int_t icount=0;
-  TGraph  *graph = new TGraph(nbinx);
+  //
+  TVectorD vecX(nbinx);
+  TVectorD vecXErr(nbinx);
+  TVectorD vecY(nbinx);
+  TVectorD vecYErr(nbinx);
+  //
   TF1 f1("f1","gaus");
-  for (Int_t ix=0; ix<nbinx;ix++){
-    Float_t xcenter = xaxis->GetBinCenter(ix); 
-    //    Float_t ycenter = yaxis->GetBinCenter(iy); 
+  TVectorD vecLTM(10);
+
+  for (Int_t jx=1; jx<=nbinx;jx++){
+    Int_t ix=jx-1;
+    Float_t xcenter = xaxis->GetBinCenter(jx); 
     snprintf(name,1000,"%s_%d",his->GetName(), ix);
-    TH1 *projection = his->ProjectionZ(name,ix-delta1,ix+delta1,0,nbiny);
-    Float_t stat= 0;
-    if (type==0) stat = projection->GetMean();
-    if (type==1) stat = projection->GetRMS();
-    if (type==2 || type==3){
-      TVectorD vec(3);
-       TStatToolkit::LTM((TH1F*)projection,&vec,0.7);
-       if (type==2) stat= vec[1];
-       if (type==3) stat= vec[0];      
+    TH1 *projection = his->ProjectionY(name,TMath::Max(jx-deltaBin,1),TMath::Min(jx+deltaBin,nbinx));
+    Double_t stat= 0;
+    Double_t err =0;
+    TStatToolkit::LTMHisto((TH1F*)projection,vecLTM,fraction);  
+    //
+    if (returnType==0) {
+      stat = projection->GetMean();
+      err  = projection->GetMeanError();
     }
-    if (type==4|| type==5){
-      projection->Fit(&f1);
-      if (type==4) stat= f1.GetParameter(1);
-      if (type==5) stat= f1.GetParameter(2);
+    if (returnType==1) {
+      stat = projection->GetRMS();
+      err = projection->GetRMSError();
     }
-      //printf("%d\t%f\t%f\t%f\n", icount,xcenter, ycenter, stat);
-    graph->SetPoint(icount,xcenter, stat);
+    if (returnType==2 || returnType==3){
+      if (returnType==2) {stat= vecLTM[1];  err =projection->GetRMSError();}
+       if (returnType==3) {stat= vecLTM[2];     err =projection->GetRMSError();}
+    }
+    if (returnType==4|| returnType==5){
+      projection->Fit(&f1,"QN","QN", vecLTM[7], vecLTM[8]);
+      if (returnType==4) {
+       stat= f1.GetParameter(1);
+       err=f1.GetParError(1);
+      }
+      if (returnType==5) {
+       stat= f1.GetParameter(2);
+       err=f1.GetParError(2);
+      }
+    }
+    vecX[icount]=xcenter;
+    vecY[icount]=stat;
+    vecYErr[icount]=err;
     icount++;
+    delete projection;
   }
+  TGraphErrors  *graph = new TGraphErrors(icount,vecX.GetMatrixArray(), vecY.GetMatrixArray(),0, vecYErr.GetMatrixArray());
+  graph->SetMarkerStyle(markerStyle);
+  graph->SetMarkerColor(markerColor);
   return graph;
 }
 
@@ -1214,79 +1406,143 @@ TString  TStatToolkit::MakeFitString(const TString &input, const TVectorD &param
   return result;
 }
 
-
-TGraph * TStatToolkit::MakeGraphSparse(TTree * tree, const char * expr, const char * cut, Int_t mstyle, Int_t mcolor, Float_t msize, Float_t offset){
+TGraphErrors * TStatToolkit::MakeGraphErrors(TTree * tree, const char * expr, const char * cut,  Int_t mstyle, Int_t mcolor, Float_t msize, Float_t offset){
   //
-  // Make a sparse draw of the variables
-  // Writen by Weilin.Yu
+  // Query a graph errors
+  // return TGraphErrors specified by expr and cut 
+  // Example  usage TStatToolkit::MakeGraphError(tree,"Y:X:ErrY","X>0", 25,2,0.4)
+  // tree   - tree with variable
+  // expr   - examp 
   const Int_t entries =  tree->Draw(expr,cut,"goff");
   if (entries<=0) {
     TStatToolkit t;
-    t.Error("TStatToolkit::MakeGraphSparse",Form("Empty or Not valid expression (%s) or cut *%s)", expr,cut));
+    t.Error("TStatToolkit::MakeGraphError",Form("Empty or Not valid expression (%s) or cut *%s)", expr,cut));
     return 0;
   }
-  //  TGraph * graph = (TGraph*)gPad->GetPrimitive("Graph"); // 2D
-  TGraph * graph = 0;
-  if (tree->GetV3()) graph = new TGraphErrors (entries, tree->GetV2(),tree->GetV1(),0,tree->GetV3());
-  graph =  new TGraphErrors (entries, tree->GetV2(),tree->GetV1(),0,0);
+  if (  tree->GetV2()==0){
+    TStatToolkit t;
+    t.Error("TStatToolkit::MakeGraphError",Form("Not valid expression (%s) ", expr));
+    return 0;
+  }
+  TGraphErrors * graph=0;
+  if ( tree->GetV3()!=0){
+    graph = new TGraphErrors (entries, tree->GetV2(),tree->GetV1(),0,tree->GetV3());
+  }else{
+    graph = new TGraphErrors (entries, tree->GetV2(),tree->GetV1(),0,0);
+  }
   graph->SetMarkerStyle(mstyle); 
   graph->SetMarkerColor(mcolor);
+  graph->SetLineColor(mcolor);
+  graph->SetTitle(expr);
+  TString chstring(expr);
+  TObjArray *charray = chstring.Tokenize(":");
+  graph->GetXaxis()->SetTitle(charray->At(1)->GetName());
+  graph->GetYaxis()->SetTitle(charray->At(0)->GetName());
+  delete charray;
+  if (msize>0) graph->SetMarkerSize(msize);
+  for(Int_t i=0;i<graph->GetN();i++) graph->GetX()[i]+=offset;
+  return graph;
+  
+}
+
+
+TGraph * TStatToolkit::MakeGraphSparse(TTree * tree, const char * expr, const char * cut, Int_t mstyle, Int_t mcolor, Float_t msize, Float_t offset){
+  //
+  // Make a sparse draw of the variables
+  // Format of expr : Var:Run or Var:Run:ErrorY or Var:Run:ErrorY:ErrorX
+  // offset : points can slightly be shifted in x for better visibility with more graphs
+  //
+  // Written by Weilin.Yu
+  // updated & merged with QA-code by Patrick Reichelt
   //
+  const Int_t entries = tree->Draw(expr,cut,"goff");
+  if (entries<=0) {
+    TStatToolkit t;
+    t.Error("TStatToolkit::MakeGraphSparse",Form("Empty or Not valid expression (%s) or cut (%s)", expr, cut));
+    return 0;
+  }
+  //  TGraph * graph = (TGraph*)gPad->GetPrimitive("Graph"); // 2D
+
+  Double_t *graphY, *graphX;
+  graphY = tree->GetV1();
+  graphX = tree->GetV2();
+
+  // sort according to run number
   Int_t *index = new Int_t[entries*4];
-  TMath::Sort(entries,graph->GetX(),index,kFALSE);
-  
-  Double_t *tempArray = new Double_t[entries];
+  TMath::Sort(entries,graphX,index,kFALSE);
 
+  // define arrays for the new graph
+  Double_t *unsortedX = new Double_t[entries];
+  Int_t *runNumber = new Int_t[entries];
   Double_t count = 0.5;
-  Double_t  *vrun = new Double_t[entries];
+
+  // evaluate arrays for the new graph according to the run-number
   Int_t icount=0;
-  //
-  tempArray[index[0]] = count;
-  vrun[0] = graph->GetX()[index[0]];
-  for(Int_t i=1;i<entries;i++){
-    if(graph->GetX()[index[i]]==graph->GetX()[index[i-1]])
-      tempArray[index[i]] = count; 
-    else if(graph->GetX()[index[i]]!=graph->GetX()[index[i-1]]){
+  //first entry
+  unsortedX[index[0]] = count;
+  runNumber[0] = graphX[index[0]];
+  // loop the rest of entries
+  for(Int_t i=1;i<entries;i++)
+  {
+    if(graphX[index[i]]==graphX[index[i-1]])
+      unsortedX[index[i]] = count;
+    else if(graphX[index[i]]!=graphX[index[i-1]]){
       count++;
       icount++;
-      tempArray[index[i]] = count;
-      vrun[icount]=graph->GetX()[index[i]];
+      unsortedX[index[i]] = count;
+      runNumber[icount]=graphX[index[i]];
     }
   }
-  
+
+  // count the number of xbins (run-wise) for the new graph
   const Int_t newNbins = int(count+0.5);
   Double_t *newBins = new Double_t[newNbins+1];
   for(Int_t i=0; i<=count+1;i++){
     newBins[i] = i;
   }
-  
+
+  // define and fill the new graph
   TGraph *graphNew = 0;
-  if (tree->GetV3()) graphNew = new TGraphErrors(entries,tempArray,graph->GetY(),0,tree->GetV3());
-  else
-    graphNew = new TGraphErrors(entries,tempArray,graph->GetY(),0,0);
+  if (tree->GetV3()) {
+    if (tree->GetV4()) {
+      graphNew = new TGraphErrors(entries,unsortedX,graphY,tree->GetV4(),tree->GetV3());
+    }
+    else { graphNew = new TGraphErrors(entries,unsortedX,graphY,0,tree->GetV3()); }
+  }
+  else { graphNew = new TGraphErrors(entries,unsortedX,graphY,0,0); }
+  // with "Set(...)", the x-axis is being sorted
   graphNew->GetXaxis()->Set(newNbins,newBins);
-  
+
+  // set the bins for the x-axis, apply shifting of points
   Char_t xName[50];
   for(Int_t i=0;i<count;i++){
-    snprintf(xName,50,"%d",Int_t(vrun[i]));
+    snprintf(xName,50,"%d",runNumber[i]);
     graphNew->GetXaxis()->SetBinLabel(i+1,xName);
+    graphNew->GetX()[i]+=offset;
   }
+
   graphNew->GetHistogram()->SetTitle("");
-  graphNew->SetMarkerStyle(mstyle); 
+  graphNew->SetMarkerStyle(mstyle);
   graphNew->SetMarkerColor(mcolor);
   if (msize>0) graphNew->SetMarkerSize(msize);
-  for(Int_t i=0;i<graphNew->GetN();i++) graphNew->GetX()[i]+=offset;
-  delete [] tempArray;
+  delete [] unsortedX;
+  delete [] runNumber;
   delete [] index;
   delete [] newBins;
-  delete [] vrun;
+  // 
+  graphNew->SetTitle(expr);
+  TString chstring(expr);
+  TObjArray *charray = chstring.Tokenize(":");
+  graphNew->GetXaxis()->SetTitle(charray->At(1)->GetName());
+  graphNew->GetYaxis()->SetTitle(charray->At(0)->GetName());
+  delete charray;
   return graphNew;
 }
 
 
 
 //
-// function used for the trending
+// functions used for the trending
 //
 
 Int_t  TStatToolkit::MakeStatAlias(TTree * tree, const char * expr, const char * cut, const char * alias) 
@@ -1301,21 +1557,20 @@ Int_t  TStatToolkit::MakeStatAlias(TTree * tree, const char * expr, const char *
   // Output - return number of entries used to define variable
   // In addition mean, rms, median, and robust mean and rms (choosing fraction of data with smallest RMS)
   // 
-  // Example usage:
-  /*
-    Example usage to create the robust estimators for  variable expr="QA.TPC.CPass1.meanTPCncl" and create a corresponding
-    aliases with the prefix alias[0]="ncl", calculated using fraction alias[1]="0.90"
-
-    TStatToolkit::MakeStatAlias(tree,"QA.TPC.CPass1.meanTPCncl","QA.TPC.CPass1.status>0","ncl:0.9");
-    root [4] tree->GetListOfAliases().Print()
-    OBJ: TNamed    ncl_Mean        (122.120387+0)
-    OBJ: TNamed    ncl_RMS (33.509623+0)
-    OBJ: TNamed    ncl_Median      (130.964333+0)
-    OBJ: TNamed    ncl_Mean90      (131.503862+0)
-    OBJ: TNamed    ncl_RMS90       (3.738260+0)    
-   */
+  /* Example usage:
+     1.) create the robust estimators for variable expr="QA.TPC.CPass1.meanTPCncl" and create a corresponding
+     aliases with the prefix alias[0]="ncl", calculated using fraction alias[1]="0.90"
+
+     TStatToolkit::MakeStatAlias(tree,"QA.TPC.CPass1.meanTPCncl","QA.TPC.CPass1.status>0","ncl:0.9");
+     root [4] tree->GetListOfAliases().Print()
+     OBJ: TNamed    ncl_Median      (130.964333+0)
+     OBJ: TNamed    ncl_Mean        (122.120387+0)
+     OBJ: TNamed    ncl_RMS         (33.509623+0)
+     OBJ: TNamed    ncl_Mean90      (131.503862+0)
+     OBJ: TNamed    ncl_RMS90       (3.738260+0)    
+  */
   // 
-  Int_t entries= tree->Draw(expr,cut,"goff");
+  Int_t entries = tree->Draw(expr,cut,"goff");
   if (entries<=1){
     printf("Expression or cut not valid:\t%s\t%s\n", expr, cut);
     return 0;
@@ -1326,14 +1581,14 @@ Int_t  TStatToolkit::MakeStatAlias(TTree * tree, const char * expr, const char *
   Float_t entryFraction = atof( oaAlias->At(1)->GetName() );
   //
   Double_t median = TMath::Median(entries,tree->GetV1());
-  Double_t mean = TMath::Mean(entries,tree->GetV1());
+  Double_t mean   = TMath::Mean(entries,tree->GetV1());
   Double_t rms    = TMath::RMS(entries,tree->GetV1());
   Double_t meanEF=0, rmsEF=0;
   TStatToolkit::EvaluateUni(entries, tree->GetV1(), meanEF, rmsEF, entries*entryFraction);
   //
+  tree->SetAlias(Form("%s_Median",oaAlias->At(0)->GetName()), Form("(%f+0)",median));
   tree->SetAlias(Form("%s_Mean",oaAlias->At(0)->GetName()), Form("(%f+0)",mean));
   tree->SetAlias(Form("%s_RMS",oaAlias->At(0)->GetName()), Form("(%f+0)",rms));
-  tree->SetAlias(Form("%s_Median",oaAlias->At(0)->GetName()), Form("(%f+0)",median));
   tree->SetAlias(Form("%s_Mean%d",oaAlias->At(0)->GetName(),Int_t(entryFraction*100)), Form("(%f+0)",meanEF));
   tree->SetAlias(Form("%s_RMS%d",oaAlias->At(0)->GetName(),Int_t(entryFraction*100)), Form("(%f+0)",rmsEF));
   delete oaAlias; 
@@ -1350,65 +1605,63 @@ Int_t  TStatToolkit::SetStatusAlias(TTree * tree, const char * expr, const char
   // format of cut  :  char like in TCut
   // format of alias:  alias:query:entryFraction(EF) (fraction of entries used for uniformity evaluation)
   //            e.g.:  varname_Out:(abs(varname-meanEF)>6.*rmsEF):0.8
-  // available internal variables are: 'varname, median, meanEF, rms, rmsEF'
-  // in the alias, 'varname' will be replaced by its content, and 'EF' by the percentage (e.g. meanEF -> mean80)
+  // available internal variables are: 'varname, Median, Mean, MeanEF, RMS, RMSEF'
+  // in the alias, 'varname' will be replaced by its content, and 'EF' by the percentage (e.g. MeanEF -> Mean80)
   //
   /* Example usage:
-     1.) Define robust  mean
-
-     TStatToolkit::SetStatusAlias(tree, "meanTPCnclF", "meanTPCnclF>0", "meanTPCnclF_meanEF:meanEF:0.80") ;
-     -->
+     1.) Define robust mean (possible, but easier done with TStatToolkit::MakeStatAlias(...)) 
+     TStatToolkit::SetStatusAlias(tree, "meanTPCnclF", "meanTPCnclF>0", "meanTPCnclF_MeanEF:MeanEF:0.80") ;
      root [10] tree->GetListOfAliases()->Print()
                Collection name='TList', class='TList', size=1
-               OBJ: TNamed    meanTPCnclF_mean80      0.899308
+               OBJ: TNamed    meanTPCnclF_Mean80      0.899308
      2.) create alias outlyers  - 6 sigma cut
-      TStatToolkit::SetStatusAlias(tree, "meanTPCnclF", "meanTPCnclF>0", "meanTPCnclF_Out:(abs(meanTPCnclF-meanEF)>6.*rmsEF):0.8")          meanTPCnclF_Out ==> (abs(meanTPCnclF-0.899308)>6.*0.016590)
+     TStatToolkit::SetStatusAlias(tree, "meanTPCnclF", "meanTPCnclF>0", "meanTPCnclF_Out:(abs(meanTPCnclF-MeanEF)>6.*RMSEF):0.8")
+     meanTPCnclF_Out ==> (abs(meanTPCnclF-0.899308)>6.*0.016590)
      3.) the same functionality as in 2.)
-         TStatToolkit::SetStatusAlias(tree, "meanTPCnclF", "meanTPCnclF>0", "varname_Out2:(abs(varname-meanEF)>6.*rmsEF):0.8") 
-        ->
-         meanTPCnclF_Out2 ==> (abs(meanTPCnclF-0.899308)>6.*0.016590)
+     TStatToolkit::SetStatusAlias(tree, "meanTPCnclF", "meanTPCnclF>0", "varname_Out2:(abs(varname-MeanEF)>6.*RMSEF):0.8") 
+     meanTPCnclF_Out2 ==> (abs(meanTPCnclF-0.899308)>6.*0.016590)
   */
   //
+  Int_t entries = tree->Draw(expr,cut,"goff");
+  if (entries<1){
+    printf("Expression or cut not valid:\t%s\t%s\n", expr, cut);
+    return 0;
+  }
+  //
   TObjArray* oaVar = TString(expr).Tokenize(":");
   char varname[50];
-  //char var_x[50];
   snprintf(varname,50,"%s", oaVar->At(0)->GetName());
-  //snprintf(var_x  ,50,"%s", oaVar->At(1)->GetName());
-  TCut userCut(cut);
+  //
   TObjArray* oaAlias = TString(alias).Tokenize(":");
+  if (oaAlias->GetEntries()<3) return 0;
   Float_t entryFraction = atof( oaAlias->At(2)->GetName() );
   //
-  Int_t entries = tree->Draw(expr, userCut, "goff");
-  if (entries<=1){
-    printf("Expression or cut not valid:\t%s\t%s\n", expr, cut);
-    return 0;
-  }
-  //printf("  entries (via tree->Draw(...)) = %d\n",entries);
-  Double_t mean = TMath::Mean(entries,tree->GetV1());
   Double_t median = TMath::Median(entries,tree->GetV1());
+  Double_t mean   = TMath::Mean(entries,tree->GetV1());
   Double_t rms    = TMath::RMS(entries,tree->GetV1());
   Double_t meanEF=0, rmsEF=0;
   TStatToolkit::EvaluateUni(entries, tree->GetV1(), meanEF, rmsEF, entries*entryFraction);
-  //printf("%s\t%f\t%f\t%f\t%f\n",varname, median, meanEF, rms, rmsEF);
   //
   TString sAlias( oaAlias->At(0)->GetName() );
   sAlias.ReplaceAll("varname",varname);
-  sAlias.ReplaceAll("MeanEF", Form("mean%1.0f",entryFraction*100) );
-  sAlias.ReplaceAll("RMSEF",  Form("rms%1.0f",entryFraction*100) );
+  sAlias.ReplaceAll("MeanEF", Form("Mean%1.0f",entryFraction*100) );
+  sAlias.ReplaceAll("RMSEF",  Form("RMS%1.0f",entryFraction*100) );
   TString sQuery( oaAlias->At(1)->GetName() );
   sQuery.ReplaceAll("varname",varname);
   sQuery.ReplaceAll("MeanEF", Form("%f",meanEF) );
-  sQuery.ReplaceAll("RMSEF",  Form("%f",rmsEF) ); //make sure to replace 'rmsEF' before 'rms'...
+  sQuery.ReplaceAll("RMSEF",  Form("%f",rmsEF) ); //make sure to replace 'RMSEF' before 'RMS'...
   sQuery.ReplaceAll("Median", Form("%f",median) );
+  sQuery.ReplaceAll("Mean",   Form("%f",mean) );
   sQuery.ReplaceAll("RMS",    Form("%f",rms) );
-  sQuery.ReplaceAll("Mean",    Form("%f",mean) );
-  printf("%s\n", sQuery.Data());
+  printf("define alias:\t%s = %s\n", sAlias.Data(), sQuery.Data());
   //
   char query[200];
   char aname[200];
   snprintf(query,200,"%s", sQuery.Data());
   snprintf(aname,200,"%s", sAlias.Data());
   tree->SetAlias(aname, query);
+  delete oaVar;
+  delete oaAlias;
   return entries;
 }
 
@@ -1422,18 +1675,20 @@ TMultiGraph*  TStatToolkit::MakeStatusMultGr(TTree * tree, const char * expr, co
   // format of cut  :  char like in TCut
   // format of alias:  (1):(varname_Out==0):(varname_Out)[:(varname_Warning):...]
   // in the alias, 'varname' will be replaced by its content (e.g. varname_Out -> meanTPCncl_Out)
-  // note: the aliases 'varname_Out' etc have to be defined by function 'SetStatisticAlias(...)'
+  // note: the aliases 'varname_Out' etc have to be defined by function TStatToolkit::SetStatusAlias(...)
   // counter igr is used to shift the multigraph in y when filling a TObjArray.
   //
   TObjArray* oaVar = TString(expr).Tokenize(":");
+  if (oaVar->GetEntries()<2) return 0;
   char varname[50];
   char var_x[50];
   snprintf(varname,50,"%s", oaVar->At(0)->GetName());
   snprintf(var_x  ,50,"%s", oaVar->At(1)->GetName());
-  TCut userCut(cut);
+  //
   TString sAlias(alias);
   sAlias.ReplaceAll("varname",varname);
   TObjArray* oaAlias = TString(sAlias.Data()).Tokenize(":");
+  if (oaAlias->GetEntries()<3) return 0;
   //
   char query[200];
   TMultiGraph* multGr = new TMultiGraph();
@@ -1444,13 +1699,15 @@ TMultiGraph*  TStatToolkit::MakeStatusMultGr(TTree * tree, const char * expr, co
   for (Int_t i=0; i<ngr; i++){
     if (i==2) continue; // the Fatal(Out) graph will be added in the end to be plotted on top!
     snprintf(query,200, "%f*(%s-0.5):%s", 1.+igr, oaAlias->At(i)->GetName(), var_x);
-    multGr->Add( (TGraphErrors*) TStatToolkit::MakeGraphSparse(tree,query,userCut,marArr[i],colArr[i],sizArr[i]) );
+    multGr->Add( (TGraphErrors*) TStatToolkit::MakeGraphSparse(tree,query,cut,marArr[i],colArr[i],sizArr[i]) );
   }
   snprintf(query,200, "%f*(%s-0.5):%s", 1.+igr, oaAlias->At(2)->GetName(), var_x);
-  multGr->Add( (TGraphErrors*) TStatToolkit::MakeGraphSparse(tree,query,userCut,marArr[2],colArr[2],sizArr[2]) );
+  multGr->Add( (TGraphErrors*) TStatToolkit::MakeGraphSparse(tree,query,cut,marArr[2],colArr[2],sizArr[2]) );
   //
   multGr->SetName(varname);
   multGr->SetTitle(varname); // used for y-axis labels. // details to be included!
+  delete oaVar;
+  delete oaAlias;
   return multGr;
 }
 
@@ -1512,3 +1769,67 @@ void  TStatToolkit::DrawStatusGraphs(TObjArray* oaMultGr)
     ylabel->Draw();
   }
 }
+
+
+TH1* TStatToolkit::DrawHistogram(TTree * tree, const char* drawCommand, const char* cuts, const char* histoname, const char* histotitle, Int_t nsigma, Float_t fraction )
+{
+  //
+  // Draw histogram from TTree with robust range
+  // Only for 1D so far!
+  // 
+  // Parameters:
+  // - histoname:  name of histogram
+  // - histotitle: title of histgram
+  // - fraction:   fraction of data to define the robust mean
+  // - nsigma:     nsigma value for range
+  //
+
+   TString drawStr(drawCommand);
+   TString cutStr(cuts);
+   Int_t dim = 1;
+
+   if(!tree) {
+     cerr<<" Tree pointer is NULL!"<<endl;
+     return 0;
+   }
+
+   // get entries
+   Int_t entries = tree->Draw(drawStr.Data(), cutStr.Data(), "goff");
+   if (entries == -1) {
+     cerr<<"TTree draw returns -1"<<endl;
+     return 0;
+   }
+
+   // get dimension
+   if(tree->GetV1()) dim = 1;
+   if(tree->GetV2()) dim = 2;
+   if(tree->GetV3()) dim = 3;
+   if(dim > 2){
+     cerr<<"TTree has more than 2 dimensions (not yet supported)"<<endl;
+     return 0;
+   }
+
+   // draw robust
+   Double_t meanX, rmsX=0;
+   Double_t meanY, rmsY=0;
+   TStatToolkit::EvaluateUni(entries, tree->GetV1(),meanX,rmsX, fraction*entries);
+   if(dim==2){
+     TStatToolkit::EvaluateUni(entries, tree->GetV1(),meanY,rmsY, fraction*entries);
+     TStatToolkit::EvaluateUni(entries, tree->GetV2(),meanX,rmsX, fraction*entries);
+   }
+   TH1* hOut;
+   if(dim==1){
+     hOut = new TH1F(histoname, histotitle, 200, meanX-nsigma*rmsX, meanX+nsigma*rmsX);
+     for (Int_t i=0; i<entries; i++) hOut->Fill(tree->GetV1()[i]);
+     hOut->GetXaxis()->SetTitle(tree->GetHistogram()->GetXaxis()->GetTitle());
+     hOut->Draw();
+   }
+   else if(dim==2){
+     hOut = new TH2F(histoname, histotitle, 200, meanX-nsigma*rmsX, meanX+nsigma*rmsX,200, meanY-nsigma*rmsY, meanY+nsigma*rmsY);
+     for (Int_t i=0; i<entries; i++) hOut->Fill(tree->GetV2()[i],tree->GetV1()[i]);
+     hOut->GetXaxis()->SetTitle(tree->GetHistogram()->GetXaxis()->GetTitle());
+     hOut->GetYaxis()->SetTitle(tree->GetHistogram()->GetYaxis()->GetTitle());
+     hOut->Draw("colz");
+   }
+   return hOut;
+}