{ "cells": [ { "cell_type": "markdown", "id": "98bee0e1", "metadata": {}, "source": [ "# gr003_errors2\n", "\n", "We first draw an empty frame with the axes, then draw the graphs on top of it\n", "Note that the graphs should have the same or very close ranges (in both axis),\n", "otherwise they may not be visible in the frame.\n", "\n", "Alternatively, an automatic axis scaling can be achieved via a\n", "[TMultiGraph](https://root.cern/doc/master/classTMultiGraph.html)\n", "\n", "See the [TGraphErrors documentation](https://root.cern/doc/master/classTGraphErrors.html)\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:37 PM." ] }, { "cell_type": "code", "execution_count": 1, "id": "9f195042", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:37:43.731280Z", "iopub.status.busy": "2026-05-19T20:37:43.731142Z", "iopub.status.idle": "2026-05-19T20:37:44.084907Z", "shell.execute_reply": "2026-05-19T20:37:44.084274Z" } }, "outputs": [], "source": [ "TCanvas *c1 = new TCanvas(\"c1\",\"2 graphs with errors\",200,10,700,500);\n", "c1->SetGrid();" ] }, { "cell_type": "markdown", "id": "685e979a", "metadata": {}, "source": [ "draw a frame to define the range" ] }, { "cell_type": "code", "execution_count": 2, "id": "4f164b94", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:37:44.092279Z", "iopub.status.busy": "2026-05-19T20:37:44.092157Z", "iopub.status.idle": "2026-05-19T20:37:44.308283Z", "shell.execute_reply": "2026-05-19T20:37:44.307645Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "
\n", "
\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "TH1F *hr = c1->DrawFrame(-0.4,0,1.2,12);\n", "hr->SetXTitle(\"X title\");\n", "hr->SetYTitle(\"Y title\");\n", "c1->GetFrame()->SetBorderSize(12);" ] }, { "cell_type": "markdown", "id": "753d2ae7", "metadata": {}, "source": [ "create first graph\n", "We will use the constructor requiring: the number of points, arrays containing the x-and y-axis values, and arrays with the x- andy-axis errors" ] }, { "cell_type": "code", "execution_count": 3, "id": "bafb8e4b", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:37:44.309936Z", "iopub.status.busy": "2026-05-19T20:37:44.309815Z", "iopub.status.idle": "2026-05-19T20:37:44.521471Z", "shell.execute_reply": "2026-05-19T20:37:44.520832Z" } }, "outputs": [], "source": [ "const Int_t n1 = 10;\n", "Double_t xval1[] = {-0.22, 0.05, 0.25, 0.35, 0.5, 0.61,0.7,0.85,0.89,0.95};\n", "Double_t yval1[] = {1,2.9,5.6,7.4,9,9.6,8.7,6.3,4.5,1};\n", "Double_t ex1[] = {.05,.1,.07,.07,.04,.05,.06,.07,.08,.05};\n", "Double_t ey1[] = {.8,.7,.6,.5,.4,.4,.5,.6,.7,.8};" ] }, { "cell_type": "markdown", "id": "a501bcc5", "metadata": {}, "source": [ "If all x-axis errors should zero, just provide a single 0 in place of ex1" ] }, { "cell_type": "code", "execution_count": 4, "id": "49051850", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:37:44.522975Z", "iopub.status.busy": "2026-05-19T20:37:44.522860Z", "iopub.status.idle": "2026-05-19T20:37:44.734426Z", "shell.execute_reply": "2026-05-19T20:37:44.733803Z" } }, "outputs": [], "source": [ "TGraphErrors *gr1 = new TGraphErrors(n1,xval1,yval1,ex1,ey1);\n", "gr1->SetMarkerColor(kBlue);\n", "gr1->SetMarkerStyle(21);" ] }, { "cell_type": "markdown", "id": "f8562ef8", "metadata": {}, "source": [ "Since we already have a frame in the canvas, we draw the graph without the option \"A\" (which draws axes for this graph)" ] }, { "cell_type": "code", "execution_count": 5, "id": "3c02c10b", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:37:44.737344Z", "iopub.status.busy": "2026-05-19T20:37:44.737224Z", "iopub.status.idle": "2026-05-19T20:37:44.944531Z", "shell.execute_reply": "2026-05-19T20:37:44.943916Z" } }, "outputs": [], "source": [ "gr1->Draw(\"LP\");" ] }, { "cell_type": "markdown", "id": "bce9d861", "metadata": {}, "source": [ "create second graph" ] }, { "cell_type": "code", "execution_count": 6, "id": "7016fd1b", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:37:44.963363Z", "iopub.status.busy": "2026-05-19T20:37:44.963221Z", "iopub.status.idle": "2026-05-19T20:37:45.170824Z", "shell.execute_reply": "2026-05-19T20:37:45.170117Z" } }, "outputs": [], "source": [ "const Int_t n2 = 10;\n", "Float_t xval2[] = {-0.28, 0.005, 0.19, 0.29, 0.45, 0.56,0.65,0.80,0.90,1.01};\n", "Float_t yval2[] = {0.82,3.86,7,9,10,10.55,9.64,7.26,5.42,2};\n", "Float_t ex2[] = {.04,.12,.08,.06,.05,.04,.07,.06,.08,.04};\n", "Float_t ey2[] = {.6,.8,.7,.4,.3,.3,.4,.5,.6,.7};\n", "TGraphErrors *gr2 = new TGraphErrors(n2,xval2,yval2,ex2,ey2);\n", "gr2->SetMarkerColor(kRed);\n", "gr2->SetMarkerStyle(20);\n", "gr2->Draw(\"LP\");" ] }, { "cell_type": "markdown", "id": "d9eaebb0", "metadata": {}, "source": [ "Draw all canvases " ] }, { "cell_type": "code", "execution_count": 7, "id": "3df7a0fa", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:37:45.178088Z", "iopub.status.busy": "2026-05-19T20:37:45.177971Z", "iopub.status.idle": "2026-05-19T20:37:45.399231Z", "shell.execute_reply": "2026-05-19T20:37:45.398633Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "
\n", "
\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%jsroot on\n", "gROOT->GetListOfCanvases()->Draw()" ] } ], "metadata": { "kernelspec": { "display_name": "ROOT C++", "language": "c++", "name": "root" }, "language_info": { "codemirror_mode": "text/x-c++src", "file_extension": ".C", "mimetype": " text/x-c++src", "name": "c++" } }, "nbformat": 4, "nbformat_minor": 5 }