Problem with closing a root file

Hi, I am pasting part of my code that I am using:
{
gROOT->Reset();
TH1F *after_accep_corr[3];
char *histname_accep_corr = new char[30];
for (Int_t k=0; k<3; k++) {
sprintf(histname_accep_corr, “after_accep_corr_%d”,k+1);
after_accep_corr[k] = new TH1F(histname_accep_corr,"",20,x_nu);
}

TFile *f_flux = new TFile(“nu_flux_output.root”,“READ”);
cout<<final_flux_1_1->GetBinContent(2)<<endl;
f_flux->Close();

cout<<after_accep_corr_1->GetBinContent(2)<<endl;
}

The first output statement prints w/o any problem. But in the second output statement I get the following message:

Error: Symbol after_accep_corr_1 is not defined in current scope

This means that somehow the code is looking for after_accep_corr_1 in the root file nu_flux_output.root instead of in the main body of the code. How can I get around this problem ?
I am using a slightly old version of root : root_4.04_02b

                                 -thanks Debdatta.

[quote]This means that somehow the code is looking for after_accep_corr_1 in the root file nu_flux_output.root instead of in the main body of the code. How can I get around this problem ?
[/quote]What about using the variable you have declared an use after_accep_corr[0] instead?

Philippe

HI Philippe,
thanks for your answer. Actually the place where I have my second cout statement, I will replace that with a function . I was planning to pass the after_accep_corr_1 (filled in the main program) histogram and the final_flux_1_1 histogram (from the nu_flux_output.root) file.

But since this is creating problems, what I can do is right at the beginnig of my code (before I declare any histogram) I can open the nu_flux_output.root file , then copy all the histogram contents in array and pass them onto the function later on. But I was hoping there would be a way out , b/c doing this would make my code a little clumsy.

                                  -thanks Debdatta.

[quote]But since this is creating problems, [/quote]I don’t understand why you still have a problem if you use “after_accep_corr[0]”. What is the issue then?

Philippe

HI Philippe,
sorry for confusing you. What I am trying to do is pass two histograms to a function. One histogram is declared in the code, the other is inside a root file. But if I open this root file to access the histogram then my code can’t identify the first histogram anymore.

                                                   -thanks Debdatta.

[quote]But if I open this root file to access the histogram then my code can’t identify the first histogram anymore.[/quote]This is what I understood and since in your case you already have a C++ variable pointing to the histogram object ( after_accep_corr[0] ), you do NOT need to refer to the histogram using its histogram name (after_accep_corr_1) ).

Could you please send me the code snippets where you attempts to apply my recommendation and tell me explicity how it is failing.

Philippe

Hi Philippe,
I am attaching the code as well as the root file that I read inside the code. I get the following error message now:

Error: Symbol final_flux_1_1 is not defined in current scope nu_xsec.C:72:
*** Interpreter error recovered ***

                                        -thanks Debdatta.

nu_flux_output.root (21.4 KB)
nu_xsec.C (1.35 KB)

So you now have a different problem :slight_smile:
Anyway the solution is simply to load the histogram explicitly from the file:

[code]
TFile *f_flux = new TFile(“nu_flux_output.root”,“READ”);

TH1F *final_flux; f_flux->GetObject(“final_flux_1_1”,final_flux);

cout<<"\ngetting the xsec(nu<1)\n"<<endl;

get_xsec(final_flux,after_accep_corr[0], final_xsec[0][0]);[/code]

Cheers,
Philippe.

Hi Philippe,
finally its working. Thanks a lot for your help. So is it safer to use the hist[i] instead of hist_i (the name) ?

                                                      -thanks debdatta.

                                              -

Yes. The variable follow the C++ syntax and grammar (and can be used in compiled code) while the ‘name’ is a CINT addon which result depend on the current ROOT file directory (and can not be used in every circunstances).

Philippe