34 TTree* toTree(THnSparse* h)
40 Int_t dim = h->GetNdimensions();
41 TString name(h->GetName()); name +=
"_tree";
42 TString title(h->GetTitle()); title +=
" tree";
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));
49 for (Int_t d = 0; d < dim; ++d) {
50 if (branchname.Length())
52 TAxis* axis = h->GetAxis(d);
53 branchname += axis->GetName();
56 tree->Branch(
"coord", x, branchname);
57 tree->Branch(
"bincontent", &x[dim],
"bincontent/D");
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]);
75 void drawsparse_draw(THnSparse* h)
79 TTree* tree = toTree(h);
82 TIter iLeaf(tree->GetListOfLeaves());
83 const TLeaf* leaf = 0;
84 while ((leaf = (
const TLeaf*)iLeaf())) {
85 if (whatToDraw.Length())
87 whatToDraw += leaf->GetName();
89 tree->Draw(whatToDraw,
"",
"para");
90 TParallelCoord* parallelCoord = (TParallelCoord*)gPad->GetListOfPrimitives()->FindObject(
"ParaCoord");
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());
100 var->SetTitle(
"bin content");
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);
115 for (Long_t i = 0; i < 100000; ++i) {
116 for (Int_t d = 0; d < ndims; ++d) {
118 case 0: x[d] = gRandom->Gaus()*2 + 3.;
break;
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.;
129 TFile* f =
new TFile(
"drawsparse.root",
"RECREATE");
131 TCanvas* canv =
new TCanvas(
"hDrawSparse",
"Drawing a sparse hist");
140 TH3D* h3proj = hs->Projection(2, 3, 6);
141 h3proj->SetLineColor(kOrange);
142 h3proj->SetDirectory(0);
143 h3proj->Draw(
"lego1");