{ "cells": [ { "cell_type": "markdown", "id": "1bb17810", "metadata": {}, "source": [ "# tree108_tree\n", "This example writes a tree with objects of the class Event.\n", "It is a simplified version of $ROOTSYS/test/MainEvent.cxx to\n", "write the tree, and $ROOTSYS/test/eventb.C\n", "It shows:\n", " - how to fill a Tree with an event class containing these data members:\n", "```\n", " char fType[20];\n", " Int_t fNtrack;\n", " Int_t fNseg;\n", " Int_t fNvertex;\n", " UInt_t fFlag;\n", " Float_t fTemperature;\n", " EventHeader fEvtHdr;\n", " TClonesArray *fTracks; //->\n", " TH1F *fH; //->\n", " Int_t fMeasures[10];\n", " Float_t fMatrix[4][4];\n", " Float_t *fClosestDistance; //[fNvertex]\n", "```\n", " - the difference in splitting or not splitting a branch\n", " - how to read selected branches of the tree, and print the first entry with less than 587 tracks.\n", " - how to browse and analyze the Tree via the TBrowser and TTreeViewer\n", "\n", "This example can be run in many different ways:\n", " - way1 using the Cling interpreter:\n", "```\n", ".x tree108_tree.C\n", "```\n", " - way2 using the Cling interpreter:\n", "```\n", ".L tree108_tree.C\n", "tree108_tree()\n", "```\n", " - way3 using ACLIC:\n", "```\n", ".L ../test/libEvent.so\n", ".x tree108_tree.C++\n", "```\n", "One can also run the write and read parts in two separate sessions.\n", "For example following one of the sessions above, one can start the session:\n", "```\n", " .L tree108_tree.C\n", " tree108_read();\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:18 PM." ] }, { "cell_type": "code", "execution_count": 1, "id": "dfaacf45", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:18:07.488725Z", "iopub.status.busy": "2026-05-19T20:18:07.488572Z", "iopub.status.idle": "2026-05-19T20:18:07.802807Z", "shell.execute_reply": "2026-05-19T20:18:07.802168Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Unbalanced braces. This cell was not processed.\n" ] } ], "source": [ "#ifdef ACTUAL_RUN // -------- Second pass: dictionary already built --------\n", "\n", "#include \"./Event.h\" // now safe to include Event, its dictionary is loaded\n", "\n", "#else // -------- First pass: build dictionary + rerun macro --------" ] }, { "cell_type": "markdown", "id": "c1bfb46a", "metadata": {}, "source": [ " Definition of a helper function: " ] }, { "cell_type": "code", "execution_count": 2, "id": "5506e020", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:18:07.804577Z", "iopub.status.busy": "2026-05-19T20:18:07.804450Z", "iopub.status.idle": "2026-05-19T20:18:07.814274Z", "shell.execute_reply": "2026-05-19T20:18:07.813785Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_53:19:4: error: unknown type name 'Event'\n", " Event *event = new Event();\n", " ^\n", "input_line_53:19:23: error: unknown type name 'Event'\n", " Event *event = new Event();\n", " ^\n" ] } ], "source": [ "%%cpp -d\n", "#include \"TFile.h\"\n", "#include \"TTree.h\"\n", "#include \"TBrowser.h\"\n", "#include \"TH2.h\"\n", "#include \"TRandom.h\"\n", "#include \"TClassTable.h\"\n", "#include \"TSystem.h\"\n", "#include \"TROOT.h\"\n", "\n", "void tree108_write()\n", "{\n", " // create a Tree file tree108.root\n", " TFile f(\"tree108.root\",\"RECREATE\");\n", "\n", " // create a ROOT Tree\n", " TTree t4(\"t4\",\"A Tree with Events\");\n", "\n", " // create a pointer to an Event object\n", " Event *event = new Event();\n", "\n", " // create two branches, split one.\n", " t4.Branch(\"event_split\", &event,16000,99);\n", " t4.Branch(\"event_not_split\", &event,16000,0);\n", "\n", " // a local variable for the event type\n", " char etype[20];\n", "\n", " // fill the tree\n", " for (Int_t ev = 0; ev < 100; ev++) {\n", " Float_t sigmat, sigmas;\n", " gRandom->Rannor(sigmat, sigmas);\n", " Int_t ntrack = Int_t(600 + 600 * sigmat / 120.);\n", " Float_t random = gRandom->Rndm(1);\n", " sprintf(etype, \"type%d\", ev%5);\n", " event->SetType(etype);\n", " event->SetHeader(ev, 200, 960312, random);\n", " event->SetNseg(Int_t(10 * ntrack + 20 * sigmas));\n", " event->SetNvertex(Int_t(1 + 20 * gRandom->Rndm()));\n", " event->SetFlag(UInt_t(random + 0.5));\n", " event->SetTemperature(random + 20.);\n", "\n", " for (UChar_t m = 0; m < 10; m++) {\n", " event->SetMeasure(m, Int_t(gRandom->Gaus(m, m + 1)));\n", " }\n", "\n", " // fill the matrix\n", " for (UChar_t i0 = 0; i0 < 4; i0++) {\n", " for(UChar_t i1 = 0; i1 < 4; i1++) {\n", " event->SetMatrix(i0, i1, gRandom->Gaus(i0 * i1, 1));\n", " }\n", " }\n", "\n", " // Create and fill the Track objects\n", " for (Int_t t = 0; t < ntrack; t++) event->AddTrack(random);\n", "\n", " // Fill the tree\n", " t4.Fill();\n", "\n", " // Clear the event before reloading it\n", " event->Clear();\n", " }\n", " // Write the file header\n", " f.Write();\n", "\n", " // Print the tree contents\n", " t4.Print();\n", "}" ] }, { "cell_type": "markdown", "id": "1364f30d", "metadata": {}, "source": [ " Definition of a helper function: " ] }, { "cell_type": "code", "execution_count": 3, "id": "0cf9fe5a", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:18:07.815555Z", "iopub.status.busy": "2026-05-19T20:18:07.815437Z", "iopub.status.idle": "2026-05-19T20:18:07.819557Z", "shell.execute_reply": "2026-05-19T20:18:07.819010Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_54:12:21: error: unknown type name 'Event'\n", " auto event = new Event();\n", " ^\n" ] } ], "source": [ "%%cpp -d\n", "void tree108_read()\n", "{\n", " // read the tree generated with tree108_write\n", "\n", " // note that we create the TFile and TTree objects on the heap !\n", " // because we want to keep these objects alive when we leave this function.\n", " auto f = TFile::Open(\"tree108.root\");\n", " auto t4 = f->Get(\"t4\");\n", "\n", " // create a pointer to an event object. This will be used\n", " // to read the branch values.\n", " auto event = new Event();\n", "\n", " // get two branches and set the branch address\n", " auto bntrack = t4->GetBranch(\"fNtrack\");\n", " auto branch = t4->GetBranch(\"event_split\");\n", " branch->SetAddress(&event);\n", "\n", " Long64_t nevent = t4->GetEntries();\n", " Int_t nselected = 0;\n", " Int_t nb = 0;\n", " for (Long64_t i=0; iGetEntry(i);\n", "\n", " // reject events with more than 587 tracks\n", " if (event->GetNtrack() > 587)\n", " continue;\n", "\n", " // read complete accepted event in memory\n", " nb += t4->GetEntry(i);\n", " nselected++;\n", "\n", " // print the first accepted event\n", " if (nselected == 1)\n", " t4->Show();\n", "\n", " // clear tracks array\n", " event->Clear();\n", " }\n", "\n", " if (gROOT->IsBatch())\n", " return;\n", " new TBrowser();\n", " t4->StartViewer();\n", "}" ] }, { "cell_type": "markdown", "id": "2ef68a0f", "metadata": {}, "source": [ " Definition of a helper function: " ] }, { "cell_type": "code", "execution_count": 4, "id": "749dc9e6", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:18:07.820730Z", "iopub.status.busy": "2026-05-19T20:18:07.820584Z", "iopub.status.idle": "2026-05-19T20:18:07.823619Z", "shell.execute_reply": "2026-05-19T20:18:07.823046Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_55:3:4: error: use of undeclared identifier 'Event'\n", " Event::Reset(); // Allow for re-run this script by cleaning static variables.\n", " ^\n", "input_line_55:4:4: error: use of undeclared identifier 'tree108_write'\n", " tree108_write();\n", " ^\n", "input_line_55:5:4: error: use of undeclared identifier 'Event'\n", " Event::Reset(); // Allow for re-run this script by cleaning static variables.\n", " ^\n", "input_line_55:6:4: error: use of undeclared identifier 'tree108_read'\n", " tree108_read();\n", " ^\n" ] } ], "source": [ "%%cpp -d\n", "void run()\n", "{\n", " Event::Reset(); // Allow for re-run this script by cleaning static variables.\n", " tree108_write();\n", " Event::Reset(); // Allow for re-run this script by cleaning static variables.\n", " tree108_read();\n", "}" ] }, { "cell_type": "code", "execution_count": 5, "id": "df4af56c", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:18:07.824718Z", "iopub.status.busy": "2026-05-19T20:18:07.824575Z", "iopub.status.idle": "2026-05-19T20:18:08.029124Z", "shell.execute_reply": "2026-05-19T20:18:08.028456Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Error in : macro /github/home/ROOT-CI/build/tutorials/io/tree/Event.cxx not found in path .:/github/home/ROOT-CI/build/macros\n", "input_line_58:1:10: fatal error: 'input_line_56' file not found\n", "#include \"input_line_56\"\n", " ^~~~~~~~~~~~~~~\n", "input_line_60:2:3: error: use of undeclared identifier 'run'\n", " (run())\n", " ^\n", "Error in : Error evaluating expression (run())\n", "Execution of your code was aborted.\n" ] } ], "source": [ "TString tutdir = gROOT->GetTutorialDir();\n", "gROOT->ProcessLine(\".L \" + tutdir + \"/io/tree/Event.cxx+\");\n", "gROOT->ProcessLine(\"#define ACTUAL_RUN yes\");\n", "gROOT->ProcessLine(\"#include \\\"\" __FILE__ \"\\\"\");\n", "gROOT->ProcessLine(\"run()\");" ] } ], "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 }