Easy way to delete a branch in one root file?

Hi,
I have a root file with trees inside. I want to delete one branch in one tree. I understand there is one way to do this is to disable this branch and copy the tree over to a new file. However, sometimes I have many trees and only need to delete one branch from one tree, so that in the meantime I have to clone other trees one by one. I am wondering if any direct way available to remove a branch a tree? Say, get the pointer of the tree and delete one branch directly and then update the file.

Thanks,
Zhiyi.

1 Like

The following code will work for a top level branch

   TFile f("myfile.root","update");
   TTree *T = (TTree*)f.Get("treename");
   TBranch *b = T->GetBranch("name of branch to delete");
   T->GetListOfBranches()->Remove(b);
   T->Write();

Rene

Great!

It works. Thanks a lot.

[quote=“brun”]The following code will work for a top level branch

TFile f("myfile.root","update"); TTree *T = (TTree*)f.Get("treename"); TBranch *b = T->GetBranch("name of branch to delete"); T->GetListOfBranches()->Remove(b); T->Write();
Rene[/quote]

Hi, the code snippet does not work for me:

root [0] TFile f("/afs/e4.physik.uni-dortmund.de/group/atlas/data/top","update");
topXsAt10pb/
toprec/
topXsAt10pb_Aug2010/
root [0] TFile f("/afs/e4.physik.uni-dortmund.de/group/atlas/data/topXsAt10pb_Aug2010/105010_merge.root","update"); 
root [1] TTree *T = (TTree*)f.Get("CollectionTree");
root [2] TBranch* b=T->GetBranch("StreamESD_ref")
root [3] T->GetListOfBranches()->Remove(b)
(class TObject*)0x103c420
root [4] T->Write();

 *** Break *** segmentation violation



===========================================================
There was a crash.
This is the entire stack trace of all threads:
===========================================================
#0  0x00007f24bf98a505 in waitpid () from /lib/libc.so.6
#1  0x00007f24bf92b461 in ?? () from /lib/libc.so.6
#2  0x00007f24c1a83cad in TUnixSystem::StackTrace () from /afs/e4.physik.uni-dortmund.de/sw/root-system/linux-ubuntu-8.04-x86_64/5.26.00a/lib/libCore.so.5.26
#3  0x00007f24c1a80fe8 in TUnixSystem::DispatchSignals () from /afs/e4.physik.uni-dortmund.de/sw/root-system/linux-ubuntu-8.04-x86_64/5.26.00a/lib/libCore.so.5.26
#4  <signal handler called>
#5  0x00007f24c1a333d0 in TObjArray::GetAbsLast () from /afs/e4.physik.uni-dortmund.de/sw/root-system/linux-ubuntu-8.04-x86_64/5.26.00a/lib/libCore.so.5.26
#6  0x00007f24bbd09883 in TBranch::GetSubBranch () from /afs/e4.physik.uni-dortmund.de/sw/root-system/linux-ubuntu-8.04-x86_64/5.26.00a/lib/libTree.so
#7  0x00007f24bbd09986 in TBranch::GetMother () from /afs/e4.physik.uni-dortmund.de/sw/root-system/linux-ubuntu-8.04-x86_64/5.26.00a/lib/libTree.so
#8  0x00007f24bbd108c8 in TBranchElement::Streamer () from /afs/e4.physik.uni-dortmund.de/sw/root-system/linux-ubuntu-8.04-x86_64/5.26.00a/lib/libTree.so
#9  0x00007f24bc6761ce in TBufferFile::WriteObjectClass () from /afs/e4.physik.uni-dortmund.de/sw/root-system/linux-ubuntu-8.04-x86_64/5.26.00a/lib/libRIO.so
#10 0x00007f24bc6724c8 in TBufferFile::WriteObjectAny () from /afs/e4.physik.uni-dortmund.de/sw/root-system/linux-ubuntu-8.04-x86_64/5.26.00a/lib/libRIO.so
#11 0x00007f24c1a34603 in TObjArray::Streamer () from /afs/e4.physik.uni-dortmund.de/sw/root-system/linux-ubuntu-8.04-x86_64/5.26.00a/lib/libCore.so.5.26
#12 0x00007f24bc6726d6 in TBufferFile::WriteFastArray () from /afs/e4.physik.uni-dortmund.de/sw/root-system/linux-ubuntu-8.04-x86_64/5.26.00a/lib/libRIO.so
#13 0x00007f24bc733b5f in TStreamerInfo::WriteBufferAux<char**> () from /afs/e4.physik.uni-dortmund.de/sw/root-system/linux-ubuntu-8.04-x86_64/5.26.00a/lib/libRIO.so
#14 0x00007f24bc673d43 in TBufferFile::WriteClassBuffer () from /afs/e4.physik.uni-dortmund.de/sw/root-system/linux-ubuntu-8.04-x86_64/5.26.00a/lib/libRIO.so
#15 0x00007f24bbd51dd5 in TTree::Streamer () from /afs/e4.physik.uni-dortmund.de/sw/root-system/linux-ubuntu-8.04-x86_64/5.26.00a/lib/libTree.so
#16 0x00007f24bc6a1c08 in TKey::TKey () from /afs/e4.physik.uni-dortmund.de/sw/root-system/linux-ubuntu-8.04-x86_64/5.26.00a/lib/libRIO.so
#17 0x00007f24bc68b4b9 in TFile::CreateKey () from /afs/e4.physik.uni-dortmund.de/sw/root-system/linux-ubuntu-8.04-x86_64/5.26.00a/lib/libRIO.so
#18 0x00007f24bc67f749 in TDirectoryFile::WriteTObject () from /afs/e4.physik.uni-dortmund.de/sw/root-system/linux-ubuntu-8.04-x86_64/5.26.00a/lib/libRIO.so
#19 0x00007f24c19d0f8c in TObject::Write () from /afs/e4.physik.uni-dortmund.de/sw/root-system/linux-ubuntu-8.04-x86_64/5.26.00a/lib/libCore.so.5.26
#20 0x00007f24c1bf809b in G__G__Base2_10_0_53 () from /afs/e4.physik.uni-dortmund.de/sw/root-system/linux-ubuntu-8.04-x86_64/5.26.00a/lib/libCore.so.5.26
#21 0x00007f24c0e9df3a in Cint::G__ExceptionWrapper () from /afs/e4.physik.uni-dortmund.de/sw/root-system/linux-ubuntu-8.04-x86_64/5.26.00a/lib/libCint.so.5.26
#22 0x00007f24c0f4bb30 in G__execute_call () from /afs/e4.physik.uni-dortmund.de/sw/root-system/linux-ubuntu-8.04-x86_64/5.26.00a/lib/libCint.so.5.26
#23 0x00007f24c0f4deea in G__call_cppfunc () from /afs/e4.physik.uni-dortmund.de/sw/root-system/linux-ubuntu-8.04-x86_64/5.26.00a/lib/libCint.so.5.26
#24 0x00007f24c0f27175 in G__interpret_func () from /afs/e4.physik.uni-dortmund.de/sw/root-system/linux-ubuntu-8.04-x86_64/5.26.00a/lib/libCint.so.5.26
#25 0x00007f24c0f142cc in G__getfunction () from /afs/e4.physik.uni-dortmund.de/sw/root-system/linux-ubuntu-8.04-x86_64/5.26.00a/lib/libCint.so.5.26
#26 0x00007f24c1002c26 in G__getstructmem () from /afs/e4.physik.uni-dortmund.de/sw/root-system/linux-ubuntu-8.04-x86_64/5.26.00a/lib/libCint.so.5.26
#27 0x00007f24c0ff8bf3 in G__getvariable () from /afs/e4.physik.uni-dortmund.de/sw/root-system/linux-ubuntu-8.04-x86_64/5.26.00a/lib/libCint.so.5.26
#28 0x00007f24c0eea2af in G__getitem () from /afs/e4.physik.uni-dortmund.de/sw/root-system/linux-ubuntu-8.04-x86_64/5.26.00a/lib/libCint.so.5.26
#29 0x00007f24c0eef054 in G__getexpr () from /afs/e4.physik.uni-dortmund.de/sw/root-system/linux-ubuntu-8.04-x86_64/5.26.00a/lib/libCint.so.5.26
#30 0x00007f24c0f7f746 in G__exec_statement () from /afs/e4.physik.uni-dortmund.de/sw/root-system/linux-ubuntu-8.04-x86_64/5.26.00a/lib/libCint.so.5.26
#31 0x00007f24c0ed681a in G__exec_tempfile_core () from /afs/e4.physik.uni-dortmund.de/sw/root-system/linux-ubuntu-8.04-x86_64/5.26.00a/lib/libCint.so.5.26
#32 0x00007f24c0ed6ade in G__exec_tempfile_fp () from /afs/e4.physik.uni-dortmund.de/sw/root-system/linux-ubuntu-8.04-x86_64/5.26.00a/lib/libCint.so.5.26
#33 0x00007f24c0f860de in G__process_cmd () from /afs/e4.physik.uni-dortmund.de/sw/root-system/linux-ubuntu-8.04-x86_64/5.26.00a/lib/libCint.so.5.26
#34 0x00007f24c1a6c9df in TCint::ProcessLine () from /afs/e4.physik.uni-dortmund.de/sw/root-system/linux-ubuntu-8.04-x86_64/5.26.00a/lib/libCore.so.5.26
#35 0x00007f24c19a53ea in TApplication::ProcessLine () from /afs/e4.physik.uni-dortmund.de/sw/root-system/linux-ubuntu-8.04-x86_64/5.26.00a/lib/libCore.so.5.26
#36 0x00007f24c081aa7e in TRint::HandleTermInput () from /afs/e4.physik.uni-dortmund.de/sw/root-system/linux-ubuntu-8.04-x86_64/5.26.00a/lib/libRint.so.5.26
#37 0x00007f24c0819eb7 in TTermInputHandler::Notify () from /afs/e4.physik.uni-dortmund.de/sw/root-system/linux-ubuntu-8.04-x86_64/5.26.00a/lib/libRint.so.5.26
#38 0x00007f24c081c11d in TTermInputHandler::ReadNotify () from /afs/e4.physik.uni-dortmund.de/sw/root-system/linux-ubuntu-8.04-x86_64/5.26.00a/lib/libRint.so.5.26
#39 0x00007f24c1a80d13 in TUnixSystem::CheckDescriptors () from /afs/e4.physik.uni-dortmund.de/sw/root-system/linux-ubuntu-8.04-x86_64/5.26.00a/lib/libCore.so.5.26
#40 0x00007f24c1a816c5 in TUnixSystem::DispatchOneEvent () from /afs/e4.physik.uni-dortmund.de/sw/root-system/linux-ubuntu-8.04-x86_64/5.26.00a/lib/libCore.so.5.26
#41 0x00007f24c19fdf66 in TSystem::InnerLoop () from /afs/e4.physik.uni-dortmund.de/sw/root-system/linux-ubuntu-8.04-x86_64/5.26.00a/lib/libCore.so.5.26
#42 0x00007f24c1a00ce4 in TSystem::Run () from /afs/e4.physik.uni-dortmund.de/sw/root-system/linux-ubuntu-8.04-x86_64/5.26.00a/lib/libCore.so.5.26
#43 0x00007f24c19a38bf in TApplication::Run () from /afs/e4.physik.uni-dortmund.de/sw/root-system/linux-ubuntu-8.04-x86_64/5.26.00a/lib/libCore.so.5.26
#44 0x00007f24c081bcfa in TRint::Run () from /afs/e4.physik.uni-dortmund.de/sw/root-system/linux-ubuntu-8.04-x86_64/5.26.00a/lib/libRint.so.5.26
#45 0x000000000040115d in main ()
===========================================================

Any help would be appreciated!

Cheers,

Moritz

You must also remove the leaf as shown below.
Note that this kind of gymnastics is not recommended!

Rene

 TTree *T = (TTree*)f.Get("CollectionTree");
 TBranch* b= T->GetBranch("StreamESD_ref");
 T->GetListOfBranches()->Remove(b);
 TLeaf* l= T->GetLeaf("StreamESD_ref");
 T->GetListOfLeaves()->Remove(l);
 T->Write();
1 Like

Is this recipe still valid?
I am trying to use it (with ROOT 6/18) and I either get the segmentation fault, or just “no-action” - the branch still in the tree.

I guess @pcanal can confirm (or not) if this recipe is still valid. May be you can give more details telling us which step provokes the seg-fault ?

Seeing “no-action” for this setup, I’ve tried to play around “Write” and add f.Write("",TObject::kOverwrite) and formally it runs ok, but then later I open the file and get the tree, segfault occurs… (likely double Write is not a good idea.)
As for the simple T->Write(), the tree that I get back from the newly opened file is the same in the sense the “deleted” branch/leaf still in the tree.

1 Like

the recipe ‘should’ still be working.
To debug your problem can you tell us what is the stack trace (it would be best if you are using a debug build of ROOT) and what is the result of running with valgrind --suppressions=$ROOTSYS/etc/valgrind-root.supp ... the failing example.

I would start with the output of: your_tree->Print();