Hi, I'm dumb

Okay so this is clearly a no-brainer but I am not seeing it. I’m new to this, and I’m just going through the user guide, but why-o-why does this:

TF1 f1(“afunc”,“sin(x)”,0,10);
f1.Draw();

correctly display a canvas with a sine function ONLY WHEN TYPED INTO THE INTERPRETER? If I copy and paste this into a macro and then execute it, all I see is a blank canvas.

Thanks.

Hi Craigb,

f1 is probably defined in some scope block, which it leaves before it has a chance to be drawn. If a variable goes out of scope it is removed:

void myfunc{ // begin block TF1 f1("afunc","sin(x)",0,10); } // end scope block, object f1 removed
What you might want to do is create f1 on the heap:

void myfunc{ TF1* f1=new TF1("afunc","sin(x)",0,10); f1->Draw(); } // end scope block, pointer f1 removed, but object called "afunc" still around
This way only the pointer goes out of scope and is removed, but the object it points to will remain in memory. Yepp, that has nothing to do with root :-]
Axel.

Executing the macro

{
TF1 f1(“afunc”,“sin(x)”,0,10);
f1.Draw();
}

works for me ?

Hi Eddy,
it only works for unnamed macros, which are (almost) identical to enterting the code on the interpreter line.
Axel.

Thanks Axel, that did it.