{ "cells": [ { "cell_type": "markdown", "id": "95777da1", "metadata": {}, "source": [ "# tree111_copy\n", "Copy a subset of a Tree to a new Tree, one branch in a separate file.\n", "\n", "One branch of the new Tree is written to a separate file\n", "The input file has been generated by the program in `$ROOTSYS/test/Event`\n", "with the command `Event 1000 1 1 1`\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": null, "id": "19fc3d2e", "metadata": { "collapsed": false }, "outputs": [], "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": "e6a29400", "metadata": {}, "source": [ " Definition of a helper function: " ] }, { "cell_type": "code", "execution_count": null, "id": "636cd117", "metadata": { "collapsed": false }, "outputs": [], "source": [ "%%cpp -d\n", "void run()\n", "{\n", " TFile oldfile(\"tree108.root\");\n", " TTree *oldtree;\n", " oldfile.GetObject(\"t4\", oldtree);\n", "\n", " // Activate only four of them\n", " for (auto activeBranchName : {\"event_split\", \"fNtrack\", \"fNseg\", \"fH\"}) {\n", " oldtree->SetBranchStatus(activeBranchName, 1);\n", " }\n", "\n", " // Create a new file + a clone of old tree header. Do not copy events\n", " TFile newfile(\"tree111.root\", \"recreate\");\n", " auto newtree = oldtree->CloneTree(0);\n", "\n", " // Divert branch fH to a separate file and copy all events\n", " newtree->GetBranch(\"fH\")->SetFile(\"small_fH.root\");\n", " newtree->CopyEntries(oldtree);\n", "\n", " newtree->Print();\n", " newfile.Write();\n", "}" ] }, { "cell_type": "code", "execution_count": null, "id": "86ce9f2d", "metadata": { "collapsed": false }, "outputs": [], "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 }