Logo ROOT   6.10/09
Reference Guide
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 }
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
virtual Int_t Write(const char *name=0, Int_t option=0, Int_t bufsize=0)
Write this object to the current directory.
Definition: TObject.cxx:778
A TLeaf describes individual elements of a TBranch See TBranch structure in TTree.
Definition: TLeaf.h:32
float xmin
Definition: THbookFile.cxx:93
long long Long64_t
Definition: RtypesCore.h:69
virtual void SetDirectory(TDirectory *dir)
By default when an histogram is created, it is added to the list of histogram objects in the current ...
Definition: TH1.cxx:8053
Long64_t Fill(const Double_t *x, Double_t w=1.)
Definition: THnBase.h:143
virtual Double_t Gaus(Double_t mean=0, Double_t sigma=1)
Samples a random number from the standard Normal (Gaussian) Distribution with the given mean and sigm...
Definition: TRandom.cxx:235
virtual Int_t Fill()
Fill all branches.
Definition: TTree.cxx:4383
TH1 * h
Definition: legend2.C:5
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:46
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:679
Basic string class.
Definition: TString.h:129
int Int_t
Definition: RtypesCore.h:41
Long64_t GetNbins() const
Definition: THnSparse.h:91
THnSparseT< TArrayD > THnSparseD
Definition: THnSparse.h:216
TParallelCoord axes.
Double_t GetXmin() const
Definition: TAxis.h:133
Double_t x[n]
Definition: legend1.C:17
TList * GetVarList()
Efficient multidimensional histogram.
Definition: THnSparse.h:36
TH1D * Projection(Int_t xDim, Option_t *option="") const
Definition: THnSparse.h:127
virtual Double_t GetBinCenter(Int_t bin) const
Return center of bin.
Definition: TAxis.cxx:464
double sin(double)
virtual Double_t Rndm()
Machine independent random number generator.
Definition: TRandom.cxx:512
Double_t GetBinContent(const Int_t *idx) const
Definition: THnSparse.h:116
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition: TAttLine.h:40
TAxis * GetAxis(Int_t dim) const
Definition: THnBase.h:125
const char * GetTitle() const
Returns title of object.
Definition: TAxis.h:129
Class to manage histogram axis.
Definition: TAxis.h:30
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2851
tomato 3-D histogram with a double per channel (see TH1 documentation)}
Definition: TH3.h:304
Ssiz_t Length() const
Definition: TString.h:388
float xmax
Definition: THbookFile.cxx:93
R__EXTERN TRandom * gRandom
Definition: TRandom.h:62
long Long_t
Definition: RtypesCore.h:50
The Canvas class.
Definition: TCanvas.h:31
double f(double x)
double Double_t
Definition: RtypesCore.h:55
virtual void Draw(Option_t *opt)
Default Draw method for all objects.
Definition: TTree.h:355
Int_t GetNdimensions() const
Definition: THnBase.h:135
Parallel Coordinates class.
virtual Int_t Branch(TCollection *list, Int_t bufsize=32000, Int_t splitlevel=99, const char *name="")
Create one branch for each element in the collection.
Definition: TTree.cxx:1660
virtual void Divide(Int_t nx=1, Int_t ny=1, Float_t xmargin=0.01, Float_t ymargin=0.01, Int_t color=0)
Automatic pad generation by division.
Definition: TPad.cxx:1135
#define gPad
Definition: TVirtualPad.h:284
Definition: tree.py:1
A TTree object has a header with a name and a title.
Definition: TTree.h:78
Int_t GetNbins() const
Definition: TAxis.h:121
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition: TNamed.cxx:155
Double_t GetXmax() const
Definition: TAxis.h:134
Definition: Rtypes.h:57
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:48
virtual TObjArray * GetListOfLeaves()
Definition: TTree.h:406