The ROOT C++ kernel (5 minutes)

Note

In The Basics, all of the practice ROOT code used C++ syntax. Yet I switched to Python when I introduced Jupyter. Now you’ll understand why.

Start a ROOT C++ notebook (either from your Jupyter home page, or from the File menu of your existing notebook). Rename it to cplusplustest or whatever you want. Paste the following into a cell and execute it.

TH1D example("example","example histogram",100,-3,3);
example.FillRandom("gaus",10000);
example.Draw();

You won’t see anything, but after the Your first notebook discussion you know why: you have to draw the canvas. The warning message says it drew the plot on TCanvas c1, so add the following line to the end of the above cell and hit SHIFT-ENTER:

c1.Draw();

Note

I’m being sneaky, aren’t I? I knew c1.Draw() would not work. The error message tells you why: the automatically-created c1 is a pointer, and requires the -> symbol.

Edit the “.” to the pointer symbol “->” and hit SHIFT-ENTER.

Note

If you think about it for a second, I could have given you a more complete example, the same way I did for the Python notebook:

TCanvas my_canvas();
TH1D example("example","example histogram",100,-3,3);
example.FillRandom("gaus",10000);
example.Draw();
my_canvas.Draw();

I presented it this way to make a couple of points. First, I wanted to remind you that in a C++ notebook you still have to keep track of what’s an object and what’s a pointer.

Second, I wanted you to see the warning messages ROOT generates when you redefine objects or pointers that you’ve created before. You can get away with this in interactive ROOT notebooks, as long as the redefinitions are in different cells or you’re executing the same cell again. You cannot do this in stand-alone ROOT programs; the C++ specification prohibits redefining variables with the same name.

Don’t let this confuse you; the following is fine:

int a = 1;
a = 2;

What’s not allowed in full-fledged stand-alone C++ is:

int a = 1;
double a = 2.0;

The following will be allowed (with a warning) in a C++ ROOT notebook, as long as the two statements are in different cells. You will get a compilation error in a stand-alone C++ program or if both statements are in the same cell in a notebook:

TH1D example("example","example histogram",100,-3,3);
TH1D example("example2","another example histogram",100,-3,3);