Cannot load/execute interpreted macro with ROOT6/CLING, worked fine with ROOT5/CINT

I am trying to load/execute a non-compiled (interpreted) macro that worked fine in ROOT5 with CINT, and now fails in ROOT6/CLING. The macro is used to plot many existing TGraphErrors objects in a file. With ROOT5/CINT I would do the following:

root[0] .L draw_results.C
root[1] draw_results(“file.root”,…additional arguments);

In ROOT5/CINT, the macro itself accesses many TGraphErrors objects by their pre-existing identifiers as they were written to the TFile without explicitly declaring these pointers or loading them from the file using e.g., TFile::GetObject(). Here is a snippet of the relevant code:

void draw_results(const char* filename="graphs_temp.root", double Rmin=-2., double Rmax=2., int showdipoleflag=0){

TFile* Fin = new TFile(filename,"READ");

Pt_Th_tgt_p->Draw("AP");
//Where "Pt_Th_tgt_p" is the name/identifier of a TGraphErrors object that exists in the ROOT file pointed to by Fin. 
}

This shortcut prevents me from having to write the following two extra lines of code for each of the dozens of individual graphs I want to draw:

TGraphErrors *Pt_Th_tgt_p;
Fin->GetObject("Pt_Th_tgt_p",Pt_Th_tgt_p);

This was a very useful feature of CINT/ROOT5. When I try to load this macro using CLING/ROOT6, I just get lots of “undeclared identifer” errors:

root[0] .L draw_results.C

Is there anyway around this in ROOT6/CLING without rewriting this macro from scratch/adding the explicit declarations for all the graphs? According to your ROOT6 documentation, CLING should still have this feature!

Hi,

one of the most powerful features of ROOT6 is an interpreter which is based on a production grade compiler. As such it guarantees that C++, even the latest standards, are supported. It also guarantees that future standards are supported too.
Being a compiler makes it picky about the correctness of the code. The usage of variables which are not defined but are names of objects in a TFile is not part of the C++ language and is not supported in macros like yours. On the other hand it is at the ROOT prompt, where the user experience is enhanced with respect to the one which could be granted by the mere interpretation of the C++ code.

If you want to loop over objects within a root file, you can programmatically do so and this will avoid you to write two extra lines of code for each of the dozens of individual graphs you want to draw. See for example:

Cheers,
Danilo

Dear Danilo,

Thanks for your reply. I interpret this to mean that my “lazy” way of accessing pre-existing objects in a file that worked with ROOT5/CINT will no longer be supported with ROOT6/CLING. This is good to know for me and for my future “scripting”. I want to start using ROOT6 exclusively, but I will need to rewrite a lot of scripts now! I will check out your suggestion of how to loop on objects in a ROOT file.

Best,
Andrew

Hi Andrew,

I would not call your way “lazy”: you were making the best of the tools at disposal to achieve the results you are interested in. In this particular case, you were leveraging a feature of CINT which was abandoned once moving the new interpreter, Cling. I hope you’ll have a chance to appreciate the many improvements in the new ROOT, not only the differences.
In any case, if you have issues to solve do not hesitate to post on this forum.

Cheers,
Danilo