Re: [ROOT] When to use a pointer for TBox?

From: Rene Brun (Rene.Brun@cern.ch)
Date: Fri Sep 06 2002 - 10:15:11 MEST


Hi Remi,

You hit a typical scoping problem. If you create your box
in the stack with:
TBox _box(4,-4,5,0);
this object is automatically deleted when leaving the scope of the
function draw.

When you run your UNNAMED macro, the objects created in the macro are
created in the global scope. They do not disappear when the macro
has executed. See the Users Guide.

Rene Brun

On Fri, 30 Aug 2002, Remigius K. Mommsen wrote:

> Hi,
> 
> I stumbled over a mistake which I don't really understand.
> I'd like to plot a 2D histogram and then draw a
> box into it with TBox. First I started with a macro like:
> 
> {
> ntuple->Draw("deltaE:mB");
> 
> TBox box(4,-4,5,0);
> box.Draw();
> }
> 
> When I execute this macro on the root command line it works as expected.
> Well, then lets make a class out of it:
> 
> class myBox {
> 
> public:
>    myBox() {}
> 
>    void draw() {
>      ntuple->Draw("deltaE:mB");
> 
>      TBox _box(4,-4,5,0);
>      _box.Draw();
>    }
> };
> 
> Using this class on the command line with
> root [1] .L myBox.C
> root [2] myBox *box = new myBox();
> root [3] box->draw();
> 
> does draw the histogram but no box is drawn.
> 
> Finally I relized that I have to define the _box as pointer inside the
> class myBox to make it work:
> 
>      TBox *_box = new TBox(4,-4,5,0);
>      _box->Draw();
> 
> I don't understand why it worked in the first place (as macro), but
> does not work when I use it inside a class. Could somebody
> enlighten me?
> 
> Thanks a lot and best regards
> 
> Remi
> 
> 
> *********************************************************************
> Remigius K. Mommsen                 e-mail: mommsen@slac.stanford.edu
> University of California, Irvine       URL:      home.cern.ch/mommsen
> c/o SLAC                             voice:        ++1 (650) 926-3595
> 2575 Sand Hill Road #35             mobile:        ++1 (650) 387-1402
> Menlo Park, CA 94025, US               fax:        ++1 (650) 926-8522
> *********************************************************************
> 



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