Copying a branch from existing tree to a new one

Hi all,

I am trying to copy an existing branch from a tree to a new tree. Old tree has a lot of branches which I don’t need, so I wanted to make a tree with only the branch I want to use. I am trying to do this following the code written here:
root.cern.ch/root/html/tutorial … ee2.C.html
I am not running it as a macro, but typing it directly in the interpreter. The problem is, when I come to the line 24, root just crashes and won’t respond.
Also to add, I am not sure what the Event on line 13 is, so I am just ignoring that line and the next one.

Does anyone have some other suggestions for how to copy a branch to a new tree (or even better convert that branch to a new tree)?

Thanks,
Andrea

Hi Andrea,

this is a simplified and commented version of the macro:

void skim() {

   //Get old file, old tree and set top branch address
   auto oldfile = TFile::Open("./Event.root");
   TTree* oldtree;
   oldfile->GetObject("T", oldtree);
   // Deactivate all branches
   oldtree->SetBranchStatus(nameStatus,0);
   // Activate 4 branches only: our skim
   oldtree->SetBranchStatus("event",1);
   oldtree->SetBranchStatus("fNtrack",1);
   oldtree->SetBranchStatus("fNseg",1);
   oldtree->SetBranchStatus("fH",1);

   //Create a new file + a clone of old tree header. Do not copy events
   auto newfile = TFile::Open("small.root","recreate");
   auto newtree = oldtree->CloneTree(0);

   // Here we copy the branches
   newtree->CopyEntries(oldtree);

   // Flush to disk
   newfile->Write();

   // Clean
   delete oldfile;
   delete newfile;
}

The input file was created with the eventexe test in $ROOTSYS/test with the command “eventexe 1000 1 1 1”.
As you can see the name of the game is to take a tree, deactivate all branches, selectively turn on some of them and then copy the “skimmed” tree to a new file.

Cheers,
Danilo

Thanks for your reply.

My problem still persists. I attached here the error message I always get when I use CloneTree, both on TChain or on TTree. I also attached an example file I am working with. From the file, I need the branch StrSimHits.

My file is too large, so is the link to it. dropbox.com/s/4utxr1ml896iw … .root?dl=0

===========================================================
There was a crash.
This is the entire stack trace of all threads:
===========================================================
#0  0x00007f39872cb06e in __libc_waitpid (pid=<optimized out>, stat_loc=0x7fff04d017ac, options=<optimized out>) at ../sysdeps/unix/sysv/linux/waitpid.c:32
#1  0x00007f398725f989 in do_system (line=<optimized out>) at ../sysdeps/posix/system.c:149
#2  0x00007f39881885fc in TUnixSystem::StackTrace() () from /usr/local/lib/root/libCore.so.5.34
#3  0x00007f398818ae23 in TUnixSystem::DispatchSignals(ESignals) () from /usr/local/lib/root/libCore.so.5.34
#4  <signal handler called>
#5  0x00007f3988142380 in TObjArray::GetAbsLast() const () from /usr/local/lib/root/libCore.so.5.34
#6  0x00007f3983eb904d in TTree::CloneTree(long long, char const*) () from /usr/local/lib/root/libTree.so
#7  0x00007f3983ee2f3d in G__G__Tree_126_0_41(G__value*, char const*, G__param*, int) () from /usr/local/lib/root/libTree.so
#8  0x00007f39866b65c7 in Cint::G__ExceptionWrapper(int (*)(G__value*, char const*, G__param*, int), G__value*, char*, G__param*, int) () from /usr/local/lib/root/libCint.so.5.34
#9  0x00007f398675aa31 in G__execute_call () from /usr/local/lib/root/libCint.so.5.34
#10 0x00007f398675adfe in G__call_cppfunc () from /usr/local/lib/root/libCint.so.5.34
#11 0x00007f398673d619 in G__interpret_func () from /usr/local/lib/root/libCint.so.5.34
#12 0x00007f3986727214 in G__getfunction () from /usr/local/lib/root/libCint.so.5.34
#13 0x00007f398680bf3c in G__getstructmem(int, G__FastAllocString&, char*, int, char*, int*, G__var_array*, int) () from /usr/local/lib/root/libCint.so.5.34
#14 0x00007f3986804dbb in G__getvariable () from /usr/local/lib/root/libCint.so.5.34
#15 0x00007f3986700f5d in G__getitem () from /usr/local/lib/root/libCint.so.5.34
#16 0x00007f3986707c3a in G__getexpr () from /usr/local/lib/root/libCint.so.5.34
#17 0x00007f39866f1dd7 in G__define_var () from /usr/local/lib/root/libCint.so.5.34
#18 0x00007f3986786e73 in G__exec_statement () from /usr/local/lib/root/libCint.so.5.34
#19 0x00007f398673b76d in G__interpret_func () from /usr/local/lib/root/libCint.so.5.34
#20 0x00007f3986727279 in G__getfunction () from /usr/local/lib/root/libCint.so.5.34
#21 0x00007f39867016c2 in G__getitem () from /usr/local/lib/root/libCint.so.5.34
#22 0x00007f3986707c3a in G__getexpr () from /usr/local/lib/root/libCint.so.5.34
#23 0x00007f398678af2e in G__exec_statement () from /usr/local/lib/root/libCint.so.5.34
#24 0x00007f39866ed73b in G__exec_tempfile_core () from /usr/local/lib/root/libCint.so.5.34
#25 0x00007f39866eec9e in G__exec_tempfile_fp () from /usr/local/lib/root/libCint.so.5.34
#26 0x00007f3986796fed in G__process_cmd () from /usr/local/lib/root/libCint.so.5.34
#27 0x00007f39881537e1 in TCint::ProcessLine(char const*, TInterpreter::EErrorCode*) () from /usr/local/lib/root/libCore.so.5.34
#28 0x00007f39880b9292 in TApplication::ProcessLine(char const*, bool, int*) () from /usr/local/lib/root/libCore.so.5.34
#29 0x00007f3987cf6f69 in TRint::HandleTermInput() () from /usr/local/lib/root/libRint.so.5.34
#30 0x00007f3988189dbd in TUnixSystem::CheckDescriptors() () from /usr/local/lib/root/libCore.so.5.34
#31 0x00007f398818b55a in TUnixSystem::DispatchOneEvent(bool) () from /usr/local/lib/root/libCore.so.5.34
#32 0x00007f398810fc96 in TSystem::InnerLoop() () from /usr/local/lib/root/libCore.so.5.34
#33 0x00007f3988111884 in TSystem::Run() () from /usr/local/lib/root/libCore.so.5.34
#34 0x00007f39880b75df in TApplication::Run(bool) () from /usr/local/lib/root/libCore.so.5.34
#35 0x00007f3987cf7c57 in TRint::Run(bool) () from /usr/local/lib/root/libRint.so.5.34
#36 0x00000000004010ec in main ()
===========================================================


The lines below might hint at the cause of the crash.
If they do not help you then please submit a bug report at
http://root.cern.ch/bugs. Please post the ENTIRE stack trace
from above as an attachment in addition to anything else
that might help us fixing this issue.
===========================================================
#5  0x00007f3988142380 in TObjArray::GetAbsLast() const () from /usr/local/lib/root/libCore.so.5.34
#6  0x00007f3983eb904d in TTree::CloneTree(long long, char const*) () from /usr/local/lib/root/libTree.so
===========================================================

mySkim.C (759 Bytes)

Hi,

There was an error in array size calculation in CloneTree which I fixed in the v5.34, v6.06 and master branches.

However note that in your code there is chain->SetBranchStatus("StrSimHits",1);which only select the top level branch. You most likely meant: chain->SetBranchStatus("StrSimHits*",1);which happens to also seemingly work around the problem.

Cheers,
Philippe.