Root has changed: how to use global variables?

This is Mac OS El Capitan (10.11.6).

In Root 5.34/30 These two files work when loaded sequentially, but in 6.12/06 they both fail. The intent is to have a global variable ‘plots’ which contains an array of pointers to all plots – it is initialized in file test1.C; the test2.C file then creates a plot.

So how do I do this in newer versions? What has changed? – this looks like ordinary C++ to me (with Root extensions, such as predefined stdio.h and T* classes).

Note that adding “#include <stdio.h>” to test1.C does not fix its error. Note also that if I type its two lines manually and then do “.X test2.C”, it works in both versions.

Tom Roberts

**** HERE ARE THE TWO FILES ****

tjrob:tmp tjrob$ cat test1.C
// test1.C
TObjArray plots;    // array of pointers to plots
printf("OK\n");

tjrob:tmp tjrob$ cat test2.C
// test2.C
void test2() {
    double x[] = {0,0.001,0.002,0.003,0.005,0.006,0.0072,0.008,0.0081,};
    double y[] = {2.5366,3.34647,4.48096,5.97079,11.7754,17.9084,36.58,73.6046,86.4753,};
    TCanvas *canvas=new TCanvas("Test Plot 0","Test Plot 0");
    TGraph *graph  = new TGraph(9,x,y);
    plots.AddLast(graph);
    graph->Draw("APL");
    canvas->Update();
}

*** HERE IS ROOT 5.34/30 WHICH WORKS ****

tjrob:tmp tjrob$ ~/root_v5.34.30/bin/root
... splash screen ...
ROOT 5.34/30 (v5-34-30@v5-34-30, Apr 23 2015, 18:31:46 on macosx64)
CINT/ROOT C/C++ Interpreter version 5.18.00, July 2, 2010
root [0] .L test1.C
root [1] .X test2.C
*** PLOT WINDOW APPEARS AS DESIRED ***
root [2] .q

**** HERE IS ROOT 6.12/06 WHICH FAILS ****

tjrob:tmp tjrob$ ~/root_v6.12.06/bin/root
... splash screen ...
root [0] .L test1.C
In file included from input_line_10:1:
/Users/tjrob/tmp/test1.C:3:1: error: C++ requires a type specifier for all declarations
printf("OK\n");
^
root [1] .X test2.C
In file included from input_line_11:1:
/Users/tjrob/tmp/test2.C:7:1: error: use of undeclared identifier 'plots'
plots.AddLast(graph);
^
root [2] .q

It works if you wrap you code in a function, like

#include "TObjArray.h"

void test1() {
TObjArray plots; // array of pointers to plots                                                                                                                                                                                               
printf("OK\n");

}

And if you put in the proper includes, you can even compile it with .L test.C+

Yes, except the global (plots) must be defined in global scope, and the file is loaded with .X (not .L). Fortunately this also works in the older version of Root.

Thanks.

Tom Roberts

I see. Well, there is an even easier solution, namely to remove the printf. Without it, it works for me on ROOT6.

{
  // test1.C ... an unnamed macro
  TObjArray plots;    // array of pointers to plots
  printf("OK\n");
}

The actual code is considerably more complicated than my
simplified example posted here. The code is generated by another
program controlling Root via pipes, and the printf is needed for
synchronization between the two programs.

  Putting the printf into a function with the same name as the file

and loading the file via .X makes it work.

Tom Roberts

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.