Problems with vector of TH1F

Hi everybody,

For some reasons, I have in my code a function called CreateHisto which create a vector of histograms and each element of the vector is a TH1F. I have a for loop which create one histogram for each index, and I also want that the name of these histograms are different according to the different index.

However I receive a segmentation violation. Could you help me? I hope that what I want to do it’s clear.

Here is the code:

[code]std::vector<TH1F*>* MyxAODAnalysis :: CreateHisto(char* name, char* title, int nbin, double xmin, double xmax)
{

std::vector<TH1F*>* histo = new std::vector<TH1F*>(m_variations.size());

innermap::const_iterator inner_itmap;
mainmap::const_iterator itmap;

for ( itmap = m_variations.begin(); itmap != m_variations.end(); itmap++) {
for (inner_itmap = (*itmap).second.begin(); inner_itmap != (*itmap).second.end(); inner_itmap++) {

            int index = (*inner_itmap).second;          
            std::string nameh = name + '  ' + (*inner_itmap).first;
            const char* namehc = nameh.c_str();   
            TH1F* histogram = new TH1F(namehc, title, nbin, xmin, xmax);
            histo[index].push_back(histogram); 
                   

 }

}
return histo;
}
[/code]

The CreateHisto function is declared in the header as following:

Try: TString nameh = TString(name) + '_' + ((Long_t)(index)); TH1F *histogram = new TH1F(nameh, title, nbin, xmin, xmax);

Thanks for your reply Pepe,

unfortunately it doesn’t work and I get again segmentation violation.

The error is the following:

[code]The lines below might hint at the cause of the crash.
If they do not help you then please submit a bug 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.

#5 0x00007f5e524f882f in __strlen_sse42 () from /lib64/libc.so.6
#6 0x00007f5e54ac2b45 in TString::TString(char const*) () from /cvmfs/atlas.cern.ch/repo/ATLASLocalRootBase/x86_64/root/6.04.16-x86_64-slc6-gcc49-opt/lib/libCore.so
#7 0x00007f5e5b342c11 in MyxAODAnalysis::CreateHisto(char*, char*, int, double, double) () at /afs/cern.ch/work/a/amurrone/TheoryTau/TheoryUncIchep/Root/MyxAODAnalysis.cxx:113
#8 0x00007f5e5b342e93 in MyxAODAnalysis::initialize() () at /afs/cern.ch/work/a/amurrone/TheoryTau/TheoryUncIchep/Root/MyxAODAnalysis.cxx:176
#9 0x00007f5e57d82a81 in EL::Worker::changeAlgState(EL::Worker::AlgInitState, EL::Worker::AlgExecState, EL::Worker::InputState) () from /afs/cern.ch/work/a/amurrone/TheoryTau/RootCoreBin/lib/x86_64-slc6-gcc49-opt/libEventLoop.so
#10 0x00007f5e57d8360f in EL::Worker::algsExecute() () from /afs/cern.ch/work/a/amurrone/TheoryTau/RootCoreBin/lib/x86_64-slc6-gcc49-opt/libEventLoop.so
#11 0x00007f5e57d8d7f3 in EL::DirectWorker::run() () from /afs/cern.ch/work/a/amurrone/TheoryTau/RootCoreBin/lib/x86_64-slc6-gcc49-opt/libEventLoop.so
#12 0x00007f5e57db4e10 in EL::DirectDriver::doSubmit(EL::Job const&, std::basic_string<char, std::char_traits, std::allocator > const&) const () from /afs/cern.ch/work/a/amurrone/TheoryTau/RootCoreBin/lib/x86_64-slc6-gcc49-opt/libEventLoop.so
#13 0x00007f5e57db8f76 in EL::Driver::submitOnly(EL::Job const&, std::basic_string<char, std::char_traits, std::allocator > const&) const () from /afs/cern.ch/work/a/amurrone/TheoryTau/RootCoreBin/lib/x86_64-slc6-gcc49-opt/libEventLoop.so
#14 0x00007f5e57db9849 in EL::Driver::submit(EL::Job const&, std::basic_string<char, std::char_traits, std::allocator > const&) const () from /afs/cern.ch/work/a/amurrone/TheoryTau/RootCoreBin/lib/x86_64-slc6-gcc49-opt/libEventLoop.so
#15 0x0000000000402947 in main () at /afs/cern.ch/work/a/amurrone/TheoryTau/TheoryUncIchep/util/testRun.cxx:112[/code]

Line 113 of MyxAODAnalysis.cxx is the one with "TString nameh = … "

Try to add a “check”:
std::cout << “name = ->” << name << “<-” << std::endl;

BTW. You do have “TString(name)”, not just “name” in your line 113 of MyxAODAnalysis.cxx.

I get:
name = ->TUnixSystem::DispatchS… ERROR segmentation violation

so it seems that it doesn’t correctly access to the variable “name”.

I fixed this issue. It was related also to another section of the code which I didn’t quote because it is a bit long and complicated.

Thanks!

Hi,

sorry, I have another question related to this topic. I want to save histograms in a root tree but as they are vector<TH1F*> and not simply TH1F, at run time it gives me this error:

Is there a way to have the output stored in a tree?

Thanks!

Hi,

Yes, there is. As hinted by the error message you need to generate an explicit dictionary for vector<TH1F*>. You can do so in several ways including adding #pragma link C++ class vector<TH1F*>+; to one of you LinkDef.h files.

Cheers,
Philippe.