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.
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 :
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]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
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.