Copytree 3


Example of Root macro to copy a subset of a Tree to a new Tree, selecting entries.

Only selected entries are copied to the new Tree. The input file has been generated by the program in $ROOTSYS/test/Event with Event 1000 1 99 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:/usr/local/lib/:.:/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");
Long64_t nentries = oldtree->GetEntries();
Event *event   = 0;
oldtree->SetBranchAddress("event",&event);


Error in <TFile::TFile>: file ./Event.root does not exist
 Generating stack trace...
 0x00007f4c36e26c5f in cling::Interpreter::executeTransaction(cling::Transaction&) + 0x6f from /cvmfs/sft.cern.ch/lcg/views/LCG_87/x86_64-slc6-gcc49-opt/lib/libCling.so
 0x00007f4c36e8876c in cling::IncrementalParser::commitTransaction(llvm::PointerIntPair<cling::Transaction*, 2u, cling::IncrementalParser::EParseResult, llvm::PointerLikeTypeTraits<cling::Transaction*>, llvm::PointerIntPairInfo<cling::Transaction*, 2u, llvm::PointerLikeTypeTraits<cling::Transaction*> > >&) + 0x4ac from /cvmfs/sft.cern.ch/lcg/views/LCG_87/x86_64-slc6-gcc49-opt/lib/libCling.so
 0x00007f4c36e8bcd6 in cling::IncrementalParser::Compile(llvm::StringRef, cling::CompilationOptions const&) + 0x56 from /cvmfs/sft.cern.ch/lcg/views/LCG_87/x86_64-slc6-gcc49-opt/lib/libCling.so
 0x00007f4c36e25b61 in cling::Interpreter::EvaluateInternal(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cling::CompilationOptions, cling::Value*, cling::Transaction**, unsigned long) + 0x71 from /cvmfs/sft.cern.ch/lcg/views/LCG_87/x86_64-slc6-gcc49-opt/lib/libCling.so
 0x00007f4c36e25e7d in cling::Interpreter::process(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cling::Value*, cling::Transaction**) + 0x12d from /cvmfs/sft.cern.ch/lcg/views/LCG_87/x86_64-slc6-gcc49-opt/lib/libCling.so
 0x00007f4c36ec971b in cling::MetaProcessor::process(char const*, cling::Interpreter::CompilationResult&, cling::Value*) + 0x22b from /cvmfs/sft.cern.ch/lcg/views/LCG_87/x86_64-slc6-gcc49-opt/lib/libCling.so
 0x00007f4c36d8b556 in <unknown> from /cvmfs/sft.cern.ch/lcg/views/LCG_87/x86_64-slc6-gcc49-opt/lib/libCling.so
 0x00007f4c36d9b6df in TCling::ProcessLine(char const*, TInterpreter::EErrorCode*) + 0xd3f from /cvmfs/sft.cern.ch/lcg/views/LCG_87/x86_64-slc6-gcc49-opt/lib/libCling.so
 0x00007f4c3e8e9929 in JupyROOTExecutorImpl(char const*) + 0x29 from /cvmfs/sft.cern.ch/lcg/views/LCG_87/x86_64-slc6-gcc49-opt/lib/libJupyROOT.so
 0x00007f4c3e8e99c9 in JupyROOTExecutor + 0x9 from /cvmfs/sft.cern.ch/lcg/views/LCG_87/x86_64-slc6-gcc49-opt/lib/libJupyROOT.so
 0x00007f4c3f7f4208 in ffi_call_unix64 at /var/build/jenkins/workspace/lcg_release/BUILDTYPE/Release/COMPILER/gcc49/LABEL/slc6-physical/build/externals/Python-2.7.10/src/Python/2.7.10/Modules/_ctypes/libffi/src/x86/unix64.S:79 from /cvmfs/sft.cern.ch/lcg/releases/Python/2.7.10-806f9/x86_64-slc6-gcc49-opt/lib/python2.7/lib-dynload/_ctypes.so
 0x00007f4c3f7f330f in ffi_call + 0x15f from /cvmfs/sft.cern.ch/lcg/releases/Python/2.7.10-806f9/x86_64-slc6-gcc49-opt/lib/python2.7/lib-dynload/_ctypes.so
 0x00007f4c3f7eae42 in _ctypes_callproc + 0x292 from /cvmfs/sft.cern.ch/lcg/releases/Python/2.7.10-806f9/x86_64-slc6-gcc49-opt/lib/python2.7/lib-dynload/_ctypes.so
 0x00007f4c3f7e1ce4 in <unknown> from /cvmfs/sft.cern.ch/lcg/releases/Python/2.7.10-806f9/x86_64-slc6-gcc49-opt/lib/python2.7/lib-dynload/_ctypes.so
 0x00007f4c49619b33 in PyObject_Call + 0x43 from /cvmfs/sft.cern.ch/lcg/releases/Python/2.7.10-806f9/x86_64-slc6-gcc49-opt/bin/../lib/libpython2.7.so.1.0
 0x00007f4c496ce1de in PyEval_EvalFrameEx at /var/build/jenkins/workspace/lcg_release/BUILDTYPE/Release/COMPILER/gcc49/LABEL/slc6-physical/build/externals/Python-2.7.10/src/Python/2.7.10/Python/ceval.c:4253 from /cvmfs/sft.cern.ch/lcg/releases/Python/2.7.10-806f9/x86_64-slc6-gcc49-opt/bin/../lib/libpython2.7.so.1.0
 0x00007f4c496d14f0 in PyEval_EvalCodeEx at /var/build/jenkins/workspace/lcg_release/BUILDTYPE/Release/COMPILER/gcc49/LABEL/slc6-physical/build/externals/Python-2.7.10/src/Python/2.7.10/Python/ceval.c:3267 from /cvmfs/sft.cern.ch/lcg/releases/Python/2.7.10-806f9/x86_64-slc6-gcc49-opt/bin/../lib/libpython2.7.so.1.0
 0x00007f4c4964b4a5 in <unknown> from /cvmfs/sft.cern.ch/lcg/releases/Python/2.7.10-806f9/x86_64-slc6-gcc49-opt/bin/../lib/libpython2.7.so.1.0
 0x00007f4c49619b33 in PyObject_Call + 0x43 from /cvmfs/sft.cern.ch/lcg/releases/Python/2.7.10-806f9/x86_64-slc6-gcc49-opt/bin/../lib/libpython2.7.so.1.0
 0x00007f4c496cb873 in PyEval_EvalFrameEx at /var/build/jenkins/workspace/lcg_release/BUILDTYPE/Release/COMPILER/gcc49/LABEL/slc6-physical/build/externals/Python-2.7.10/src/Python/2.7.10/Python/ceval.c:4348 from /cvmfs/sft.cern.ch/lcg/releases/Python/2.7.10-806f9/x86_64-slc6-gcc49-opt/bin/../lib/libpython2.7.so.1.0
 0x00007f4c496d0109 in PyEval_EvalFrameEx at /var/build/jenkins/workspace/lcg_release/BUILDTYPE/Release/COMPILER/gcc49/LABEL/slc6-physical/build/externals/Python-2.7.10/src/Python/2.7.10/Python/ceval.c:4122 from /cvmfs/sft.cern.ch/lcg/releases/Python/2.7.10-806f9/x86_64-slc6-gcc49-opt/bin/../lib/libpython2.7.so.1.0
 0x00007f4c496d0109 in PyEval_EvalFrameEx at /var/build/jenkins/workspace/lcg_release/BUILDTYPE/Release/COMPILER/gcc49/LABEL/slc6-physical/build/externals/Python-2.7.10/src/Python/2.7.10/Python/ceval.c:4122 from /cvmfs/sft.cern.ch/lcg/releases/Python/2.7.10-806f9/x86_64-slc6-gcc49-opt/bin/../lib/libpython2.7.so.1.0
 0x00007f4c496d14f0 in PyEval_EvalCodeEx at /var/build/jenkins/workspace/lcg_release/BUILDTYPE/Release/COMPILER/gcc49/LABEL/slc6-physical/build/externals/Python-2.7.10/src/Python/2.7.10/Python/ceval.c:3267 from /cvmfs/sft.cern.ch/lcg/releases/Python/2.7.10-806f9/x86_64-slc6-gcc49-opt/bin/../lib/libpython2.7.so.1.0
 0x00007f4c4964b3dc in <unknown> from /cvmfs/sft.cern.ch/lcg/releases/Python/2.7.10-806f9/x86_64-slc6-gcc49-opt/bin/../lib/libpython2.7.so.1.0
 0x00007f4c49619b33 in PyObject_Call + 0x43 from /cvmfs/sft.cern.ch/lcg/releases/Python/2.7.10-806f9/x86_64-slc6-gcc49-opt/bin/../lib/libpython2.7.so.1.0
 0x00007f4c4962853d in <unknown> from /cvmfs/sft.cern.ch/lcg/releases/Python/2.7.10-806f9/x86_64-slc6-gcc49-opt/bin/../lib/libpython2.7.so.1.0
 0x00007f4c49619b33 in PyObject_Call + 0x43 from /cvmfs/sft.cern.ch/lcg/releases/Python/2.7.10-806f9/x86_64-slc6-gcc49-opt/bin/../lib/libpython2.7.so.1.0
 0x00007f4c496ca0b7 in PyEval_CallObjectWithKeywords at /var/build/jenkins/workspace/lcg_release/BUILDTYPE/Release/COMPILER/gcc49/LABEL/slc6-physical/build/externals/Python-2.7.10/src/Python/2.7.10/Python/ceval.c:3905 from /cvmfs/sft.cern.ch/lcg/releases/Python/2.7.10-806f9/x86_64-slc6-gcc49-opt/bin/../lib/libpython2.7.so.1.0
 0x00007f4c4970e602 in <unknown> from /cvmfs/sft.cern.ch/lcg/releases/Python/2.7.10-806f9/x86_64-slc6-gcc49-opt/bin/../lib/libpython2.7.so.1.0
 0x00007f4c493b1aa1 in <unknown> from /lib64/libpthread.so.0
 0x00007f4c48a73aad in clone + 0x6d from /lib64/libc.so.6
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_29:9:21: warning: null passed to a callee that requires a non-null argument [-Wnonnull]
Long64_t nentries = oldtree->GetEntries();
                    ^~~~~~~

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(0);

for (Long64_t i=0;i<nentries; i++) {
   oldtree->GetEntry(i);
   if (event->GetNtrack() > 605) newtree->Fill();
   event->Clear();
}
newtree->Print();
newtree->AutoSave();
delete oldfile;
delete newfile;


input_line_32:7:8: error: use of undeclared identifier 'event'
   if (event->GetNtrack() > 605) newtree->Fill();
       ^
input_line_32:8:4: error: use of undeclared identifier 'event'
   event->Clear();
   ^