Logo ROOT   6.08/07
Reference Guide
transp_text.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_cocoa
3 /// This macro is based on labels1.C by Rene Brun.
4 /// Updated by Timur Pocheptsov to use transparent text (12/10/2012).
5 /// Requires OS X and ROOT configured with --enable-cocoa.
6 ///
7 /// \macro_code
8 ///
9 /// \author Timur Pocheptsov
10 
11 //Includes for ACLiC (cling does not need them).
12 #include "TVirtualX.h"
13 #include "TPaveText.h"
14 #include "TCanvas.h"
15 #include "TRandom.h"
16 #include "TError.h"
17 #include "TColor.h"
18 #include "TH1F.h"
19 
20 //Aux. functions for tutorials/cocoa.
21 #include "customcolor.h"
22 
23 void transp_text()
24 {
25  //1. Try to 'allocate' free indices for our custom colors -
26  //we can use hard-coded indices like 1001, 1002, 1003 ... but
27  //I prefer to find free indices in a ROOT's color table
28  //to avoid possible conflicts with other tutorials.
29  Color_t indices[2] = {};
31  ::Error("transp_text", "failed to create new custom colors");
32  return;
33  }
34 
35  //2. Create special transparent colors for both pavetext fill color and text color.
36  const Color_t grayColorIndex = indices[0], blackColorIndex = indices[1];
37  new TColor(grayColorIndex, 0.8, 0.8, 0.8, "transparent_gray", 0.85);
38  new TColor(blackColorIndex, 0., 0., 0., "transparent_black", 0.5);
39 
40  //3. Create a TCanvas first to force gVirtualX initialization.
41  TCanvas * const c1 = new TCanvas("transparent text","transparent text demo", 10, 10, 900, 500);
42  //We can check gVirtualX (its type):
43  if (gVirtualX && !gVirtualX->InheritsFrom("TGCocoa")) {
44  ::Warning("transt_text", "You can see the transparency ONLY in a pdf or png output (\"File\"->\"Save As\" ->...)\n"
45  "To have transparency in a canvas graphics, you need OS X version with cocoa enabled");
46  }
47 
48  c1->SetGrid();
49  c1->SetBottomMargin(0.15);
50 
51  const Int_t nx = 20;
52  const char *people[nx] = {"Jean","Pierre","Marie","Odile",
53  "Sebastien","Fons","Rene","Nicolas","Xavier","Greg",
54  "Bjarne","Anton","Otto","Eddy","Peter","Pasha",
55  "Philippe","Suzanne","Jeff","Valery"};
56 
57  TH1F * const h = new TH1F("h4", "test", nx, 0, nx);
58 
59  h->SetFillColor(38);
60  for (Int_t i = 0; i < 5000; ++i)
61  h->Fill(gRandom->Gaus(0.5 * nx, 0.2 * nx));
62 
63  h->SetStats(0);
64  for (Int_t i = 1; i <= nx; ++i)
65  h->GetXaxis()->SetBinLabel(i, people[i - 1]);
66 
67  h->Draw();
68 
69  TPaveText * const pt = new TPaveText(0.3, 0.3, 0.98, 0.98, "brNDC");
70  //Transparent 'rectangle' with transparent text.
71  pt->SetFillColor(grayColorIndex);
72  pt->SetTextColor(blackColorIndex);
73 
74  pt->SetTextSize(0.5);
75  pt->SetTextAlign(12);
76 
77  pt->AddText("Hello");
78  pt->Draw();
79 }
const int nx
Definition: kalman.C:16
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition: TH1.cxx:3125
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
return c1
Definition: legend1.C:41
THist< 1, float, THistStatContent, THistStatUncertainty > TH1F
Definition: THist.hxx:302
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
tomato 1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:575
int Int_t
Definition: RtypesCore.h:41
virtual void SetGrid(Int_t valuex=1, Int_t valuey=1)
Definition: TPad.h:318
void Error(const char *location, const char *msgfmt,...)
short Color_t
Definition: RtypesCore.h:79
virtual void SetTextAlign(Short_t align=11)
Set the text alignment.
Definition: TAttText.h:47
virtual void SetBottomMargin(Float_t bottommargin)
Set Pad bottom margin in fraction of the pad height.
Definition: TAttPad.cxx:100
TPaveText * pt
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2851
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition: TAttFill.h:42
void Warning(const char *location, const char *msgfmt,...)
R__EXTERN TRandom * gRandom
Definition: TRandom.h:66
#define gVirtualX
Definition: TVirtualX.h:362
The Canvas class.
Definition: TCanvas.h:41
A Pave (see TPave) with text, lines or/and boxes inside.
Definition: TPaveText.h:27
The color creation and management class.
Definition: TColor.h:23
virtual void SetBinLabel(Int_t bin, const char *label)
Set label for bin.
Definition: TAxis.cxx:808
virtual void SetTextColor(Color_t tcolor=1)
Set the text color.
Definition: TAttText.h:49
virtual void SetTextSize(Float_t tsize=1)
Set the text size.
Definition: TAttText.h:52
virtual void SetStats(Bool_t stats=kTRUE)
Set statistics option on/off.
Definition: TH1.cxx:8101
TAxis * GetXaxis()
Definition: TH1.h:324