{
"cells": [
{
"cell_type": "markdown",
"id": "b8e0f8ee",
"metadata": {},
"source": [
"# tree112_copy\n",
"Example of Root macro to copy a subset of a Tree to a new Tree, selecting entries.\n",
"\n",
"Only selected entries are copied to the new Tree.\n",
"The input file has been generated by the program in `$ROOTSYS/test/Event`\n",
"with `Event 1000 1 99 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": 1,
"id": "81d8c7b2",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:18:32.288294Z",
"iopub.status.busy": "2026-05-19T20:18:32.288183Z",
"iopub.status.idle": "2026-05-19T20:18:32.605908Z",
"shell.execute_reply": "2026-05-19T20:18:32.605376Z"
}
},
"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": "c71e76b2",
"metadata": {},
"source": [
" Definition of a helper function: "
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "3eda1295",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:18:32.607512Z",
"iopub.status.busy": "2026-05-19T20:18:32.607392Z",
"iopub.status.idle": "2026-05-19T20:18:32.616933Z",
"shell.execute_reply": "2026-05-19T20:18:32.616463Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_53:9:4: error: unknown type name 'Event'\n",
" Event *event = nullptr;\n",
" ^\n"
]
}
],
"source": [
"%%cpp -d\n",
"void run()\n",
"{\n",
" TFile oldfile(\"tree108.root\");\n",
" TTree *oldtree;\n",
" oldfile.GetObject(\"t4\", oldtree);\n",
"\n",
" const auto nentries = oldtree->GetEntries();\n",
"\n",
" Event *event = nullptr;\n",
" oldtree->SetBranchAddress(\"event_split\", &event);\n",
"\n",
" // Create a new file + a clone of old tree in new file\n",
" TFile newfile(\"tree112.root\", \"recreate\");\n",
" auto newtree = oldtree->CloneTree(0);\n",
"\n",
" for (auto i : ROOT::TSeqI(nentries)) {\n",
" oldtree->GetEntry(i);\n",
" if (event->GetNtrack() > 605)\n",
" newtree->Fill();\n",
" event->Clear();\n",
" }\n",
"\n",
" newtree->Print();\n",
" newfile.Write();\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "94816371",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:18:32.618231Z",
"iopub.status.busy": "2026-05-19T20:18:32.618118Z",
"iopub.status.idle": "2026-05-19T20:18:32.822468Z",
"shell.execute_reply": "2026-05-19T20:18:32.821923Z"
}
},
"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_56:1:10: fatal error: 'input_line_54' file not found\n",
"#include \"input_line_54\"\n",
" ^~~~~~~~~~~~~~~\n",
"input_line_58: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
}