ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
transp_text.C
Go to the documentation of this file.
1 //This macro is based on labels1.C by Rene Brun.
2 //Updated by Timur Pocheptsov to use transparent text (12/10/2012).
3 //Requires OS X and ROOT configured with --enable-cocoa.
4 
5 
6 //Includes for ACLiC (cling does not need them).
7 #include "TVirtualX.h"
8 #include "TPaveText.h"
9 #include "TCanvas.h"
10 #include "TRandom.h"
11 #include "TError.h"
12 #include "TColor.h"
13 #include "TH1F.h"
14 
15 //Aux. functions for tutorials/cocoa.
16 #include "customcolor.h"
17 
19 {
20  //1. Try to 'allocate' free indices for our custom colors -
21  //we can use hard-coded indices like 1001, 1002, 1003 ... but
22  //I prefer to find free indices in a ROOT's color table
23  //to avoid possible conflicts with other tutorials.
24  Color_t indices[2] = {};
26  ::Error("transp_text", "failed to create new custom colors");
27  return;
28  }
29 
30  //2. Create special transparent colors for both pavetext fill color and text color.
31  const Color_t grayColorIndex = indices[0], blackColorIndex = indices[1];
32  new TColor(grayColorIndex, 0.8, 0.8, 0.8, "transparent_gray", 0.85);
33  new TColor(blackColorIndex, 0., 0., 0., "transparent_black", 0.5);
34 
35  //3. Create a TCanvas first to force gVirtualX initialization.
36  TCanvas * const c1 = new TCanvas("transparent text","transparent text demo", 10, 10, 900, 500);
37  //We can check gVirtualX (its type):
38  if (gVirtualX && !gVirtualX->InheritsFrom("TGCocoa")) {
39  ::Warning("transt_text", "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 OS X version with cocoa enabled");
41  }
42 
43  c1->SetGrid();
44  c1->SetBottomMargin(0.15);
45 
46  const Int_t nx = 20;
47  const char *people[nx] = {"Jean","Pierre","Marie","Odile",
48  "Sebastien","Fons","Rene","Nicolas","Xavier","Greg",
49  "Bjarne","Anton","Otto","Eddy","Peter","Pasha",
50  "Philippe","Suzanne","Jeff","Valery"};
51 
52  TH1F * const h = new TH1F("h4", "test", nx, 0, nx);
53 
54  h->SetFillColor(38);
55  for (Int_t i = 0; i < 5000; ++i)
56  h->Fill(gRandom->Gaus(0.5 * nx, 0.2 * nx));
57 
58  h->SetStats(0);
59  for (Int_t i = 1; i <= nx; ++i)
60  h->GetXaxis()->SetBinLabel(i, people[i - 1]);
61 
62  h->Draw();
63 
64  TPaveText * const pt = new TPaveText(0.3, 0.3, 0.98, 0.98, "brNDC");
65  //Transparent 'rectangle' with transparent text.
66  pt->SetFillColor(grayColorIndex);
67  pt->SetTextColor(blackColorIndex);
68 
69  pt->SetTextSize(0.5);
70  pt->SetTextAlign(12);
71 
72  pt->AddText("Hello");
73  pt->Draw();
74 }
const int nx
Definition: kalman.C:16
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition: TH1.cxx:3159
void transp_text()
Definition: transp_text.C:18
virtual void Draw(Option_t *option="")
Draw this pavetext with its current attributes.
Definition: TPaveText.cxx:211
unsigned FindFreeCustomColorIndices(Color_t(&indices)[N])
Definition: customcolor.h:44
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:235
TCanvas * c1
Definition: legend1.C:2
TH1 * h
Definition: legend2.C:5
virtual TText * AddText(Double_t x1, Double_t y1, const char *label)
Add a new Text line to this pavetext at given coordinates.
Definition: TPaveText.cxx:160
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:570
int Int_t
Definition: RtypesCore.h:41
virtual void SetGrid(Int_t valuex=1, Int_t valuey=1)
Definition: TPad.h:326
void Error(const char *location, const char *msgfmt,...)
short Color_t
Definition: RtypesCore.h:79
virtual void SetTextAlign(Short_t align=11)
Definition: TAttText.h:55
virtual void SetBottomMargin(Float_t bottommargin)
Set Pad bottom margin in fraction of the pad height.
Definition: TAttPad.cxx:97
TPaveText * pt
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2878
virtual void SetFillColor(Color_t fcolor)
Definition: TAttFill.h:50
void Warning(const char *location, const char *msgfmt,...)
R__EXTERN TRandom * gRandom
Definition: TRandom.h:62
#define gVirtualX
Definition: TVirtualX.h:362
The Canvas class.
Definition: TCanvas.h:48
A Pave (see TPave) with text, lines or/and boxes inside.
Definition: TPaveText.h:35
The color creation and management class.
Definition: TColor.h:47
virtual void SetBinLabel(Int_t bin, const char *label)
Set label for bin.
Definition: TAxis.cxx:793
virtual void SetTextColor(Color_t tcolor=1)
Definition: TAttText.h:57
virtual void SetTextSize(Float_t tsize=1)
Definition: TAttText.h:60
virtual void SetStats(Bool_t stats=kTRUE)
Set statistics option on/off.
Definition: TH1.cxx:8320
TAxis * GetXaxis()
Definition: TH1.h:319