Is there any tutorial on running root entirely with g++?

I am a beginner in ROOT and C++ in general. Currently I am learning how to create libraries and linking them etc. I was wondering if all these things can be applied while working on ROOT. For example, I have a few functions that I use often: f1(), f2(), …, f(n) etc. I want to create a library of these functions and use them in programs where I will manipulate .root files and create TGraphs draw them in TCanvas(es) etc. How can do all these things without entering a ROOT shell ? I could not find a beginner level nice step by step tutorial on this topic, like

  1. What are the header files I have to include in my main.cpp so that the compiler doesn’t complain about the types TCanvas, TString, TfFile … etc when I compile using g++

  2. Considering that step 1 is done and it creates a .o file, will executing this file open Canvases and draw ? If it does, how can I then close this root session in a proper way… because I am in my terminal and not ROOT shell.

All I need is a nice step by step tutorial on using ROOT features like TCanvas, TGraphs and every TStuff staying on my terminal and using g++ as if I am doing just C++.

Thanks a lot.

See the “${ROOTSYS}/test” subdirectory.

Thanks but I see a lot of .cxx files there, no tutorial !

Hi,

You will have to deal with things like event loops (for the GUI) - it’s not as trivial as just compiling and linking an executable. As Pepe points out there are sources that show what to do, and Makefiles that show how to build these sources.

Unless you really need to build binaries I’d recommend against it and suggest you use ROOT’s interpreter instead, or you use ACLiC (root -l -b -q myCode.C+ - note the “+” at the end) to compile your code, especially in ROOT 5.

Cheers, Axel.

1 Like

Thanks a lot everyone, I have finally solved my problem.

Steps:

[color=#BF0080]Usual C++ steps[/color] like, [color=#BF4000]main.cpp[/color] containing int main(), then we have [color=#8000BF]header1.cpp[/color], [color=#8000BF]header2.cpp[/color], … [color=#8000BF]headern.cpp[/color] containing the detailed definitions of the functions or classes and corresponding [color=#FF0000]*.h[/color] files containing the declarations of the stuff that we defined inside [color=#8000BF]headeri.cpp[/color] files. Of course everyone knows about the last steps… then:

g++ main.cpp header1.cpp header2.cpp ... headern.cpp `root-config --libs --cflags` -o program

where [color=#80BF40]program[/color] is the final executable. But [color=#FF4000]this is not all[/color], “[color=#BF0040]TApplication[/color]” is the guy ! I have the following structure of [color=#FF4000]main[/color] that works for me:

#include "TStuff" //Stuff = File, Tree, blah blah blah
#include "header1.h"
#include "header2.h"
.
.
.
#include "headern.h"


int main(int argc, char* argv[])
{

//here I introduce TApplication that does the trick

TApplication* app = new TApplication("app",&argc, argv);

/*

 do whatever needed to be done.. creating canvases etc etc....

*/



app->Run();
return 0;
}

To quit one can just click “[color=#BF0040]File -> Quit ROOT[/color]” on the canvas.