TCanvas and TPad
The following may be needed if not using
CINT Shortcuts
#include "TCanvas.h"
Description
The TCanvas and TPad
classes
between them manage graphical representations of
objects
in a rectangular window on the display. The TCanvas object manages all global
properties of the window. When a TCanvas object is created,
(by calling its constructor)
it is given the initial size of the window. For example:-
TCanvas *A3 = new TCanvas("A3","Plotting Canvas",150,10,990,660);
creates a TCanvas
pointer
called A3. The TCanvas object is also named A3, and has a title bar that has
"Plotting Canvas". The top lefthand corner of the window is at pixel
coordinates 150,10. The width is 990 pixels and height 660. These are just
the initial settings; the window can be moved and resized in the normal
manner. Resizing also causes the all display elements to be redrawn at the
new size.
TPad is a rectangular sub-window within a TCanvas, rather like a zone in PAW.
The principle function of a TPad is to hold a list of objects to be displayed
within the sub-window. The list can include other TPad objects, so
sub-windows can be nested. A TCanvas
IsA
TPad, that is to say it contains an embedded TPad so creating a TCanvas is
all that is required before starting to displaying objects. Having created a
TCanvas object, its properties can be changed by sending it
messages.
For example, to set the background colour to 10:-
A3->SetFillColor(10);
To see a list of colours, click on View menu and select Colors.
It is possible to divide the window into a regular grid of TPads, in exactly
the same way as the ZONE command works in PAW. For example, to create a set
of 6 TPads, 3 in X and 2 in Y, with a separation in X of 0.1 of a the TCanvas
width and in Y of 0.1 of the TCanvas height, and a background colour of 0:-
A3->Divide(3,2,0.01,0.01,0);
The TPads are named A3_1, A3_2, .. A3_6 working left to right, top to bottom.
If working interactively, variables with names like A3_1 can be used at once,
CINT goes off and finds the objects they point to. If working with compiled
code then the TCanvas can be asked for its list of primitives
(its a TList)
and then this list asked to find the named objects which can then be used to
explicitly declare a pointer:-
TPad *A3_1 = (TPad*) A3->GetListOfPrimitives()->FindObject("A3_1");
Displaying objects involves sending them their Draw message. For example to
create a 2d histogram of floating point numbers
(i.e. a TH2F object):-
TH2F *H_ADC = new TH2F("H_ADC","RawA vs RawB",100,0.,1000.,100,0.,1000.);
H_ADC->Draw();
H_ADC->Fill(1.,2.);
A3->Update();
The first thing to notice about this trivial example is that, having created
an empty TH2F, it is drawn before it is filled. Normally this is done the
other way round although the result is the same. That is because sending
objects their Draw message results in them being added to the list of objects
in the current TPad. Later, when the TCanvas is asked to Update, it tells all
its modified TPads to repaint themselves, which in turn tells all its objects
to Paint themselves; its the Paint message does the real work.
So objects should only ever be asked to Draw themselves
once, after that, each time the display updates they will be shown in their
current form. Adding an object to a TPad will signal that the TPad has been
modified, and the next Update will repaint it. Of course a TPad has no idea if
its objects subsequently change
and need to be repainted, but this can be fixed by sending it the Modified
message e.g.:-
A3->Modified();
Alternatively, the TCanvas can be sent this message, to mark all its TPads as
modified.
If working interactively, each time a carriage return is pressed all
TCanvas objects are sent their Update message, so if typing in the above
example interactively the empty histogram will be displayed as soon as the
Draw message is typed. Then the Update message does nothing unless the
Modified message is sent first.
So Draw updates the current Tpad, but what defines the current TPad? ROOT has
a
global
variable call gPad, which is automatically set to the last TPad that was
created. Any TPad can be made the current one by sending it its cd message
(from UNIX - make it the current "directory") e.g.:-
A3_1->cd();
If an object is sent its Draw message before a TPad exists, ROOT
automatically creates a default TCanvas called c1.
TPad has a number of
member functions
to manipulate TPads, such as SetLogx and SetLogy to set scales log/linear,
XtoPixel etc. to map between pixel and user coordinates and a series of Paint*
functions e.g. PaintLine to draw simple graphical primitives.
Example Use in MINFast
The
loopmacro
shows how TCanvas and TPad objects are created and used.
For more information see:-
Go Back to the
The ROOT Crib Top Page
If you have any comments about this page please send them to
Nick West