Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
tree143_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/io/tree/tree143_drawsparse.C+
13/// ~~~
14///
15/// \macro_image
16/// \macro_code
17///
18/// \author Axel Naumann
19
20#include "TParallelCoord.h"
21#include "TParallelCoordVar.h"
22#include "TROOT.h"
23#include "TTree.h"
24#include "TLeaf.h"
25#include "THnSparse.h"
26#include "TAxis.h"
27#include "TCanvas.h"
28#include "TRandom.h"
29#include "TFile.h"
30#include "TH3.h"
31
32TTree* toTree(THnSparse* h)
33{
34 // Creates a TTree and fills it with the coordinates of all
35 // filled bins. The tree will have one branch for each dimension,
36 // and one for the bin content.
37
38 Int_t dim = h->GetNdimensions();
39 TString name(h->GetName()); name += "_tree";
40 TString title(h->GetTitle()); title += " tree";
41
42 TTree* tree = new TTree(name, title);
43 Double_t* x = new Double_t[dim + 1];
44 memset(x, 0, sizeof(Double_t) * (dim + 1));
45
46 TString branchname;
47 for (Int_t d = 0; d < dim; ++d) {
48 if (branchname.Length())
49 branchname += ":";
50 TAxis* axis = h->GetAxis(d);
51 branchname += axis->GetName();
52 branchname += "/D";
53 }
54 tree->Branch("coord", x, branchname);
55 tree->Branch("bincontent", &x[dim], "bincontent/D");
56
57 Int_t *bins = new Int_t[dim];
58 for (Long64_t i = 0; i < h->GetNbins(); ++i) {
59 x[dim] = h->GetBinContent(i, bins);
60 for (Int_t d = 0; d < dim; ++d) {
61 x[d] = h->GetAxis(d)->GetBinCenter(bins[d]);
62 }
63
64 tree->Fill();
65 }
66
67 delete [] bins;
68 //delete [] x;
69 return tree;
70}
71
72
73void drawsparse_draw(THnSparse* h)
74{
75 // Draw a THnSparse using TParallelCoord, creating a temporary TTree.
76
77 TTree* tree = toTree(h);
78
79 TString whatToDraw;
80 TIter iLeaf(tree->GetListOfLeaves());
81 const TLeaf* leaf = nullptr;
82 while ((leaf = (const TLeaf*)iLeaf())) {
83 if (whatToDraw.Length())
84 whatToDraw += ":";
85 whatToDraw += leaf->GetName();
86 }
87 tree->Draw(whatToDraw, "", "para");
88 TParallelCoord* parallelCoord = (TParallelCoord*)gPad->GetListOfPrimitives()->FindObject("ParaCoord");
89
90 TIter iVar(parallelCoord->GetVarList());
91 TParallelCoordVar* var = nullptr;
92 for (Int_t d = 0;(var = (TParallelCoordVar*) iVar()) && d < h->GetNdimensions(); ++d) {
93 TAxis* axis = h->GetAxis(d);
94 var->SetHistogramBinning(axis->GetNbins());
95 var->SetCurrentLimits(axis->GetXmin(), axis->GetXmax());
96 var->SetTitle(axis->GetTitle());
97 }
98 var->SetTitle("bin content");
99}
100
101void tree143_drawsparse()
102{
103 // create a THnSparse and draw it.
104
105 const Int_t ndims = 8;
106 Int_t bins[ndims] = {10, 10, 5, 30, 10, 4, 18, 12};
107 Double_t xmin[ndims] = {-5., -10., -1000., -3., 0., 0., 0., 0.};
108 Double_t xmax[ndims] = {10., 70., 3000., 3., 5., 2., 2., 5.};
109 auto hs = new THnSparseD("hs", "Sparse Histogram", ndims, bins, xmin, xmax);
110
111 // fill it
112 Double_t x[ndims];
113 for (Long_t i = 0; i < 100000; ++i) {
114 for (Int_t d = 0; d < ndims; ++d) {
115 switch (d) {
116 case 0:
117 x[d] = gRandom->Gaus() * 2 + 3.;
118 break;
119 case 1:
120 case 2:
121 case 3:
122 x[d] = (x[d - 1] * x[d - 1] - 1.5) / 1.5 + (0.5 * gRandom->Rndm());
123 break;
124 default:
125 x[d] = sin(gRandom->Gaus()*i/1000.) + 1.;
126 }
127 }
128 hs->Fill(x);
129 }
130
131 auto f = TFile::Open("drawsparse.root", "RECREATE");
132
133 auto canv = new TCanvas("hDrawSparse", "Drawing a sparse hist");
134 canv->Divide(2);
135
136 // draw it
137 canv->cd(1);
138 drawsparse_draw(hs);
139
140 // project it
141 canv->cd(2);
142 TH3D* h3proj = hs->Projection(2, 3, 6);
143 h3proj->SetLineColor(kOrange);
144 h3proj->SetDirectory(nullptr);
145 h3proj->Draw("lego1");
146
147 // save everything to a file
148 canv->Write();
149 hs->Write();
150 h3proj->Write();
151
152 delete f;
153}
#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:69
@ kOrange
Definition Rtypes.h:67
char name[80]
Definition TGX11.cxx:110
float xmin
float xmax
THnSparseT< TArrayD > THnSparseD
Definition THnSparse.h:221
R__EXTERN TRandom * gRandom
Definition TRandom.h:62
#define gPad
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition TAttLine.h:40
Class to manage histogram axis.
Definition TAxis.h:32
const char * GetTitle() const override
Returns title of object.
Definition TAxis.h:137
Double_t GetXmax() const
Definition TAxis.h:142
Double_t GetXmin() const
Definition TAxis.h:141
Int_t GetNbins() const
Definition TAxis.h:127
The Canvas class.
Definition TCanvas.h:23
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
Create / open a file.
Definition TFile.cxx:4086
virtual void SetDirectory(TDirectory *dir)
By default, when a histogram is created, it is added to the list of histogram objects in the current ...
Definition TH1.cxx:8970
void Draw(Option_t *option="") override
Draw this histogram with options.
Definition TH1.cxx:3068
3-D histogram with a double per channel (see TH1 documentation)
Definition TH3.h:364
Efficient multidimensional histogram.
Definition THnSparse.h:37
A TLeaf describes individual elements of a TBranch See TBranch structure in TTree.
Definition TLeaf.h:57
const char * GetName() const override
Returns name of object.
Definition TNamed.h:47
virtual TObject * FindObject(const char *name) const
Must be redefined in derived classes.
Definition TObject.cxx:420
virtual Int_t Write(const char *name=nullptr, Int_t option=0, Int_t bufsize=0)
Write this object to the current directory.
Definition TObject.cxx:898
TParallelCoord axes.
void SetTitle(const char *) override
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:275
Double_t Rndm() override
Machine independent random number generator.
Definition TRandom.cxx:559
Basic string class.
Definition TString.h:139
Ssiz_t Length() const
Definition TString.h:417
A TTree represents a columnar dataset.
Definition TTree.h:79
RVec< PromoteType< T > > sin(const RVec< T > &v)
Definition RVec.hxx:1851
Double_t x[n]
Definition legend1.C:17