KAHIBARO
Discord Login Register

8.5. Text and Annotations

TLatex

In ROOT, TLatex is the main class for writing mathematical and nicely formatted text on plots. Its syntax is similar to LaTeX math mode, but it is a ROOT implementation, not a full LaTeX engine.

You typically create a TLatex object, set its position in Normalized Device Coordinates (NDC) or user (axis) coordinates, then draw it on a canvas. Positions in NDC are given in the range from 0 to 1 across the visible pad, which makes annotations independent of axis ranges and very convenient for titles, labels, and comments.

A minimal example inside a running ROOT session is:

cpp
auto c = new TCanvas("c", "Example", 800, 600);
auto h = new TH1F("h", "Histogram", 50, -4, 4);
h->FillRandom("gaus", 10000);
h->Draw();
TLatex latex;
latex.SetNDC();                // use NDC coordinates
latex.SetTextSize(0.04);       // relative to pad
latex.DrawLatex(0.15, 0.85, "Preliminary result");

When you call SetNDC(), the coordinates given to DrawLatex(x,y,text) are interpreted in the interval [0,1] horizontally and vertically across the pad. If you omit SetNDC(), the coordinates are in the current user coordinate system, usually axis units, which is useful if you want a label at a particular x or y value, for example at a peak position.

TLatex supports a subset of LaTeX style notation. For example:

cpp
latex.DrawLatex(0.2, 0.8, "E = mc^{2}");
latex.DrawLatex(0.2, 0.75, "#chi^{2}/ndf = 12.4/10");
latex.DrawLatex(0.2, 0.70, "#int f(x) dx");
latex.DrawLatex(0.2, 0.65, "#sigma = 1.3 #pm 0.2");

In these strings, some characters have special meaning. The hash symbol introduces a LaTeX style command or Greek letter, the caret introduces superscripts, and the underscore introduces subscripts. Some useful forms are summarized here.

Visual goalTLatex string
Greek letter alpha"#alpha"
Greek letter sigma"#sigma"
Subscript"x_{0}"
Superscript"E^{2}"
Combined sub and super"x_{0}^{2}"
Plus or minus"#pm"
Multiplication sign"#times"
Greater or equal"#geq"
Chi squared over ndf"#chi^{2}/ndf"
Integral"#int f(x) dx"

In TLatex strings, LaTeX style commands must begin with # instead of \. Use ^ for superscripts and _ for subscripts. Use SetNDC() if you want text position to be independent of axis ranges.

You can adjust the appearance of TLatex text with methods such as SetTextSize, SetTextFont, SetTextColor, and SetTextAlign. For example:

cpp
latex.SetTextSize(0.05);
latex.SetTextFont(42);    // standard ROOT font for publications
latex.SetTextColor(kRed+1);
latex.SetTextAlign(13);   // left-adjusted, top-aligned
latex.DrawLatex(0.15, 0.85, "CMS Preliminary");

The alignment code is a two digit integer. The tens digit controls horizontal alignment 1 for left, 2 for center, 3 for right. The units digit controls vertical alignment 1 for bottom, 2 for middle, 3 for top. So 13 means left and top, 22 means centered in both directions.

If you want to reuse a TLatex object many times with different text content, you can create it once, set its properties, then call DrawLatex repeatedly. ROOT also offers TLatex *l = new TLatex(x, y, "text"); l->SetNDC(); l->Draw(); if you prefer to construct with position and text in one step.

TText

TText is a simpler text drawing class that does not support LaTeX style expressions. It is useful for plain labels or simple annotations where you do not need Greek letters, subscripts, or superscripts.

Using TText is similar to TLatex, but you pass plain C strings and you cannot use the # commands. A simple use case looks like this:

cpp
auto c = new TCanvas("c2", "TText example", 800, 600);
auto h = new TH1F("h2", "Histogram", 100, 0, 10);
h->FillRandom("expo", 5000);
h->Draw();
TText text;
text.SetNDC();
text.SetTextSize(0.04);
text.DrawText(0.15, 0.85, "Run 12345, file A");

TText also supports SetTextFont, SetTextColor, and SetTextAlign with the same alignment codes as TLatex. The main difference is in the rendering and the lack of LaTeX like syntactic features.

You can construct and draw in separate steps:

cpp
TText *label = new TText(0.5, 0.3, "Plain text");
label->SetNDC();
label->SetTextAlign(22);    // center
label->SetTextSize(0.05);
label->Draw();

If you mostly annotate plots with simple words or run numbers, TText is sufficient and can be slightly faster or more predictable. If you need mathematical notation, use TLatex instead.

Use TText for plain, non mathematical text. Use TLatex whenever you need Greek letters, subscripts, superscripts, or more sophisticated scientific notation.

Adding labels to plots

Text and annotations are essential to make your plots understandable. In ROOT you typically combine axis titles, legends, and explicit text labels produced by TLatex or TText.

The basic workflow to add labels is:

  1. Draw the object you want to annotate, such as a histogram, graph, or function.
  2. Create TLatex or TText objects and set their positions, either in user coordinates or NDC.
  3. Set visual properties such as size, font, color, and alignment.
  4. Draw the text on the current pad or canvas.
  5. Update or save the canvas.

For example, you can add a title at the top left, a dataset label at the top right, and a note at the bottom:

cpp
auto c = new TCanvas("c_label", "Labels", 800, 600);
auto h = new TH1F("h3", ";Energy (GeV);Events", 50, 0, 5);
h->FillRandom("gaus", 20000);
h->Draw();
TLatex label;
label.SetNDC();
label.SetTextFont(42);
label.SetTextSize(0.045);
// main title, top left
label.SetTextAlign(13);
label.DrawLatex(0.15, 0.88, "Energy spectrum");
// dataset info, top right
label.SetTextAlign(33);
label.DrawLatex(0.85, 0.88, "Simulation");
// note at bottom
label.SetTextAlign(11);
label.SetTextSize(0.035);
label.DrawLatex(0.15, 0.15, "Events with basic selection");

Axis titles are usually set directly on histograms, graphs, or axes, for instance:

cpp
h->GetXaxis()->SetTitle("Energy (GeV)");
h->GetYaxis()->SetTitle("Events");

or through the histogram title string "title;X axis label;Y axis label" when the object is created.

To annotate specific features, such as a peak position or a cut value, you can use user coordinates so the text follows the data range. For example, suppose you have a histogram in x from 0 to 10 and you fit a peak at x = 4.5:

cpp
double peakX = 4.5;
double peakY = h->GetMaximum();
TLatex latex;
latex.SetTextSize(0.035);
latex.SetTextAlign(21);   // center horizontally, bottom vertically
latex.DrawLatex(peakX, 1.05*peakY, "Peak position");

Here, the x coordinate uses axis units, and the y coordinate is slightly above the maximum bin content. This way, if the canvas is resized but the axis range stays the same, the annotation remains correctly attached to the feature.

If you have multiple pads on a single canvas, make sure you select the pad you want to annotate before drawing the text:

cpp
c->cd(2);          // select second pad
hist2->Draw();
// now draw TLatex or TText, which will be attached to pad 2

Consistent font choices, sizes, and alignment make your figures clearer. A common choice for publication quality figures is font 42, text sizes around 0.04 for main labels and 0.03 for secondary notes, and positions specified in NDC so labels do not overlap with the data or axes when ranges change.

Always decide whether your annotation should follow the pad (use NDC and SetNDC()) or follow a particular x or y value (use axis coordinates and do not call SetNDC()). Mixing them accidentally leads to labels that move or disappear when you change axis ranges.

By combining TLatex or TText with axes, legends, and other plot elements, you can create fully annotated plots that are suitable both for quick diagnostics and for presentations or publications.

Views: 11

Comments

Please login to add a comment.

Don't have an account? Register now!