ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
glvox2.C
Go to the documentation of this file.
1 // This macro demonstrates how to use "glcol" option for TH3
2 // and how to create user defined TRANSFER FUNCTION:
3 // transfer function maps bin value to voxel's opacity.
4 // codomain is [0, 1] (1. - non-transparent, 0.5 is semitransparent, etc.)
5 // To pass transparency function into painting algorithm, you have to:
6 // 1) Create TF1 object (with symbolic expression like "0.5 * (sin(x) + 1)":
7 // ...
8 // TF1 * tf = new TF1("TransferFunction", "0.5 * (sin(x) + 1)", -10., 10.);
9 // ...
10 // IMPORTANT, the name of TF1 object MUST be "TransferFunction".
11 // 2) Add this function into a hist's list of functions:
12 // ...
13 // TList * lof = hist->GetListOfFunctions();
14 // if (lof) lof->Add(tf);
15 // ...
16 // It's also possible to use your own function and pass it into TF1, please read
17 // TF1 documentation to learn how.
18 //
19 // This macro is to be compiled: TF1 is extremely slow with CINT's function
20 // as an argument.
21 //
22 //
23 // Author: Timur Pocheptsov
24 //
25 
26 #if defined(__CINT__) && !defined(__MAKECINT__)
27 
28 {
29  //gSystem->AddIncludePath("-I$ROOTSYS/include");
30  TString macroName;
31  const char *rootSysPath = gSystem->Getenv("ROOTSYS");
32  if (rootSysPath) {
33  macroName = rootSysPath;
34  if (!macroName.EndsWith("/") && !macroName.EndsWith("\\"))
35  macroName += "/";
36  macroName += "tutorials/gl/";
37  }
38 
39  macroName += "glvox2.C++";
40  gROOT->LoadMacro(macroName.Data());
41  glvox2();
42 }
43 
44 #else
45 
46 #include "TStyle.h"
47 #include "TList.h"
48 #include "TH3.h"
49 #include "TF1.h"
50 
51 namespace {
52 
53 Double_t my_transfer_function(const Double_t *x, const Double_t * /*param*/)
54 {
55  // Bin values in our example range from -2 to 1.
56  // Let's make values from -2. to -1.5 more transparent:
57  if (*x < -1.5)
58  return 0.08;
59 
60  if (*x > -1.5 && *x < -0.5)
61  return 0.015;
62 
63  if (*x < -0.5 && *x < 0.)
64  return 0.02;
65 
66  if (*x > 0 && *x < 0.5)
67  return 0.03;
68 
69  if (*x > 0.8)
70  return 0.01;
71 
72  return 0.2;
73 }
74 
75 }
76 
77 void glvox2()
78 {
79  //Create and fill TH3.
80  const UInt_t nX = 30;
81  const Double_t xMin = -1., xMax = 1., xStep = (xMax - xMin) / (nX - 1);
82 
83  const UInt_t nY = 30;
84  const Double_t yMin = -1., yMax = 1., yStep = (yMax - yMin) / (nY - 1);
85 
86  const UInt_t nZ = 30;
87  const Double_t zMin = -1., zMax = 1., zStep = (zMax - zMin) / (nZ - 1);
88 
89  TH3F *hist = new TH3F("glvoxel", "glvoxel", nX, -1., 1., nY, -1., 1., nZ, -1., 1.);
90 
91  //Fill the histogram to create a "sphere".
92  for (UInt_t i = 0; i < nZ; ++i) {
93  const Double_t z = zMin + i * zStep;
94 
95  for (UInt_t j = 0; j < nY; ++j) {
96  const Double_t y = yMin + j * yStep;
97 
98  for (UInt_t k = 0; k < nX; ++k) {
99  const Double_t x = xMin + k * xStep;
100 
101  const Double_t val = 1. - (x * x + y * y + z * z);
102  hist->SetBinContent(k + 1, j + 1, i + 1, val);
103  }
104  }
105  }
106 
107  //Now, specify the transfer function.
108  TList * lf = hist->GetListOfFunctions();
109  if (lf) {
110  TF1 * tf = new TF1("TransferFunction", my_transfer_function);
111  lf->Add(tf);
112  }
113 
115  gStyle->SetPalette(1);
116 
117  hist->Draw("glcol");
118 }
119 
120 #endif
TList * GetListOfFunctions() const
Definition: TH1.h:244
3-D histogram with a float per channel (see TH1 documentation)}
Definition: TH3.h:272
virtual void SetBinContent(Int_t bin, Double_t content)
Set bin content.
Definition: TH3.cxx:3495
R__EXTERN TStyle * gStyle
Definition: TStyle.h:423
#define gROOT
Definition: TROOT.h:344
Basic string class.
Definition: TString.h:137
void glvox2()
Definition: glvox2.C:77
const char * Data() const
Definition: TString.h:349
virtual const char * Getenv(const char *env)
Get environment variable.
Definition: TSystem.cxx:1575
Float_t z[5]
Definition: Ifit.C:16
A doubly linked list.
Definition: TList.h:47
void SetCanvasPreferGL(Bool_t prefer=kTRUE)
Definition: TStyle.h:337
Bool_t EndsWith(const char *pat, ECaseCompare cmp=kExact) const
Return true if string ends with the specified string.
Definition: TString.cxx:2207
R__EXTERN TSystem * gSystem
Definition: TSystem.h:545
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2878
unsigned int UInt_t
Definition: RtypesCore.h:42
double Double_t
Definition: RtypesCore.h:55
Double_t y[n]
Definition: legend1.C:17
virtual void Add(TObject *obj)
Definition: TList.h:81
1-Dim function class
Definition: TF1.h:149
void SetPalette(Int_t ncolors=kBird, Int_t *colors=0, Float_t alpha=1.)
See TColor::SetPalette.
Definition: TStyle.cxx:1445