Am I using this function right?

Dear Rooters,
I’m using the following code to merge histograms(all TH1Ds) from different backgrounds, sometimes I got crashes, the error message always points to the line “result->Add(histos[i]);”. The error messages are not exactly the same every time, I also listed them.
Could you please give me any idea about the problem? Thank you in advance.

TH1* TComparison_base::GetSumOfHistograms(std::vector<TH1*>histos, std::vector<bool> isAdd)throw(std::domain_error){ if(histos.size()!=isAdd.size()){ std::string errMes=" The size of histos : "+intTostring(histos.size()) +"!= isAdd : "+intTostring(isAdd.size()); throw domain_error(message+errMes); } TH1 * result=NULL; for(int i=0; i<histos.size(); i++){ if(!isAdd[i])continue; if(result==NULL){ result=(TH1*)histos[i]->Clone(); result->SetDirectory(0); }else{ result->Add(histos[i]); } } return result; };

Error message 1:
#0 0x00000033d4499d75 in waitpid () from /lib64/libc.so.6
#1 0x00000033d443c331 in do_system () from /lib64/libc.so.6
#2 0x00002b6232cb5146 in TUnixSystem::StackTrace() () from /afs/cern.ch/sw/lcg/app/releases/ROOT/5.26.00a/x86_64-slc5-gcc34-opt/root/lib/libCore.so
#3 0x00002b6232cb19fa in TUnixSystem::DispatchSignals(ESignals) () from /afs/cern.ch/sw/lcg/app/releases/ROOT/5.26.00a/x86_64-slc5-gcc34-opt/root/lib/libCore.so
#4
#5 0x00002b6234a7739e in TH1::CheckConsistency(TH1 const*, TH1 const*) () from /afs/cern.ch/sw/lcg/app/releases/ROOT/5.26.00a/x86_64-slc5-gcc34-opt/root/lib/libHist.so
#6 0x00002b6234a775e4 in TH1::Add(TH1 const*, TH1 const*, double, double) () from /afs/cern.ch/sw/lcg/app/releases/ROOT/5.26.00a/x86_64-slc5-gcc34-opt/root/lib/libHist.so
#7 0x00002b62383faf04 in TComparison_base::GetSumOfHistograms (this=, histos=…, isAdd=…) at TComparison_base.cpp:242

Error message 2:
*** Break *** segmentation violation
#0 0x00000033d4499d75 in waitpid () from /lib64/libc.so.6
#1 0x00000033d443c331 in do_system () from /lib64/libc.so.6
#2 0x00002b714d38c9bf in TUnixSystem::StackTrace() () from /afs/cern.ch/sw/lcg/app/releases/ROOT/5.25.02/x86_64-slc5-gcc34-opt/root/lib/libCore.so
#3 0x00002b714d38948a in TUnixSystem::DispatchSignals(ESignals) () from /afs/cern.ch/sw/lcg/app/releases/ROOT/5.25.02/x86_64-slc5-gcc34-opt/root/lib/libCore.so
#4
#5 0x00000033d44797c0 in strlen () from /lib64/libc.so.6
#6 0x00002b714d2f3674 in TString::TString(char const*) () from /afs/cern.ch/sw/lcg/app/releases/ROOT/5.25.02/x86_64-slc5-gcc34-opt/root/lib/libCore.so
#7 0x00002b714f141b32 in TH1::Add(TF1*, double, char const*) () from /afs/cern.ch/sw/lcg/app/releases/ROOT/5.25.02/x86_64-slc5-gcc34-opt/root/lib/libHist.so
#8 0x00002b7152aaef04 in TComparison_base::GetSumOfHistograms (this=, histos=…, isAdd=…) at TComparison_base.cpp:242
PS: In the Error message2, I don’t know why it complains TH1::Add(TF1*, double, char const*), there is definitely no TF1 in all these processes…

Hi,

could histos[i] be NULL? Can you run with valgrind (see valgrind.org) - does it show any errors in your code?

Otherwise we’ll need a standalone example that we can run and debug.

Cheers, Axel.