Simple_ROOTbook_cpp.ipynb Open in SWAN Download

Simple ROOTbook (C++)


This simple ROOTbook shows how to create a [histogram](https://root.cern.ch/doc/master/classTH1F.html), [fill it](https://root.cern.ch/doc/master/classTH1.html#a77e71290a82517d317ea8d05e96b6c4a) and [draw it](https://root.cern.ch/doc/master/classTH1.html#aa53a024a9e94d5ec91e3ef49e49563da). The language chosen is C++.

In order to activate the interactive visualsisation we can use the JSROOT magic:

In [1]:
%jsroot on

Now we will create a histogram specifying its title and axes titles:

In [2]:
TH1F h("myHisto","My Histo;X axis;Y axis",64, -4, 4)
(TH1F &) Name: myHisto Title: My Histo NbinsX: 64

If you are wondering what this output represents, it is what we call a "printed value". The ROOT interpreter can indeed be instructed to "print" according to certain rules instances of a particular class.

Time to create a random generator and fill our histogram:

In [3]:
TRandom3 rndmGenerator;
for (auto i : ROOT::TSeqI(1000)){
    auto rndm = rndmGenerator.Gaus();
    h.Fill(rndm);
}

We can now draw the histogram. We will at first create a canvas, the entity which in ROOT holds graphics primitives.

In [4]:
TCanvas c;
h.Draw();
c.Draw();

We'll try now to beutify the plot a bit, for example filling the histogram with a colour and setting a grid on the canvas.

In [5]:
h.SetFillColor(kBlue-10);
c.SetGrid();
h.Draw();
c.Dra();
input_line_57:5:3: error: no member named 'Dra' in 'TCanvas'
c.Dra();
~ ^

Oops! We mispelled a method. Luckily ROOT informed us about the typo. Let's draw the canvas properly:

In [6]:
c.Draw();

Alright: we are done with our first step into the ROOTbooks world!