It is very important to note that the previous example will not work as expected if copied into a ROOT macro. The reason is that all objects are created on the stack, hence they will disappear as soon as the function creating them returns. The graphics would hence appear for a while and then disappear immediately! This is OK if you know this and save the result inside the macro, by adding commands like the following, saving a PDF file:
root [] c1.Print("c1.pdf", "pdf"); Info in <TCanvas::Print>: pdf file c1.pdf has been created
However, in most cases this is not what you desire, because you usually want to use the macro to produce some plot, then you may need to think about it for a while, before adding few lines of code and reprocessing the macro to see the new result.
To extend the lifetime of any object beyond the duration if the block in which it is created, one needs to create it on the heap. To dynamically allocate a variable, the new C++ operator needs to be used. The example above needs to be rewritted as follows:
root [] TCanvas* c1 = new TCanvas("c1", "First canvas", 400, 300); root [] c1->Divide(2,3); root [] c1->cd(4); root [] TLatex* l = new TLatex(0.1,0.4,"C(x) = d #sqrt{#frac{2}{#lambdaD}} #int^{x}_{0}cos(#frac{#pi}{2}t^{2})dt"); root [] l->SetTextSize(0.15); root [] l->Draw(); root [] c1->Print("c1.pdf", "pdf");
All objects created on the heap must be explicitly destroyed by the programmer when they are not needed any more. If you don't use delete to destroy them, they will remain in the memory space handled by ROOT. This means that the lines above, when copied into a macro, produce results that survive the end of the macro. They will be destroyed when exiting ROOT.