ROOT logo

From $ROOTSYS/tutorials/math/kdTreeBinning.C

// ------------------------------------------------------------------------
//
// kdTreeBinning tutorial: bin the data in cells of equal content using a kd-tree
//
// Using TKDTree wrapper class as a data binning structure
//  Plot the 2D data using the TH2Poly class
//
//
// Author:   Bartolomeu Rabacal    11/2010
//
// ------------------------------------------------------------------------

#include <math.h>

#include "TKDTreeBinning.h"
#include "TH2D.h"
#include "TH2Poly.h"
#include "TStyle.h"
#include "TGraph2D.h"
#include "TRandom3.h"
#include "TCanvas.h"
#include <iostream>

void kdTreeBinning() {

   // -----------------------------------------------------------------------------------------------
   //  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
   // -----------------------------------------------------------------------------------------------

   const UInt_t DATASZ = 100000;
   const UInt_t DATADIM = 2;
   const UInt_t NBINS = 100;

   Double_t smp[DATASZ * DATADIM];

   TRandom3 r;
   r.SetSeed(1);
   for (UInt_t i = 0; i < DATADIM; ++i)
      for (UInt_t j = 0; j < DATASZ; ++j)
         smp[DATASZ * i + j] = r.Gaus(0., 2.);

   UInt_t h1bins = (UInt_t) sqrt(NBINS);

   TH2D* h1 = new TH2D("h1BinTest", "Regular binning", h1bins, -5., 5., h1bins, -5., 5.);
   for (UInt_t j = 0; j < DATASZ; ++j)
      h1->Fill(smp[j], smp[DATASZ + j]);

   TCanvas* c1 = new TCanvas("c1", "c1");
   c1->Update();
   c1->cd(1);

   h1->Draw("LEGO");

   // ---------------------------------------------------------------------------------------------
   // 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
   // ---------------------------------------------------------------------------------------------

   TKDTreeBinning* fBins = new TKDTreeBinning(DATASZ, DATADIM, smp, NBINS);

   UInt_t nbins = fBins->GetNBins();
   UInt_t dim   = fBins->GetDim();

   const Double_t* binsMinEdges = fBins->GetBinsMinEdges();
   const Double_t* binsMaxEdges = fBins->GetBinsMaxEdges();

   gStyle->SetCanvasPreferGL(1);
   TH2Poly* h2pol = new TH2Poly("h2PolyBinTest", "KDTree binning", fBins->GetDataMin(0), fBins->GetDataMax(0), fBins->GetDataMin(1), fBins->GetDataMax(1));

   for (UInt_t i = 0; i < nbins; ++i) {
      UInt_t edgeDim = i * dim;
      h2pol->AddBin(binsMinEdges[edgeDim], binsMinEdges[edgeDim + 1], binsMaxEdges[edgeDim], binsMaxEdges[edgeDim + 1]);
   }

   for (UInt_t i = 1; i <= fBins->GetNBins(); ++i)
      h2pol->SetBinContent(i, fBins->GetBinDensity(i - 1));

   std::cout << "Bin with minimum density: " << fBins->GetBinMinDensity() << std::endl;
   std::cout << "Bin with maximum density: " << fBins->GetBinMaxDensity() << std::endl;

   TCanvas* c2 = new TCanvas("glc2", "c2");
   c2->Update();
   c2->cd(1);

   h2pol->Draw("gllego");

   /* Draw an equivalent plot showing the data points */
   /*-------------------------------------------------*/

   std::vector<Double_t> z = std::vector<Double_t>(DATASZ, 0.);
   for (UInt_t i = 0; i < DATASZ; ++i)
      z[i] = (Double_t) h2pol->GetBinContent(h2pol->FindBin(smp[i], smp[DATASZ + i]));

   TGraph2D *g = new TGraph2D(DATASZ, smp, &smp[DATASZ], &z[0]);
   gStyle->SetPalette(1);
   g->SetMarkerStyle(20);

   TCanvas* c3 = new TCanvas("c3", "c3");
   c3->Update();
   c3->cd(1);

   g->Draw("pcol");

   // ---------------------------------------------------------
   // 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
   // ---------------------------------------------------------

   fBins->SetNBins(200);

   TH2Poly* h2polrebin = new TH2Poly("h2PolyBinTest", "KDTree binning", fBins->GetDataMin(0), fBins->GetDataMax(0), fBins->GetDataMin(1), fBins->GetDataMax(1));
   h2polrebin->SetFloat();

   /* Sort the bins by their density  */
   /*---------------------------------*/

   fBins->SortBinsByDensity();

   for (UInt_t i = 0; i < fBins->GetNBins(); ++i) {
      const Double_t* binMinEdges = fBins->GetBinMinEdges(i);
      const Double_t* binMaxEdges = fBins->GetBinMaxEdges(i);
      h2polrebin->AddBin(binMinEdges[0], binMinEdges[1], binMaxEdges[0], binMaxEdges[1]);
   }

   for (UInt_t i = 1; i <= fBins->GetNBins(); ++i){
      h2polrebin->SetBinContent(i, fBins->GetBinDensity(i - 1));}

   std::cout << "Bin with minimum density: " << fBins->GetBinMinDensity() << std::endl;
   std::cout << "Bin with maximum density: " << fBins->GetBinMaxDensity() << std::endl;

   for (UInt_t i = 0; i < DATASZ; ++i)
      z[i] = (Double_t) h2polrebin->GetBin(h2polrebin->FindBin(smp[i], smp[DATASZ + i]));

   TCanvas* c4 = new TCanvas("glc4", "TH2Poly with kd-tree bin data",10,10,700,700);
   c4->Update();
   c4->Divide(1,2);
   c4->cd(1);
   h2polrebin->Draw("COLZ");  // draw as scatter plot

   c4->cd(2);
   h2polrebin->Draw("gllego");  // draw as lego

}
 kdTreeBinning.C:1
 kdTreeBinning.C:2
 kdTreeBinning.C:3
 kdTreeBinning.C:4
 kdTreeBinning.C:5
 kdTreeBinning.C:6
 kdTreeBinning.C:7
 kdTreeBinning.C:8
 kdTreeBinning.C:9
 kdTreeBinning.C:10
 kdTreeBinning.C:11
 kdTreeBinning.C:12
 kdTreeBinning.C:13
 kdTreeBinning.C:14
 kdTreeBinning.C:15
 kdTreeBinning.C:16
 kdTreeBinning.C:17
 kdTreeBinning.C:18
 kdTreeBinning.C:19
 kdTreeBinning.C:20
 kdTreeBinning.C:21
 kdTreeBinning.C:22
 kdTreeBinning.C:23
 kdTreeBinning.C:24
 kdTreeBinning.C:25
 kdTreeBinning.C:26
 kdTreeBinning.C:27
 kdTreeBinning.C:28
 kdTreeBinning.C:29
 kdTreeBinning.C:30
 kdTreeBinning.C:31
 kdTreeBinning.C:32
 kdTreeBinning.C:33
 kdTreeBinning.C:34
 kdTreeBinning.C:35
 kdTreeBinning.C:36
 kdTreeBinning.C:37
 kdTreeBinning.C:38
 kdTreeBinning.C:39
 kdTreeBinning.C:40
 kdTreeBinning.C:41
 kdTreeBinning.C:42
 kdTreeBinning.C:43
 kdTreeBinning.C:44
 kdTreeBinning.C:45
 kdTreeBinning.C:46
 kdTreeBinning.C:47
 kdTreeBinning.C:48
 kdTreeBinning.C:49
 kdTreeBinning.C:50
 kdTreeBinning.C:51
 kdTreeBinning.C:52
 kdTreeBinning.C:53
 kdTreeBinning.C:54
 kdTreeBinning.C:55
 kdTreeBinning.C:56
 kdTreeBinning.C:57
 kdTreeBinning.C:58
 kdTreeBinning.C:59
 kdTreeBinning.C:60
 kdTreeBinning.C:61
 kdTreeBinning.C:62
 kdTreeBinning.C:63
 kdTreeBinning.C:64
 kdTreeBinning.C:65
 kdTreeBinning.C:66
 kdTreeBinning.C:67
 kdTreeBinning.C:68
 kdTreeBinning.C:69
 kdTreeBinning.C:70
 kdTreeBinning.C:71
 kdTreeBinning.C:72
 kdTreeBinning.C:73
 kdTreeBinning.C:74
 kdTreeBinning.C:75
 kdTreeBinning.C:76
 kdTreeBinning.C:77
 kdTreeBinning.C:78
 kdTreeBinning.C:79
 kdTreeBinning.C:80
 kdTreeBinning.C:81
 kdTreeBinning.C:82
 kdTreeBinning.C:83
 kdTreeBinning.C:84
 kdTreeBinning.C:85
 kdTreeBinning.C:86
 kdTreeBinning.C:87
 kdTreeBinning.C:88
 kdTreeBinning.C:89
 kdTreeBinning.C:90
 kdTreeBinning.C:91
 kdTreeBinning.C:92
 kdTreeBinning.C:93
 kdTreeBinning.C:94
 kdTreeBinning.C:95
 kdTreeBinning.C:96
 kdTreeBinning.C:97
 kdTreeBinning.C:98
 kdTreeBinning.C:99
 kdTreeBinning.C:100
 kdTreeBinning.C:101
 kdTreeBinning.C:102
 kdTreeBinning.C:103
 kdTreeBinning.C:104
 kdTreeBinning.C:105
 kdTreeBinning.C:106
 kdTreeBinning.C:107
 kdTreeBinning.C:108
 kdTreeBinning.C:109
 kdTreeBinning.C:110
 kdTreeBinning.C:111
 kdTreeBinning.C:112
 kdTreeBinning.C:113
 kdTreeBinning.C:114
 kdTreeBinning.C:115
 kdTreeBinning.C:116
 kdTreeBinning.C:117
 kdTreeBinning.C:118
 kdTreeBinning.C:119
 kdTreeBinning.C:120
 kdTreeBinning.C:121
 kdTreeBinning.C:122
 kdTreeBinning.C:123
 kdTreeBinning.C:124
 kdTreeBinning.C:125
 kdTreeBinning.C:126
 kdTreeBinning.C:127
 kdTreeBinning.C:128
 kdTreeBinning.C:129
 kdTreeBinning.C:130
 kdTreeBinning.C:131
 kdTreeBinning.C:132
 kdTreeBinning.C:133
 kdTreeBinning.C:134
 kdTreeBinning.C:135
 kdTreeBinning.C:136
 kdTreeBinning.C:137
 kdTreeBinning.C:138
 kdTreeBinning.C:139
 kdTreeBinning.C:140
 kdTreeBinning.C:141
 kdTreeBinning.C:142
thumb
thumb
thumb
thumb