{ "cells": [ { "cell_type": "markdown", "id": "9a0e812b", "metadata": {}, "source": [ "# gr007_multigraph\n", "Allowing to overlay different graphs can be useful for comparing different datasets\n", "or for plotting multiple related graphs on the same canvas.\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:37 PM." ] }, { "cell_type": "code", "execution_count": 1, "id": "c582da81", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:37:51.332334Z", "iopub.status.busy": "2026-05-19T20:37:51.332200Z", "iopub.status.idle": "2026-05-19T20:37:51.743091Z", "shell.execute_reply": "2026-05-19T20:37:51.742671Z" } }, "outputs": [], "source": [ "gStyle->SetOptFit();\n", "auto c1 = new TCanvas(\"c1\",\"multigraph\",700,500);\n", "c1->SetGrid();" ] }, { "cell_type": "markdown", "id": "418f73db", "metadata": {}, "source": [ "Initialize a TMultiGraph to hold multiple graphs\n", "This ensures the entire dataset from all added graphs is visible without manual range adjustments." ] }, { "cell_type": "code", "execution_count": 2, "id": "2b4e9d3b", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:37:51.754040Z", "iopub.status.busy": "2026-05-19T20:37:51.753905Z", "iopub.status.idle": "2026-05-19T20:37:51.986080Z", "shell.execute_reply": "2026-05-19T20:37:51.980392Z" } }, "outputs": [], "source": [ "auto mg = new TMultiGraph();" ] }, { "cell_type": "markdown", "id": "a3240f17", "metadata": {}, "source": [ "Create first graph" ] }, { "cell_type": "code", "execution_count": 3, "id": "fd55c904", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:37:51.992354Z", "iopub.status.busy": "2026-05-19T20:37:51.992233Z", "iopub.status.idle": "2026-05-19T20:37:52.363897Z", "shell.execute_reply": "2026-05-19T20:37:52.347400Z" } }, "outputs": [], "source": [ "const Int_t n1 = 10;\n", "Double_t px1[] = {-0.1, 0.05, 0.25, 0.35, 0.5, 0.61,0.7,0.85,0.89,0.95};\n", "Double_t py1[] = {-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};\n", "auto gr1 = new TGraphErrors(n1,px1,py1,ex1,ey1);\n", "gr1->SetMarkerColor(kBlue);\n", "gr1->SetMarkerStyle(21);\n", "\n", "gr1->Fit(\"gaus\",\"q\");\n", "auto func1 = (TF1 *) gr1->GetListOfFunctions()->FindObject(\"gaus\");\n", "func1->SetLineColor(kBlue);" ] }, { "cell_type": "markdown", "id": "d96c3b91", "metadata": {}, "source": [ "Add the first graph to the multigraph" ] }, { "cell_type": "code", "execution_count": 4, "id": "05d153d0", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:37:52.365724Z", "iopub.status.busy": "2026-05-19T20:37:52.365577Z", "iopub.status.idle": "2026-05-19T20:37:52.586082Z", "shell.execute_reply": "2026-05-19T20:37:52.585600Z" } }, "outputs": [], "source": [ "mg->Add(gr1);" ] }, { "cell_type": "markdown", "id": "02e2c282", "metadata": {}, "source": [ "Create second graph" ] }, { "cell_type": "code", "execution_count": 5, "id": "9c648954", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:37:52.596449Z", "iopub.status.busy": "2026-05-19T20:37:52.596308Z", "iopub.status.idle": "2026-05-19T20:37:52.857878Z", "shell.execute_reply": "2026-05-19T20:37:52.845653Z" } }, "outputs": [], "source": [ "const Int_t n2 = 10;\n", "Float_t x2[] = {-0.28, 0.005, 0.19, 0.29, 0.45, 0.56,0.65,0.80,0.90,1.01};\n", "Float_t y2[] = {2.1,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", "auto gr2 = new TGraphErrors(n2,x2,y2,ex2,ey2);\n", "gr2->SetMarkerColor(kRed);\n", "gr2->SetMarkerStyle(20);\n", "\n", "gr2->Fit(\"pol5\",\"q\");\n", "auto func2 = (TF1 *) gr2->GetListOfFunctions()->FindObject(\"pol5\");\n", "func2->SetLineColor(kRed);\n", "func2->SetLineStyle(2);" ] }, { "cell_type": "markdown", "id": "89087baf", "metadata": {}, "source": [ "Add the second graph to the multigraph" ] }, { "cell_type": "code", "execution_count": 6, "id": "4cd59cc3", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:37:52.862261Z", "iopub.status.busy": "2026-05-19T20:37:52.862107Z", "iopub.status.idle": "2026-05-19T20:37:53.073447Z", "shell.execute_reply": "2026-05-19T20:37:53.072854Z" } }, "outputs": [], "source": [ "mg->Add(gr2);\n", "\n", "mg->Draw(\"ap\");" ] }, { "cell_type": "markdown", "id": "b5f3e2b3", "metadata": {}, "source": [ "Force drawing of canvas to generate the fit TPaveStats" ] }, { "cell_type": "code", "execution_count": 7, "id": "26056247", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:37:53.075642Z", "iopub.status.busy": "2026-05-19T20:37:53.075500Z", "iopub.status.idle": "2026-05-19T20:37:53.414427Z", "shell.execute_reply": "2026-05-19T20:37:53.412751Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "