You are here

How to Draw Objects?

You draw an object via object->Draw(option). This function adds a reference to the object in the list of objects to be drawn in the current pad or canvas. If a canvas does not exist, a default canvas (named c1) is automatically created.

A canvas object (see TCanvas) has an associated screen window (unless you are in batch mode) and a list of pads (see TPad). The canvas itself is a pad. You do not need to create a pad in case you want to get only one pad. Pads are resizable and moveable areas in the canvas. Pads in turn may contain other pads to build a hierarchy. Pads may be automatically generated by TPad::Divide. In case a canvas or pad is resized, its contents are automatically redrawn.

An instruction like object->Draw() in fact does not draw directly. It simply adds a reference to object in the list of pad objects. For each object drawn, a TObjLink object is created. This TObjLink object contains a reference to object. This means that if the content of object changes, your picture will change. When the canvas/pad must be painted (following a moose-driven resize operation or by calling TCanvas::Update), the TPad::Paint function is called. This function, in turn, scans the list of all objects and for each object invokes object->Paint(option). The parameter "option" corresponds to the parameter given in the object->Draw(option) call.

The TCanvas::Update function is automatically called in a ROOT interactive session when carriage return is typed. When executing a macro, one can force a screen update by calling gPad->Update(), where gPad is a global variable pointing to the current pad. One can change the current pad with pad->cd() where pad is the pointer to an existing pad.

For many classes (notably histograms and graphs), the Draw() method also tells the pad that it has been modified, triggering the pad redrawing. However, this is not true for other classes, usually representing minor graphical objects, for example lines. To actual paint them, one needs to call TPad::Modified() method, that signals the pad that it has been modified, so that it needs to be redrawn.

The object->Paint() function may invoke other utility functions from the TPad class to paint basic primitives (see for example, TPad::PaintLine, TPad::PaintPolyLine, TPad::PaintText, etc..), or create higher level objects and invoke their Paint member function (see for example TH1::Paint).

When an object has been drawn, one can redefine its drawing option by invoking:


When a pad is created, the user coordinates are by default set to the range [0,1] for both x and y. You can use TPad::Range to specify different values. When drawing high level objects such as histograms or graphs, the pad range is automatically set by the Paint member function of these classes.

See the list of utility functions in TPad to set or get parameters. You can set a logarithmic scale in x and/or y and/or z via TPad::SetLogx, etc.

Use functions such as TPad::XtoPixel or TPad::PixeltoX to convert to pixel coordinates from user coordinates and vice-versa.