#include "TMath.h"
#include "Riostream.h"
#include "TH1F.h"
+#include "TH2F.h"
#include "TH3.h"
#include "TF1.h"
#include "TTree.h"
#include "TChain.h"
#include "TObjString.h"
#include "TLinearFitter.h"
+#include "TGraph2D.h"
+#include "TGraph.h"
+#include "TGraphErrors.h"
+#include "TMultiGraph.h"
+#include "TCanvas.h"
+#include "TLatex.h"
+#include "TCut.h"
//
// includes neccessary for test functions
}
Double_t norm = 1./Double_t(hh);
- Double_t norm2 = 1./Double_t(hh-1);
+ Double_t norm2 = (hh-1)>0 ? 1./Double_t(hh-1):1;
for (Int_t i=hh; i<nvectors; i++){
Double_t cmean = sumx*norm;
Double_t csigma = (sumx2 - hh*cmean*cmean)*norm2;
Int_t * sindexS = new Int_t[n]; // temp array for sorting
Int_t * sindexF = new Int_t[2*n];
- for (Int_t i=0;i<n;i++) sindexF[i]=0;
+ for (Int_t i=0;i<n;i++) sindexS[i]=0;
+ for (Int_t i=0;i<2*n;i++) sindexF[i]=0;
//
TMath::Sort(n,inlist, sindexS, down);
Int_t last = inlist[sindexS[0]];
}
//___TStatToolkit__________________________________________________________________________
-void TStatToolkit::TruncatedMean(TH1F * his, TVectorD *param, Float_t down, Float_t up, Bool_t verbose){
+void TStatToolkit::TruncatedMean(const TH1 * his, TVectorD *param, Float_t down, Float_t up, Bool_t verbose){
//
//
//
}
}
-Double_t TStatToolkit::FitGaus(TH1F* his, TVectorD *param, TMatrixD *matrix, Float_t xmin, Float_t xmax, Bool_t verbose){
+Double_t TStatToolkit::FitGaus(TH1* his, TVectorD *param, TMatrixD */*matrix*/, Float_t xmin, Float_t xmax, Bool_t verbose){
//
// Fit histogram with gaussian function
//
Double_t chi2 = fitter.GetChisquare()/Float_t(npoints);
//fitter.GetParameters();
if (!param) param = new TVectorD(3);
- if (!matrix) matrix = new TMatrixD(3,3);
+ // if (!matrix) matrix = new TMatrixD(3,3); // Covariance matrix to be implemented
(*param)[1] = par[1]/(-2.*par[2]);
(*param)[2] = 1./TMath::Sqrt(TMath::Abs(-2.*par[2]));
(*param)[0] = TMath::Exp(par[0]+ par[1]* (*param)[1] + par[2]*(*param)[1]*(*param)[1]);
return chi2;
}
-Double_t TStatToolkit::FitGaus(Float_t *arr, Int_t nBins, Float_t xMin, Float_t xMax, TVectorD *param, TMatrixD *matrix, Bool_t verbose){
+Double_t TStatToolkit::FitGaus(Float_t *arr, Int_t nBins, Float_t xMin, Float_t xMax, TVectorD *param, TMatrixD */*matrix*/, Bool_t verbose){
//
// Fit histogram with gaussian function
//
fitter.ClearPoints();
TVectorD par(3);
TVectorD sigma(3);
- TMatrixD A(3,3);
+ TMatrixD matA(3,3);
TMatrixD b(3,1);
Float_t rms = TMath::RMS(nBins,arr);
Float_t max = TMath::MaxElement(nBins,arr);
Float_t val = TMath::Log(Float_t(entriesI));
fitter.AddPoint(&xcenter,val,error);
if (npoints<3){
- A(npoints,0)=1;
- A(npoints,1)=xcenter;
- A(npoints,2)=xcenter*xcenter;
+ matA(npoints,0)=1;
+ matA(npoints,1)=xcenter;
+ matA(npoints,2)=xcenter*xcenter;
b(npoints,0)=val;
meanCOG+=xcenter*entriesI;
rms2COG +=xcenter*entriesI*xcenter;
if (npoints>=3){
if ( npoints == 3 ){
//analytic calculation of the parameters for three points
- A.Invert();
+ matA.Invert();
TMatrixD res(1,3);
- res.Mult(A,b);
+ res.Mult(matA,b);
par[0]=res(0,0);
par[1]=res(0,1);
par[2]=res(0,2);
if (TMath::Abs(par[2])<kTol) return -4;
if (!param) param = new TVectorD(3);
- if (!matrix) matrix = new TMatrixD(3,3); // !!!!might be a memory leek. use dummy matrix pointer to call this function!
+ //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
(*param)[1] = par[1]/(-2.*par[2]);
(*param)[2] = 1./TMath::Sqrt(TMath::Abs(-2.*par[2]));
}
-Float_t TStatToolkit::GetCOG(Short_t *arr, Int_t nBins, Float_t xMin, Float_t xMax, Float_t *rms, Float_t *sum)
+Float_t TStatToolkit::GetCOG(const Short_t *arr, Int_t nBins, Float_t xMin, Float_t xMax, Float_t *rms, Float_t *sum)
{
//
// calculate center of gravity rms and sum for array 'arr' with nBins an a x range xMin to xMax
for (Int_t iy=0; iy<nbiny;iy++){
Float_t xcenter = xaxis->GetBinCenter(ix);
Float_t ycenter = yaxis->GetBinCenter(iy);
- sprintf(name,"%s_%d_%d",his->GetName(), ix,iy);
+ snprintf(name,1000,"%s_%d_%d",his->GetName(), ix,iy);
TH1 *projection = his->ProjectionZ(name,ix-delta0,ix+delta0,iy-delta1,iy+delta1);
Float_t stat= 0;
if (type==0) stat = projection->GetMean();
for (Int_t ix=0; ix<nbinx;ix++){
Float_t xcenter = xaxis->GetBinCenter(ix);
// Float_t ycenter = yaxis->GetBinCenter(iy);
- sprintf(name,"%s_%d",his->GetName(), ix);
+ 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();
-TString* TStatToolkit::FitPlane(TTree *tree, const char* drawCommand, const char* formula, const char* cuts, Double_t & chi2, Int_t &npoints, TVectorD &fitParam, TMatrixD &covMatrix, Int_t start, Int_t stop){
+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){
//
// fit an arbitrary function, specified by formula into the data, specified by drawCommand and cuts
// returns chi2, fitParam and covMatrix
// returns TString with fitted formula
//
-
+
TString formulaStr(formula);
TString drawStr(drawCommand);
TString cutStr(cuts);
+ TString ferr("1");
+
+ TString strVal(drawCommand);
+ if (strVal.Contains(":")){
+ TObjArray* valTokens = strVal.Tokenize(":");
+ drawStr = valTokens->At(0)->GetName();
+ ferr = valTokens->At(1)->GetName();
+ delete valTokens;
+ }
+
formulaStr.ReplaceAll("++", "~");
TObjArray* formulaTokens = formulaStr.Tokenize("~");
fitter->ClearPoints();
Int_t entries = tree->Draw(drawStr.Data(), cutStr.Data(), "goff", stop-start, start);
- if (entries == -1) return new TString("An ERROR has occured during fitting!");
+ if (entries == -1) {
+ delete formulaTokens;
+ return new TString("An ERROR has occured during fitting!");
+ }
+ Double_t **values = new Double_t*[dim+1] ;
+ for (Int_t i=0; i<dim+1; i++) values[i]=NULL;
+ //
+ entries = tree->Draw(ferr.Data(), cutStr.Data(), "goff", stop-start, start);
+ if (entries == -1) {
+ delete formulaTokens;
+ delete []values;
+ return new TString("An ERROR has occured during fitting!");
+ }
+ Double_t *errors = new Double_t[entries];
+ memcpy(errors, tree->GetV1(), entries*sizeof(Double_t));
+
+ for (Int_t i = 0; i < dim + 1; i++){
+ Int_t centries = 0;
+ if (i < dim) centries = tree->Draw(((TObjString*)formulaTokens->At(i))->GetName(), cutStr.Data(), "goff", stop-start,start);
+ else centries = tree->Draw(drawStr.Data(), cutStr.Data(), "goff", stop-start,start);
+
+ if (entries != centries) {
+ delete []errors;
+ delete []values;
+ return new TString("An ERROR has occured during fitting!");
+ }
+ values[i] = new Double_t[entries];
+ memcpy(values[i], tree->GetV1(), entries*sizeof(Double_t));
+ }
+
+ // add points to the fitter
+ for (Int_t i = 0; i < entries; i++){
+ Double_t x[1000];
+ for (Int_t j=0; j<dim;j++) x[j]=values[j][i];
+ fitter->AddPoint(x, values[dim][i], errors[i]);
+ }
+
+ fitter->Eval();
+ if (frac>0.5 && frac<1){
+ fitter->EvalRobust(frac);
+ }else{
+ if (fix0) {
+ fitter->FixParameter(0,0);
+ fitter->Eval();
+ }
+ }
+ fitter->GetParameters(fitParam);
+ fitter->GetCovarianceMatrix(covMatrix);
+ chi2 = fitter->GetChisquare();
+ npoints = entries;
+ TString *preturnFormula = new TString(Form("( %f+",fitParam[0])), &returnFormula = *preturnFormula;
+
+ for (Int_t iparam = 0; iparam < dim; iparam++) {
+ returnFormula.Append(Form("%s*(%f)",((TObjString*)formulaTokens->At(iparam))->GetName(),fitParam[iparam+1]));
+ if (iparam < dim-1) returnFormula.Append("+");
+ }
+ returnFormula.Append(" )");
+
+
+ for (Int_t j=0; j<dim+1;j++) delete [] values[j];
+
+
+ delete formulaTokens;
+ delete fitter;
+ delete[] values;
+ delete[] errors;
+ return preturnFormula;
+}
+
+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){
+ //
+ // fit an arbitrary function, specified by formula into the data, specified by drawCommand and cuts
+ // returns chi2, fitParam and covMatrix
+ // returns TString with fitted formula
+ //
+
+ TString formulaStr(formula);
+ TString drawStr(drawCommand);
+ TString cutStr(cuts);
+ TString ferr("1");
+
+ TString strVal(drawCommand);
+ if (strVal.Contains(":")){
+ TObjArray* valTokens = strVal.Tokenize(":");
+ drawStr = valTokens->At(0)->GetName();
+ ferr = valTokens->At(1)->GetName();
+ delete valTokens;
+ }
+
+
+ formulaStr.ReplaceAll("++", "~");
+ TObjArray* formulaTokens = formulaStr.Tokenize("~");
+ Int_t dim = formulaTokens->GetEntriesFast();
+
+ fitParam.ResizeTo(dim);
+ covMatrix.ResizeTo(dim,dim);
+
+ TLinearFitter* fitter = new TLinearFitter(dim+1, Form("hyp%d",dim));
+ fitter->StoreData(kTRUE);
+ fitter->ClearPoints();
+
+ Int_t entries = tree->Draw(drawStr.Data(), cutStr.Data(), "goff", stop-start, start);
+ if (entries == -1) {
+ delete formulaTokens;
+ return new TString("An ERROR has occured during fitting!");
+ }
Double_t **values = new Double_t*[dim+1] ;
+ for (Int_t i=0; i<dim+1; i++) values[i]=NULL;
+ //
+ entries = tree->Draw(ferr.Data(), cutStr.Data(), "goff", stop-start, start);
+ if (entries == -1) {
+ delete formulaTokens;
+ delete [] values;
+ return new TString("An ERROR has occured during fitting!");
+ }
+ Double_t *errors = new Double_t[entries];
+ memcpy(errors, tree->GetV1(), entries*sizeof(Double_t));
for (Int_t i = 0; i < dim + 1; i++){
Int_t centries = 0;
if (i < dim) centries = tree->Draw(((TObjString*)formulaTokens->At(i))->GetName(), cutStr.Data(), "goff", stop-start,start);
else centries = tree->Draw(drawStr.Data(), cutStr.Data(), "goff", stop-start,start);
- if (entries != centries) return new TString("An ERROR has occured during fitting!");
+ if (entries != centries) {
+ delete []errors;
+ delete []values;
+ delete formulaTokens;
+ return new TString("An ERROR has occured during fitting!");
+ }
values[i] = new Double_t[entries];
memcpy(values[i], tree->GetV1(), entries*sizeof(Double_t));
}
for (Int_t i = 0; i < entries; i++){
Double_t x[1000];
for (Int_t j=0; j<dim;j++) x[j]=values[j][i];
- fitter->AddPoint(x, values[dim][i], 1);
+ fitter->AddPoint(x, values[dim][i], errors[i]);
+ }
+ if (constrain>0){
+ for (Int_t i = 0; i < dim; i++){
+ Double_t x[1000];
+ for (Int_t j=0; j<dim;j++) if (i!=j) x[j]=0;
+ x[i]=1.;
+ fitter->AddPoint(x, 0, constrain);
+ }
}
+
fitter->Eval();
+ if (frac>0.5 && frac<1){
+ fitter->EvalRobust(frac);
+ }
fitter->GetParameters(fitParam);
fitter->GetCovarianceMatrix(covMatrix);
chi2 = fitter->GetChisquare();
- chi2 = chi2;
npoints = entries;
-// TString *preturnFormula = new TString(Form("%f*(",fitParam[0])), &returnFormula = *preturnFormula;
-
-// for (Int_t iparam = 0; iparam < dim; iparam++) {
-// returnFormula.Append(Form("%s*(%f)",((TObjString*)formulaTokens->At(iparam))->GetName(),fitParam[iparam+1]/fitParam[0]));
-// if (iparam < dim-1) returnFormula.Append("+");
-// }
-// returnFormula.Append(" )");
TString *preturnFormula = new TString(Form("( %f+",fitParam[0])), &returnFormula = *preturnFormula;
}
returnFormula.Append(" )");
+ for (Int_t j=0; j<dim+1;j++) delete [] values[j];
delete formulaTokens;
delete fitter;
delete[] values;
+ delete[] errors;
+ return preturnFormula;
+}
+
+
+
+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){
+ //
+ // fit an arbitrary function, specified by formula into the data, specified by drawCommand and cuts
+ // returns chi2, fitParam and covMatrix
+ // returns TString with fitted formula
+ //
+
+ TString formulaStr(formula);
+ TString drawStr(drawCommand);
+ TString cutStr(cuts);
+ TString ferr("1");
+
+ TString strVal(drawCommand);
+ if (strVal.Contains(":")){
+ TObjArray* valTokens = strVal.Tokenize(":");
+ drawStr = valTokens->At(0)->GetName();
+ ferr = valTokens->At(1)->GetName();
+ delete valTokens;
+ }
+
+
+ formulaStr.ReplaceAll("++", "~");
+ TObjArray* formulaTokens = formulaStr.Tokenize("~");
+ Int_t dim = formulaTokens->GetEntriesFast();
+
+ fitParam.ResizeTo(dim);
+ covMatrix.ResizeTo(dim,dim);
+ TString fitString="x0";
+ for (Int_t i=1; i<dim; i++) fitString+=Form("++x%d",i);
+ TLinearFitter* fitter = new TLinearFitter(dim, fitString.Data());
+ fitter->StoreData(kTRUE);
+ fitter->ClearPoints();
+
+ Int_t entries = tree->Draw(drawStr.Data(), cutStr.Data(), "goff", stop-start, start);
+ if (entries == -1) {
+ delete formulaTokens;
+ return new TString("An ERROR has occured during fitting!");
+ }
+ Double_t **values = new Double_t*[dim+1] ;
+ for (Int_t i=0; i<dim+1; i++) values[i]=NULL;
+ //
+ entries = tree->Draw(ferr.Data(), cutStr.Data(), "goff", stop-start, start);
+ if (entries == -1) {
+ delete []values;
+ delete formulaTokens;
+ return new TString("An ERROR has occured during fitting!");
+ }
+ Double_t *errors = new Double_t[entries];
+ memcpy(errors, tree->GetV1(), entries*sizeof(Double_t));
+
+ for (Int_t i = 0; i < dim + 1; i++){
+ Int_t centries = 0;
+ if (i < dim) centries = tree->Draw(((TObjString*)formulaTokens->At(i))->GetName(), cutStr.Data(), "goff", stop-start,start);
+ else centries = tree->Draw(drawStr.Data(), cutStr.Data(), "goff", stop-start,start);
+
+ if (entries != centries) {
+ delete []errors;
+ delete []values;
+ delete formulaTokens;
+ return new TString("An ERROR has occured during fitting!");
+ }
+ values[i] = new Double_t[entries];
+ memcpy(values[i], tree->GetV1(), entries*sizeof(Double_t));
+ }
+
+ // add points to the fitter
+ for (Int_t i = 0; i < entries; i++){
+ Double_t x[1000];
+ for (Int_t j=0; j<dim;j++) x[j]=values[j][i];
+ fitter->AddPoint(x, values[dim][i], errors[i]);
+ }
+
+ fitter->Eval();
+ if (frac>0.5 && frac<1){
+ fitter->EvalRobust(frac);
+ }
+ fitter->GetParameters(fitParam);
+ fitter->GetCovarianceMatrix(covMatrix);
+ chi2 = fitter->GetChisquare();
+ npoints = entries;
+
+ TString *preturnFormula = new TString("("), &returnFormula = *preturnFormula;
+
+ for (Int_t iparam = 0; iparam < dim; iparam++) {
+ returnFormula.Append(Form("%s*(%f)",((TObjString*)formulaTokens->At(iparam))->GetName(),fitParam[iparam]));
+ if (iparam < dim-1) returnFormula.Append("+");
+ }
+ returnFormula.Append(" )");
+
+
+ for (Int_t j=0; j<dim+1;j++) delete [] values[j];
+
+ delete formulaTokens;
+ delete fitter;
+ delete[] values;
+ delete[] errors;
return preturnFormula;
}
+
+
+
+
+
+Int_t TStatToolkit::GetFitIndex(const TString fString, const TString subString){
+ //
+ // fitString - ++ separated list of fits
+ // substring - ++ separated list of the requiered substrings
+ //
+ // return the last occurance of substring in fit string
+ //
+ TObjArray *arrFit = fString.Tokenize("++");
+ TObjArray *arrSub = subString.Tokenize("++");
+ Int_t index=-1;
+ for (Int_t i=0; i<arrFit->GetEntries(); i++){
+ Bool_t isOK=kTRUE;
+ TString str =arrFit->At(i)->GetName();
+ for (Int_t isub=0; isub<arrSub->GetEntries(); isub++){
+ if (str.Contains(arrSub->At(isub)->GetName())==0) isOK=kFALSE;
+ }
+ if (isOK) index=i;
+ }
+ delete arrFit;
+ delete arrSub;
+ return index;
+}
+
+
+TString TStatToolkit::FilterFit(const TString &input, const TString filter, TVectorD ¶m, TMatrixD & covar){
+ //
+ // Filter fit expression make sub-fit
+ //
+ TObjArray *array0= input.Tokenize("++");
+ TObjArray *array1= filter.Tokenize("++");
+ //TString *presult=new TString("(0");
+ TString result="(0.0";
+ for (Int_t i=0; i<array0->GetEntries(); i++){
+ Bool_t isOK=kTRUE;
+ TString str(array0->At(i)->GetName());
+ for (Int_t j=0; j<array1->GetEntries(); j++){
+ if (str.Contains(array1->At(j)->GetName())==0) isOK=kFALSE;
+ }
+ if (isOK) {
+ result+="+"+str;
+ result+=Form("*(%f)",param[i+1]);
+ printf("%f\t%f\t%s\n",param[i+1], TMath::Sqrt(covar(i+1,i+1)),str.Data());
+ }
+ }
+ result+="-0.)";
+ delete array0;
+ delete array1;
+ return result;
+}
+
+void TStatToolkit::Update1D(Double_t delta, Double_t sigma, Int_t s1, TMatrixD &vecXk, TMatrixD &covXk){
+ //
+ // Update parameters and covariance - with one measurement
+ // Input:
+ // vecXk - input vector - Updated in function
+ // covXk - covariance matrix - Updated in function
+ // delta, sigma, s1 - new measurement, rms of new measurement and the index of measurement
+ const Int_t knMeas=1;
+ Int_t knElem=vecXk.GetNrows();
+
+ TMatrixD mat1(knElem,knElem); // update covariance matrix
+ TMatrixD matHk(1,knElem); // vector to mesurement
+ TMatrixD vecYk(knMeas,1); // Innovation or measurement residual
+ TMatrixD matHkT(knElem,knMeas); // helper matrix Hk transpose
+ TMatrixD matSk(knMeas,knMeas); // Innovation (or residual) covariance
+ TMatrixD matKk(knElem,knMeas); // Optimal Kalman gain
+ TMatrixD covXk2(knElem,knElem); // helper matrix
+ TMatrixD covXk3(knElem,knElem); // helper matrix
+ TMatrixD vecZk(1,1);
+ TMatrixD measR(1,1);
+ vecZk(0,0)=delta;
+ measR(0,0)=sigma*sigma;
+ //
+ // reset matHk
+ for (Int_t iel=0;iel<knElem;iel++)
+ for (Int_t ip=0;ip<knMeas;ip++) matHk(ip,iel)=0;
+ //mat1
+ for (Int_t iel=0;iel<knElem;iel++) {
+ for (Int_t jel=0;jel<knElem;jel++) mat1(iel,jel)=0;
+ mat1(iel,iel)=1;
+ }
+ //
+ matHk(0, s1)=1;
+ vecYk = vecZk-matHk*vecXk; // Innovation or measurement residual
+ matHkT=matHk.T(); matHk.T();
+ matSk = (matHk*(covXk*matHkT))+measR; // Innovation (or residual) covariance
+ matSk.Invert();
+ matKk = (covXk*matHkT)*matSk; // Optimal Kalman gain
+ vecXk += matKk*vecYk; // updated vector
+ covXk2= (mat1-(matKk*matHk));
+ covXk3 = covXk2*covXk;
+ covXk = covXk3;
+ Int_t nrows=covXk3.GetNrows();
+
+ for (Int_t irow=0; irow<nrows; irow++)
+ for (Int_t icol=0; icol<nrows; icol++){
+ // rounding problems - make matrix again symteric
+ covXk(irow,icol)=(covXk3(irow,icol)+covXk3(icol,irow))*0.5;
+ }
+}
+
+
+
+void TStatToolkit::Constrain1D(const TString &input, const TString filter, TVectorD ¶m, TMatrixD & covar, Double_t mean, Double_t sigma){
+ //
+ // constrain linear fit
+ // input - string description of fit function
+ // filter - string filter to select sub fits
+ // param,covar - parameters and covariance matrix of the fit
+ // mean,sigma - new measurement uning which the fit is updated
+ //
+
+ TObjArray *array0= input.Tokenize("++");
+ TObjArray *array1= filter.Tokenize("++");
+ TMatrixD paramM(param.GetNrows(),1);
+ for (Int_t i=0; i<=array0->GetEntries(); i++){paramM(i,0)=param(i);}
+
+ if (filter.Length()==0){
+ TStatToolkit::Update1D(mean, sigma, 0, paramM, covar);//
+ }else{
+ for (Int_t i=0; i<array0->GetEntries(); i++){
+ Bool_t isOK=kTRUE;
+ TString str(array0->At(i)->GetName());
+ for (Int_t j=0; j<array1->GetEntries(); j++){
+ if (str.Contains(array1->At(j)->GetName())==0) isOK=kFALSE;
+ }
+ if (isOK) {
+ TStatToolkit::Update1D(mean, sigma, i+1, paramM, covar);//
+ }
+ }
+ }
+ for (Int_t i=0; i<=array0->GetEntries(); i++){
+ param(i)=paramM(i,0);
+ }
+ delete array0;
+ delete array1;
+}
+
+TString TStatToolkit::MakeFitString(const TString &input, const TVectorD ¶m, const TMatrixD & covar, Bool_t verbose){
+ //
+ //
+ //
+ TObjArray *array0= input.Tokenize("++");
+ TString result=Form("(%f",param[0]);
+ printf("%f\t%f\t\n", param[0], TMath::Sqrt(covar(0,0)));
+ for (Int_t i=0; i<array0->GetEntries(); i++){
+ TString str(array0->At(i)->GetName());
+ result+="+"+str;
+ result+=Form("*(%f)",param[i+1]);
+ if (verbose) printf("%f\t%f\t%s\n", param[i+1], TMath::Sqrt(covar(i+1,i+1)),str.Data());
+ }
+ result+="-0.)";
+ delete array0;
+ return result;
+}
+
+TGraphErrors * TStatToolkit::MakeGraphErrors(TTree * tree, const char * expr, const char * cut, Int_t mstyle, Int_t mcolor, Float_t msize, Float_t offset){
+ //
+ // 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::MakeGraphError",Form("Empty or Not valid expression (%s) or cut *%s)", expr,cut));
+ return 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);
+ 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,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;
+
+ // evaluate arrays for the new graph according to the run-number
+ Int_t icount=0;
+ //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++;
+ 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()) {
+ 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",runNumber[i]);
+ graphNew->GetXaxis()->SetBinLabel(i+1,xName);
+ graphNew->GetX()[i]+=offset;
+ }
+
+ graphNew->GetHistogram()->SetTitle("");
+ graphNew->SetMarkerStyle(mstyle);
+ graphNew->SetMarkerColor(mcolor);
+ if (msize>0) graphNew->SetMarkerSize(msize);
+ delete [] unsortedX;
+ delete [] runNumber;
+ delete [] index;
+ delete [] newBins;
+ //
+ return graphNew;
+}
+
+
+
+//
+// functions used for the trending
+//
+
+Int_t TStatToolkit::MakeStatAlias(TTree * tree, const char * expr, const char * cut, const char * alias)
+{
+ //
+ // Add alias using statistical values of a given variable.
+ // (by MI, Patrick Reichelt)
+ //
+ // tree - input tree
+ // expr - variable expression
+ // cut - selection criteria
+ // 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:
+ 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");
+ if (entries<=1){
+ printf("Expression or cut not valid:\t%s\t%s\n", expr, cut);
+ return 0;
+ }
+ //
+ TObjArray* oaAlias = TString(alias).Tokenize(":");
+ if (oaAlias->GetEntries()<2) return 0;
+ 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 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_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;
+ return entries;
+}
+
+Int_t TStatToolkit::SetStatusAlias(TTree * tree, const char * expr, const char * cut, const char * alias)
+{
+ //
+ // Add alias to trending tree using statistical values of a given variable.
+ // (by MI, Patrick Reichelt)
+ //
+ // format of expr : varname (e.g. meanTPCncl)
+ // 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, 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 (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
+ 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)
+ 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)
+ */
+ //
+ 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];
+ snprintf(varname,50,"%s", oaVar->At(0)->GetName());
+ //
+ TObjArray* oaAlias = TString(alias).Tokenize(":");
+ if (oaAlias->GetEntries()<3) return 0;
+ Float_t entryFraction = atof( oaAlias->At(2)->GetName() );
+ //
+ 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);
+ //
+ 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) );
+ 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("Median", Form("%f",median) );
+ sQuery.ReplaceAll("Mean", Form("%f",mean) );
+ sQuery.ReplaceAll("RMS", Form("%f",rms) );
+ 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;
+}
+
+TMultiGraph* TStatToolkit::MakeStatusMultGr(TTree * tree, const char * expr, const char * cut, const char * alias, Int_t igr)
+{
+ //
+ // Compute a trending multigraph that shows for which runs a variable has outliers.
+ // (by MI, Patrick Reichelt)
+ //
+ // format of expr : varname:xaxis (e.g. meanTPCncl:run)
+ // 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 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());
+ //
+ 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();
+ Int_t marArr[6] = {24+igr%2, 20+igr%2, 20+igr%2, 20+igr%2, 22, 23};
+ Int_t colArr[6] = {kBlack, kBlack, kRed, kOrange, kMagenta, kViolet};
+ Double_t sizArr[6] = {1.2, 1.1, 1.0, 1.0, 1, 1};
+ const Int_t ngr = oaAlias->GetEntriesFast();
+ 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,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,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;
+}
+
+
+void TStatToolkit::AddStatusPad(TCanvas* c1, Float_t padratio, Float_t bottommargin)
+{
+ //
+ // add pad to bottom of canvas for Status graphs (by Patrick Reichelt)
+ // call function "DrawStatusGraphs(...)" afterwards
+ //
+ TCanvas* c1_clone = (TCanvas*) c1->Clone("c1_clone");
+ c1->Clear();
+ // produce new pads
+ c1->cd();
+ TPad* pad1 = new TPad("pad1", "pad1", 0., padratio, 1., 1.);
+ pad1->Draw();
+ pad1->SetNumber(1); // so it can be called via "c1->cd(1);"
+ c1->cd();
+ TPad* pad2 = new TPad("pad2", "pad2", 0., 0., 1., padratio);
+ pad2->Draw();
+ pad2->SetNumber(2);
+ // draw original canvas into first pad
+ c1->cd(1);
+ c1_clone->DrawClonePad();
+ pad1->SetBottomMargin(0.001);
+ pad1->SetRightMargin(0.01);
+ // set up second pad
+ c1->cd(2);
+ pad2->SetGrid(3);
+ pad2->SetTopMargin(0);
+ pad2->SetBottomMargin(bottommargin); // for the long x-axis labels (runnumbers)
+ pad2->SetRightMargin(0.01);
+}
+
+
+void TStatToolkit::DrawStatusGraphs(TObjArray* oaMultGr)
+{
+ //
+ // draw Status graphs into active pad of canvas (by MI, Patrick Reichelt)
+ // ...into bottom pad, if called after "AddStatusPad(...)"
+ //
+ const Int_t nvars = oaMultGr->GetEntriesFast();
+ TGraph* grAxis = (TGraph*) ((TMultiGraph*) oaMultGr->At(0))->GetListOfGraphs()->At(0);
+ grAxis->SetMaximum(0.5*nvars+0.5);
+ grAxis->SetMinimum(0);
+ grAxis->GetYaxis()->SetLabelSize(0);
+ Int_t entries = grAxis->GetN();
+ printf("entries (via GetN()) = %d\n",entries);
+ grAxis->GetXaxis()->SetLabelSize(5.7*TMath::Min(TMath::Max(5./entries,0.01),0.03));
+ grAxis->GetXaxis()->LabelsOption("v");
+ grAxis->Draw("ap");
+ //
+ // draw multigraphs & names of status variables on the y axis
+ for (Int_t i=0; i<nvars; i++){
+ ((TMultiGraph*) oaMultGr->At(i))->Draw("p");
+ TLatex* ylabel = new TLatex(-0.1, 0.5*i+0.5, ((TMultiGraph*) oaMultGr->At(i))->GetTitle());
+ ylabel->SetTextAlign(32); //hor:right & vert:centered
+ ylabel->SetTextSize(0.025/gPad->GetHNDC());
+ ylabel->Draw();
+ }
+}
+
+
+void 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;
+ }
+
+ // get entries
+ Int_t entries = tree->Draw(drawStr.Data(), cutStr.Data(), "goff");
+ if (entries == -1) {
+ cerr<<"TTree draw returns -1"<<endl;
+ return;
+ }
+
+ // 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;
+ }
+
+ // 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");
+ }
+
+}