{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# PyROOT Exercise 3: Canvas and Legends" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise\n", "\n", "Write a python macro `ExerciseCanvas.py`.\n", "\n", "1. Create two histograms with 50 bins ranging from -3 to 3 with two different names.\n", "2. Fill first histogram with Gaussian distribution with 10000 entries.\n", "3. Fill second histogram with a second order polynomial and 5000 entries (hint: `hist2.FillRandom(\"pol2\", 500)`).\n", "4. Set the line color of the first histogram to `kRed` and second to `kBlue`.\n", "5. Draw both histograms on a canvas.\n", "6. Clone both histograms and normalise them (scale with inverse of the integral).\n", "7. Draw both histograms on a different canvas.\n", "8. Draw a legend on both canvases at position `(0.16, 0.63, 0.45, 0.91)`; bonus: do it after you created both canvases.\n", "9. Save both canvases in a PDF; bonus: save them in the same file." ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Solution" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "vscode": { "languageId": "python" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Welcome to JupyROOT 6.28/00\n" ] } ], "source": [ "import ROOT\n", "%jsroot on" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "vscode": { "languageId": "python" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Info in : pdf file output.pdf has been created using the current canvas\n", "Info in : Current canvas added to pdf file output.pdf and file closed\n" ] }, { "data": { "text/html": [ "\n", "\n", "
\n", "
\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "\n", "\n", "
\n", "
\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from ROOT import gStyle, TCanvas, TH1D, TLegend, kRed, kBlue\n", "\n", "gStyle.SetOptStat(0)\n", "\n", "canvas1 = TCanvas(\"canvas1\", \"\", 800, 600)\n", "\n", "hist1 = TH1D(\"hist1\", \"Gauss\", 50, -3, 3)\n", "hist2 = TH1D(\"hist2\", \"Polynomial\", 50, -3, 3)\n", "\n", "hist1.FillRandom(\"gaus\", 10000)\n", "hist2.FillRandom(\"pol2\", 5000)\n", "\n", "hist1.SetLineColor(kRed)\n", "hist2.SetLineColor(kBlue)\n", "\n", "hist1.Draw()\n", "hist2.Draw(\"same\")\n", "\n", "canvas2 = TCanvas(\"canvas2\", \"\", 800, 600)\n", "\n", "hist1_norm = hist1.Clone(\"hist1_norm\")\n", "hist1_norm.Scale(1 / hist1.Integral())\n", "hist2_norm = hist2.Clone(\"hist2_norm\")\n", "hist2_norm.Scale(1 / hist2.Integral())\n", "\n", "hist1_norm.Draw(\"hist\")\n", "hist2_norm.Draw(\"hist same\")\n", "\n", "legend = TLegend(0.7, 0.75, 0.90, 0.87)\n", "legend.AddEntry(hist1, hist1.GetTitle(), \"l\")\n", "legend.AddEntry(hist2, hist2.GetTitle(), \"l\")\n", "legend.SetBorderSize(0)\n", "\n", "canvas1.cd()\n", "legend.Draw()\n", "canvas2.cd()\n", "legend.Draw()\n", "\n", "canvas1.Draw()\n", "canvas2.Draw()\n", "\n", "canvas1.SaveAs(\"output.pdf(\")\n", "canvas2.SaveAs(\"output.pdf)\")" ] } ], "metadata": { "kernelspec": { "display_name": "LCG 103", "language": "python", "name": "lcg_103" }, "orig_nbformat": 4, "vscode": { "interpreter": { "hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6" } } }, "nbformat": 4, "nbformat_minor": 2 }