{
"cells": [
{
"cell_type": "markdown",
"id": "8703d94b",
"metadata": {},
"source": [
"# gr001_simple\n",
"- The first section plots data generated from arrays.\n",
"- The second section plots data read from a text file.\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": "8fe79648",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:37:40.231360Z",
"iopub.status.busy": "2026-05-19T20:37:40.231233Z",
"iopub.status.idle": "2026-05-19T20:37:40.253234Z",
"shell.execute_reply": "2026-05-19T20:37:40.246626Z"
}
},
"outputs": [],
"source": [
"%%cpp -d\n",
"#include //Include the library for reading the data file"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "ac8d25e0",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:37:40.254765Z",
"iopub.status.busy": "2026-05-19T20:37:40.254640Z",
"iopub.status.idle": "2026-05-19T20:37:40.620368Z",
"shell.execute_reply": "2026-05-19T20:37:40.619738Z"
}
},
"outputs": [],
"source": [
"TCanvas *c1 = new TCanvas(\"c1\",\"Two simple graphs\",200,10,700,500);\n",
"\n",
"c1->Divide(2,1); //Dividing the canvas in subpads for distinguishing the two examples, [See documentation](https://root.cern/doc/master/classTCanvas.html)"
]
},
{
"cell_type": "markdown",
"id": "e73a8201",
"metadata": {},
"source": [
"FIRST EXAMPLE (Available data)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "41c77131",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:37:40.633232Z",
"iopub.status.busy": "2026-05-19T20:37:40.633100Z",
"iopub.status.idle": "2026-05-19T20:37:40.858200Z",
"shell.execute_reply": "2026-05-19T20:37:40.857307Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" i 0 0.000000 1.986693 \n",
" i 1 0.100000 2.955202 \n",
" i 2 0.200000 3.894183 \n",
" i 3 0.300000 4.794255 \n",
" i 4 0.400000 5.646425 \n",
" i 5 0.500000 6.442177 \n",
" i 6 0.600000 7.173561 \n",
" i 7 0.700000 7.833269 \n",
" i 8 0.800000 8.414710 \n",
" i 9 0.900000 8.912074 \n",
" i 10 1.000000 9.320391 \n",
" i 11 1.100000 9.635582 \n",
" i 12 1.200000 9.854497 \n",
" i 13 1.300000 9.974950 \n",
" i 14 1.400000 9.995736 \n",
" i 15 1.500000 9.916648 \n",
" i 16 1.600000 9.738476 \n",
" i 17 1.700000 9.463001 \n",
" i 18 1.800000 9.092974 \n",
" i 19 1.900000 8.632094 \n"
]
}
],
"source": [
"c1->cd(1);\n",
"\n",
"const Int_t n = 20; //Fill the arrays x and y with the data points\n",
"Double_t x[n], y[n];\n",
"for (Int_t i=0;iSetLineColor(2);\n",
"gr1->SetLineWidth(4);\n",
"gr1->SetMarkerColor(4);\n",
"gr1->SetMarkerStyle(21);\n",
"gr1->SetTitle(\"Graph from available data\"); //Choose title for the graph\n",
"gr1->GetXaxis()->SetTitle(\"X title\"); //Choose title for the axis\n",
"gr1->GetYaxis()->SetTitle(\"Y title\");"
]
},
{
"cell_type": "markdown",
"id": "095c7af4",
"metadata": {},
"source": [
"Uncomment the following line to set a custom range for the x-axis (respectively for the y-axis):\n",
"gr1->GetXaxis()->SetRangeUser(0, 1.8);"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "d48bac76",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:37:41.069468Z",
"iopub.status.busy": "2026-05-19T20:37:41.069333Z",
"iopub.status.idle": "2026-05-19T20:37:41.292100Z",
"shell.execute_reply": "2026-05-19T20:37:41.275901Z"
}
},
"outputs": [],
"source": [
"gr1->Draw(\"ACP\"); //\"A\" draw axes, \"C\" = draw a smooth line through the markers (optional) and \"P\" = draw markers for data points "
]
},
{
"cell_type": "markdown",
"id": "12835aff",
"metadata": {},
"source": [
"Optional customization can be done on a ROOT interactive session"
]
},
{
"cell_type": "markdown",
"id": "3e072e93",
"metadata": {},
"source": [
"SECOND EXAMPLE (Data stored in a text file)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "da217699",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:37:41.294127Z",
"iopub.status.busy": "2026-05-19T20:37:41.293977Z",
"iopub.status.idle": "2026-05-19T20:37:41.503256Z",
"shell.execute_reply": "2026-05-19T20:37:41.502758Z"
}
},
"outputs": [],
"source": [
"c1->cd(2);\n",
"\n",
"const Int_t m = 20; //Known number of data points in the file\n",
"Double_t w[m], z[m];\n",
"\n",
"std::ifstream file(gROOT->GetTutorialDir() + \"/visualisation/graphs/data_basic.txt\"); // Open the data file"
]
},
{
"cell_type": "markdown",
"id": "86a4693d",
"metadata": {},
"source": [
"Use a for loop to read the data"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "0e7b5915",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:37:41.505447Z",
"iopub.status.busy": "2026-05-19T20:37:41.505329Z",
"iopub.status.idle": "2026-05-19T20:37:41.735694Z",
"shell.execute_reply": "2026-05-19T20:37:41.718717Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" i 0 -3.000000 -0.989992 \n",
" i 1 -2.684211 -0.897212 \n",
" i 2 -2.368421 -0.715699 \n",
" i 3 -2.052632 -0.463406 \n",
" i 4 -1.736842 -0.165284 \n",
" i 5 -1.421053 0.149185 \n",
" i 6 -1.105263 0.448899 \n",
" i 7 -0.789474 0.704219 \n",
" i 8 -0.473684 0.889894 \n",
" i 9 -0.157895 0.987561 \n",
" i 10 0.157895 0.987561 \n",
" i 11 0.473684 0.889894 \n",
" i 12 0.789474 0.704219 \n",
" i 13 1.105263 0.448899 \n",
" i 14 1.421053 0.149185 \n",
" i 15 1.736842 -0.165284 \n",
" i 16 2.052632 -0.463406 \n",
" i 17 2.368421 -0.715699 \n",
" i 18 2.684211 -0.897212 \n",
" i 19 3.000000 -0.989992 \n"
]
}
],
"source": [
"for (Int_t i = 0; i < m; i++) {\n",
" file >> w[i] >> z[i]; //Fill the arrays with the data from the file\n",
" printf(\" i %i %f %f \\n\",i,w[i],z[i]);\n",
"}\n",
"\n",
"file.close(); //Close the file after reading\n",
"\n",
"TGraph *gr2 = new TGraph(m, w, z); //Create a TGraph object for the file data\n",
"gr2->SetLineColor(4);\n",
"gr2->SetLineWidth(2);\n",
"gr2->SetMarkerColor(2);\n",
"gr2->SetMarkerStyle(20);\n",
"gr2->SetTitle(\"Graph from data file\");\n",
"gr2->GetXaxis()->SetTitle(\"W title\");\n",
"gr2->GetYaxis()->SetTitle(\"Z title\");\n",
"\n",
"\n",
"gr2->Draw(\"ACP\");"
]
},
{
"cell_type": "markdown",
"id": "883463b3",
"metadata": {},
"source": [
"Draw all canvases "
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "4487a01b",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:37:41.750271Z",
"iopub.status.busy": "2026-05-19T20:37:41.750115Z",
"iopub.status.idle": "2026-05-19T20:37:41.961371Z",
"shell.execute_reply": "2026-05-19T20:37:41.960960Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"\n",
"
\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"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
}