\n",
"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++.\n",
"\n",
"In order to activate the interactive visualsisation we can use the [JSROOT](https://root.cern.ch/js/) magic:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"%jsroot on"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we will create a [histogram](https://root.cern.ch/doc/master/classTH1F.html) specifying its title and axes titles:\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(TH1F &) Name: myHisto Title: My Histo NbinsX: 64\n"
]
}
],
"source": [
"TH1F h(\"myHisto\",\"My Histo;X axis;Y axis\",64, -4, 4)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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.\n",
"\n",
"Time to create a random generator and fill our histogram:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"TRandom3 rndmGenerator;\n",
"for (auto i : ROOT::TSeqI(1000)){\n",
" auto rndm = rndmGenerator.Gaus();\n",
" h.Fill(rndm);\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can now draw the histogram. We will at first create a [canvas](https://root.cern.ch/doc/master/classTCanvas.html), the entity which in ROOT holds graphics primitives."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"
\n",
"
\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"TCanvas c;\n",
"h.Draw();\n",
"c.Draw();"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"\u001b[1minput_line_57:5:3: \u001b[0m\u001b[0;1;31merror: \u001b[0m\u001b[1mno member named 'Dra' in 'TCanvas'\u001b[0m\n",
"c.Dra();\n",
"\u001b[0;1;32m~ ^\n",
"\u001b[0m"
]
}
],
"source": [
"h.SetFillColor(kBlue-10);\n",
"c.SetGrid();\n",
"h.Draw();\n",
"c.Dra();"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Oops! We mispelled a method. Luckily ROOT informed us about the typo. Let's draw the canvas properly:"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"