How to Draw Objects?

One can 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 (seeTCanvas) 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. 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 actually 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.

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.

1 Like