Plots with ROOT

From LHEP Wiki
Revision as of 09:49, 18 March 2015 by Lhep (talk | contribs) (1 revision imported)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
  • To "turn off" the gray background: gROOT->SetStyle("Plain"); (make sure to #include <TROOT.h> in compiled applications otherwise compiler won't know what gROOT is)
  • To keep title from being displayed: gStyle -> SetOptTitle(kFALSE);
  • To keep statistics box from being displayed: gStyle -> SetOptStat(kFALSE);
  • To change histogram line width (default = 1): histo -> SetLineWidth(2);
  • Line style (0: solid; 1:dotted; 2:dashed 3:dot-dash): histo -> SetLineStyle(3);
  • To change marker style: histo -> SetMarkerStyle(23);
  • To change marker colour: histo -> SetMarkerColor();
  • To scale a histogram: histo -> Scale( float scale_factor);
  • To add labels to the x-axis of a histogram: histo -> GetXaxis() -> SetTitle("#sqrt{s} (GeV)");
  • Changing the size of the axis title: histo -> GetXaxis() -> SetTitleSize(0.048);
  • To move the title closer/farther away from the plot: histo -> GetXaxis() -> SetTitleOffset(1.4); // arg is multiplicative factor.
  • Axis labels if you have a function: funcn -> GetHistogram() -> GetXaxis() -> SetTitle("#sqrt{s} (GeV)");
  • Axis labels if you have a graph: graph -> GetHistogram() -> GetXaxis() -> SetTitle("#sqrt{s} (GeV)"); for the y-axis, replace GetXaxis() with GetYaxis().
  • Change color palette on histogram: gStyle->SetPalette(8,NULL);


Sometimes making a TGraph, data is plotted but you don't like the ranges on the axes:

  • Step 1) Make a canvas as usual (eg. mycanvas)
  • Step 2) TH1F* histo = mycanvas -> DrawFrame(lowx, lowy, hix, hiy); (DrawFrame returns a pointer to a TH1F object)
  • Step 3) Label axes with histogram pointer: histo -> GetXaxis() -> SetTitle("#sqrt{s} (GeV)");
  • Step 4) Draw graph (do not use the "A" option as DrawFrame has drawn the axes for you already): graph -> Draw("P*");


How to fit a graph with an arbitrary function:

  • Create a function with parameters. Here, they are [0] and [1].
  • TF1* f90 = new TF1("f90", "[0]+[1]/x", 1., 40.);
  • graph -> Fit (f90, "", "", 4., 40.);


Filling Histograms: It's a really good idea to set the fill style as well as the color.

  • eg. histo -> SetFillColor(7);
  • hist0 -> SetFillStyle(1001); // This is for a solid fill.


Sometimes drawing your histogram or whatever, you find it doesn't fit on the canvas. To give the margins a little more room for things to fit properly, do the following:

  • canvas -> SetRightMargin(0.15); (the default canvas has this set at 0.1, so if you want bigger, you need to put in a value of >0.1.) (also comes in Left, Top, abd Bottom flavours)


To make quantities displayed in the axis use scientific notation:

  • TGaxis::SetMaxDigits(3);