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 Origin: marian.ivanov@cern.ch
21 Frequenlty used function for visualization
25 #if !defined(__CINT__) || defined(__MAKECINT__)
36 #include "TBenchmark.h"
37 #include "TStopwatch.h"
38 #include "TParticle.h"
49 #include "TGeometry.h"
50 #include "TPolyLine3D.h"
51 #include "TPolyMarker3D.h"
52 #include "TObjString.h"
56 #include "AliTrackPointArray.h"
57 #include "AliTreeDraw.h"
62 // Class for visualization and some statistacal analysis using tree
63 // To be used in comparisons
64 // and calib viewers based on tree
70 AliTreeDraw::AliTreeDraw():
76 // default constructor
80 void AliTreeDraw::ClearHisto(){
91 TH1F * AliTreeDraw::DrawXY(const char * chx, const char *chy, const char* selection,
92 const char * quality, Int_t nbins, Float_t minx, Float_t maxx, Float_t miny, Float_t maxy, Int_t nBinsRes)
95 Double_t* bins = CreateLogBins(nbins, minx, maxx);
96 TH2F* hRes2 = new TH2F("hRes2", "residuals", nbins, minx, maxx, nBinsRes, miny, maxy);
98 sprintf(cut,"%s&&%s",selection,quality);
99 char expression[1000];
100 sprintf(expression,"%s:%s>>hRes2",chy,chx);
101 fTree->Draw(expression, cut, "groff");
103 TH1F* hRes = CreateResHisto(hRes2, &hMean);
104 AliLabelAxes(hRes, chx, chy);
116 TH1F * AliTreeDraw::DrawLogXY(const char * chx, const char *chy, const char* selection,
117 const char * quality, Int_t nbins, Float_t minx, Float_t maxx, Float_t miny, Float_t maxy, Int_t nBinsRes)
122 Double_t* bins = CreateLogBins(nbins, minx, maxx);
123 TH2F* hRes2 = new TH2F("hRes2", "residuals", nbins, bins, nBinsRes, miny, maxy);
125 sprintf(cut,"%s&&%s",selection,quality);
126 char expression[1000];
127 sprintf(expression,"%s:%s>>hRes2",chy,chx);
128 fTree->Draw(expression, cut, "groff");
130 TH1F* hRes = CreateResHisto(hRes2, &hMean);
131 AliLabelAxes(hRes, chx, chy);
141 ///////////////////////////////////////////////////////////////////////////////////
142 ///////////////////////////////////////////////////////////////////////////////////
143 TH1F * AliTreeDraw::Eff(const char *variable, const char* selection, const char * quality,
144 Int_t nbins, Float_t min, Float_t max)
148 TH1F* hGen = new TH1F("hGen", "gen. tracks", nbins, min, max);
149 TH1F* hRec = new TH1F("hRec", "rec. tracks", nbins, min, max);
151 sprintf(inputGen,"%s>>hGen", variable);
152 fTree->Draw(inputGen, selection, "groff");
153 char selectionRec[256];
154 sprintf(selectionRec, "%s && %s", selection, quality);
156 sprintf(inputRec,"%s>>hRec", variable);
157 fTree->Draw(inputRec, selectionRec, "groff");
159 TH1F* hEff = CreateEffHisto(hGen, hRec);
160 AliLabelAxes(hEff, variable, "#epsilon [%]");
169 ///////////////////////////////////////////////////////////////////////////////////
170 ///////////////////////////////////////////////////////////////////////////////////
171 TH1F * AliTreeDraw::EffLog(const char *variable, const char* selection, const char * quality,
172 Int_t nbins, Float_t min, Float_t max)
176 Double_t* bins = CreateLogBins(nbins, min, max);
177 TH1F* hGen = new TH1F("hGen", "gen. tracks", nbins, bins);
178 TH1F* hRec = new TH1F("hRec", "rec. tracks", nbins, bins);
180 sprintf(inputGen,"%s>>hGen", variable);
181 fTree->Draw(inputGen, selection, "groff");
182 char selectionRec[256];
183 sprintf(selectionRec, "%s && %s", selection, quality);
185 sprintf(inputRec,"%s>>hRec", variable);
186 fTree->Draw(inputRec, selectionRec, "groff");
188 TH1F* hEff = CreateEffHisto(hGen, hRec);
189 AliLabelAxes(hEff, variable, "#epsilon [%]");
198 ///////////////////////////////////////////////////////////////////////////////////
199 ///////////////////////////////////////////////////////////////////////////////////
201 Double_t* AliTreeDraw::CreateLogBins(Int_t nBins, Double_t xMin, Double_t xMax)
203 Double_t* bins = new Double_t[nBins+1];
205 Double_t factor = pow(xMax/xMin, 1./nBins);
206 for (Int_t i = 1; i <= nBins; i++)
207 bins[i] = factor * bins[i-1];
214 void AliTreeDraw::AliLabelAxes(TH1* histo, const char* xAxisTitle, const char* yAxisTitle)
217 histo->GetXaxis()->SetTitle(xAxisTitle);
218 histo->GetYaxis()->SetTitle(yAxisTitle);
222 TH1F* AliTreeDraw::CreateEffHisto(TH1F* hGen, TH1F* hRec)
225 Int_t nBins = hGen->GetNbinsX();
226 TH1F* hEff = (TH1F*) hGen->Clone("hEff");
228 hEff->SetStats(kFALSE);
229 hEff->SetMinimum(0.);
230 hEff->SetMaximum(110.);
232 for (Int_t iBin = 0; iBin <= nBins; iBin++) {
233 Double_t nGen = hGen->GetBinContent(iBin);
234 Double_t nRec = hRec->GetBinContent(iBin);
236 Double_t eff = nRec/nGen;
237 hEff->SetBinContent(iBin, 100. * eff);
238 Double_t error = sqrt((eff*(1.-eff)+0.01) / nGen);
239 // if (error == 0) error = sqrt(0.1/nGen);
241 if (error == 0) error = 0.0001;
242 hEff->SetBinError(iBin, 100. * error);
244 hEff->SetBinContent(iBin, 100. * 0.5);
245 hEff->SetBinError(iBin, 100. * 0.5);
253 TH1F* AliTreeDraw::CreateResHisto(TH2F* hRes2, TH1F **phMean, Bool_t drawBinFits,
254 Bool_t overflowBinFits)
256 TVirtualPad* currentPad = gPad;
257 TAxis* axis = hRes2->GetXaxis();
258 Int_t nBins = axis->GetNbins();
260 if (axis->GetXbins()->GetSize()){
261 hRes = new TH1F("hRes", "", nBins, axis->GetXbins()->GetArray());
262 hMean = new TH1F("hMean", "", nBins, axis->GetXbins()->GetArray());
265 hRes = new TH1F("hRes", "", nBins, axis->GetXmin(), axis->GetXmax());
266 hMean = new TH1F("hMean", "", nBins, axis->GetXmin(), axis->GetXmax());
269 hRes->SetStats(false);
270 hRes->SetOption("E");
271 hRes->SetMinimum(0.);
273 hMean->SetStats(false);
274 hMean->SetOption("E");
276 // create the fit function
277 TF1 * fitFunc = new TF1("G","[0]*exp(-(x-[1])*(x-[1])/(2.*[2]*[2]))",-3,3);
279 fitFunc->SetLineWidth(2);
280 fitFunc->SetFillStyle(0);
281 // create canvas for fits
282 TCanvas* canBinFits = NULL;
283 Int_t nPads = (overflowBinFits) ? nBins+2 : nBins;
284 Int_t nx = Int_t(sqrt(nPads-1.));// + 1;
285 Int_t ny = (nPads-1) / nx + 1;
287 canBinFits = (TCanvas*)gROOT->FindObject("canBinFits");
288 if (canBinFits) delete canBinFits;
289 canBinFits = new TCanvas("canBinFits", "fits of bins", 200, 100, 500, 700);
290 canBinFits->Divide(nx, ny);
293 // loop over x bins and fit projection
294 Int_t dBin = ((overflowBinFits) ? 1 : 0);
295 for (Int_t bin = 1-dBin; bin <= nBins+dBin; bin++) {
296 if (drawBinFits) canBinFits->cd(bin + dBin);
297 TH1D* hBin = hRes2->ProjectionY("hBin", bin, bin);
299 if (hBin->GetEntries() > 5) {
300 fitFunc->SetParameters(hBin->GetMaximum(),hBin->GetMean(),hBin->GetRMS());
301 hBin->Fit(fitFunc,"s");
302 Double_t sigma = TMath::Abs(fitFunc->GetParameter(2));
305 hRes->SetBinContent(bin, TMath::Abs(fitFunc->GetParameter(2)));
306 hMean->SetBinContent(bin, fitFunc->GetParameter(1));
309 hRes->SetBinContent(bin, 0.);
310 hMean->SetBinContent(bin,0);
312 hRes->SetBinError(bin, fitFunc->GetParError(2));
313 hMean->SetBinError(bin, fitFunc->GetParError(1));
319 hRes->SetBinContent(bin, 0.);
320 hRes->SetBinError(bin, 0.);
321 hMean->SetBinContent(bin, 0.);
322 hMean->SetBinError(bin, 0.);
329 sprintf(name, "%s < %.4g", axis->GetTitle(), axis->GetBinUpEdge(bin));
330 } else if (bin == nBins+1) {
331 sprintf(name, "%.4g < %s", axis->GetBinLowEdge(bin), axis->GetTitle());
333 sprintf(name, "%.4g < %s < %.4g", axis->GetBinLowEdge(bin),
334 axis->GetTitle(), axis->GetBinUpEdge(bin));
336 canBinFits->cd(bin + dBin);
337 hBin->SetTitle(name);
338 hBin->SetStats(kTRUE);
340 canBinFits->Update();
341 canBinFits->Modified();
342 canBinFits->Update();
357 void AliTreeDraw::GetPoints3D(const char * label, const char * chpoints,
358 const char* selection, TTree * tpoints, Int_t color,Float_t rmin){
360 // load selected points from tree
362 if (!fPoints) fPoints = new TObjArray;
363 if (tpoints->GetIndex()==0) tpoints->BuildIndex("fLabel","Label");
364 TBranch * br = tpoints->GetBranch(chpoints);
366 AliTrackPointArray * points = new AliTrackPointArray;
367 br->SetAddress(&points);
369 Int_t npoints = fTree->Draw(label,selection);
372 for (Int_t i=0;i<npoints;i++){
373 Int_t index = (Int_t)fTree->GetV1()[i];
374 tpoints->GetEntryWithIndex(index,index);
375 if (points->GetNPoints()<2) continue;
377 for (Int_t i=0;i<points->GetNPoints(); i++){
378 xyz[goodpoints*3] = points->GetX()[i];
379 xyz[goodpoints*3+1] = points->GetY()[i];
380 xyz[goodpoints*3+2] = points->GetZ()[i];
381 if ( points->GetX()[i]*points->GetX()[i]+points->GetY()[i]*points->GetY()[i]>rmin) goodpoints++;
383 TPolyMarker3D * marker = new TPolyMarker3D(goodpoints,xyz);
384 marker->SetMarkerColor(color);
385 marker->SetMarkerStyle(1);
386 fPoints->AddLast(marker);
393 TString* AliTreeDraw::FitPlane(const char* drawCommand, const char* formula, const char* cuts, Double_t & chi2, TVectorD &fitParam, TMatrixD &covMatrix, Int_t start, Int_t stop){
395 // fit an arbitrary function, specified by formula into the data, specified by drawCommand and cuts
396 // returns chi2, fitParam and covMatrix
397 // returns TString with fitted formula
400 TString formulaStr(formula);
401 TString drawStr(drawCommand);
402 TString cutStr(cuts);
404 formulaStr.ReplaceAll("++", "~");
405 TObjArray* formulaTokens = formulaStr.Tokenize("~");
406 Int_t dim = formulaTokens->GetEntriesFast();
408 fitParam.ResizeTo(dim);
409 covMatrix.ResizeTo(dim,dim);
411 TLinearFitter* fitter = new TLinearFitter(dim+1, Form("hyp%d",dim));
412 fitter->StoreData(kTRUE);
413 fitter->ClearPoints();
415 Int_t entries = fTree->Draw(drawStr.Data(), cutStr.Data(), "goff", stop-start, start);
416 if (entries == -1) return new TString("An ERROR has occured during fitting!");
417 Double_t **values = new Double_t*[dim+1] ;
419 for (Int_t i = 0; i < dim + 1; i++){
421 if (i < dim) centries = fTree->Draw(((TObjString*)formulaTokens->At(i))->GetName(), cutStr.Data(), "goff", stop-start,start);
422 else centries = fTree->Draw(drawStr.Data(), cutStr.Data(), "goff", stop-start,start);
424 if (entries != centries) return new TString("An ERROR has occured during fitting!");
425 values[i] = new Double_t[entries];
426 memcpy(values[i], fTree->GetV1(), entries*sizeof(Double_t));
429 // add points to the fitter
430 for (Int_t i = 0; i < entries; i++){
432 for (Int_t j=0; j<dim;j++) x[j]=values[j][i];
433 fitter->AddPoint(x, values[dim][i], 1);
437 fitter->GetParameters(fitParam);
438 fitter->GetCovarianceMatrix(covMatrix);
439 chi2 = fitter->GetChisquare();
442 TString *preturnFormula = new TString(Form("( %f+",fitParam[0])), &returnFormula = *preturnFormula;
444 for (Int_t iparam = 0; iparam < dim; iparam++) {
445 returnFormula.Append(Form("%s*(%f)",((TObjString*)formulaTokens->At(iparam))->GetName(),fitParam[iparam+1]));
446 if (iparam < dim-1) returnFormula.Append("+");
448 returnFormula.Append(" )");
449 delete formulaTokens;
452 return preturnFormula;