{ "cells": [ { "cell_type": "markdown", "id": "e18f925e", "metadata": {}, "source": [ "# hist015_TH1_read_and_draw\n", "We attach (or generate) the ROOT file in `$ROOTSYS/tutorials/hsimple.root`\n", "or `$PWD/hsimple.root`\n", "We draw one histogram in different formats.\n", "\n", "\n", "\n", "\n", "**Author:** Rene Brun \n", "This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Tuesday, May 19, 2026 at 08:12 PM." ] }, { "cell_type": "markdown", "id": "9d16be11", "metadata": {}, "source": [ "Fetch and open the hsimple.root File" ] }, { "cell_type": "code", "execution_count": 1, "id": "3f32f546", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:12:12.662363Z", "iopub.status.busy": "2026-05-19T20:12:12.662254Z", "iopub.status.idle": "2026-05-19T20:12:12.978217Z", "shell.execute_reply": "2026-05-19T20:12:12.977471Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "TFile**\t\t/github/home/ROOT-CI/build/tutorials/hsimple.root\tDemo ROOT file with histograms\n", " TFile*\t\t/github/home/ROOT-CI/build/tutorials/hsimple.root\tDemo ROOT file with histograms\n", " KEY: TH1F\thpx;1\tThis is the px distribution\n", " KEY: TH2F\thpxpy;1\tpy vs px\n", " KEY: TProfile\thprof;1\tProfile of pz versus px\n", " KEY: TNtuple\tntuple;1\tDemo ntuple\n" ] } ], "source": [ "TFile *example = TFile::Open(gROOT->GetTutorialDir() + \"/hsimple.root\");\n", "\n", "example->ls(); // Show the file contents" ] }, { "cell_type": "markdown", "id": "05ce8a89", "metadata": {}, "source": [ "Get the histogram from the ROOT File" ] }, { "cell_type": "code", "execution_count": 2, "id": "fc98368f", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:12:12.979705Z", "iopub.status.busy": "2026-05-19T20:12:12.979551Z", "iopub.status.idle": "2026-05-19T20:12:13.190352Z", "shell.execute_reply": "2026-05-19T20:12:13.189774Z" } }, "outputs": [], "source": [ "TH1 *hpx = nullptr; // pointer to base class TH1 is enough\n", "example->GetObject(\"hpx\", hpx);\n", "\n", "TCanvas *c1 = new TCanvas(\"c1\", \"Histogram Drawing Options\", 200, 10, 700, 900);\n", "TPad *pad1 = new TPad(\"pad1\", \"The pad with the function\", 0.03, 0.62, 0.50, 0.92);\n", "TPad *pad2 = new TPad(\"pad2\", \"The pad with the histogram\", 0.51, 0.62, 0.98, 0.92);\n", "TPad *pad3 = new TPad(\"pad3\", \"The pad with the histogram\", 0.03, 0.02, 0.97, 0.57);\n", "pad1->Draw();\n", "pad2->Draw();\n", "pad3->Draw();" ] }, { "cell_type": "markdown", "id": "aaaeb1da", "metadata": {}, "source": [ "Draw a global picture title" ] }, { "cell_type": "code", "execution_count": 3, "id": "3c75b11d", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:12:13.192308Z", "iopub.status.busy": "2026-05-19T20:12:13.192187Z", "iopub.status.idle": "2026-05-19T20:12:13.396762Z", "shell.execute_reply": "2026-05-19T20:12:13.395713Z" } }, "outputs": [], "source": [ "TPaveLabel *title = new TPaveLabel(0.1, 0.94, 0.9, 0.98, \"Drawing options for one dimensional histograms\");\n", "title->SetTextFont(52);\n", "title->Draw();" ] }, { "cell_type": "markdown", "id": "88e3de96", "metadata": {}, "source": [ "Draw histogram hpx in first pad with the default option." ] }, { "cell_type": "code", "execution_count": 4, "id": "c9fe989e", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:12:13.398374Z", "iopub.status.busy": "2026-05-19T20:12:13.398254Z", "iopub.status.idle": "2026-05-19T20:12:13.602480Z", "shell.execute_reply": "2026-05-19T20:12:13.601897Z" } }, "outputs": [], "source": [ "pad1->cd();\n", "pad1->GetFrame()->SetFillColor(18);\n", "hpx->SetFillColor(45);\n", "hpx->DrawCopy();\n", "TPaveLabel *label1 = new TPaveLabel(-3.5, 700, -1, 800, \"Default option\");\n", "label1->Draw();" ] }, { "cell_type": "markdown", "id": "46b478d5", "metadata": {}, "source": [ "Draw hpx as a lego. Clicking on the lego area will show\n", "a \"transparent cube\" to guide you rotating the lego in real time." ] }, { "cell_type": "code", "execution_count": 5, "id": "86fd3f26", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:12:13.604425Z", "iopub.status.busy": "2026-05-19T20:12:13.604306Z", "iopub.status.idle": "2026-05-19T20:12:13.809262Z", "shell.execute_reply": "2026-05-19T20:12:13.807797Z" } }, "outputs": [], "source": [ "pad2->cd();\n", "hpx->DrawCopy(\"lego1\");\n", "TPaveLabel *label2 = new TPaveLabel(-0.72, 0.74, -0.22, 0.88, \"option Lego1\");\n", "label2->Draw();\n", "TPaveLabel *label2a = new TPaveLabel(-0.93, -1.08, 0.25, -0.92, \"Click on lego to rotate\");\n", "label2a->Draw();" ] }, { "cell_type": "markdown", "id": "4c925bee", "metadata": {}, "source": [ "Draw hpx with its errors and a marker." ] }, { "cell_type": "code", "execution_count": 6, "id": "9791f371", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:12:13.811055Z", "iopub.status.busy": "2026-05-19T20:12:13.810936Z", "iopub.status.idle": "2026-05-19T20:12:14.015519Z", "shell.execute_reply": "2026-05-19T20:12:14.014419Z" } }, "outputs": [], "source": [ "pad3->cd();\n", "pad3->SetGridx();\n", "pad3->SetGridy();\n", "hpx->SetMarkerStyle(21);\n", "hpx->Draw(\"e1p\");\n", "TPaveLabel *label3 = new TPaveLabel(2, 600, 3.5, 650, \"option e1p\");\n", "label3->Draw();" ] }, { "cell_type": "markdown", "id": "9a234163", "metadata": {}, "source": [ "The following illustrates how to add comments using a PaveText.\n", "Attributes of text/lines/boxes added to a PaveText can be modified.\n", "The AddText function returns a pointer to the added object." ] }, { "cell_type": "code", "execution_count": 7, "id": "2ca60099", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:12:14.017223Z", "iopub.status.busy": "2026-05-19T20:12:14.017102Z", "iopub.status.idle": "2026-05-19T20:12:14.228882Z", "shell.execute_reply": "2026-05-19T20:12:14.228394Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "