Re: Another problem with directories (I think)

From: Rene Brun (Rene.Brun@cern.ch)
Date: Fri Feb 20 1998 - 09:32:22 MET


Chris Jillings wrote:
> 
> Hi,
>     This is a problem in fitting and then copying the fit parameter to
> another variable.
> 
> TCanvas* c1 = new TCanvas("c1","The Ntuple canvas",200,10,700,780);
> 
> // The next few lines are repeated 5 times with new histograms and new
> // pads in the real macro
> 
> TPad* pad1 = new TPad("pad1","This is pad1",0.02,0.68,0.48,0.98,kWhite);
> pad1->Draw();
> pad1->SetLogy();
> TH1F* h4 = new TH1F("h4","4 MeV Electrons",80,-1,1);
> char fileName[6][80];
> strcpy(fileName[0],"/net/eagle/usr5/chris/ntp/angres/cjj_e4.root");
> 
> Int_t i;
> for(i=0;i<6;i++) {
>   TFile* ntp = new TFile(fileName[i]);
>   TTree *h509 = (TTree*)ntp->Get("h509");
> 
> // Thanks for explaining this next line to me.
>   gROOT->cd();

You have created the histogram "h4" above in the default directory
in memory. This default directory is pointed by gROOT.
If you had created "h4" after opening the file "ntp" above, then
"h4" would have been created in the list of objects in memory
associated to the file "ntp". A few lines below, you "delete ntp"
after your "h509->Draw.."). When deleting a file, all lists in memory
associated with this file are also deleted. That's why, correctly,
you create "h4" (and I also assume, h6,..h14) in gROOT directory,
because you want to use these histograms after having deleted ntp.
> 
>   if(i==0) h509->Draw("(Ue*Uft+Ve*Vft+We*Wft)>>+h4","Rfitt<600","goff");
>   if(i==1) h509->Draw("(Ue*Uft+Ve*Vft+We*Wft)>>+h6","Rfitt<600","goff");
>   if(i==2) h509->Draw("(Ue*Uft+Ve*Vft+We*Wft)>>+h8","Rfitt<600","goff");
>   if(i==3) h509->Draw("(Ue*Uft+Ve*Vft+We*Wft)>>+h10","Rfitt<600","goff");
>   if(i==4) h509->Draw("(Ue*Uft+Ve*Vft+We*Wft)>>+h12","Rfitt<600","goff");
>   if(i==5) h509->Draw("(Ue*Uft+Ve*Vft+We*Wft)>>+h14","Rfitt<600","goff");
>   delete ntp;
> }
> 
> TF1* gaussExp = new TF1("gaussExp",gaussExp,-1,1,4); // x=-1 to 1, 4params
> gaussExp->SetParNames("Exp co-eff","Gauss Amp","Gau sigma","scale fac");
> gaussExp->SetParameters(6,0.1,2,1000);
> 
> Int_t lNPlots = 6;
> TArrayD energy(lNPlots);
> TArrayD expSlope(lNPlots);
> TArrayD gaussAmp(lNPlots);
> TArrayD gaussSigma(lNPlots);
> 
> pad1->cd();
> h4->Fit("gaussExp");
> energy[0] = 4.0;
> expSlope[0] = gaussExp->GetParameter(0);
> gaussAmp[0] = gaussExp->GetParameter(1);
> gaussSigma[0] = gaussExp->GetParameter(2);
> 
> // This does get printed to screen correctly
> printf("%f\n",expSlope[0]);
> 
> pad2->cd();
> h6->Fit("gaussExp");
> energy[1] = 6.0;
> expSlope[1] = gaussExp->GetParameter(0);
> gaussAmp[1] = gaussExp->GetParameter(1);
> gaussSigma[1] = gaussExp->GetParameter(2);
> 
> ...
> 
> The problem is I can't find the arrays energy,expSlope,gaussAmp, and
> gaussSigma *anywhere*. I want to make a plot to see how these parameters
> change from histogram to histogram.
> 
> I try typing printf("%f\n",energy[0]); at the prompt and I get
> Error: No symbol energy[0] in current scope  FILE:/tmp/03374iaa LINE:1

You show only an extract of your macro. My explanation is that
very likely you have given a name to your macro. When you exit
from the macro all objects created as objects (not via new)
are deleted (standard C++ scoping rules). Your TArrayD objects
in particular will be destroyed when exiting from the macro.
You cannot access them from the command line.
The solution to this problem is to not give a name to the macro.
In this case, it will be executed in the main scope and all objects
created in the macro will still be accessible from the command line.
Note that when you run with unnamed macros, I strongly suggest
that the first statement in your macro be:
   gROOt->Reset();
to make sure to delete all objects created in a previous execution
if you want to execute your unnamed macro several times in
the same Root session.

Rene Brun



This archive was generated by hypermail 2b29 : Tue Jan 04 2000 - 00:34:30 MET