gROOT pointer in C++

Hello ROOTers
One of the neat things about ROOT is to be able to fetch objects by name after they have been drawn on a canvas. Using scripts, this is easily done by

I am working in my C++ program and I would like to get access to this pointer. Any ideas how I could do that? Would this pointer be global, in the same way as gPad, where it can be accessed by any object I instantiate in run time (I think the answer is yes… I am only double checking)?
Thanks,

You can store any named object in gROOT->GetListOfSpecials() with
gROOT->GetListOfSpecials()->Add(myobject);
and retrive it later with gROOT->FindObject.

Rene

1 Like

This is really useful Rene. Thanks.

However, I am still having a bit of trouble when trying handling the gROOT pointer:

I have the following code in my main program:

[code]
#include “TROOT.h”
#include “TRint.h”

TROOT root(“Rint”, “The ROOT Interactive Interface”);

int main(int argc, char **argv){
//TApplication theApp(“APIanalyzer”, &argc, argv);
TRint *theApp = new TRint(“ROOT example”, &argc, argv);
APIanalyzer mainWindow(gClient->GetRoot(),600,500,theApp.Argc(), theApp.Argv());

theApp->Run();
return 0;
}[/code]

I was hoping this last main will allow me to have access to the gROOT pointer but it did not work. What am I missing?
Cheers,

Remove the line
TROOT root(“Rint”, “The ROOT Interactive Interface”);

The TROOT constructor is automatically created as soon as you link with libCore.

Rene

I found my problem. I was accessing the gROOT pointer through one of my objects that gets indirectly created in the main function. Because of the way how I declare my header files, my objects was not seeing the declaraition of the TROOT header file. I solved my problem including

#include "TROOT.h" #include "TRint.h"

in my object’s header file.
Thanks for your help!
Cristian

[quote=“brun”]You can store any named object in gROOT->GetListOfSpecials() with
gROOT->GetListOfSpecials()->Add(myobject);
and retrive it later with gROOT->FindObject.

Rene[/quote]

Rene,
I have a list of graphs in a Canvas with multiples pads. I have assigned a unique ID (via TGraph::SetuniqueID(int) ) and then I have added these graphs to the list of specials as you suggested. If I want to retrieve a gr based on its uniqueID, how can I retrieve the list of specials so to iterate through this list to find such ID (hence the gr of interest)???

Thanks,

[quote] If I want to retrieve a gr based on its uniqueID, how can I retrieve the list of specials so to iterate through this list to find such ID (hence the gr of interest)??? [/quote]The only way is to iterate through the list; for example: TIter next(gROOT->GetListOfSpecials); TGraph *graph; while ( ( graph = (TGraph*) next() ) ) { if ( graph->GetUniqueID() == what_we_are_looking_for ) { break; // exit the loop, leave graph pointing to the desired result } } if (graph) { // Use the correct graph }

Cheers,
Philippe.