Copytree


Copy a subset of a Tree to a new Tree

The input file has been generated by the program in $ROOTSYS/test/Event with Event 1000 1 1 1.

Author: Rene Brun
This notebook tutorial was automatically generated with ROOTBOOK-izer (Beta) from the macro found in the ROOT repository on Tuesday, January 17, 2017 at 02:42 PM.


In [1]:
.! $ROOTSYS/test/eventexe 1000 1 1 1



sh: /cvmfs/sft.cern.ch/lcg/releases/ROOT/6.08.02-99084/x86_64-slc6-gcc49-opt/test/eventexe: No such file or directory

In [2]:
gSystem->Load("$ROOTSYS/test/libEvent");


Error in <TUnixSystem::FindDynamicLibrary>: $ROOTSYS/test/libEvent[.so | .dll | .dylib | .sl | .dl | .a] does not exist in /cvmfs/sft.cern.ch/lcg/views/LCG_87/x86_64-slc6-gcc49-opt/lib64:/cvmfs/sft.cern.ch/lcg/views/LCG_87/x86_64-slc6-gcc49-opt/lib:/cvmfs/sft.cern.ch/lcg/contrib/gcc/4.9/x86_64-slc6/lib64:.:/cvmfs/sft.cern.ch/lcg/releases/ROOT/6.08.02-99084/x86_64-slc6-gcc49-opt/lib:/lib64/tls/x86_64:/lib64/tls:/lib64/x86_64:/lib64:/usr/lib64/tls/x86_64:/usr/lib64/tls:/usr/lib64/x86_64:/usr/lib64

Get old file, old tree and set top branch address


In [3]:
TFile *oldfile;
TString dir = "$ROOTSYS/test/Event.root";
gSystem->ExpandPathName(dir);
if (!gSystem->AccessPathName(dir))
    {oldfile = new TFile("$ROOTSYS/test/Event.root");}
else {oldfile = new TFile("./Event.root");}
TTree *oldtree = (TTree*)oldfile->Get("T");
Event *event   = new Event();
oldtree->SetBranchAddress("event",&event);
oldtree->SetBranchStatus("*",0);
oldtree->SetBranchStatus("event",1);
oldtree->SetBranchStatus("fNtrack",1);
oldtree->SetBranchStatus("fNseg",1);
oldtree->SetBranchStatus("fH",1);


input_line_29:9:22: error: unknown type name 'Event'
Event *event   = new Event();
                     ^
input_line_29:10:36: error: use of undeclared identifier 'event'
oldtree->SetBranchAddress("event",&event);
                                   ^

Create a new file + a clone of old tree in new file


In [4]:
TFile *newfile = new TFile("small.root","recreate");
TTree *newtree = oldtree->CloneTree();

newtree->Print();
newfile->Write();
delete oldfile;
delete newfile;


input_line_31:2:3: error: use of undeclared identifier 'oldtree'
 (oldtree->CloneTree())
  ^
    __boot()
    import os
Error in <HandleInterpreterException>: Trying to dereference null pointer or trying to call routine taking non-null arguments.
Execution of your code was aborted.
input_line_30:5:1: warning: null passed to a callee that requires a non-null argument [-Wnonnull]
newtree->Print();
^~~~~~~