Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
drawsparse.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_tree
3 /// \notebook
4 /// Convert a THnSparse to a TTree using efficient iteration through the THnSparse
5 /// and draw a THnSparse using TParallelCoord.
6 /// The plot will contain one line for each filled bin,
7 /// with the bin's coordinates on each axis, and the bin's content on
8 /// the rightmost axis.
9 ///
10 /// Run as
11 /// ~~~
12 /// .L $ROOTSYS/tutorials/tree/drawsparse.C+
13 /// ~~~
14 ///
15 /// \macro_image
16 /// \macro_code
17 ///
18 /// \author Axel Naumann
19 
20 
21 
22 #include "TParallelCoord.h"
23 #include "TParallelCoordVar.h"
24 #include "TROOT.h"
25 #include "TTree.h"
26 #include "TLeaf.h"
27 #include "THnSparse.h"
28 #include "TAxis.h"
29 #include "TCanvas.h"
30 #include "TRandom.h"
31 #include "TFile.h"
32 #include "TH3.h"
33 
34 TTree* toTree(THnSparse* h)
35 {
36  // Creates a TTree and fills it with the coordinates of all
37  // filled bins. The tree will have one branch for each dimension,
38  // and one for the bin content.
39 
40  Int_t dim = h->GetNdimensions();
41  TString name(h->GetName()); name += "_tree";
42  TString title(h->GetTitle()); title += " tree";
43 
44  TTree* tree = new TTree(name, title);
45  Double_t* x = new Double_t[dim + 1];
46  memset(x, 0, sizeof(Double_t) * (dim + 1));
47 
48  TString branchname;
49  for (Int_t d = 0; d < dim; ++d) {
50  if (branchname.Length())
51  branchname += ":";
52  TAxis* axis = h->GetAxis(d);
53  branchname += axis->GetName();
54  branchname += "/D";
55  }
56  tree->Branch("coord", x, branchname);
57  tree->Branch("bincontent", &x[dim], "bincontent/D");
58 
59  Int_t *bins = new Int_t[dim];
60  for (Long64_t i = 0; i < h->GetNbins(); ++i) {
61  x[dim] = h->GetBinContent(i, bins);
62  for (Int_t d = 0; d < dim; ++d) {
63  x[d] = h->GetAxis(d)->GetBinCenter(bins[d]);
64  }
65 
66  tree->Fill();
67  }
68 
69  delete [] bins;
70  //delete [] x;
71  return tree;
72 }
73 
74 
75 void drawsparse_draw(THnSparse* h)
76 {
77  // Draw a THnSparse using TParallelCoord, creating a temporary TTree.
78 
79  TTree* tree = toTree(h);
80 
81  TString whatToDraw;
82  TIter iLeaf(tree->GetListOfLeaves());
83  const TLeaf* leaf = 0;
84  while ((leaf = (const TLeaf*)iLeaf())) {
85  if (whatToDraw.Length())
86  whatToDraw += ":";
87  whatToDraw += leaf->GetName();
88  }
89  tree->Draw(whatToDraw, "", "para");
90  TParallelCoord* parallelCoord = (TParallelCoord*)gPad->GetListOfPrimitives()->FindObject("ParaCoord");
91 
92  TIter iVar(parallelCoord->GetVarList());
93  TParallelCoordVar* var = 0;
94  for (Int_t d = 0;(var = (TParallelCoordVar*) iVar()) && d < h->GetNdimensions(); ++d) {
95  TAxis* axis = h->GetAxis(d);
96  var->SetHistogramBinning(axis->GetNbins());
97  var->SetCurrentLimits(axis->GetXmin(), axis->GetXmax());
98  var->SetTitle(axis->GetTitle());
99  }
100  var->SetTitle("bin content");
101 }
102 
103 void drawsparse()
104 {
105  // create a THnSparse and draw it.
106 
107  const Int_t ndims = 8;
108  Int_t bins[ndims] = {10, 10, 5, 30, 10, 4, 18, 12};
109  Double_t xmin[ndims] = {-5., -10., -1000., -3., 0., 0., 0., 0.};
110  Double_t xmax[ndims] = {10., 70., 3000., 3., 5., 2., 2., 5.};
111  THnSparse* hs = new THnSparseD("hs", "Sparse Histogram", ndims, bins, xmin, xmax);
112 
113  // fill it
114  Double_t x[ndims];
115  for (Long_t i = 0; i < 100000; ++i) {
116  for (Int_t d = 0; d < ndims; ++d) {
117  switch (d) {
118  case 0: x[d] = gRandom->Gaus()*2 + 3.; break;
119  case 1:
120  case 2:
121  case 3: x[d] = (x[d-1]*x[d-1] - 1.5)/1.5 + (0.5*gRandom->Rndm()); break;
122  default: x[d] = sin(gRandom->Gaus()*i/1000.) + 1.;
123  }
124  }
125  hs->Fill(x);
126  }
127 
128 
129  TFile* f = new TFile("drawsparse.root","RECREATE");
130 
131  TCanvas* canv = new TCanvas("hDrawSparse", "Drawing a sparse hist");
132  canv->Divide(2);
133 
134  // draw it
135  canv->cd(1);
136  drawsparse_draw(hs);
137 
138  // project it
139  canv->cd(2);
140  TH3D* h3proj = hs->Projection(2, 3, 6);
141  h3proj->SetLineColor(kOrange);
142  h3proj->SetDirectory(0);
143  h3proj->Draw("lego1");
144 
145  // save everything to a file
146  canv->Write();
147  hs->Write();
148  h3proj->Write();
149 
150  delete f;
151 }