ROOT   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");
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 }
TParallelCoordVar
TParallelCoord axes.
Definition: TParallelCoordVar.h:24
TAxis
Class to manage histogram axis.
Definition: TAxis.h:30
TRandom::Gaus
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:263
f
#define f(i)
Definition: RSha256.hxx:104
TH3D
3-D histogram with a double per channel (see TH1 documentation)}
Definition: TH3.h:305
tree
Definition: tree.py:1
xmax
float xmax
Definition: THbookFile.cxx:95
THnBase::Fill
Long64_t Fill(const Double_t *x, Double_t w=1.)
Definition: THnBase.h:144
Long64_t
long long Long64_t
Definition: RtypesCore.h:73
TParallelCoord::GetVarList
TList * GetVarList()
Definition: TParallelCoord.h:102
TTree
A TTree represents a columnar dataset.
Definition: TTree.h:79
TRandom.h
TParallelCoordVar.h
TParallelCoord
Parallel Coordinates class.
Definition: TParallelCoord.h:28
Int_t
int Int_t
Definition: RtypesCore.h:45
kOrange
@ kOrange
Definition: Rtypes.h:67
sin
double sin(double)
x
Double_t x[n]
Definition: legend1.C:17
TString::Length
Ssiz_t Length() const
Definition: TString.h:410
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)
TAttLine::SetLineColor
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition: TAttLine.h:40
TCanvas.h
TTree.h
TString
Basic string class.
Definition: TString.h:136
TFile.h
TAxis::GetTitle
const char * GetTitle() const
Returns title of object.
Definition: TAxis.h:129
TROOT.h
THnSparse
Efficient multidimensional histogram.
Definition: THnSparse.h:36
TLeaf.h
TAxis::GetXmin
Double_t GetXmin() const
Definition: TAxis.h:133
xmin
float xmin
Definition: THbookFile.cxx:95
h
#define h(i)
Definition: RSha256.hxx:106
TLeaf
A TLeaf describes individual elements of a TBranch See TBranch structure in TTree.
Definition: TLeaf.h:57
TCanvas::cd
Definition: TCanvas.cxx:704
TParallelCoordVar::SetCurrentLimits
void SetCurrentLimits(Double_t min, Double_t max)
Set the limits within which one the entries must be painted.
Definition: TParallelCoordVar.cxx:920
Long_t
long Long_t
Definition: RtypesCore.h:54
gRandom
R__EXTERN TRandom * gRandom
Definition: TRandom.h:62
TParallelCoord.h
TH1::SetDirectory
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:8612
TRandom::Rndm
virtual Double_t Rndm()
Machine independent random number generator.
Definition: TRandom.cxx:541
TParallelCoordVar::SetHistogramBinning
void SetHistogramBinning(Int_t n=100)
Set the histogram binning.
Definition: TParallelCoordVar.cxx:882
THnSparse::Projection
TH1D * Projection(Int_t xDim, Option_t *option="") const
Forwards to THnBase::Projection().
Definition: THnSparse.h:133
TFile
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition: TFile.h:54
TH3.h
THnSparse.h
Double_t
double Double_t
Definition: RtypesCore.h:59
TCanvas
The Canvas class.
Definition: TCanvas.h:23
TObject::Write
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:795
TAxis.h
name
char name[80]
Definition: TGX11.cxx:110
TParallelCoordVar::SetTitle
void SetTitle(const char *)
Set the title of the TNamed.
Definition: TParallelCoordVar.h:111
d
#define d(i)
Definition: RSha256.hxx:102
TIter
Definition: TCollection.h:233
TNamed::GetName
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
TAxis::GetXmax
Double_t GetXmax() const
Definition: TAxis.h:134
TAxis::GetNbins
Int_t GetNbins() const
Definition: TAxis.h:121
THnSparseD
THnSparseT< TArrayD > THnSparseD
Definition: THnSparse.h:219
TH1::Draw
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:3050