\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 [write it](https://root.cern.ch/doc/master/classTObject.html#a19782a4717dbfd4857ccd9ffa68aa06d)."
]
},
{
"cell_type": "code",
"execution_count": 1,
"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": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"%jsroot on"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We now define a function that will create a histogram, fill it and write it to a file. Later, we will read back the histogram from disk."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def writeHisto(outputFileName):\n",
" outputFile = ROOT.TFile(outputFileName, \"RECREATE\")\n",
" h = ROOT.TH1F(\"theHisto\",\"My Test Histogram;X Title; Y Title\",64, -4, 4)\n",
" h.FillRandom(\"gaus\")\n",
" # now we write to the file\n",
" h.Write()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"All objects the class of which has a dictionary can be written on disk. By default, the most widely used ROOT classes are shipped with a dictionary: the histogram is one of those.\n",
"Writing on a file is as simple as invoking the [*Write* method](https://root.cern.ch/doc/master/classTObject.html#a19782a4717dbfd4857ccd9ffa68aa06d).\n",
"\n",
"Now we invoke the function:"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"writeHisto(\"output.root\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Before reading the object, we can check from the commandline the content of the file with the [**rootls** utility](https://root.cern.ch/how/how-quickly-inspect-content-file):"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"TH1F Apr 12 14:06 theHisto \"My Test Histogram\"\n"
]
}
],
"source": [
"%%bash\n",
"rootls -l output.root"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We see that the file contains one object of type TH1F, the name of which is *theHisto* and the title of which is *My Test Histogram*. \n",
"\n",
"Let's now use the ROOT interface to read it and draw it:"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"