Reading a TChain twice

Hello,
I have some data stored into a TTree. I need to:
1 - loop on all events, filling a histogram;
2 - fit the histogram and extract a fit parameter;
3 - loop on all the events again and do some analysis.
When doing this with a TTree I don’t have any problem.
Now I’d like to combine several runs together. I thought of modifying my code using a TChain instead of a TTree. This is a minimal code that reproduces the problem:

for(Int_t i=0;DataClass->GetEntry(i)>0;i++)
if(DataClass->mm_nclu==1)
reshisto0->Fill(DataClass->mm_clcogpos->at(0));
reshisto0->Fit(resfit0,“QR”);
DataClass->GetEntry(0);

DataClass is the class I get from TTree::MakeClass, reshisto0 is of type TH1S* and resfit0 is of type TF1*.
I get a segmentation violation only when trying to GetEntry(0) after fitting. If I remove Fit it doesn’t crash. Here below the error message I get. Please let me know if you need any further info.

*** Break *** segmentation violation

===========================================================
There was a crash (#7 0x007ada1d in SigHandler(ESignals) () from /afs/cern.ch/sw/lcg/app/releases/ROOT/5.26.00b/i686-slc5-gcc43-opt/root/lib/libCore.so).
This is the entire stack trace of all threads:

#0 0x00f35402 in __kernel_vsyscall ()
#1 0x01f5a8a3 in __waitpid_nocancel () from /lib/i686/nosegneg/libc.so.6
#2 0x01efe4fb in do_system () from /lib/i686/nosegneg/libc.so.6
#3 0x00f1c07d in system () from /lib/i686/nosegneg/libpthread.so.0
#4 0x007a730d in TUnixSystem::Exec(char const*) () from /afs/cern.ch/sw/lcg/app/releases/ROOT/5.26.00b/i686-slc5-gcc43-opt/root/lib/libCore.so
#5 0x007acd3e in TUnixSystem::StackTrace() () from /afs/cern.ch/sw/lcg/app/releases/ROOT/5.26.00b/i686-slc5-gcc43-opt/root/lib/libCore.so
#6 0x007ad91d in TUnixSystem::DispatchSignals(ESignals) () from /afs/cern.ch/sw/lcg/app/releases/ROOT/5.26.00b/i686-slc5-gcc43-opt/root/lib/libCore.so
#7 0x007ada1d in SigHandler(ESignals) () from /afs/cern.ch/sw/lcg/app/releases/ROOT/5.26.00b/i686-slc5-gcc43-opt/root/lib/libCore.so
#8 0x007a3f52 in sighandler(int) () from /afs/cern.ch/sw/lcg/app/releases/ROOT/5.26.00b/i686-slc5-gcc43-opt/root/lib/libCore.so
#9
#10 0x0017f0cf in TStreamerInfo::BuildCheck() () from /afs/cern.ch/sw/lcg/app/releases/ROOT/5.26.00b/i686-slc5-gcc43-opt/root/lib/libRIO.so
#11 0x0015865d in TFile::ReadStreamerInfo() () from /afs/cern.ch/sw/lcg/app/releases/ROOT/5.26.00b/i686-slc5-gcc43-opt/root/lib/libRIO.so
#12 0x00159ee7 in TFile::Init(bool) () from /afs/cern.ch/sw/lcg/app/releases/ROOT/5.26.00b/i686-slc5-gcc43-opt/root/lib/libRIO.so
#13 0x0015b649 in TFile::TFile(char const*, char const*, char const*, int) ()
from /afs/cern.ch/sw/lcg/app/releases/ROOT/5.26.00b/i686-slc5-gcc43-opt/root/lib/libRIO.so
#14 0x00160462 in TFile::Open(char const*, char const*, char const*, int, int) ()
from /afs/cern.ch/sw/lcg/app/releases/ROOT/5.26.00b/i686-slc5-gcc43-opt/root/lib/libRIO.so
#15 0x03de124a in TChain::LoadTree(long long) () from /afs/cern.ch/sw/lcg/app/releases/ROOT/5.26.00b/i686-slc5-gcc43-opt/root/lib/libTree.so
#16 0x03ddeb58 in TChain::GetEntry(long long, int) () from /afs/cern.ch/sw/lcg/app/releases/ROOT/5.26.00b/i686-slc5-gcc43-opt/root/lib/libTree.so
#17 0x0804b73a in ntp::GetEntry(long long) ()
#18 0x08051990 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
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.

#10 0x0017f0cf in TStreamerInfo::BuildCheck() () from /afs/cern.ch/sw/lcg/app/releases/ROOT/5.26.00b/i686-slc5-gcc43-opt/root/lib/libRIO.so
#11 0x0015865d in TFile::ReadStreamerInfo() () from /afs/cern.ch/sw/lcg/app/releases/ROOT/5.26.00b/i686-slc5-gcc43-opt/root/lib/libRIO.so
#12 0x00159ee7 in TFile::Init(bool) () from /afs/cern.ch/sw/lcg/app/releases/ROOT/5.26.00b/i686-slc5-gcc43-opt/root/lib/libRIO.so
#13 0x0015b649 in TFile::TFile(char const*, char const*, char const*, int) ()
from /afs/cern.ch/sw/lcg/app/releases/ROOT/5.26.00b/i686-slc5-gcc43-opt/root/lib/libRIO.so
#14 0x00160462 in TFile::Open(char const*, char const*, char const*, int, int) ()
from /afs/cern.ch/sw/lcg/app/releases/ROOT/5.26.00b/i686-slc5-gcc43-opt/root/lib/libRIO.so
#15 0x03de124a in TChain::LoadTree(long long) () from /afs/cern.ch/sw/lcg/app/releases/ROOT/5.26.00b/i686-slc5-gcc43-opt/root/lib/libTree.so
#16 0x03ddeb58 in TChain::GetEntry(long long, int) () from /afs/cern.ch/sw/lcg/app/releases/ROOT/5.26.00b/i686-slc5-gcc43-opt/root/lib/libTree.so
#17 0x0804b73a in ntp::GetEntry(long long) ()
#18 0x08051990 in main ()

Hi,

I suspect that reshisto0 is attached to the input file rather than to an ouput and is thus delete when the TChain switch over from one file to the other. To guarantee the correct association call something like reshisto0->SetDirectory(myoutput_file);

Cheers,
Philippe.

Hi,
I tried to force the histogram to be associated to an output file. Here the code:

TFile* myoutputfile=new TFile("Try.root","RECREATE"); reshisto0->SetDirectory(myoutputfile); for(Int_t i=0;DataClass->GetEntry(i)>0;i++) if(DataClass->mm_nclu==1) reshisto0->Fill(DataClass->mm_nclu); reshisto0->Fit(resfit0,"QR"); DataClass->GetEntry(0); return 0;

It is still crashing with a segmentation violation. The fit completes successfully. The crash happens when, after fitting, I call GetEntry on the TChain. I tried to get different entries. Sometimes it’s ok, sometimes it crashes. :confused:

Hi,

Try running your example compiled and try with valgrind, it should pin-point the problem.

Philippe.