ROOT   Reference Guide
Searching...
No Matches
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
34TTree* 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
75void 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
103void 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}
#define d(i)
Definition RSha256.hxx:102
#define f(i)
Definition RSha256.hxx:104
#define h(i)
Definition RSha256.hxx:106
int Int_t
Definition RtypesCore.h:45
long Long_t
Definition RtypesCore.h:54
double Double_t
Definition RtypesCore.h:59
long long Long64_t
Definition RtypesCore.h:73
@ kOrange
Definition Rtypes.h:67
char name[80]
Definition TGX11.cxx:110
float xmin
float xmax
THnSparseT< TArrayD > THnSparseD
Definition THnSparse.h:219
double sin(double)
R__EXTERN TRandom * gRandom
Definition TRandom.h:62
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition TAttLine.h:40
Class to manage histogram axis.
Definition TAxis.h:30
Double_t GetXmax() const
Definition TAxis.h:134
Double_t GetXmin() const
Definition TAxis.h:133
Int_t GetNbins() const
Definition TAxis.h:121
const char * GetTitle() const
Returns title of object.
Definition TAxis.h:129
The Canvas class.
Definition TCanvas.h:23
Definition TCanvas.cxx:708
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition TFile.h:54
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:8777
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition TH1.cxx:3073
3-D histogram with a double per channel (see TH1 documentation)}
Definition TH3.h:305
Long64_t Fill(const Double_t *x, Double_t w=1.)
Definition THnBase.h:144
Efficient multidimensional histogram.
Definition THnSparse.h:36
TH1D * Projection(Int_t xDim, Option_t *option="") const
Forwards to THnBase::Projection().
Definition THnSparse.h:133
A TLeaf describes individual elements of a TBranch See TBranch structure in TTree.
Definition TLeaf.h:57
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:798
virtual TObject * FindObject(const char *name) const
Must be redefined in derived classes.
Definition TObject.cxx:323
void Divide(Int_t nx=1, Int_t ny=1, Float_t xmargin=0.01, Float_t ymargin=0.01, Int_t color=0) override
TParallelCoord axes.
void SetTitle(const char *)
Set the title of the TNamed.
void SetHistogramBinning(Int_t n=100)
Set the histogram binning.
void SetCurrentLimits(Double_t min, Double_t max)
Set the limits within which one the entries must be painted.
Parallel Coordinates class.
TList * GetVarList()
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:274
virtual Double_t Rndm()
Machine independent random number generator.
Definition TRandom.cxx:552
Basic string class.
Definition TString.h:136
Ssiz_t Length() const
Definition TString.h:410
A TTree represents a columnar dataset.
Definition TTree.h:79
Double_t x[n]
Definition legend1.C:17
Definition tree.py:1