Writing a new ttree with selected events

Hello

I am writing a new ttree from an existing one with the following code (MakeClass extracted):

=============================================================
outputfile = new TFile(“red.root”, “RECREATE”);

redtree= fChain->CloneTree(0);

Long64_t nentries = fChain->GetEntries();

Long64_t nbytes = 0, nb = 0;

for (Long64_t jentry=0; jentry<nentries;jentry++) {
Long64_t ientry = LoadTree(jentry);
if (ientry < 0) break;
nb = fChain->GetEntry(jentry); nbytes += nb;

if(a_true_condition) redtree->Fill();

}

redtree->AutoSave();
outputfile->Write();
outputfile->Close();

it seems sometimes I get a crash, in particular when I have few entries selected, below we can find part of the stack-trace.
Any advice to avoid this ?
thanks
michela

#10 0xf799fa9a in TCollection::GarbageCollect(TObject*) ()
from /sw/atlas/sl5/prod/releases/rel_16-2.1/DetCommon/16.0.3/InstallArea/i686-slc5-gcc43-opt/lib/libCore.so
#11 0xf79a9ef4 in TObjArray::Delete(char const*) ()

from /sw/atlas/sl5/prod/releases/rel_16-2.1/DetCommon/16.0.3/InstallArea/i686-slc5-gcc43-opt/lib/libCore.so
#12 0xf79aa7f0 in TObjArray::~TObjArray() ()
from /sw/atlas/sl5/prod/releases/rel_16-2.1/DetCommon/16.0.3/InstallArea/i686-slc5-gcc43-opt/lib/libCore.so

#13 0xf79e8bc9 in TCint::AutoLoad(char const*) ()
from /sw/atlas/sl5/prod/releases/rel_16-2.1/DetCommon/16.0.3/InstallArea/i686-slc5-gcc43-opt/lib/libCore.so
#14 0xf7952157 in TROOT::LoadClass(char const*, bool) const ()

from /sw/atlas/sl5/prod/releases/rel_16-2.1/DetCommon/16.0.3/InstallArea/i686-slc5-gcc43-opt/lib/libCore.so
#15 0xf79bdedd in TClass::GetClass(char const*, bool, bool) ()
from /sw/atlas/sl5/prod/releases/rel_16-2.1/DetCommon/16.0.3/InstallArea/i686-slc5-gcc43-opt/lib/libCore.so

#16 0xf79c4d24 in TClassRef::InternalGetClass() const ()
from /sw/atlas/sl5/prod/releases/rel_16-2.1/DetCommon/16.0.3/InstallArea/i686-slc5-gcc43-opt/lib/libCore.so
#17 0xf65e34be in TBranchElement::ReleaseObject() ()

from /sw/atlas/sl5/prod/releases/rel_16-2.1/DetCommon/16.0.3/InstallArea/i686-slc5-gcc43-opt/lib/libTree.so
#18 0xf65e35f6 in TBranchElement::ResetAddress() ()
from /sw/atlas/sl5/prod/releases/rel_16-2.1/DetCommon/16.0.3/InstallArea/i686-slc5-gcc43-opt/lib/libTree.so

#19 0xf65ecf66 in TBranchElement::~TBranchElement() ()
from /sw/atlas/sl5/prod/releases/rel_16-2.1/DetCommon/16.0.3/InstallArea/i686-slc5-gcc43-opt/lib/libTree.so
#20 0xf799fa9d in TCollection::GarbageCollect(TObject*) ()

from /sw/atlas/sl5/prod/releases/rel_16-2.1/DetCommon/16.0.3/InstallArea/i686-slc5-gcc43-opt/lib/libCore.so
#21 0xf79a9ef4 in TObjArray::Delete(char const*) ()
from /sw/atlas/sl5/prod/releases/rel_16-2.1/DetCommon/16.0.3/InstallArea/i686-slc5-gcc43-opt/lib/libCore.so

#22 0xf663288a in TTree::~TTree() ()
from /sw/atlas/sl5/prod/releases/rel_16-2.1/DetCommon/16.0.3/InstallArea/i686-slc5-gcc43-opt/lib/libTree.so
#23 0xf799fa9d in TCollection::GarbageCollect(TObject*) ()
from /sw/atlas/sl5/prod/releases/rel_16-2.1/DetCommon/16.0.3/InstallArea/i686-slc5-gcc43-opt/lib/libCore.so

#24 0xf79a6bc8 in TList::Delete(char const*) ()
from /sw/atlas/sl5/prod/releases/rel_16-2.1/DetCommon/16.0.3/InstallArea/i686-slc5-gcc43-opt/lib/libCore.so
#25 0xf79a2ac7 in THashList::Delete(char const*) ()
from /sw/atlas/sl5/prod/releases/rel_16-2.1/DetCommon/16.0.3/InstallArea/i686-slc5-gcc43-opt/lib/libCore.so

#26 0xf708d8d0 in TDirectoryFile::Close(char const*) ()
from /sw/atlas/sl5/prod/releases/rel_16-2.1/DetCommon/16.0.3/InstallArea/i686-slc5-gcc43-opt/lib/libRIO.so
#27 0xf70a8074 in TFile::Close(char const*) ()

from /sw/atlas/sl5/prod/releases/rel_16-2.1/DetCommon/16.0.3/InstallArea/i686-slc5-gcc43-opt/lib/libRIO.so
#28 0xf5e6b1ca in TDCacheFile::~TDCacheFile() ()
from /sw/atlas/sl5/prod/releases/rel_16-2.1/DetCommon/16.0.3/InstallArea/i686-slc5-gcc43-opt/lib/libDCache.so

#29 0xf799fa9d in TCollection::GarbageCollect(TObject*) ()
from /sw/atlas/sl5/prod/releases/rel_16-2.1/DetCommon/16.0.3/InstallArea/i686-slc5-gcc43-opt/lib/libCore.so
#30 0xf79a6b60 in TList::Delete(char const*) ()

from /sw/atlas/sl5/prod/releases/rel_16-2.1/DetCommon/16.0.3/InstallArea/i686-slc5-gcc43-opt/lib/libCore.so
#31 0xf7950746 in TROOT::~TROOT() ()
from /sw/atlas/sl5/prod/releases/rel_16-2.1/DetCommon/16.0.3/InstallArea/i686-slc5-gcc43-opt/lib/libCore.so

#32 0x002ec059 in __cxa_finalize () from /lib/libc.so.6
#33 0xf78ffb34 in __do_global_dtors_aux ()
from /sw/atlas/sl5/prod/releases/rel_16-2.1/DetCommon/16.0.3/InstallArea/i686-slc5-gcc43-opt/lib/libCore.so
#34 0xf7dcda20 in _fini ()

from /sw/atlas/sl5/prod/releases/rel_16-2.1/DetCommon/16.0.3/InstallArea/i686-slc5-gcc43-opt/lib/libCore.so
#35 0x002af74e in _dl_fini () from /lib/ld-linux.so.2
#36 0x002ebda9 in exit () from /lib/libc.so.6
#37 0x002d5ea4 in __libc_start_main () from /lib/libc.so.6

#38 0x0804aa61 in _start ()

[quote]I am writing a new ttree from an existing one with the following code (MakeClass extracted):[/quote]A TTree in MakeClass mode can not be cloned automatically. For now you will need to create 2 chains from the list of input files, one to use with the MakeClass and one to use for the Cloning.

Cheers,
Philippe.