[ROOT] accessing TH1F arrays outside of macro

From: Ben Kilminster (bjk@fnal.gov)
Date: Tue Jul 02 2002 - 16:38:49 MEST


I am having problems accessing existing histogram arrays outside of a
named macro.

The following does not work:

root [0] .L draw_hists.C
root [1] hists()        
root [2] .ls
TROOT*          Rint    The ROOT of EVERYTHING
 OBJ: TH1F      H       Single histogram : 0
 OBJ: TH1F      HArray[0]       Set 0 : 0
 OBJ: TH1F      HArray[1]       Set 1 : 0
 OBJ: TH1F      HArray[2]       Set 2 : 0
root [3] H->Draw();
root [4] c1->cd(4);
root [5] HArray[2]->Draw();

Error: Symbol HArray[2] is not defined in current scope
FILE:/tmp/filekSHEtv_cint LINE:1
Error: Failed to evaluate HArray[2]->Draw()Possible candidates are...
filename       line:size busy function type and name  
*** Interpreter error recovered ***


So there is an error trying to draw the histogram from the array,
"HArray[2]", but not the single histogram, "H".



However, if I comment out the line, "void hists ()", making the macro
unnamed, and then run it, it works:

root [0] .X draw_hists.C
root [1] .ls
TROOT*          Rint    The ROOT of EVERYTHING
 OBJ: TH1F      H       Single histogram : 0
 OBJ: TH1F      HArray[0]       Set 0 : 0
 OBJ: TH1F      HArray[1]       Set 1 : 0
 OBJ: TH1F      HArray[2]       Set 2 : 0
root [2] H->Draw()
root [3] c1->cd(4)
root [4] HArray[2]->Draw();
// everything is drawn


Of course, in the first (non-working) example, I could do something like :
root [5] TH1F *Q = (TH1F *) gROOT->FindObject("HArray[2]") 
root [6] Q->Draw()

but this sort of defeats the purpose of having an object already in
memory, and also seems unnecessary since the second example does work the
way I would think it should.  



Here is the macro, (I tried this in root v3_02_07c KCC_4_0 Linux+2.2):

// file name: draw_hists.C
void hists () 
{
// create single histogram
TH1F *H = new TH1F("H","Single histogram",10,-0.5,1.5);
for (int j=0;j<100;j++) H->Fill(gRandom->Gaus());

// create array of histograms
   TH1F *HArray[3];
   for (int i = 0; i < 3; i++) {
     char hname[15];
     char htitle[15];  
     sprintf( hname, "HArray[%d]", i);
     sprintf( htitle, "Set %d", i);
     HArray[i] = new TH1F(hname,htitle,10,-0.5,1.5);
     
     for (int j=0;j<100;j++) HArray[i]->Fill(gRandom->Gaus());
   }

// draw histograms within macro
c1 = new TCanvas();
c1->Divide(2,2);
c1->cd(1);
H->Draw();
c1->cd(2);
HArray[1]->Draw();
c1->cd(3);

// Now draw the above two outside the macro.

}



This archive was generated by hypermail 2b29 : Sat Jan 04 2003 - 23:50:58 MET