KAHIBARO
Discord Login Register

8.1. TCanvas

Creating a canvas

The central drawing surface in ROOT is the TCanvas class. Almost everything you draw, such as histograms, graphs, and functions, appears on a canvas. When you work interactively in the ROOT shell, ROOT usually creates a default canvas for you the first time you call Draw(). For more control, you should explicitly create your own canvas objects.

You create a canvas by constructing a TCanvas object. In the ROOT prompt this is usually done with:

cpp
TCanvas *c1 = new TCanvas("c1", "My first canvas");

The first argument is the internal ROOT name of the object. This name must be unique in the current session if you want to find or reuse the canvas later. The second argument is the title, which appears in the window title bar and on some outputs.

It is important to keep a pointer or variable referring to the canvas. If you overwrite the pointer or let it go out of scope in a macro without any other reference, the canvas can be destroyed automatically and you will lose it. In interactive work, using global pointers such as c1, c2 and so on is common.

Once the canvas exists, you can draw other ROOT objects on it. Typically, you first create and fill the object to be visualized, then call its Draw() method. If there is only one visible canvas, ROOT will use it as the target. If you have several canvases, you can choose which one is current with:

cpp
c1->cd();
h1->Draw();

This tells ROOT to make c1 the active drawing surface before drawing the histogram h1. You can call Update() on the canvas to force a redraw, which is useful in scripts that modify objects after drawing them:

cpp
c1->Update();

You can also let ROOT create a default canvas implicitly. For example, if you have not created any canvas and you run:

cpp
h1->Draw();

ROOT will create a canvas named c1 for you. However, for reproducible analysis and for scripts it is usually better practice to create the canvas yourself.

Important rule: Always keep a valid pointer to any canvas you want to reuse, and explicitly select it with c->cd() before drawing or updating objects on it.

Canvas dimensions

A TCanvas has a size in pixels, both in width and in height. You can specify this size when you create it by using a constructor that includes position and dimensions:

cpp
TCanvas *c1 = new TCanvas("c1", "Sized canvas", 200, 100, 800, 600);

This version of the constructor has five arguments. The first two are again the name and title. The third and fourth are the X and Y position of the top left corner of the window on your screen, in pixels. The fifth and sixth are the width and height of the canvas window, also in pixels. The position is mostly a convenience for interactive work, while the size is important for how your plots look and for exported figures.

You can also adjust the size after creation by calling:

cpp
c1->SetCanvasSize(800, 600);

or by manually resizing the window with the mouse in the graphical interface. When you save a canvas to an image file such as PNG, the output will respect the current canvas size, so setting sensible dimensions is part of producing readable figures.

Internally, a canvas is divided into a drawing area and margins. The overall width and height in pixels include these margins. The effective area where histograms and graphs are drawn is smaller than the full canvas. You can configure margins later when working on plot styling, but the initial size still controls the total number of pixels available.

For many simple plots, sizes like 800 by 600 or 1200 by 900 pixels work well. For publication figures you may want wider canvases for multiple pads or for long axis labels. If you plan to divide the canvas into pads, for example with c1->Divide(2,2) in a later section, it is helpful to start with dimensions that divide nicely into equal subregions so that each pad has enough space.

Practical tip: Choose canvas dimensions large enough that all labels, titles, and tick marks are clearly readable in the final exported image, especially when preparing figures for talks or publications.

Views: 9

Comments

Please login to add a comment.

Don't have an account? Register now!