Generating stack trace with std::vector

I need to use a simple code that makes me to push_back some histograms in a vector<TH2D*>.
The problem is that I can generate the histograms and push_back them in the vector (the vector’s size is correct) but when I try to fill them I’m able only to fill the first and not the other. The execution crash…
Someone can help me?

Thank you,

Without any source code which demonstrates this problem?

I know that the code is very simple… :blush:

[code]#include <TFile.h>
#include <TMath.h>
#include <TF2.h>
#include <TCanvas.h>
#include <TH2D.h>
#include <TString.h>
#include <string.h>

using namespace std;

double BinormalDistribution(double mu1, double sigma1, double mu2, double sigma2, double ro, double x1, double x2)
return 1/(2TMath::Pi()sigma1sigma2TMath::Sqrt(1-TMath::Power(ro,2)))TMath::Exp( -(TMath::Power(x1-mu1,2)/TMath::Power(sigma1,2)- 2ro*(x1-mu1)(x2-mu2)/(sigma1sigma2) + TMath::Power(x2-mu2,2)/TMath::Power(sigma2,2) ));

std::string ToString(double& value)
std::stringstream sstr;

sstr << value;
return sstr.str();


void result(){

TFile *f = TFile::Open(“Noemi_Binormal_Distribution.root”,“RECREATE”);

const unsigned int numhisto = 9;
int NbinX = 100;
float minX = -10.0;
float maxX = 10.0;
int NbinY = 100;
float minY = -10.0;
float maxY = 10.0;

vector<TH2D*> histo;

double mu1 = 0;
double mu2 = 0;
double sigma1 = 2;
double sigma2 = 2;
double ro = 0.8;

TRandom3 x(0);

unsigned int n = 10000;

for( unsigned int k = 0; k < numhisto; k++)
string titolo = “Istogramma_”;
titolo+=“ro = “;
titolo+= ToString(ro);
titolo+=”; A.U.; A.U.; Eventi";
string nome = "Istogramma

  histo.push_back( new TH2D(nome.c_str(), titolo.c_str(), NbinX, minX, maxX,  NbinY, minY, maxY) );
  for(int i = 0; i < n; i++){
double x1 = x.Uniform( minX, maxX);
double x2 = x.Uniform( minY, maxY); -> Fill( x1, x2, BinormalDistribution(mu1, sigma1, mu2, sigma2, ro, x1, x2)); 


The error message is:

*** Break *** segmentation violation
 Generating stack trace...
 0x00007f9be9673c8b in G__exec_asm + 0x4d2b from /usr/lib/root/
 0x00007f9be9541060 in <unknown> from /usr/lib/root/
 0x00007f9be953c245 in G__exec_statement + 0x95c5 from /usr/lib/root/
 0x00007f9be95a0f99 in G__interpret_func + 0x2989 from /usr/lib/root/
 0x00007f9be95250a1 in G__getfunction + 0x1901 from /usr/lib/root/
 0x00007f9be960e272 in G__getitem + 0x842 from /usr/lib/root/
 0x00007f9be9614248 in G__getexpr + 0x4f18 from /usr/lib/root/
 0x00007f9be9538f77 in G__exec_statement + 0x62f7 from /usr/lib/root/
 0x00007f9be9644aa4 in <unknown> from /usr/lib/root/
 0x00007f9be964604e in G__exec_tempfile_fp + 0xe from /usr/lib/root/
 0x00007f9be966688b in G__process_cmd + 0x128b from /usr/lib/root/
 0x00007f9beb416101 in TCint::ProcessLine(char const*, TInterpreter::EErrorCode*) + 0x451 from /usr/lib/root/
 0x00007f9beb3e7da2 in TApplication::ProcessLine(char const*, bool, int*) + 0x362 from /usr/lib/root/
 0x00007f9beafdd479 in TRint::HandleTermInput() + 0x219 from /usr/lib/root/
 0x00007f9beb4447ed in TUnixSystem::CheckDescriptors() + 0x14d from /usr/lib/root/
 0x00007f9beb445f88 in TUnixSystem::DispatchOneEvent(bool) + 0xc8 from /usr/lib/root/
 0x00007f9beb3d7d86 in TSystem::InnerLoop() + 0x16 from /usr/lib/root/
 0x00007f9beb3d9964 in TSystem::Run() + 0x74 from /usr/lib/root/
 0x00007f9beb3e60ff in TApplication::Run(bool) + 0x1f from /usr/lib/root/
 0x00007f9beafde157 in TRint::Run(bool) + 0x527 from /usr/lib/root/
 0x0000000000400fcc in main + 0x4c from /usr/bin/root.exe
 0x00007f9bea506725 in __libc_start_main + 0xf5 from /usr/lib/
 0x000000000040103d in <unknown> from /usr/bin/root.exe
Root > Function result() busy flag cleared

You’ve got (at least) two problems …

  1. You meet a CINT limitation … replace: histo.push_back( new TH2D(nome.c_str(), titolo.c_str(), NbinX, minX, maxX, NbinY, minY, maxY) ); with: TH2D *htemp = new TH2D(nome.c_str(), titolo.c_str(), NbinX, minX, maxX, NbinY, minY, maxY); histo.push_back(htemp);

  2. A potential design flaw … you open a ROOT file, then you create a vector in which you “store” pointers to histograms which you also create, then you close the ROOT file. At this moment, all your histograms are gone (i.e. they are automatically “deleted”). All histogram pointers “stored” in your vector are INVALID then. Note: when the “result()” routine terminates, it will delete the vector itself.

BTW. I’m not really sure if you are allowed to “return sstr.str();” from your “ToString” routine. I would simply replace (that should work fine): titolo+= ToString(ro); with: titolo+= TString::Format("%g", ro).Data();

Ok… I replaced you lines and now it works!! :smiley:
About the last thing I used my routine because if I put titolo += ro the title is empty…

Thank you very much!!!

If you use “TString titolo;” instead of “std::string titolo;”, then you can simply say “titolo += k + 1;”, “titolo += ro;”.