Hello
I’m trying to merge trees from different root files into one file.
I use TChain to do the jobs
{
...
TChain *trees_chain = new TChain( tree_name );
trees_chain -> CanDeleteRefs( true );
for ( int i=0; i<number_of_subtrees; i++ )
{
trees_chain -> Add ( subtree_files[i].data( ) );
printf( "| | added: %s\n", subtree_files[i].data( ) );
}
long target_tree_size = trees_chain->GetEntries( );
TFile* output_file = new TFile( target_path, merge_mode );
output_file -> cd( );
trees_chain -> Merge( output_file, 0, "fast" );
printf ( "| |_ merged [%ld] event to: %s\n", target_tree_size, target_path );
output_file -> Close( );
...
}
here subtree_files
is just a std::vector<string>
containing the files’ names I want to merge.
I managed to merge the trees into the target file, but when I close it, I got segmentation violation error.
===========================================================
There was a crash.
This is the entire stack trace of all threads:
===========================================================
#0 0x00007f92b4366337 in __GI___waitpid (pid=27692, stat_loc=stat_loc
entry=0x7ffdf7aadde8, options=options
entry=0) at ../sysdeps/unix/sysv/linux/waitpid.c:30
#1 0x00007f92b42d1047 in do_system (line=<optimized out>) at ../sysdeps/posix/system.c:149
#2 0x00007f92b58760f3 in TUnixSystem::StackTrace() () from /home/longhoa/ROOT_Source/ROOT_v06_24_00/lib/libCore.so.6.24
#3 0x00007f92b5878be5 in TUnixSystem::DispatchSignals(ESignals) () from /home/longhoa/ROOT_Source/ROOT_v06_24_00/lib/libCore.so.6.24
#4 <signal handler called>
#5 0x0000561cc5cc0ac9 in main ()
===========================================================
The lines below might hint at the cause of the crash.
You may get help by asking at the ROOT forum https://root.cern.ch/forum
Only if you are really convinced it is a bug in ROOT then please submit a
report at https://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 0x0000561cc5cc0ac9 in main ()
===========================================================
I even tried
trees_chain -> SetDirectory( 0 );
But it still doesn’t help.
How do I avoid this error? Thank you very much.
Update
I managed to avoid the segmentation violation by using
trees_chain->Write( "fast", TObject::kOverwrite ); //not sure if 'fast' is a valid option here
instead of
trees_chain->Merge( output_file, 0, "fast" );
But I’m not sure if this is the proper way to save the chain, as this is not instructed in the TChain class reference.
Solution update
As @dastudillo pointed out, the output file is closed and deleted automatically, so any action on output_file
after the Merge( )
will result in error.
ROOT Version: 6.24/00
Platform: Ubuntu 18.04LTS
Compiler: g++