Weird code behaviour when loading histograms from root file

I have the following rather simple code

[code]#include “Riostream.h”
#include
#include “TH1.h”
#include “TF1.h”
#include “TGraph.h”
#include “TH2F.h”
#include
#include “TProfile.h”
#include “TFile.h”

TH2F Stack(std::vector<TH1F> hists) {
TH2F *hStack = new TH2F(“hStack”,“Stack”,5000,0,5000,300,0,300);
for (size_t i=0;i < hists.size(); ++i) {
for (int bin = 0; bin < hists[i]->GetNbinsX(); ++bin) {
int resultBin = hStack->FindBin(bin, hists[i]->GetBinContent(bin));
int binContent = hStack->GetBinContent(resultBin) + 1;
hStack->SetBinContent(resultBin,binContent);
}
}
return hStack;
}

TH2F* psa_test(){

cout << "-------------------------------------------------------------------" << endl;	
    //TCanvas * c1 = new TCanvas("c", "c", 1280, 720);
TFile *f = new TFile("FIMG6.root");
std::vector<TH1F*> hists;

for (int i=2;i<13;i++){
	for (int j=2;j<100;j++){
		TH1F *hist = (TH1F*)f->Get(Form("FIMG6_EV_%d;%d",i,j));
		if (hist){hists.push_back(hist);}
		//hists[0]->Draw();
	}
}
cout << "The size of hists is : " << hists.size() << endl;
TH2F *hStack = Stack(hists);
//hStack->Draw("colz");
hists[1000]->Draw();
return hStack;

}[/code]

The FIMG6.root file consists of histograms under the name FIMG6_EV_1;1, FIMG6_EV_1;2, FIMG6_EV_1;3, …, FIMG6_EV_2;1, FIMG6_EV_2;2, …, FIMG6_EV_i;j where i=1,2,…,12 and j=1,2,…,150…

If I try to compile the previous code using .L psa_test.C++ it compiles without error.
If I try to run it using .x psa_test.C I get a segmenation violation.
If I replace the for loops with the following code lines

//for (int i=2;i<13;i++){ for (int j=2;j<100;j++){ TH1F *hist = (TH1F*)f->Get(Form("FIMG6_EV_2;%d",j)); if (hist){hists.push_back(hist);} //hists[0]->Draw(); } //}

i.e. to loop only the j index the code runs as it should.
If I only loop over the i index, I always get to plot the last histogram that is filled on the hists histogram array.

The weird thing is that when I try to loop over the j index I get a segmentation violation.

Any idea what might be the problem?

Thank you very much in advance!

[root.cern.ch/phpBB3/viewtopic.p … 048#p65048](Plotting Multiple Histograms with Same Title