Setting histogram title size in ROOT

Here is a discourse on how to set the histogram title size in ROOT, which I have found to be far less obvious than it ought to be. The existing documentation, though rather impressive in range and depth, is not always clear or complete, causing the user (or at least me), to spend numerous hours, reading the root manual, web pages, and searching through ROOT forum entries, trying test root scripts, and making educated guesses in order to eventually obtain some measure of understanding of how to set the histogram title size. Here are several methods I have found.

Method 1. Following root.cern.ch/root/HowtoConvertFromPAW.html or looking at the TStyle or TH1 reference pages the naive user will try:

gStyle->SetTitleSize(0.3);
or
hist->SetTitleSize(0.3); //hist is pointer to a histogram object

These do not work, however. I was stuck at this point for quite some time. Eventually, I have come to learn that to make these commands work you must provide a 2nd argument which must not be “x”,“y”, or “z”. That is to say the TStyle page does say that this function takes a 2nd argument which can be “x”,“y”, or “z” to set an axis title size, and that any other argument will set the pad title size, which I have come to learn which is how ROOT treats the histogram title size. Apparently, though, the default is not to change the pad title size, but one of the other axes, probably the “x”, but I have not bothered to track it down.

So the commands which will change the title size are:
gStyle->SetTitleSize(0.3,“t”);
or
hist->SetTitleSize(0.3,“t”);

The 2nd argument does not have to be “t”, but has to be something which is not “x”, “y” or “z”.

The units of the size seem to be “fraction of the pad”, for which you can also use “f”, as in hist->SetTitleSize(0.3f,“t”); I suspect that other units are possible, but I am not sure.

As a digression I note that if you want to change the title font you can do this:

gStyle->SetTitleFont(32,“t”);

But not

hist->SetTitleFont(32,“t”);

which does not seem entirely consistent, but so be it. (In contrast, for example, change histogram label fonts using hist objects).

Method 2.: After reading a lot of ROOT forum entries I discovered you can manipulate the size and location of the histogram title with the functions below (I see them in TStyle ref page, too, but with no documentation):

gStyle_style->SetTitleX(0.1) //title X location
gStyle_style->SetTitleY(0.9) //title Y location
gStyle_style->SetTitleW(0.5) //title width
gStyle_style->SetTitleH(0.1) //title height
(see TStyle ref page for more)

These functions do not work with histogram objects. However, they give you very nice control on various aspects of the histogram title.

Method 3. I’ve also learned that the histogram title is also the pad “title” and so can be manipulated as such, though I have to say this seems like a really tedious way to do it. Here is an example:

// Create histogram and plot on a canvas
TH1F *hpx = new TH1F( “hpx”, “histo title”, 100, -4., 4. );
TCanvas *c1 = new TCanvas(“c1”,"",10,10,700,700);
hpx->Draw();

// Here change the title size
TPaveText pt = (TPaveText)(c1->GetPrimitive(“title”));
pt->SetTextSize(0.1);
c1->Modified();

I have not completely figured out this method actually. This method works interactively, but in a script pt ends up NULL. Another comment is that the units of SetTextSize are different that those for other methods.

My next task is to figure out how to manipulate the stats box text size, which I’ve also found is not so obvious.

2 Likes

Concerning TStyle::SetTitleSize, I believe that this is correctly documented. See
root.cern.ch/root/htmldoc/TStyle … tTitleSize

Concerning the asymmetry TStyle::SetTitleFont and no TH1::SetTitleFont,
you are correct, one should provide TH1::SetTitleFont.

Concerning TStyle::SetTitleX,Y,etc, you are also right. A better explanation is required.

Concerning your last point, I have two remarks:
-The TPaveText object is only generated when the histogram is
effectively painted in the pad. You can force the painting by calling
gPad->Update();
It is very important to understand what the Draw functions do and what
the Paint functions do. I believe that this correctly explained in
the Users Guide chapter about Graphics.

However, instead of doing:

// Create histogram and plot on a canvas TH1F *hpx = new TH1F( "hpx", "histo title", 100, -4., 4. ); TCanvas *c1 = new TCanvas("c1","",10,10,700,700); hpx->Draw(); c1->Update(); //this line was missing in your case // Here change the title size TPaveText *pt = (TPaveText*)(c1->GetPrimitive("title")); pt->SetTextSize(0.1); c1->Modified();
simply do

TH1F *hpx = new TH1F( "hpx", "histo title", 100, -4., 4. ); TCanvas *c1 = new TCanvas("c1","",10,10,700,700); gStyle->SetTitleFontSize(0.1); hpx->Draw();
Concerning the documentation about text font size and types, see
root.cern.ch/root/htmldoc/TAttText.html

We are well aware that the documentation is still missing in many places
and not always easy to follow. We are currently in the process of
improving the documentation of the most important classes.

Rene

This is 2022 and the question is 2007. Is this “feature” fixed now?

Which feature ? that’s a very old thread. Can you create a new one with a clear question and possibly a reproducer macro.