00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "TParallelCoord.h"
00015 #include "TParallelCoordVar.h"
00016 #include "TROOT.h"
00017 #include "TTree.h"
00018 #include "TLeaf.h"
00019 #include "THnSparse.h"
00020 #include "TAxis.h"
00021 #include "TCanvas.h"
00022 #include "TRandom.h"
00023 #include "TFile.h"
00024 #include "TH3.h"
00025
00026
00027 TTree* toTree(THnSparse* h)
00028 {
00029
00030
00031
00032
00033 Int_t dim = h->GetNdimensions();
00034 TString name(h->GetName()); name += "_tree";
00035 TString title(h->GetTitle()); title += " tree";
00036
00037 TTree* tree = new TTree(name, title);
00038 Double_t* x = new Double_t[dim + 1];
00039 memset(x, 0, sizeof(Double_t) * (dim + 1));
00040
00041 TString branchname;
00042 for (Int_t d = 0; d < dim; ++d) {
00043 if (branchname.Length())
00044 branchname += ":";
00045 TAxis* axis = h->GetAxis(d);
00046 branchname += axis->GetName();
00047 branchname += "/D";
00048 }
00049 tree->Branch("coord", x, branchname);
00050 tree->Branch("bincontent", &x[dim], "bincontent/D");
00051
00052 Int_t *bins = new Int_t[dim];
00053 for (Long64_t i = 0; i < h->GetNbins(); ++i) {
00054 x[dim] = h->GetBinContent(i, bins);
00055 for (Int_t d = 0; d < dim; ++d) {
00056 x[d] = h->GetAxis(d)->GetBinCenter(bins[d]);
00057 }
00058
00059 tree->Fill();
00060 }
00061
00062 delete [] bins;
00063
00064 return tree;
00065 }
00066
00067
00068
00069 void drawsparse_draw(THnSparse* h)
00070 {
00071
00072
00073 TTree* tree = toTree(h);
00074
00075 TString whatToDraw;
00076 TIter iLeaf(tree->GetListOfLeaves());
00077 const TLeaf* leaf = 0;
00078 while ((leaf = (const TLeaf*)iLeaf())) {
00079 if (whatToDraw.Length())
00080 whatToDraw += ":";
00081 whatToDraw += leaf->GetName();
00082 }
00083 tree->Draw(whatToDraw, "", "para");
00084 TParallelCoord* parallelCoord = (TParallelCoord*)gPad->GetListOfPrimitives()->FindObject("ParaCoord");
00085
00086 TIter iVar(parallelCoord->GetVarList());
00087 TParallelCoordVar* var = 0;
00088 for (Int_t d = 0;(var = (TParallelCoordVar*) iVar()) && d < h->GetNdimensions(); ++d) {
00089 TAxis* axis = h->GetAxis(d);
00090 var->SetHistogramBinning(axis->GetNbins());
00091 var->SetCurrentLimits(axis->GetXmin(), axis->GetXmax());
00092 var->SetTitle(axis->GetTitle());
00093 }
00094 var->SetTitle("bin content");
00095 }
00096
00097
00098 void drawsparse()
00099 {
00100
00101
00102 #ifdef __CINT__
00103 printf("For performance reasons we advise to run \".x drawsparse.C+\"\n");
00104 #endif
00105
00106 const Int_t ndims = 8;
00107 Int_t bins[ndims] = {10, 10, 5, 30, 10, 4, 18, 12};
00108 Double_t xmin[ndims] = {-5., -10., -1000., -3., 0., 0., 0., 0.};
00109 Double_t xmax[ndims] = {10., 70., 3000., 3., 5., 2., 2., 5.};
00110 THnSparse* hs = new THnSparseD("hs", "Sparse Histogram", ndims, bins, xmin, xmax);
00111
00112
00113 Double_t x[ndims];
00114 for (Long_t i = 0; i < 100000; ++i) {
00115 for (Int_t d = 0; d < ndims; ++d) {
00116 switch (d) {
00117 case 0: x[d] = gRandom->Gaus()*2 + 3.; break;
00118 case 1:
00119 case 2:
00120 case 3: x[d] = (x[d-1]*x[d-1] - 1.5)/1.5 + (0.5*gRandom->Rndm()); break;
00121 default: x[d] = sin(gRandom->Gaus()*i/1000.) + 1.;
00122 }
00123 }
00124 hs->Fill(x);
00125 }
00126
00127
00128 TFile* f = new TFile("drawsparse.root","RECREATE");
00129
00130 TCanvas* canv = new TCanvas("hDrawSparse", "Drawing a sparse hist");
00131 canv->Divide(2);
00132
00133
00134 canv->cd(1);
00135 drawsparse_draw(hs);
00136
00137
00138 canv->cd(2);
00139 TH3D* h3proj = hs->Projection(2, 3, 6);
00140 h3proj->SetLineColor(kOrange);
00141 h3proj->SetDirectory(0);
00142 h3proj->Draw("lego1");
00143
00144
00145 canv->Write();
00146 hs->Write();
00147 h3proj->Write();
00148
00149 delete f;
00150 }