Root

Root's documentation can be found at the following address http://root.cern.ch/root/html534/ClassIndex.html.

TFile

// Include the TFile Library
#include "TFile.h"

{
    /*  Create a new file with the following parameters
        - Filename          : myfile.root
        - Creation option   : recreate

        The "recreate" option will delete the file if it exists and create a new version of it. Alternatively, you can use "update" to append data to a file that already exists or "read" if you do not want to modify the file.
    */
    TFile* file = new TFile("myfile.root", "recreate");

    /*  Get an object from the file */
    Object* obj = file->Get("object_name");

    /*  Close the file */
    file->Close();
}

TGraph

// Include the TGraph Library
#include "TGraph.h"

{
    /*  Create a new graph with the following parameters
        - Number of points  : 10

        Notes :
        You can not directly assign a title or axis labels to a TGraph.
        If you do not know the number of points, you can remove this parameter.
    */
    TGraph* graph = new TGraph(10);

    /* Set the title of the graph */
    graph->SetTitle("My graph");

    /* Set the label for the X axis */
    graph->GetXaxis()->SetTitle("X axis");

    /* Set the label for the Y axis */
    graph->GetYaxis()->SetTitle("Y axis");

    /*  Change the color of the line (see http://root.cern.ch/root/html/TAttLine.html for a list of colors) */
    graph->SetLineColor(color);

    /* Change the thickness of the line from 1 to 10 */
    graph->SetLineWidth(thickness);

    /* Change the line's style (see http://root.cern.ch/root/html/TAttLine.html for a list of styles) */
    graph->SetLineStyle(style);

    /*  Change the color of the marker (see http://root.cern.ch/root/html/TAttMarker.html for a list of colors) */
    graph->SetMarkerColor(color);

    /* Change the size of the marker relatively to the initial size which is equal to 1 */
    graph->SetMarkerSize(size);

    /* Change the marker's style (see http://root.cern.ch/root/html/TAttMarker.html for a list of styles) */
    graph->SetMarkerStyle(style);

    /*  Add a value to the graph */
    graph->SetPoint(pointNumber, valueX, valueY);

    /*  Draw the histogram on the screen using the AC* option. Here is a list of options you can use to render you graph :
        "A"  Axis are drawn around the graph
        "L"  A simple polyline is drawn
        "F"  A fill area is drawn ('CF' draw a smoothed fill area)
        "C"  A smooth Curve is drawn
        "*"  A Star is plotted at each point
        "P"  The current marker is plotted at each point
        "B"  A Bar chart is drawn
        "1"  When a graph is drawn as a bar chart, this option makes the bars start from the bottom of the pad. By default they start at 0.
        "X+" The X-axis is drawn on the top side of the plot.
        "Y+" The Y-axis is drawn on the right side of the plot.
    */
    graph->Draw("AC*");

    /*  or write it in a file */
    graph->Write();
}

TGraph2D

// Include the TGraph2D Library
#include "TGraph2D.h"

{
    /*  Create a new graph with the following parameters
        - Number of points  : 10

        Notes :
        You can not directly assign a title or axis labels to a TGraph2D.
        If you do not know the number of points, you can remove this parameter.
    */
    TGraph2D* graph = new TGraph2D(10);

    /* Set the title of the graph */
    graph->SetTitle("My graph");

    /* Set the label for the X axis */
    graph->GetXaxis()->SetTitle("X axis");

    /* Set the label for the Y axis */
    graph->GetYaxis()->SetTitle("Y axis");

    /* Set the label for the Y axis */
    graph->GetZaxis()->SetTitle("Z axis");

    /*  Add a value to the graph */
    graph->SetPoint(pointNumber, valueX, valueY, value Z);

    /*  Draw the histogram on the screen using the surf1 option. Here is a list of options you can use to render you graph :
    " "  Default (scatter plot).
    "LINE"   Draw a 3D polyline.
    "ARR"    Arrow mode. Shows gradient between adjacent cells.
    "BOX"    A box is drawn for each cell with surface proportional to the content's absolute value. A negative content is marked with a X.
    "BOX1"   A button is drawn for each cell with surface proportional to content's absolute value. A sunken button is drawn for negative values a raised one for positive.
    "COL"    A box is drawn for each cell with a color scale varying with contents. All the none empty bins are painted. Empty bins are not painted unless some bins have a negative content because in that case the null bins might be not empty.
    "COLZ"   Same as "COL". In addition the color palette is also drawn.
    "CONT"   Draw a contour plot (same as CONT0).
    "CONT0"  Draw a contour plot using surface colors to distinguish contours.
    "CONT1"  Draw a contour plot using line styles to distinguish contours.
    "CONT2"  Draw a contour plot using the same line style for all contours.
    "CONT3"  Draw a contour plot using fill area colors.
    "CONT4"  Draw a contour plot using surface colors (SURF option at theta = 0).
    "CONT5"  (TGraph2D only) Draw a contour plot using Delaunay triangles.
    "LIST"   Generate a list of TGraph objects for each contour.
    "CYL"    Use Cylindrical coordinates. The X coordinate is mapped on the angle and the Y coordinate on the cylinder length.
    "P"      Draw a marker at each vertex
    "P0"     Draw a circle at each vertex. Each circle background is white.
    "PCOL"   Draw a marker at each vertex. The color of each marker is defined according to its Z position.
    "POL"    Use Polar coordinates. The X coordinate is mapped on the angle and the Y coordinate on the radius.
    "SPH"    Use Spherical coordinates. The X coordinate is mapped on the latitude and the Y coordinate on the longitude.
    "PSR"    Use PseudoRapidity/Phi coordinates. The X coordinate is mapped on Phi.
    "SURF"   Draw a surface plot with hidden line removal.
    "SURF1"  Draw a surface plot with hidden surface removal.
    "SURF2"  Draw a surface plot using colors to show the cell contents.
    "SURF3"  Same as SURF with in addition a contour view drawn on the top.
    "SURF4"  Draw a surface using Gouraud shading.
    "SURF5"  Same as SURF3 but only the colored contour is drawn. Used with option CYL, SPH or PSR it allows to draw colored contours on a sphere, a cylinder or a in pseudo rapidity space. In cartesian or polar coordinates, option SURF3 is used.
    "FB"     With LEGO or SURFACE, suppress the Front-Box.
    "BB"     With LEGO or SURFACE, suppress the Back-Box.
    "A"      With LEGO or SURFACE, suppress the axis.
    "SCAT"   Draw a scatter-plot (default).
    "TRI"    The Delaunay triangles are drawn using filled area. An hidden surface drawing technique is used. The surface is painted with the current fill area color. The edges of each triangles are painted with the current line color.
    "TRIW"   The Delaunay triangles are drawn as wire frame
    "TRI1"   The Delaunay triangles are painted with color levels. The edges of each triangles are painted with the current line color.
    "TRI2"   the Delaunay triangles are painted with color levels.
    */
    graph->Draw("surf1");

    /*  or write it in a file */
    graph->Write();
}

TGraphErrors

// Include the TGraphErrors Library
#include "TGraphErrors.h"

{
    /*  Create a new graph with the following parameters
        - Number of points  : 10

        Notes :
        You can not directly assign a title or axis labels to a TGraphErrors.
        If you do not know the number of points, you can remove this parameter.
    */
    TGraphErrors* graph = new TGraphErrors(10);

    /* Set the title of the graph */
    graph->SetTitle("My graph");

    /* Set the label for the X axis */
    graph->GetXaxis()->SetTitle("X axis");

    /* Set the label for the Y axis */
    graph->GetYaxis()->SetTitle("Y axis");

    /*  Change the color of the line (see http://root.cern.ch/root/html/TAttLine.html for a list of colors) */
    graph->SetLineColor(color);

    /* Change the thickness of the line from 1 to 10 */
    graph->SetLineWidth(thickness);

    /* Change the line's style (see http://root.cern.ch/root/html/TAttLine.html for a list of styles) */
    graph->SetLineStyle(style);

    /*  Change the color of the marker (see http://root.cern.ch/root/html/TAttMarker.html for a list of colors) */
    graph->SetMarkerColor(color);

    /* Change the size of the marker relatively to the initial size which is equal to 1 */
    graph->SetMarkerSize(size);

    /* Change the marker's style (see http://root.cern.ch/root/html/TAttMarker.html for a list of styles) */
    graph->SetMarkerStyle(style);

    /*  Add a value to the graph */
    graph->SetPoint(pointNumber, valueX, valueY);

    /* Set the error of a point */
    graph->SetPointError(pointNumber, errorX, errorY);

    /*  Draw the histogram on the screen using the AC* option. Here is a list of options you can use to render you graph :
        "A"  Axis are drawn around the graph
        "L"  A simple polyline is drawn
        "F"  A fill area is drawn ('CF' draw a smoothed fill area)
        "C"  A smooth Curve is drawn
        "*"  A Star is plotted at each point
        "P"  The current marker is plotted at each point
        "B"  A Bar chart is drawn
        "1"  When a graph is drawn as a bar chart, this option makes the bars start from the bottom of the pad. By default they start at 0.
        "X+" The X-axis is drawn on the top side of the plot.
        "Y+" The Y-axis is drawn on the right side of the plot.
        "0"  The error bars for data points outside range are shown.
        "3"  The errors are drawn as a band.
        "4"  Is similar to the option "3" except that the band is smoothed.
    */
    graph->Draw("AC*");

    /*  or write it in a file */
    graph->Write();
}

TH1D

// Include the TH1D Library
#include "TH1D.h"

{
    /*  Create a new histogram with the following parameters
        - Name                  : hist_gaus
        - Title                 : Gaussian distribution
        - X axis label          : X
        - Y axis label          : Number of events
        - Number of bins in X   : 1000
        - Lower X limit         : -10
        - Upper X limit         : 10

        Notes :
        1. LaTeX can be used for the title and the axis' labels. For special characters, use # instead of \ (ex. #mu not \mu).
        2. If you do not want to set limits for you X axis (because for example you do not know the range), you can set both the upper and lower limits to 0. Root will choose what the limits will be
    */
    TH1D* histogram = new TH1D("hist_gaus", "Gaussian distribution;X;Number of events", 1000, -10., 10.);

    /*  Change the color of the line (see http://root.cern.ch/root/html/TAttLine.html for a list of colors) */
    graph->SetLineColor(color);

    /* Change the thickness of the line from 1 to 10 */
    graph->SetLineWidth(thickness);

    /* Change the line's style (see http://root.cern.ch/root/html/TAttLine.html for a list of styles) */
    graph->SetLineStyle(style);

    /*  Change the color of the bins (see http://root.cern.ch/root/html/TAttFill.html for a list of colors) */
    histogram->SetFillColor(color);

    /* Change the area's style (see http://root.cern.ch/root/html/TAttFill.html for a list of styles) */
    histogram->SetFillStyle(thickness);

    /*  Add a value to the histogram */
    histogram->Fill(value);

    /*  Draw the histogram on the screen with no options. Here is a list of options you can use to render you graph :
        " "      Default.
        "AH"     Draw histogram without axis. "A" can be combined with any drawing option. For instance, "AC" draws the histogram as a smooth Curve without axis.
        "]["     When this option is selected the first and last vertical lines of the histogram are not drawn.
        "B"      Bar chart option.
        "BAR"    Like option "B", but bars can be drawn with a 3D effect.
        "HBAR"   Like option "BAR", but bars are drawn horizontally.
        "C"      Draw a smooth Curve through the histogram bins.
        "E0"     Draw error bars. Markers are drawn for bins with 0 contents.
        "E1"     Draw error bars with perpendicular lines at the edges.
        "E2"     Draw error bars with rectangles.
        "E3"     Draw a fill area through the end points of the vertical error bars.
        "E4"     Draw a smoothed filled area through the end points of the error bars.
        "E5"     Like E3 but ignore the bins with 0 contents.
        "E6"     Like E4 but ignore the bins with 0 contents.
        "X0"     When used with one of the "E" option, it suppress the error bar along X as gStyle->SetErrorX(0) would do.
        "L"      Draw a line through the bin contents.
        "P"      Draw current marker at each bin except empty bins.
        "P0"     Draw current marker at each bin including empty bins.
        "PIE"    Draw histogram as a Pie Chart.
        "*H"     Draw histogram with a * at each bin.
        "LF2"    Draw histogram like with option "L" but with a fill area. Note that "L" draws also a fill area if the hist fill color is set but the fill area corresponds to the histogram contour.
        "9"      Force histogram to be drawn in high resolution mode. By default, the histogram is drawn in low resolution in case the number of bins is greater than the number of pixels in the current pad. This option should be combined with a "drawing option" like "H" or "L".
        "E"      Draw error bars.
        "AXIS"   Draw only axis.
        "AXIG"   Draw only grid (if the grid is requested).
        "HIST"   When an histogram has errors it is visualized by default with error bars. To visualize it without errors use the option "HIST" together with the required option (eg "hist same c"). The "HIST" option can also be used to plot only the histogram and not the associated function(s).
        "FUNC"   When an histogram has a fitted function, this option allows to draw the fit result only.
        "SAME"   Superimpose on previous picture in the same pad.
        "LEGO"   Draw a lego plot with hidden line removal.
        "LEGO1"  Draw a lego plot with hidden surface removal.
        "LEGO2"  Draw a lego plot using colors to show the cell contents When the option "0" is used with any LEGO option, the empty bins are not drawn.
        "TEXT"   Draw bin contents as text (format set via gStyle->SetPaintTextFormat).
        "TEXTnn" Draw bin contents as text at angle nn (0 < nn < 90).
        "X+"     The X-axis is drawn on the top side of the plot.
        "Y+"     The Y-axis is drawn on the right side of the plot.
    */
    histogram->Draw("");

    /*  or write it in a file */
    histogram->Write();
}

TH2D

// Include the TH2D Library
#include "TH2D.h"

{
    /*  Create a new 2D histogram with the following parameters
        - Name                  : hist_gaus
        - Title                 : 2D Gaussian distribution
        - X axis label          : X
        - Y axis label          : Y
        - Z axis label          : Number of events
        - Number of bins in X   : 1000
        - Lower X limit         : -10
        - Upper X limit         : 10
        - Number of bins in Y   : 500
        - Lower Y limit         : -5
        - Upper Y limit         : 5

        Notes :
        1. LaTeX can be used for the title and the axis' labels. For special characters, use # instead of \ (ex. #mu not \mu).
        2. If you do not want to set limits for you X axis (because for example you do not know the range), you can set both the upper and lower limits to 0. Root will choose what the limits will be
    */
    TH1D* histogram = new TH1D("hist_gaus", "2D gaussian distribution;X;Y;Number of events", 1000, -10., 10., 500, -5., -5.);

    /*  Add a value to the histogram */
    histogram->Fill(valueX, valueY);

    /*  Draw the histogram on the screen with the "colz" option which will display a 3D plot with on column for each bin in the XY plane. Here is a list of options you can use to render you graph :
    " "      Default (scatter plot).
    "ARR"    Arrow mode. Shows gradient between adjacent cells.
    "BOX"    A box is drawn for each cell with surface proportional to the content's absolute value. A negative content is marked with a X.
    "BOX1"   A button is drawn for each cell with surface proportional to content's absolute value. A sunken button is drawn for negative values a raised one for positive.
    "COL"    A box is drawn for each cell with a color scale varying with contents. All the none empty bins are painted. Empty bins are not painted unless some bins have a negative content because in that case the null bins might be not empty.
    "COLZ"   Same as "COL". In addition the color palette is also drawn.
    "CONT"   Draw a contour plot (same as CONT0).
    "CONT0"  Draw a contour plot using surface colors to distinguish contours.
    "CONT1"  Draw a contour plot using line styles to distinguish contours.
    "CONT2"  Draw a contour plot using the same line style for all contours.
    "CONT3"  Draw a contour plot using fill area colors.
    "CONT4"  Draw a contour plot using surface colors (SURF option at theta = 0).
    "CONT5"  (TGraph2D only) Draw a contour plot using Delaunay triangles.
    "LIST"   Generate a list of TGraph objects for each contour.
    "CYL"    Use Cylindrical coordinates. The X coordinate is mapped on the angle and the Y coordinate on the cylinder length.
    "POL"    Use Polar coordinates. The X coordinate is mapped on the angle and the Y coordinate on the radius.
    "SPH"    Use Spherical coordinates. The X coordinate is mapped on the latitude and the Y coordinate on the longitude.
    "PSR"    Use PseudoRapidity/Phi coordinates. The X coordinate is mapped on Phi.
    "SURF"   Draw a surface plot with hidden line removal.
    "SURF1"  Draw a surface plot with hidden surface removal.
    "SURF2"  Draw a surface plot using colors to show the cell contents.
    "SURF3"  Same as SURF with in addition a contour view drawn on the top.
    "SURF4"  Draw a surface using Gouraud shading.
    "SURF5"  Same as SURF3 but only the colored contour is drawn. Used with option CYL, SPH or PSR it allows to draw colored contours on a sphere, a cylinder or a in pseudo rapidity space. In cartesian or polar coordinates, option SURF3 is used.
    "FB"     With LEGO or SURFACE, suppress the Front-Box.
    "BB"     With LEGO or SURFACE, suppress the Back-Box.
    "A"      With LEGO or SURFACE, suppress the axis.
    "SCAT"   Draw a scatter-plot (default).
    "[cutg]" Draw only the sub-range selected by the TCutG named "cutg".
    "E"      Draw error bars.
    "AXIS"   Draw only axis.
    "AXIG"   Draw only grid (if the grid is requested).
    "HIST"   When an histogram has errors it is visualized by default with error bars. To visualize it without errors use the option "HIST" together with the required option (eg "hist same c"). The "HIST" option can also be used to plot only the histogram and not the associated function(s).
    "FUNC"   When an histogram has a fitted function, this option allows to draw the fit result only.
    "SAME"   Superimpose on previous picture in the same pad.
    "LEGO"   Draw a lego plot with hidden line removal.
    "LEGO1"  Draw a lego plot with hidden surface removal.
    "LEGO2"  Draw a lego plot using colors to show the cell contents When the option "0" is used with any LEGO option, the empty bins are not drawn.
    "TEXT"   Draw bin contents as text (format set via gStyle->SetPaintTextFormat).
    "TEXTnn" Draw bin contents as text at angle nn (0 < nn < 90).
    "X+"     The X-axis is drawn on the top side of the plot.
    "Y+"     The Y-axis is drawn on the right side of the plot.
    */
    histogram->Draw("colz");

    /*  or write it in a file */
    histogram->Write();
}

TMath

// Include the TMath Library
#include "TMath.h"

{
    /*  Tan */
    double tan = TMath::Tan(x);

    /*  Full list of functions on http://root.cern.ch/root/htmldoc/TMath.html */
}

TMatrixD

// Include the TMatrixD Library
#include "TMatrixD.h"

{
    /*  Create a new matrix of size n x m */
    TMatrixD A(5, 5);

    /*  Transpose the matrix */
    A.T();

    /*  Invert the matrix */
    A.Invert();

    /*  Set an element of the matrix */
    A(i, j) = value;
}

TRandom3

// Include the TRandom3 Library
#include "TRandom3.h"

{
    /*  Create a new random generator */
    TRandom3* generator = new TRandom3(0);

    /*  Get a random number distributed along a Gaussian */
    double value = generator->Gaus(mean, sigma);

    /*  Get a random number distributed along a Poisson */
    double value = generator->Poisson(mean);

    /*  Get a random number distributed uniformly between min and max */
    double value = generator->Uniform(min, max);

    /*  Get a random number distributed uniformly between 0 and max */
    double value = generator->Uniform(max);
}

TTree

// Include the TTree Library
#include "TTree.h"

{
    /*  Create a new file with the following parameters
        - Object name   : mytree
        - description   : A description"
    */
    TTree* tree = new TTree("mytree", "A description");

    /*  Add a branch to the tree when you create the tree.
        The first parameter is the name of the branch.
        The second parameter is the address of the variable that will be saved. If you have a variable declared as such: double value, then you can get its address by typing &value.
    */
    tree->Branch("variable", &var);

    /*  You can also save arrays of variables (double array[11]) */
    tree->Branch("array", array, "array[11]/D");

    /*  If you want to create an array with a variable number of entries, do the following */
    int nEntries = 10;
    double array[nEntries];
    tree->Branch("nEntries", &nEntries);
    tree->Branch("array", array, "array[nEntries]/D");


    /*  Add an entry to the tree using the current values of the variables attached to the branches */
    tree->Fill();

    /*  Attach a branch to a variable when you read a tree */
    tree->SetBranchAddress("variable", &var);
    tree->SetBranchAddress("array", array);

    /*  Loop over the elements */
    for (unsigned int i = 0; i < tree->GetEntries(); ++i) {
        tree->GetEntry(i);
    }

    /*  Write the tree to a file */
    tree->Close();
}

TVectorD

// Include the TVectorD Library
#include "TVectorD.h"

{
    /*  Create a new vector of size n */
    TVectorD A(5);

    /*  Set an element of the vector */
    A(i) = value;
}