Logo ROOT  
Reference Guide
transparentpad.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_gl
3/// This macro demonstrates semi-transparent pads.
4/// Requires OpenGL.
5///
6/// \macro_image(nobatch)
7/// \macro_code
8///
9/// \author Timur Pocheptsov
10
11//Includes for ACLiC (cling does not need them).
12#include "TCanvas.h"
13#include "TStyle.h"
14#include "TError.h"
15#include "TColor.h"
16#include "TH1F.h"
17
18//Aux. functions for tutorials/gl.
19#include "customcolorgl.h"
20
21void transparentpad()
22{
23 //1. Try to 'allocate' free indices for our custom colors -
24 //we can use hard-coded indices like 1001, 1002, 1003 ... but
25 //I prefer to find free indices in a ROOT's color table
26 //to avoid possible conflicts with other tutorials.
28
29 Int_t indices[3] = {};
31 ::Error("transparentpad", "failed to create new custom colors");
32 return;
33 }
34
35 //2. Create a TCanvas.
36 TCanvas * const c1 = new TCanvas("transparent pad","transparent pad demo", 10, 10, 900, 500);
37 //We can check gVirtualX (its type):
38 if (!c1->UseGL()) {
39 ::Warning("transparentpad", "You can see the transparency ONLY in a pdf or png output (\"File\"->\"Save As\" ->...)\n"
40 "To have transparency in a canvas graphics, you need OpenGL rendering enabled");
41 }
42
43 //2. Create special transparent colors.
44 new TColor(indices[0], 1., 0.2, 0.2, "transparent_pink", 0.25);
45 new TColor(indices[1], 0.2, 1., 0.2, "transparent_green", 0.25);
46 new TColor(indices[2], 0.2, 2., 1., "transparent_blue", 0.15);
47
48 //3. Some arbitrary histograms.
49 TH1F * const h1 = new TH1F("TH1F 1", "TH1F 1", 100, -1.5, 1.5);
50 h1->FillRandom("gaus");
51
52 TH1F * const h2 = new TH1F("TH1F 2", "TH1F 2", 100, -1.5, 0.);
53 h2->FillRandom("gaus");
54
55 TH1F * const h3 = new TH1F("TH1F 3", "TH1F 3", 100, 0.5, 2.);
56 h3->FillRandom("landau");
57
58 //4. Now overlapping transparent pads.
59 TPad * const pad1 = new TPad("transparent pad 1", "transparent pad 1", 0.1, 0.1, 0.7, 0.7);
60 pad1->SetFillColor(indices[0]);//here's the magic!
61 pad1->cd();
62 h1->Draw("lego2");
63 c1->cd();
64 pad1->Draw();
65
66 TPad * const pad2 = new TPad("transparent pad 2", "transparent pad 2", 0.2, 0.2, 0.8, 0.8);
67 pad2->SetFillColor(indices[1]);//here's the magic!
68 pad2->cd();
69 h2->Draw();
70 c1->cd();
71 pad2->Draw();
72
73 TPad * const pad3 = new TPad("transparent pad 3", "transparent pad 3", 0.3, 0.3, 0.9, 0.9);
74 pad3->SetFillColor(indices[2]);//here's the magic!
75 pad3->cd();
76 h3->Draw();
77 c1->cd();
78 pad3->Draw();
79}
80
int Int_t
Definition: RtypesCore.h:45
const Bool_t kTRUE
Definition: RtypesCore.h:100
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition: TError.cxx:187
void Warning(const char *location, const char *msgfmt,...)
Use this function in warning situations.
Definition: TError.cxx:231
R__EXTERN TStyle * gStyle
Definition: TStyle.h:414
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition: TAttFill.h:37
The Canvas class.
Definition: TCanvas.h:23
The color creation and management class.
Definition: TColor.h:19
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:574
virtual void FillRandom(const char *fname, Int_t ntimes=5000, TRandom *rng=nullptr)
Fill histogram following distribution in function fname.
Definition: TH1.cxx:3523
void Draw(Option_t *option="") override
Draw this histogram with options.
Definition: TH1.cxx:3070
The most important graphics class in the ROOT system.
Definition: TPad.h:27
TVirtualPad * cd(Int_t subpadnumber=0) override
Set Current pad.
Definition: TPad.cxx:597
void Draw(Option_t *option="") override
Draw Pad in Current pad (re-parent pad if necessary).
Definition: TPad.cxx:1267
void SetCanvasPreferGL(Bool_t prefer=kTRUE)
Definition: TStyle.h:326
return c1
Definition: legend1.C:41
TH1F * h1
Definition: legend1.C:5
unsigned FindFreeCustomColorIndices(T(&indices)[N])
Definition: customcolorgl.h:40