{ "cells": [ { "cell_type": "markdown", "id": "f30cfce1", "metadata": {}, "source": [ "# tree113_getval\n", "Illustrates how to retrieve TTree variables in arrays.\n", "\n", "This example:\n", " - creates a simple TTree,\n", " - generates TTree variables thanks to the `Draw` method with `goff` option,\n", " - retrieves some of them in arrays thanks to `GetVal`,\n", " - generates and draw graphs with these arrays.\n", "\n", "The option `goff` in `TTree::Draw` behaves like any other drawing option except\n", "that, at the end, no graphics is produced ( `goff`= graphics off). This allows\n", "to generate as many TTree variables as needed. All the graphics options\n", "(except `para` and `candle`) are limited to four variables only. And `para`\n", "and `candle` need at least two variables.\n", "\n", "Note that by default TTree::Draw creates the arrays obtained\n", "with GetVal with a length corresponding to the parameter `fEstimate`.\n", "By default fEstimate=1000000 and can be modified\n", "via TTree::SetEstimate. To keep in memory all the results use:\n", "```cpp\n", " tree->SetEstimate(-1);\n", "```\n", "SetEstimate should be called if the expected number of selected rows\n", "is greater than 1000000.\n", "\n", "\n", "\n", "\n", "**Author:** Olivier Couet \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:18 PM." ] }, { "cell_type": "markdown", "id": "0544bb8f", "metadata": {}, "source": [ "create a simple TTree with 5 branches" ] }, { "cell_type": "code", "execution_count": 1, "id": "acca831c", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:18:36.169057Z", "iopub.status.busy": "2026-05-19T20:18:36.168948Z", "iopub.status.idle": "2026-05-19T20:18:36.489798Z", "shell.execute_reply": "2026-05-19T20:18:36.489216Z" } }, "outputs": [], "source": [ "Int_t run, evt;\n", "Float_t x, y, z;\n", "auto T = new TTree(\"T\", \"test friend trees\");\n", "T->Branch(\"Run\", &run, \"Run/I\");\n", "T->Branch(\"Event\", &evt, \"Event/I\");\n", "T->Branch(\"x\", &x, \"x/F\");\n", "T->Branch(\"y\", &y, \"y/F\");\n", "T->Branch(\"z\", &z, \"z/F\");\n", "TRandom r;\n", "for (Int_t i=0; i<10000; i++) {\n", " if (i < 5000)\n", " run = 1;\n", " else\n", " run = 2;\n", " evt = i;\n", " x = r.Gaus(10, 1);\n", " y = r.Gaus(20, 2);\n", " z = r.Landau(2, 1);\n", " T->Fill();\n", "}" ] }, { "cell_type": "markdown", "id": "527cd6c8", "metadata": {}, "source": [ "Draw with option goff and generate seven variables" ] }, { "cell_type": "code", "execution_count": 2, "id": "0c218642", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:18:36.491575Z", "iopub.status.busy": "2026-05-19T20:18:36.491456Z", "iopub.status.idle": "2026-05-19T20:18:36.696991Z", "shell.execute_reply": "2026-05-19T20:18:36.696558Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The arrays' dimension is 5000\n" ] } ], "source": [ "Int_t n = T->Draw(\"x:y:z:Run:Event:sin(x):cos(x)\", \"Run==1\", \"goff\");\n", "printf(\"The arrays' dimension is %d\\n\", n);" ] }, { "cell_type": "markdown", "id": "4fa55942", "metadata": {}, "source": [ "Retrieve variables 0, 5 et 6" ] }, { "cell_type": "code", "execution_count": 3, "id": "9f64bc65", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:18:36.698421Z", "iopub.status.busy": "2026-05-19T20:18:36.698303Z", "iopub.status.idle": "2026-05-19T20:18:36.903638Z", "shell.execute_reply": "2026-05-19T20:18:36.903082Z" } }, "outputs": [], "source": [ "Double_t *vx = T->GetVal(0);\n", "Double_t *vxs = T->GetVal(5);\n", "Double_t *vxc = T->GetVal(6);" ] }, { "cell_type": "markdown", "id": "4167adae", "metadata": {}, "source": [ "Create and draw graphs" ] }, { "cell_type": "code", "execution_count": 4, "id": "310c33a9", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:18:36.905340Z", "iopub.status.busy": "2026-05-19T20:18:36.905224Z", "iopub.status.idle": "2026-05-19T20:18:37.109476Z", "shell.execute_reply": "2026-05-19T20:18:37.108955Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Info in : created default TCanvas with name c1\n" ] } ], "source": [ "auto gs = new TGraph(n, vx, vxs);\n", "auto gc = new TGraph(n, vx, vxc);\n", "gs->Draw(\"ap\");\n", "gc->Draw(\"p\");" ] }, { "cell_type": "markdown", "id": "7389cb47", "metadata": {}, "source": [ "Draw all canvases " ] }, { "cell_type": "code", "execution_count": 5, "id": "c6ebe6d0", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:18:37.111186Z", "iopub.status.busy": "2026-05-19T20:18:37.111068Z", "iopub.status.idle": "2026-05-19T20:18:37.316407Z", "shell.execute_reply": "2026-05-19T20:18:37.315740Z" } }, "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 }