Segmentation fault after CopyTree

Hi all,

I am trying to use CopyTree to cleanup some ntuples, applying some cuts and removing leaves from the TTree. For that, I use the following PyROOT functions (ROOT 5.34/03 on MacOsX 10.8):

from ROOT import *

def copyFileStructure(oldFile, treeName, newFile):
    oldFile = TFile.Open(oldFile)
    oldTree = oldFile.Get(treeName)
    if not oldTree:
        print "Cannot find tree -> %s" % treeName
        return False
    newFile = TFile.Open(newFile, 'RECREATE')
    dirs = treeName.split('/')
    if len(dirs) > 1:
        for d in dirs[:-1]:
            newFile.mkdir(d)
            newFile.cd(d)
    return oldFile, oldTree, newFile

def copyTreeWithCuts(oldFile, treeName, newFile, cuts, activeBranches=["*"]):
    oldFile, oldTree, newFile = copyFileStructure(oldFile, treeName, newFile)
    if not isinstance(cuts, (list, tuple)):
        cuts = [cuts]
    cutString = '&&'.join(cuts)
    oldTree.SetBranchStatus("*", 0)
    for branch in activeBranches:
        oldTree.SetBranchStatus(branch, 1)
    newTree = oldTree.CopyTree(cutString)
    newTree.Write("", TObject.kOverwrite)
    newFile.Write("", TObject.kOverwrite)
    newFile.Close()
    oldFile.Close()

It works fine, but then if I open the ROOT file with the normal root, open a TBrowser and plot something, when exiting ROOT (.q) I get a segmentation fault

[code] *******************************************

  •                                     *
    
  •    W E L C O M E  to  R O O T       *
    
  •                                     *
    
  • Version 5.34/03 27 October 2012 *
  •                                     *
    
  • You are welcome to visit our Web site *
  •      http://root.cern.ch            *
    
  •                                     *
    

ROOT 5.34/03 (tags/v5-34-03@46856, Oct 27 2012, 23:19:27 on macosx64)

CINT/ROOT C/C++ Interpreter version 5.18.00, July 2, 2010
Type ? for help. Commands must be C++ statements.
Enclose multiple statements between { }.
root [0]
Attaching file test.root as _file0…
root [1] t = TBrowser()
(class TBrowser)140409792254400
root [2] .q

*** Break *** segmentation violation

===========================================================
There was a crash.
This is the entire stack trace of all threads:

Thread 1 (process 6773):
#0 0x00007fff87cf66ac in wait4 ()
#1 0x00007fff8646d03a in system ()
#2 0x00000001067add5f in TUnixSystem::StackTrace ()
#3 0x00000001067abea4 in TUnixSystem::DispatchSignals ()
#4
#5 0x000000010676f9d2 in THashTable::FindObject ()
#6 0x00000001067721da in TMap::GetValue ()
#7 0x0000000107945e9d in TFile::SetCacheRead ()
#8 0x0000000107ccf10b in TTreeCache::~TTreeCache ()
#9 0x0000000107ccf0a5 in TTreeCache::~TTreeCache ()
#10 0x0000000107941d1c in TFile::~TFile ()
#11 0x0000000107941c95 in TFile::~TFile ()
#12 0x000000010676dd1e in TCollection::GarbageCollect ()
#13 0x00000001067709f8 in TList::Delete ()
#14 0x00000001067303d1 in TROOT::~TROOT ()
#15 0x00007fff86429307 in __cxa_finalize ()
#16 0x00007fff8642af57 in exit ()
#17 0x00000001067ad605 in TUnixSystem::Exit ()
#18 0x00000001066fa1eb in TApplication::ProcessLine ()
#19 0x00000001075b7b18 in TRint::HandleTermInput ()
#20 0x00000001075b6285 in TTermInputHandler::Notify ()
#21 0x00000001075b8215 in TTermInputHandler::ReadNotify ()
#22 0x00000001067ab75e in TUnixSystem::CheckDescriptors ()
#23 0x00000001067ab3d6 in TUnixSystem::DispatchOneEvent ()
#24 0x0000000106747548 in TSystem::InnerLoop ()
#25 0x0000000106747394 in TSystem::Run ()
#26 0x00000001066fb14a in TApplication::Run ()
#27 0x00000001075b74f9 in TRint::Run ()
#28 0x00000001066f2abf 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 0x000000010676f9d2 in THashTable::FindObject ()
#6 0x00000001067721da in TMap::GetValue ()
#7 0x0000000107945e9d in TFile::SetCacheRead ()
#8 0x0000000107ccf10b in TTreeCache::~TTreeCache ()
#9 0x0000000107ccf0a5 in TTreeCache::~TTreeCache ()
#10 0x0000000107941d1c in TFile::~TFile ()
#11 0x0000000107941c95 in TFile::~TFile ()
#12 0x000000010676dd1e in TCollection::GarbageCollect ()
#13 0x00000001067709f8 in TList::Delete ()
#14 0x00000001067303d1 in TROOT::~TROOT ()
#15 0x00007fff86429307 in __cxa_finalize ()
#16 0x00007fff8642af57 in exit ()
#17 0x00000001067ad605 in TUnixSystem::Exit ()

root.exe(6773) malloc: *** error for object 0x7fb3b406d990: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
[/code]

If I open the TBrowser but I don’t plot anything, this doesn’t happen. Can you help me spot what I’m doing wrong?

Thanks,
Albert

Albert,

this is a known issue in ROOT’s cleanup (see Chris Jones’ questions on the roottalk mailing list). Since fixed by Philippe in v5-34-00-patches.

If the e-group link (which is rather long) works, then read it here.

Cheers,
Wim

Wim,

thanks for the answer, I missed it when you posted it.

Actually, I am using ROOT 5.34/03, is this supposed to be older than the v5-34-00-patches?

Thanks,
Albert

Albert,

yes, -patches is the “trunk” version of a branch setup after a release. I don’t think a release has been cut yet that contains the fix.

Cheers,
Wim