Re: Adding text to histogram

From: Rene Brun (Rene.Brun@cern.ch)
Date: Thu Jul 09 1998 - 15:44:44 MEST


lijowski@cosray2.wustl.edu wrote:
> 
>    Hello,
> 
>   Below is a ROOT code which reads a contents of the histogram
>   from the ASCII file, and draws it. I want to put a title above
>   the histogram and two labels for each axis. But they don't show
>   up on the canvas when I draw a histogram with Be_hist -> Draw.
>   I get the title and two labels for the axes but not a histogram
>   when I comment Be_hist -> Draw.
> 
>   I would like to have those labels for the axes in the center
>   not at the end of the axis. May be there is an easy way to do it, but
>   I could not find it.
> 
>   Also how do I put ticks on all four axes of the histogram?
>   I have been using ROOT v2.09 on Solaris 2.5.
> 
>   Any help is very appreciated.
> 


Michal,
I have modified your code to show the two possibilities of annotation.
Some remarks:
 - TH1::Draw erases the current pad by default. Everything drawn before
   is erased.
 - When you specify the axis titles, you can give blanks at the end
   of the title strings. Blanks will be taken into account.
 - When you want to draw a text at a fixed position on a picture,
   independently of the picture range, you can set the Normalized Device
   Coordinate (NDC) option in the text object.
 - I have removed the part of your code reading your data file 
  (not available) and I am filling one bin only.
 - The function below is directly executable.

Rene Brun



void Be()
{
   Int_t NBINS = 173;
   Int_t NLINES = 34;

   char sline[80];
   Int_t  ii, jj;

   TCanvas *c1 = new TCanvas("c1","Be Isotopes", 900, 700);


// Create Be histogram

 Float_t xmin = 4.22;
 Float_t xmax = xmin + (float) NBINS * 0.05;
 Be_hist = new TH1F("Be_hist", "Be Isotopes", NBINS, xmin, xmax);
 Be_hist -> Fill(6,10);
 Be_hist -> SetXTitle("Calculated Mass      ");
 Be_hist -> SetYTitle("No. of Events per 0.05 Mass Units         ");
 Be_hist -> Draw();

 TText *xlabel = new TText();
 xlabel-> SetNDC();
 xlabel -> SetTextFont(1);
 xlabel -> SetTextColor(1);
 xlabel -> SetTextSize(0.03);
 xlabel -> SetTextAlign(22);
 xlabel -> SetTextAngle(0);
 xlabel -> DrawText(0.5, 0.04, "Calculated Mass");
 TText *ylabel = new TText();
 ylabel-> SetNDC();
 ylabel -> SetTextFont(1);
 ylabel -> SetTextColor(1);
 ylabel -> SetTextSize(0.03);
 ylabel -> SetTextAlign(22);
 ylabel -> SetTextAngle(90);
 ylabel -> DrawText(0.06, 0.5, "No. of Events per 0.05 Mass Unit");

 title = new TPaveLabel(0.1, 0.94, 0.9, 0.98, "Be Isotopes");
 title -> SetFillColor(16);
 title -> Draw();
 
}



This archive was generated by hypermail 2b29 : Tue Jan 04 2000 - 00:34:34 MET