{ "cells": [ { "cell_type": "markdown", "id": "e53a87b2", "metadata": {}, "source": [ "# hsimple\n", " This program creates :\n", " - a one dimensional histogram\n", " - a two dimensional histogram\n", " - a profile histogram\n", " - a memory-resident ntuple\n", "\n", " These objects are filled with some random numbers and saved on a file.\n", " If get=1 the macro returns a pointer to the TFile of \"hsimple.root\"\n", " if this file exists, otherwise it is created.\n", " The file \"hsimple.root\" is created in $ROOTSYS/tutorials if the caller has\n", " write access to this directory, otherwise the file is created in $PWD\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:13 PM." ] }, { "cell_type": "markdown", "id": "8af16151", "metadata": {}, "source": [ " Arguments are defined. " ] }, { "cell_type": "code", "execution_count": 1, "id": "27ece587", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:13:27.637883Z", "iopub.status.busy": "2026-05-19T20:13:27.637704Z", "iopub.status.idle": "2026-05-19T20:13:27.981551Z", "shell.execute_reply": "2026-05-19T20:13:27.980986Z" } }, "outputs": [], "source": [ "Int_t getFile=0;" ] }, { "cell_type": "code", "execution_count": 2, "id": "6875fd1b", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:13:27.983206Z", "iopub.status.busy": "2026-05-19T20:13:27.983085Z", "iopub.status.idle": "2026-05-19T20:13:28.185189Z", "shell.execute_reply": "2026-05-19T20:13:28.184635Z" } }, "outputs": [], "source": [ "TString filename = \"hsimple.root\";\n", "TString dir = gROOT->GetTutorialDir();\n", "dir.ReplaceAll(\"/./\",\"/\");\n", "TFile *hfile = nullptr;\n", "if (getFile) {\n", " // if the argument getFile =1 return the file \"hsimple.root\"\n", " // if the file does not exist, it is created\n", " TString fullPath = dir+\"hsimple.root\";\n", " if (!gSystem->AccessPathName(fullPath,kFileExists)) {\n", " hfile = TFile::Open(fullPath); //in $ROOTSYS/tutorials\n", " if (hfile) return hfile;\n", " }\n", " //otherwise try $PWD/hsimple.root\n", " if (!gSystem->AccessPathName(\"hsimple.root\",kFileExists)) {\n", " hfile = TFile::Open(\"hsimple.root\"); //in current dir\n", " if (hfile) return hfile;\n", " }\n", "}" ] }, { "cell_type": "markdown", "id": "ff988893", "metadata": {}, "source": [ "no hsimple.root file found. Must generate it !\n", "generate hsimple.root in current directory if we have write access" ] }, { "cell_type": "code", "execution_count": 3, "id": "5af26044", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:13:28.187007Z", "iopub.status.busy": "2026-05-19T20:13:28.186892Z", "iopub.status.idle": "2026-05-19T20:13:28.390316Z", "shell.execute_reply": "2026-05-19T20:13:28.389695Z" } }, "outputs": [], "source": [ "if (gSystem->AccessPathName(\".\",kWritePermission)) {\n", " printf(\"you must run the script in a directory with write access\\n\");\n", " return nullptr;\n", "}\n", "hfile = (TFile*)gROOT->FindObject(filename); if (hfile) hfile->Close();\n", "hfile = new TFile(filename,\"RECREATE\",\"Demo ROOT file with histograms\");" ] }, { "cell_type": "markdown", "id": "defb12af", "metadata": {}, "source": [ "Create some histograms, a profile histogram and an ntuple, add them to hfile" ] }, { "cell_type": "code", "execution_count": 4, "id": "85697db9", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:13:28.392092Z", "iopub.status.busy": "2026-05-19T20:13:28.391945Z", "iopub.status.idle": "2026-05-19T20:13:28.595707Z", "shell.execute_reply": "2026-05-19T20:13:28.595118Z" } }, "outputs": [], "source": [ "TH1F *hpx = new TH1F(\"hpx\",\"This is the px distribution\",100,-4,4);\n", "hpx->SetDirectory(hfile);\n", "hpx->SetFillColor(48);\n", "TH2F *hpxpy = new TH2F(\"hpxpy\",\"py vs px\",40,-4,4,40,-4,4);\n", "hpxpy->SetDirectory(hfile);\n", "TProfile *hprof = new TProfile(\"hprof\",\"Profile of pz versus px\",100,-4,4,0,20);\n", "hprof->SetDirectory(hfile);\n", "TNtuple *ntuple = new TNtuple(\"ntuple\",\"Demo ntuple\",\"px:py:pz:random:i\");" ] }, { "cell_type": "markdown", "id": "836b9e6b", "metadata": {}, "source": [ "Set to \"true\" if you want to see in the output how much time it took to fill the histogram:" ] }, { "cell_type": "code", "execution_count": 5, "id": "1b4dea9d", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:13:28.597244Z", "iopub.status.busy": "2026-05-19T20:13:28.597131Z", "iopub.status.idle": "2026-05-19T20:13:28.800414Z", "shell.execute_reply": "2026-05-19T20:13:28.799863Z" } }, "outputs": [], "source": [ "const bool doBenchmark = false;\n", "\n", "if (doBenchmark)\n", " gBenchmark->Start(\"hsimple\");" ] }, { "cell_type": "markdown", "id": "90eeb47f", "metadata": {}, "source": [ "Create a new canvas." ] }, { "cell_type": "code", "execution_count": 6, "id": "8ecf537e", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:13:28.812271Z", "iopub.status.busy": "2026-05-19T20:13:28.812120Z", "iopub.status.idle": "2026-05-19T20:13:29.016309Z", "shell.execute_reply": "2026-05-19T20:13:29.015796Z" } }, "outputs": [], "source": [ "TCanvas *c1 = new TCanvas(\"c1\",\"Dynamic Filling Example\",200,10,700,500);\n", "c1->SetFillColor(42);\n", "c1->GetFrame()->SetFillColor(21);\n", "c1->GetFrame()->SetBorderSize(6);\n", "c1->GetFrame()->SetBorderMode(-1);" ] }, { "cell_type": "markdown", "id": "7fa97daa", "metadata": {}, "source": [ "Fill histograms randomly" ] }, { "cell_type": "code", "execution_count": 7, "id": "50aa282d", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:13:29.018273Z", "iopub.status.busy": "2026-05-19T20:13:29.018148Z", "iopub.status.idle": "2026-05-19T20:13:29.221330Z", "shell.execute_reply": "2026-05-19T20:13:29.220788Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "