Segmentation violation while writing histogram

,

Hi,

I’m reading some histograms in different files and adding them. When I’m trying to save the final histogram it shows a segmentation violation. I’ve tried changing the directory of this final histogram for the TFile in which I want it to save it but the error persist, any ideas?

void testWrite()
{
  TFile* fSim;
  TH1F* hSim0;
  TH1F* hTemp;

  Int_t nFiles = 2;
  for (Int_t i = 0; i < nFiles; i++)
  {
      fSim = new TFile(TString::Format("file_%i.root",i),"READ");
      if(i<1)
        hSim0 = (TH1F*) fSim->Get("3");
      else
      {
        hTemp = (TH1F*) fSim->Get("3");
        hSim0->Add(hTemp);
      }
  }

  TFile* outFile = new TFile("added.root","RECREATE");
  outFile->WriteObject(hSim0,"Ekine");
  outFile->Close();
}

Thank you for your help

ROOT Version: 6.18/04
Platform: Ubuntu 18.04


I tried to reproduce the problem with a slightly newer ROOT 6.19, but here the macro works (using hsimple.root and the hpx histogram from the tutorials). Would it be possible to share your files, and the details about how you run the code (e.g. root -l testWrite.C)? It would also help to see the stack trace.

Cheers,
Jakob

@jblomer Thanks for the answer, I’ve attached the files.xraytube_0.root (90.3 KB) xraytube_1.root (90.5 KB)

And this is the stack trace after running the macro as root -l testWrite.C:

Processing testWrite.C...

 *** Break *** segmentation violation



===========================================================
There was a crash.
This is the entire stack trace of all threads:
===========================================================
#0  0x00007f7d58acf687 in __GI___waitpid (pid=9140, stat_loc=stat_loc
entry=0x7ffd3998eca8, options=options
entry=0) at ../sysdeps/unix/sysv/linux/waitpid.c:30
#1  0x00007f7d58a3a067 in do_system (line=<optimized out>) at ../sysdeps/posix/system.c:149
#2  0x00007f7d596de823 in TUnixSystem::Exec (shellcmd=<optimized out>, this=0x5581fc453b80) at /home/fmf-017/Documents/Programs/Root/root-6.18.04/core/unix/src/TUnixSystem.cxx:2106
#3  TUnixSystem::StackTrace (this=0x5581fc453b80) at /home/fmf-017/Documents/Programs/Root/root-6.18.04/core/unix/src/TUnixSystem.cxx:2400
#4  0x00007f7d596e1214 in TUnixSystem::DispatchSignals (this=0x5581fc453b80, sig=kSigSegmentationViolation) at /home/fmf-017/Documents/Programs/Root/root-6.18.04/core/unix/src/TUnixSystem.cxx:3631
#5  <signal handler called>
#6  0x00007f7d5967b0aa in TClass::StreamerTObject (pThis=0x5581fd5b8dd0, object=<optimized out>, b=...) at /home/fmf-017/Documents/Programs/Root/root-6.18.04/core/meta/src/TClass.cxx:6534
#7  0x00007f7d57111858 in TClass::Streamer (onfile_class=0x0, b=..., obj=0x5581fd0da261, this=0x5581fd5b8dd0) at /home/fmf-017/Documents/Programs/Root/root-6.18.04-build/include/TClass.h:570
#8  TKey::TKey (this=0x5581fd735c00, obj=<optimized out>, cl=<optimized out>, name=<optimized out>, bufsize=<optimized out>, motherDir=0x5581fd737400) at /home/fmf-017/Documents/Programs/Root/root-6.18.04/io/io/src/TKey.cxx:342
#9  0x00007f7d570ca471 in TFile::CreateKey (this=<optimized out>, mother=0x5581fd737400, obj=0x5581fd0da260, cl=0x5581fd5a3900, name=0x7f7d59e960df "Ekine", bufsize=1024) at /home/fmf-017/Documents/Programs/Root/root-6.18.04/io/io/src/TFile.cxx:1022
#10 0x00007f7d570bc817 in TDirectoryFile::WriteObjectAny (this=0x5581fd737400, obj=0x5581fd0da260, cl=0x5581fd5a3900, name=<optimized out>, option=<optimized out>, bufsize=<optimized out>) at /home/fmf-017/Documents/Programs/Root/root-6.18.04/io/io/src/TDirectoryFile.cxx:2057
#11 0x00007f7d59e9761a in ?? ()
#12 0x00007f7d570bc490 in ?? () at /home/fmf-017/Documents/Programs/Root/root-6.18.04/io/io/src/TDirectoryFile.cxx:1850 from /home/fmf-017/Documents/Programs/Root/root-6.18.04-install/lib/libRIO.so
#13 0x00005581fd737400 in ?? ()
#14 0x0000000000000001 in ?? ()
#15 0x00005581fd0da260 in ?? ()
#16 0x00007f7d59e97650 in ?? ()
#17 0x0000000053ed1730 in ?? ()
#18 0x00007f7d59e960be in ?? ()
#19 0x00007f7d59e960df in ?? ()
#20 0x00005581fd0da260 in ?? ()
#21 0x00005581fd737400 in ?? ()
#22 0x00007ffd39991970 in ?? ()
#23 0x00007f7d59e97493 in ?? ()
#24 0x0000000000000000 in ?? ()
===========================================================


The lines below might hint at the cause of the crash.
You may get help by asking at the ROOT forum http://root.cern.ch/forum
Only if you are really convinced it is a bug in ROOT then please submit a
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.
===========================================================
#6  0x00007f7d5967b0aa in TClass::StreamerTObject (pThis=0x5581fd5b8dd0, object=<optimized out>, b=...) at /home/fmf-017/Documents/Programs/Root/root-6.18.04/core/meta/src/TClass.cxx:6534
#7  0x00007f7d57111858 in TClass::Streamer (onfile_class=0x0, b=..., obj=0x5581fd0da261, this=0x5581fd5b8dd0) at /home/fmf-017/Documents/Programs/Root/root-6.18.04-build/include/TClass.h:570
#8  TKey::TKey (this=0x5581fd735c00, obj=<optimized out>, cl=<optimized out>, name=<optimized out>, bufsize=<optimized out>, motherDir=0x5581fd737400) at /home/fmf-017/Documents/Programs/Root/root-6.18.04/io/io/src/TKey.cxx:342
#9  0x00007f7d570ca471 in TFile::CreateKey (this=<optimized out>, mother=0x5581fd737400, obj=0x5581fd0da260, cl=0x5581fd5a3900, name=0x7f7d59e960df "Ekine", bufsize=1024) at /home/fmf-017/Documents/Programs/Root/root-6.18.04/io/io/src/TFile.cxx:1022
#10 0x00007f7d570bc817 in TDirectoryFile::WriteObjectAny (this=0x5581fd737400, obj=0x5581fd0da260, cl=0x5581fd5a3900, name=<optimized out>, option=<optimized out>, bufsize=<optimized out>) at /home/fmf-017/Documents/Programs/Root/root-6.18.04/io/io/src/TDirectoryFile.cxx:2057
#11 0x00007f7d59e9761a in ?? ()
#12 0x00007f7d570bc490 in ?? () at /home/fmf-017/Documents/Programs/Root/root-6.18.04/io/io/src/TDirectoryFile.cxx:1850 from /home/fmf-017/Documents/Programs/Root/root-6.18.04-install/lib/libRIO.so
#13 0x00005581fd737400 in ?? ()
#14 0x0000000000000001 in ?? ()
#15 0x00005581fd0da260 in ?? ()
#16 0x00007f7d59e97650 in ?? ()
#17 0x0000000053ed1730 in ?? ()
#18 0x00007f7d59e960be in ?? ()
#19 0x00007f7d59e960df in ?? ()
#20 0x00005581fd0da260 in ?? ()
#21 0x00005581fd737400 in ?? ()
#22 0x00007ffd39991970 in ?? ()
#23 0x00007f7d59e97493 in ?? ()
#24 0x0000000000000000 in ?? ()
===========================================================



I see, thanks! I can reproduce the crash and I’m not exactly sure why it breaks, perhaps @pcanal has an idea?

But here are is a workaround: You can modify the writing part of the macro to

TFile* outFile = new TFile("added.root","RECREATE");
auto hResult = new TH1F(*hSim0);
outFile->WriteObject(hResult,"Ekine");
outFile->Close();

That works for me.

(You can of course also use hadd, like hadd added.root file_0.root file_1.root but that would merge all the objects in your input files and create the “3” histogram under the name “3” and not “Ekine”.)

Cheers,
Jakob

Thanks a lot!, I had no idea of the existence of hadd that should work perfectly.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.