TH2F array GetBinContent problem

I have an array of 16 2D histograms TH2F *histo_newMC[16]. I want to get the contents of say 3rd bin on x and 10 th bin on y, to do that i am using

histo_newMC[11]->GetBinContent(3,10);

But using this i am getting following error:
*** Break *** bus error
[]
[]
[]
[]
[]
[]
[]

can anybody help me figure out where i am going wrong? Let me know if more details are required

Try:

if (!(histo_newMC[11])) std::cout << "KMA" <<< std::endl;
else std::cout << histo_newMC[11]->GetBinContent(3,10) << std::endl;

I get the same error again,
*** Break *** segmentation violation
[unknown binary]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]

How do you initialize “histo_newMC[11]”?

The line of code you have shown is correct, i.e. your problem is elsewhere in the code. You need to show more code.

My general advice is to avoid C-style arrays. Prefer std::vector (or std::array, if you really want the properties of the C array). Also, using histo_newMC[11] is already bad because the 11 is hard coded. What does “11” mean in this context? (avoid magic constants).

TFile *f0 = new TFile("efficiency_2d_3173_time1_eff_mean0.root","READ"); 
	
TFile *f1 = new TFile("efficiency_2d_3173_time1_eff_mean0_MC.root","READ"); 
	
		TH2F *histo_data[16] ; 
	
		TH2F *histo_MC[16];  
		char hist_name[100];
		
		TH2F *histo_newMC[16] ; //weighted MC histograms

		
		TCanvas *c_test = new TCanvas("c_test","c_test",550,400); 
		c_test->Divide(4,4);
		
		for(int i =0 ; i<16 ; i++)
		{
			
			sprintf(hist_name,"zthta_m%d",i+1);
		 
			histo_data[i] = (TH2F*)f0->Get(hist_name);  // getting data histograms from root file saved in 
		
		
		
			histo_MC[i] = (TH2F*)f1->Get(hist_name) ; // getting MC histograms from root file saved in 
		}
		
		int order_module[16] = {5,4,3,2,7,6,1,0,8,9,14,15,10,11,12,13};
		for(int i=0; i<16 ; i++)
		{
			
		c_test->cd(i+1);
		n = order_module[i];
		
		double scale = 1/histo_MC[n]->Integral();
		histo_MC[n]->Scale(scale);
		scale = 1/histo_data[n]->Integral();
		histo_data[n]->Scale(scale);
		
		
		histo_newMC[n] = (TH2F*)histo_MC[n]->Clone();
		histo_newMC[n]->Divide(histo_data[n]);//*(ratio);
	
		histo_newMC[n]->Draw("COLZ");
		
		}
		
		c_test->Draw();

After this i want to use the numbers stored in various bin elsewhere, for that i am using:
weight = histo_newMC[1]->GetBinContent(3,17); // 3,17 is just an example i want to access various other bins also
But this gives me the error i mentioned previously. The numbers 1-16 are for different parts of hardware, i.e. i have 16 such 2D histograms.

Try (see if both files contain “TH2F” histograms or maybe “zthta_m*” are another types):

f0->ls();
f1->ls();

I already checked that the files contain TH2F histograms, Also if i just use the above line of code that i provided i get nice plots for histo_newMC[i], also giving the command(shown in bold below) inside last for loop :


for(int i=0; i<16 ; i++)
	{
		
	c_test->cd(i+1);
	n = order_module[i];
	
	double scale = 1/histo_MC[n]->Integral();
	histo_MC[n]->Scale(scale);
	scale = 1/histo_data[n]->Integral();
	histo_data[n]->Scale(scale);
	
	
	histo_newMC[n] = (TH2F*)histo_MC[n]->Clone();
	histo_newMC[n]->Divide(histo_data[n]);//*(ratio);
	cout<<"this is "<<histo_newMC[n]->GetBinContent(7,17)<<endl; // HERE!
	histo_newMC[n]->Draw("COLZ");
	
	}

i don’t get any error but zero is printed on screen 16 times.

So, do you get any error if you try your line directly after this “for” loop?

Well, unless you closed “f0” and “f1” afterrwards, I see no reason for a “bus error”.

so if i write
std::cout << histo_newMC[11]->GetBinContent(3,10) << std::endl;
just after the previous “for” loop, i get this error:
*** Break *** segmentation violation
[unknown binary]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]

I didn’t close f0 and f1

I’m afraid we need both your root files and a small macro which reproduces this problem.

One more idea: are you sure you have TH2F’s in the file? Could it be a TH2D? That could explain this behavior.

So:
Instead of explicitly coding TH2F *, use TH2 * whereever you can, except for the “new” call, i.e. replace ALL TH2F with TH2 in this snippet. That way you are independent of the underlying type.

Also avoid casts. Don’t write:

histo_data[i] = (TH2F*)f0->Get(hist_name); 

instead use:

f0->GetObject(hist_name, histo_data[i]);
if (!histo_data[i]) 
    cerr << "Error reading object" << hist_name << "!\n"; // or other error handling

Hi Wile_E_Coyote, I got my problem resolved. Quite embarassed to say that there was very small mistake , in the second for loop i forget to declare the variable n before , that was causing the trouble

Thanks for all the suggestions , they really helped me debugg the problem.

Hi behrenhoff, I got my problem resolved. Quite embarassed to say that there was very small mistake , in the second for loop i forget to declare the variable n before , that was causing the trouble

amroo:
n = order_module[i];
Thanks for all the suggestions , they really helped me debugg the problem.

Well, this seems to be a bug then. What’s your ROOT version? At least in ROOT 5, you should have got an explicit warning that an “automatic variable” is allocated.

My ROOT version is 6.08/06 i am using it on macosx64

Indeed a bug - this was fixed in 6.10.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.