Logo ROOT  
Reference Guide
drawsparse.C File Reference

Detailed Description

View in nbviewer Open in SWAN Convert a THnSparse to a TTree using efficient iteration through the THnSparse and draw a THnSparse using TParallelCoord. The plot will contain one line for each filled bin, with the bin's coordinates on each axis, and the bin's content on the rightmost axis.

Run as

.L $ROOTSYS/tutorials/tree/drawsparse.C+
#include "TParallelCoord.h"
#include "TROOT.h"
#include "TTree.h"
#include "TLeaf.h"
#include "THnSparse.h"
#include "TAxis.h"
#include "TCanvas.h"
#include "TRandom.h"
#include "TFile.h"
#include "TH3.h"
TTree* toTree(THnSparse* h)
{
// Creates a TTree and fills it with the coordinates of all
// filled bins. The tree will have one branch for each dimension,
// and one for the bin content.
Int_t dim = h->GetNdimensions();
TString name(h->GetName()); name += "_tree";
TString title(h->GetTitle()); title += " tree";
TTree* tree = new TTree(name, title);
Double_t* x = new Double_t[dim + 1];
memset(x, 0, sizeof(Double_t) * (dim + 1));
TString branchname;
for (Int_t d = 0; d < dim; ++d) {
if (branchname.Length())
branchname += ":";
TAxis* axis = h->GetAxis(d);
branchname += axis->GetName();
branchname += "/D";
}
tree->Branch("coord", x, branchname);
tree->Branch("bincontent", &x[dim], "bincontent/D");
Int_t *bins = new Int_t[dim];
for (Long64_t i = 0; i < h->GetNbins(); ++i) {
x[dim] = h->GetBinContent(i, bins);
for (Int_t d = 0; d < dim; ++d) {
x[d] = h->GetAxis(d)->GetBinCenter(bins[d]);
}
tree->Fill();
}
delete [] bins;
//delete [] x;
return tree;
}
void drawsparse_draw(THnSparse* h)
{
// Draw a THnSparse using TParallelCoord, creating a temporary TTree.
TTree* tree = toTree(h);
TString whatToDraw;
TIter iLeaf(tree->GetListOfLeaves());
const TLeaf* leaf = 0;
while ((leaf = (const TLeaf*)iLeaf())) {
if (whatToDraw.Length())
whatToDraw += ":";
whatToDraw += leaf->GetName();
}
tree->Draw(whatToDraw, "", "para");
TParallelCoord* parallelCoord = (TParallelCoord*)gPad->GetListOfPrimitives()->FindObject("ParaCoord");
TIter iVar(parallelCoord->GetVarList());
for (Int_t d = 0;(var = (TParallelCoordVar*) iVar()) && d < h->GetNdimensions(); ++d) {
TAxis* axis = h->GetAxis(d);
var->SetCurrentLimits(axis->GetXmin(), axis->GetXmax());
var->SetTitle(axis->GetTitle());
}
var->SetTitle("bin content");
}
void drawsparse()
{
// create a THnSparse and draw it.
const Int_t ndims = 8;
Int_t bins[ndims] = {10, 10, 5, 30, 10, 4, 18, 12};
Double_t xmin[ndims] = {-5., -10., -1000., -3., 0., 0., 0., 0.};
Double_t xmax[ndims] = {10., 70., 3000., 3., 5., 2., 2., 5.};
THnSparse* hs = new THnSparseD("hs", "Sparse Histogram", ndims, bins, xmin, xmax);
// fill it
Double_t x[ndims];
for (Long_t i = 0; i < 100000; ++i) {
for (Int_t d = 0; d < ndims; ++d) {
switch (d) {
case 0: x[d] = gRandom->Gaus()*2 + 3.; break;
case 1:
case 2:
case 3: x[d] = (x[d-1]*x[d-1] - 1.5)/1.5 + (0.5*gRandom->Rndm()); break;
default: x[d] = sin(gRandom->Gaus()*i/1000.) + 1.;
}
}
hs->Fill(x);
}
TFile* f = new TFile("drawsparse.root","RECREATE");
TCanvas* canv = new TCanvas("hDrawSparse", "Drawing a sparse hist");
canv->Divide(2);
// draw it
canv->cd(1);
drawsparse_draw(hs);
// project it
canv->cd(2);
TH3D* h3proj = hs->Projection(2, 3, 6);
h3proj->SetLineColor(kOrange);
h3proj->SetDirectory(0);
h3proj->Draw("lego1");
// save everything to a file
canv->Write();
hs->Write();
h3proj->Write();
delete f;
}
Author
Axel Naumann

Definition in file drawsparse.C.

TParallelCoordVar
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:122
TH3D
3-D histogram with a double per channel (see TH1 documentation)}
Definition: TH3.h:304
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
Definition: TTree.h:79
TRandom.h
TParallelCoordVar.h
TParallelCoord
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
TPad::Divide
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:1166
TCanvas.h
TTree.h
TString
Definition: TString.h:136
TFile.h
TAxis::GetTitle
const char * GetTitle() const
Returns title of object.
Definition: TAxis.h:129
TROOT.h
THnSparse
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:124
TLeaf
Definition: TLeaf.h:57
TCanvas::cd
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
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:8392
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
Definition: TFile.h:54
TH3.h
THnSparse.h
Double_t
double Double_t
Definition: RtypesCore.h:59
TCanvas
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:120
gPad
#define gPad
Definition: TVirtualPad.h:287
TIter
Definition: TCollection.h:233
TNamed::GetName
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:53
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:2997