Accessing gPad in a macro

Hi Rooters

how can I access a global variable in a macro? For instance the following minimal script doesn’t work:

root [0] .L doIt.C
root [1] doIt()
Error: Symbol gPad is not defined in current scope doIt.C:10:
*** Interpreter error recovered ***

where doIt.C is the following script:

#include "TPad.h"
#include "TVirtualPad.h"
#include <iostream>

void doIt(void)

	if (gPad) {
		std::cout << "OK" << std::endl;
	else {
		std::cout << "Bad" << std::endl;

	return ;

I am missing either an iclude file, either a library that was not loaded?


Using AClic it works:

root [0] .L doIt.C++
Info in <TUnixSystem::ACLiC>: creating shared library /home/couet/roottest/./
root [1] doIt()
root [2]


You can try to use this syntax: if (TVirtualPad::Pad()) { std::cout << "OK" << std::endl; } else { std::cout << "Bad" << std::endl; }


The ROOT global variable (gPad for example) are currently available in the interpreter only when they are not equal to zero. So you ought to use the syntax proposed by Bertrand.


Thanks a lot.

I will keep Bertrand and Olivier 's sugestions. Especially Olivier’s. The reason is that my script was a bit more complex, something like:

void doIt()
     Options options = ... ;
     TH2D* histo = drawHisto(options)   ; // a compiled function (in a shared library) that creates a histogram, draws it on a new TCanvas,  and returns a pointer on the histogram drawn
    gPad->BlahBlah();   // I wanted to do something on the TCanvas created before


Using AClic got rid of the problem. I can also retrieve the TCanvas through its name ang gROOT (available in scripts).

Thanks for your suggestions