kdTreeBinning.C

Go to the documentation of this file.
00001 // ------------------------------------------------------------------------
00002 //
00003 // kdTreeBinning tutorial: bin the data in cells of equal content using a kd-tree
00004 //
00005 // Using TKDTree wrapper class as a data binning structure
00006 //  Plot the 2D data using the TH2Poly class
00007 //
00008 //
00009 // Author:   Bartolomeu Rabacal    11/2010
00010 //
00011 // ------------------------------------------------------------------------
00012 
00013 #include <math.h>
00014 
00015 #include "TKDTreeBinning.h"
00016 #include "TH2D.h"
00017 #include "TH2Poly.h"
00018 #include "TStyle.h"
00019 #include "TGraph2D.h"
00020 #include "TRandom3.h"
00021 #include "TCanvas.h"
00022 #include <iostream>
00023 
00024 void kdTreeBinning() {
00025 
00026    // -----------------------------------------------------------------------------------------------
00027    //  C r e a t e  r a n d o m  s a m p l e  w i t h  r e g u l a r  b i n n i n g  p l o t t i n g
00028    // -----------------------------------------------------------------------------------------------
00029 
00030    const UInt_t DATASZ = 100000;
00031    const UInt_t DATADIM = 2;
00032    const UInt_t NBINS = 100;
00033 
00034    Double_t smp[DATASZ * DATADIM];
00035 
00036    TRandom3 r;
00037    r.SetSeed(1);
00038    for (UInt_t i = 0; i < DATADIM; ++i)
00039       for (UInt_t j = 0; j < DATASZ; ++j)
00040          smp[DATASZ * i + j] = r.Gaus(0., 2.);
00041 
00042    UInt_t h1bins = (UInt_t) sqrt(NBINS);
00043 
00044    TH2D* h1 = new TH2D("h1BinTest", "Regular binning", h1bins, -5., 5., h1bins, -5., 5.);
00045    for (UInt_t j = 0; j < DATASZ; ++j)
00046       h1->Fill(smp[j], smp[DATASZ + j]);
00047 
00048    TCanvas* c1 = new TCanvas("c1", "c1");
00049    c1->Update();
00050    c1->cd(1);
00051 
00052    h1->Draw("LEGO");
00053 
00054    // ---------------------------------------------------------------------------------------------
00055    // C r e a t e  K D T r e e B i n n i n g  o b j e c t  w i t h  T H 2 P o l y  p l o t t i n g
00056    // ---------------------------------------------------------------------------------------------
00057 
00058    TKDTreeBinning* fBins = new TKDTreeBinning(DATASZ, DATADIM, smp, NBINS);
00059 
00060    UInt_t nbins = fBins->GetNBins();
00061    UInt_t dim   = fBins->GetDim();
00062 
00063    const Double_t* binsMinEdges = fBins->GetBinsMinEdges();
00064    const Double_t* binsMaxEdges = fBins->GetBinsMaxEdges();
00065 
00066    gStyle->SetCanvasPreferGL(1);
00067    TH2Poly* h2pol = new TH2Poly("h2PolyBinTest", "KDTree binning", fBins->GetDataMin(0), fBins->GetDataMax(0), fBins->GetDataMin(1), fBins->GetDataMax(1));
00068 
00069    for (UInt_t i = 0; i < nbins; ++i) {
00070       UInt_t edgeDim = i * dim;
00071       h2pol->AddBin(binsMinEdges[edgeDim], binsMinEdges[edgeDim + 1], binsMaxEdges[edgeDim], binsMaxEdges[edgeDim + 1]);
00072    }
00073 
00074    for (UInt_t i = 1; i <= fBins->GetNBins(); ++i)
00075       h2pol->SetBinContent(i, fBins->GetBinDensity(i - 1));
00076 
00077    std::cout << "Bin with minimum density: " << fBins->GetBinMinDensity() << std::endl;
00078    std::cout << "Bin with maximum density: " << fBins->GetBinMaxDensity() << std::endl;
00079 
00080    TCanvas* c2 = new TCanvas("glc2", "c2");
00081    c2->Update();
00082    c2->cd(1);
00083 
00084    h2pol->Draw("gllego");
00085 
00086    /* Draw an equivalent plot showing the data points */
00087    /*-------------------------------------------------*/
00088 
00089    std::vector<Double_t> z = std::vector<Double_t>(DATASZ, 0.);
00090    for (UInt_t i = 0; i < DATASZ; ++i)
00091       z[i] = (Double_t) h2pol->GetBinContent(h2pol->FindBin(smp[i], smp[DATASZ + i]));
00092 
00093    TGraph2D *g = new TGraph2D(DATASZ, smp, &smp[DATASZ], &z[0]);
00094    gStyle->SetPalette(1);
00095    g->SetMarkerStyle(20);
00096 
00097    TCanvas* c3 = new TCanvas("c3", "c3");
00098    c3->Update();
00099    c3->cd(1);
00100 
00101    g->Draw("pcol");
00102 
00103    // ---------------------------------------------------------
00104    // R e b i n  t h e  K D T r e e B i n n i n g  o b j e c t
00105    // ---------------------------------------------------------
00106 
00107    fBins->SetNBins(200);
00108 
00109    TH2Poly* h2polrebin = new TH2Poly("h2PolyBinTest", "KDTree binning", fBins->GetDataMin(0), fBins->GetDataMax(0), fBins->GetDataMin(1), fBins->GetDataMax(1));
00110    h2polrebin->SetFloat();
00111 
00112    /* Sort the bins by their density  */
00113    /*---------------------------------*/
00114 
00115    fBins->SortBinsByDensity();
00116 
00117    for (UInt_t i = 0; i < fBins->GetNBins(); ++i) {
00118       const Double_t* binMinEdges = fBins->GetBinMinEdges(i);
00119       const Double_t* binMaxEdges = fBins->GetBinMaxEdges(i);
00120       h2polrebin->AddBin(binMinEdges[0], binMinEdges[1], binMaxEdges[0], binMaxEdges[1]);
00121    }
00122 
00123    for (UInt_t i = 1; i <= fBins->GetNBins(); ++i){
00124       h2polrebin->SetBinContent(i, fBins->GetBinDensity(i - 1));}
00125 
00126    std::cout << "Bin with minimum density: " << fBins->GetBinMinDensity() << std::endl;
00127    std::cout << "Bin with maximum density: " << fBins->GetBinMaxDensity() << std::endl;
00128 
00129    for (UInt_t i = 0; i < DATASZ; ++i)
00130       z[i] = (Double_t) h2polrebin->GetBin(h2polrebin->FindBin(smp[i], smp[DATASZ + i]));
00131 
00132    TCanvas* c4 = new TCanvas("glc4", "TH2Poly with kd-tree bin data",10,10,700,700);
00133    c4->Update();
00134    c4->Divide(1,2);
00135    c4->cd(1);
00136    h2polrebin->Draw("COLZ");  // draw as scatter plot
00137 
00138    c4->cd(2);
00139    h2polrebin->Draw("gllego");  // draw as lego
00140 
00141 }

Generated on Tue Jul 5 15:44:49 2011 for ROOT_528-00b_version by  doxygen 1.5.1