{ "cells": [ { "cell_type": "markdown", "id": "589050e6", "metadata": {}, "source": [ "# copyFiles\n", "Example of script showing how to copy all objects (including directories)\n", "from a source file.\n", "For each input file, a new directory is created in the current directory\n", "with the name of the source file.\n", "After the execution of:\n", "```bash\n", "root [0] .x copyFiles.C\n", "```\n", "the file result.root will contain 4 subdirectories:\n", "\"tot100.root\", \"hsimple.root\", \"hs1.root\",\"hs2.root\"\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:13 PM." ] }, { "cell_type": "markdown", "id": "5f5a25bc", "metadata": {}, "source": [ " Definition of a helper function: " ] }, { "cell_type": "code", "execution_count": 1, "id": "f3c1724f", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:13:30.336557Z", "iopub.status.busy": "2026-05-19T20:13:30.336446Z", "iopub.status.idle": "2026-05-19T20:13:30.350995Z", "shell.execute_reply": "2026-05-19T20:13:30.350477Z" } }, "outputs": [], "source": [ "%%cpp -d\n", "\n", "#include \"TROOT.h\"\n", "#include \"TKey.h\"\n", "#include \"TFile.h\"\n", "#include \"TSystem.h\"\n", "#include \"TTree.h\"\n", "\n", "void CopyDir(TDirectory *source) {\n", " //copy all objects and subdirs of directory source as a subdir of the current directory\n", " source->ls();\n", " TDirectory *savdir = gDirectory;\n", " TDirectory *adir = savdir->mkdir(source->GetName());\n", " adir->cd();\n", " //loop on all entries of this directory\n", " TKey *key;\n", " //Loop in reverse order to make sure that the order of cycles is\n", " //preserved.\n", " TIter nextkey(source->GetListOfKeys(),kIterBackward);\n", " while ((key = (TKey*)nextkey())) {\n", " const char *classname = key->GetClassName();\n", " TClass *cl = gROOT->GetClass(classname);\n", " if (!cl) continue;\n", " if (cl->InheritsFrom(TDirectory::Class())) {\n", " source->cd(key->GetName());\n", " TDirectory *subdir = gDirectory;\n", " adir->cd();\n", " CopyDir(subdir);\n", " adir->cd();\n", " } else if (cl->InheritsFrom(TTree::Class())) {\n", " TTree *T = (TTree*)source->Get(key->GetName());\n", " // Avoid writing the data of a TTree more than once.\n", " // Note this assume that older cycles are (as expected) older\n", " // snapshots of the TTree meta data.\n", " if (!adir->FindObject(key->GetName())) {\n", " adir->cd();\n", " TTree *newT = T->CloneTree(-1,\"fast\");\n", " newT->Write();\n", " }\n", " } else {\n", " source->cd();\n", " TObject *obj = key->ReadObj();\n", " adir->cd();\n", " obj->Write();\n", " delete obj;\n", " }\n", " }\n", " adir->SaveSelf(kTRUE);\n", " savdir->cd();\n", "}" ] }, { "cell_type": "markdown", "id": "1cfc29f3", "metadata": {}, "source": [ " Definition of a helper function: " ] }, { "cell_type": "code", "execution_count": 2, "id": "d5aad765", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:13:30.352666Z", "iopub.status.busy": "2026-05-19T20:13:30.352537Z", "iopub.status.idle": "2026-05-19T20:13:30.358458Z", "shell.execute_reply": "2026-05-19T20:13:30.357980Z" } }, "outputs": [], "source": [ "%%cpp -d\n", "void CopyFile(const char *fname) {\n", " //Copy all objects and subdirs of file fname as a subdir of the current directory\n", " TDirectory *target = gDirectory;\n", " TFile *f = TFile::Open(fname);\n", " if (!f || f->IsZombie()) {\n", " printf(\"Cannot copy file: %s\\n\",fname);\n", " target->cd();\n", " return;\n", " }\n", " target->cd();\n", " CopyDir(f);\n", " delete f;\n", " target->cd();\n", "}" ] }, { "cell_type": "markdown", "id": "7944e29e", "metadata": {}, "source": [ "prepare files to be copied" ] }, { "cell_type": "code", "execution_count": 3, "id": "def0de99", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:13:30.360084Z", "iopub.status.busy": "2026-05-19T20:13:30.359969Z", "iopub.status.idle": "2026-05-19T20:13:30.682107Z", "shell.execute_reply": "2026-05-19T20:13:30.681644Z" } }, "outputs": [], "source": [ "if(gSystem->AccessPathName(\"tot100.root\")) {\n", " gSystem->CopyFile(\"hsimple.root\", \"tot100.root\");\n", " gSystem->CopyFile(\"hsimple.root\", \"hs1.root\");\n", " gSystem->CopyFile(\"hsimple.root\", \"hs2.root\");\n", "}" ] }, { "cell_type": "markdown", "id": "4aefb3a5", "metadata": {}, "source": [ "main function copying 4 files as subdirectories of a new file" ] }, { "cell_type": "code", "execution_count": 4, "id": "d87c45ce", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:13:30.693520Z", "iopub.status.busy": "2026-05-19T20:13:30.693385Z", "iopub.status.idle": "2026-05-19T20:13:30.899778Z", "shell.execute_reply": "2026-05-19T20:13:30.899140Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "TFile**\t\ttot100.root\tDemo ROOT file with histograms\n", " TFile*\t\ttot100.root\tDemo ROOT file with histograms\n", " KEY: TH1F\thpx;1\tThis is the px distribution\n", " KEY: TH2F\thpxpy;1\tpy vs px\n", " KEY: TProfile\thprof;1\tProfile of pz versus px\n", " KEY: TNtuple\tntuple;1\tDemo ntuple\n", "TFile**\t\thsimple.root\tDemo ROOT file with histograms\n", " TFile*\t\thsimple.root\tDemo ROOT file with histograms\n", " KEY: TH1F\thpx;1\tThis is the px distribution\n", " KEY: TH2F\thpxpy;1\tpy vs px\n", " KEY: TProfile\thprof;1\tProfile of pz versus px\n", " KEY: TNtuple\tntuple;1\tDemo ntuple\n", "TFile**\t\ths1.root\tDemo ROOT file with histograms\n", " TFile*\t\ths1.root\tDemo ROOT file with histograms\n", " KEY: TH1F\thpx;1\tThis is the px distribution\n", " KEY: TH2F\thpxpy;1\tpy vs px\n", " KEY: TProfile\thprof;1\tProfile of pz versus px\n", " KEY: TNtuple\tntuple;1\tDemo ntuple\n", "TFile**\t\ths2.root\tDemo ROOT file with histograms\n", " TFile*\t\ths2.root\tDemo ROOT file with histograms\n", " KEY: TH1F\thpx;1\tThis is the px distribution\n", " KEY: TH2F\thpxpy;1\tpy vs px\n", " KEY: TProfile\thprof;1\tProfile of pz versus px\n", " KEY: TNtuple\tntuple;1\tDemo ntuple\n", "TFile**\t\tresult.root\t\n", " TFile*\t\tresult.root\t\n", " TDirectoryFile*\t\ttot100.root\ttot100.root\n", " OBJ: TNtuple\tntuple\tDemo ntuple : 0 at: 0x7f1714fdc910\n", " KEY: TNtuple\tntuple;1\tDemo ntuple\n", " KEY: TProfile\thprof;1\tProfile of pz versus px\n", " KEY: TH2F\thpxpy;1\tpy vs px\n", " KEY: TH1F\thpx;1\tThis is the px distribution\n", " TDirectoryFile*\t\thsimple.root\thsimple.root\n", " OBJ: TNtuple\tntuple\tDemo ntuple : 0 at: 0x7f1714057450\n", " KEY: TNtuple\tntuple;1\tDemo ntuple\n", " KEY: TProfile\thprof;1\tProfile of pz versus px\n", " KEY: TH2F\thpxpy;1\tpy vs px\n", " KEY: TH1F\thpx;1\tThis is the px distribution\n", " TDirectoryFile*\t\ths1.root\ths1.root\n", " OBJ: TNtuple\tntuple\tDemo ntuple : 0 at: 0x7f1714037bc0\n", " KEY: TNtuple\tntuple;1\tDemo ntuple\n", " KEY: TProfile\thprof;1\tProfile of pz versus px\n", " KEY: TH2F\thpxpy;1\tpy vs px\n", " KEY: TH1F\thpx;1\tThis is the px distribution\n", " TDirectoryFile*\t\ths2.root\ths2.root\n", " OBJ: TNtuple\tntuple\tDemo ntuple : 0 at: 0x7f1714101890\n", " KEY: TNtuple\tntuple;1\tDemo ntuple\n", " KEY: TProfile\thprof;1\tProfile of pz versus px\n", " KEY: TH2F\thpxpy;1\tpy vs px\n", " KEY: TH1F\thpx;1\tThis is the px distribution\n", " KEY: TDirectoryFile\ttot100.root;1\ttot100.root\n", " KEY: TDirectoryFile\thsimple.root;1\thsimple.root\n", " KEY: TDirectoryFile\ths1.root;1\ths1.root\n", " KEY: TDirectoryFile\ths2.root;1\ths2.root\n" ] } ], "source": [ "TFile *f = new TFile(\"result.root\",\"recreate\");\n", "CopyFile(\"tot100.root\");\n", "CopyFile(\"hsimple.root\");\n", "CopyFile(\"hs1.root\");\n", "CopyFile(\"hs2.root\");\n", "f->ls();\n", "delete f;" ] } ], "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 }