Hi Ben, When you run with an unnamed macro, all the variables created in the macro are created in the global scope and are still visible once the macro has been been executed. This is convenient for simple tests, but not for real analysis. You better use normal C++ named functions, what you are doing. In your case, you have three solutions: - declare your array TH1F *HArray[3] as global, outside your named function. - access the pointers via gROOT->FindObject("HArray[2]") - name your histograms with no symbols like "[", "]" If at the commmand line level, you execute a statement like root > xxx.Draw() if xxx is unknown to CINT, CINT gives control to ROOT. ROOT searches in the list of files,directories,histograms,canvases/pads for an object with name "xxx". If it finds one, ROOT informs CINT to automatically create a global variable with the same name. If you execute something like xxx[2].Draw(), CINT will pass to ROOT only the string xxx. Rene Brun On Tue, 2 Jul 2002, Ben Kilminster wrote: > > 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