Creating a user application with ROOT

Starting from a simple ROOT macro creating and displaying a canvas, this section shows how to create a standalone user application based on ROOT libraries.

Three different kind of standalone user applications are presented:

The simple ROOT macro

The following code can be executed interactively at the ROOT prompt:

   TCanvas* c = new TCanvas("c", "Something", 0, 0, 800, 600);
   TF1 *f1 = new TF1("f1","sin(x)", -5, 5);
   f1->SetLineColor(kBlue+1);
   f1->SetTitle("My graph;x; sin(x)");
   f1->Draw();

Figure: A simple canvas.

Now, let’s see how to create a standalone application with this simple code.

Batch example generating a PDF file

A C++ standalone application must contain the main() function, the starting point for the application execution.

The first lines of the C++ file include ROOT header files. The names of the ROOT header files are almost always the same as the class names (here TF1 and TCanvas).

#include "TF1.h"
#include "TCanvas.h"

int main(int argc, char **argv)
{
   TCanvas* c = new TCanvas("c", "Something", 0, 0, 800, 600);
   TF1 *f1 = new TF1("f1","sin(x)", -5, 5);
   f1->SetLineColor(kBlue+1);
   f1->SetTitle("My graph;x; sin(x)");
   f1->Draw();
   c->Print("demo1.pdf");
   return 0;
}

Save the code in a file, for example as demo1.cxx.

On Linux and MacOS compile and run the demo1.cxx file as following :

$ g++ demo1.cxx $(root-config --glibs --cflags --libs) -o demo1
$ ./demo1

The equivalent command on Windows is:

> cl -nologo -MD -GR -EHsc demo1.cxx -I %ROOTSYS%\include /link -LIBPATH:%ROOTSYS%\lib libCore.lib libGpad.lib libHist.lib
> demo1

Note

You can use root-config --cflags to be sure to use the correct compiler flags (Debug vs Release)

The following message is displayed:

Info in <TCanvas::Print>: pdf file demo1.pdf has been created

The demo1.pdf file is saved in the current working directory. The pdf file contains the plot of the f1 function.

Interactive example displaying a canvas

Use TApplication to display the output on a screen. TApplication creates a ROOT application environment that provides an interface to the windowing system event loops and event handlers.

To run the canvas as a standalone application you must create a TApplication object. Calling the Run() method starts the event loop.

#include "TF1.h"
#include "TApplication.h"
#include "TCanvas.h"
#include "TRootCanvas.h"

int main(int argc, char **argv)
{
   TApplication app("app", &argc, argv);
   TCanvas* c = new TCanvas("c", "Something", 0, 0, 800, 600);
   TF1 *f1 = new TF1("f1","sin(x)", -5, 5);
   f1->SetLineColor(kBlue+1);
   f1->SetTitle("My graph;x; sin(x)");
   f1->Draw();
   c->Modified(); c->Update();
   TRootCanvas *rc = (TRootCanvas *)c->GetCanvasImp();
   rc->Connect("CloseWindow()", "TApplication", gApplication, "Terminate()");
   app.Run();
   return 0;
}

Save the code in a file, for example as demo2.cxx.

On Linux and MacOS compile and run the demo2.cxx file as following :

$ g++ demo2.cxx $(root-config --glibs --cflags --libs) -o demo2
$ ./demo2

The equivalent command on Windows is:

> cl -nologo -MD -GR -EHsc demo2.cxx -I %ROOTSYS%\include /link -LIBPATH:%ROOTSYS%\lib libCore.lib libGpad.lib libHist.lib
> demo2

Note

You can use root-config --cflags to be sure to use the correct compiler flags (Debug vs Release)

Example using ROOT prompt

You can use TRint to create an environment provides an interface to the windows manager and event loop via the inheritance of TApplication. In addition TRintprovides an interactive access to the Cling C++ interpreter via the command line.

#include "TF1.h"
#include "TRint.h"
#include "TCanvas.h"

int main(int argc, char **argv)
{
   TRint app("app", &argc, argv);
   TCanvas* c = new TCanvas("c", "Something", 0, 0, 800, 600);
   TF1 *f1 = new TF1("f1","sin(x)", -5, 5);
   f1->SetLineColor(kBlue+1);
   f1->SetTitle("My graph;x; sin(x)");
   f1->Draw();
   c->Modified(); c->Update();
   app.Run();
   return 0;
}

Save the code in a file, for example demo3.cxx.

On Linux and MacOS compile and run the demo3.cxx file as following :

$ g++ demo3.cxx $(root-config --glibs --cflags --libs) -o demo3
$ ./demo3

The equivalent command on Windows is:

> cl -nologo -MD -GR -EHsc demo3.cxx -I %ROOTSYS%\include /link -LIBPATH:%ROOTSYS%\lib libCore.lib libGpad.lib libHist.lib
> demo3

Note

You can use root-config --cflags to be sure to use the correct compiler flags (Debug vs Release)