{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Simple ROOTbook (Python)\n", "
\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).\n", "\n", "Let's start importing the [ROOT](https://root.cern.ch) module, which gives us access to all [ROOT](https://root.cern.ch) classes." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "application/javascript": [ "IPython.CodeCell.config_defaults.highlight_modes['magic_text/x-c++src'] = {'reg':[/^%%cpp/]};" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "Welcome to JupyROOT 6.07/07\n" ] } ], "source": [ "import ROOT" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In order to activate the interactive visualisation we can use the [JSROOT](https://root.cern.ch/js/) magic:" ] }, { "cell_type": "code", "execution_count": 3, "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": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "h = ROOT.TH1F(\"myHisto\",\"My Histo;X axis;Y axis\",64, -4, 4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Time to create a random generator and fill our histogram:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "rndmGenerator = ROOT.TRandom3()\n", "for i in xrange(1000):\n", " rndm = rndmGenerator.Gaus()\n", " h.Fill(rndm)" ] }, { "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. Note that thanks to [JSROOT](https://root.cern.ch/js/), this is not a static plot but an interactive visualisation. Try to play with it and save it as image when you are satisfied!" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "c = ROOT.TCanvas()\n", "h.Draw()\n", "c.Draw()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We'll try now to beautify the plot a bit, for example filling the histogram with a colour and setting a grid on the canvas." ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "h.SetFillColor(ROOT.kBlue-10)\n", "c.SetGrid()\n", "h.Draw()\n", "c.Draw()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Alright: we are done with our first step into the ROOTbooks world!" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.10" } }, "nbformat": 4, "nbformat_minor": 0 }