Hello,
I would like to unify “named” and “anonymous” scripts in a single file.
Named Script
Feeding Sources Files To ROOT: C++ Scripts mentions named script, for instance:
void named()
{
int a=0;
}
named.C (30 Bytes)
This type may be relevant for testing purpose because:
- it accepts arguments
- variables scope is well-defined:
In a named script, the objects created on the stack are deleted when the function exits.
Example:
$ root named.C
------------------------------------------------------------------
| Welcome to ROOT 6.30/06 https://root.cern |
| (c) 1995-2024, The ROOT Team; conception: R. Brun, F. Rademakers |
| Built for linuxx8664gcc on Apr 04 2024, 12:44:07 |
| From tags/v6.30.06@v6.30.06 |
| With c++ (Ubuntu 9.4.0-1ubuntu1~20.04.2) 9.4.0 |
| Try '.help'/'.?', '.demo', '.license', '.credits', '.quit'/'.q' |
------------------------------------------------------------------
root [0]
Processing named.C...
root [1] a
input_line_11:2:3: error: use of undeclared identifier 'a'
(a)
^
Error in <HandleInterpreterException>: Error evaluating expression (a)
Execution of your code was aborted.
Anonymous Script
Let’s also consider “anonymous script”, e.g.:
{
int a=0;
}
anonymous.C (17 Bytes)
What is the official name?
Since the scope is global, they may be useful for:
- inspecting created objects
- drawing figures
Example:
$ root anonymous.C
------------------------------------------------------------------
| Welcome to ROOT 6.30/06 https://root.cern |
| (c) 1995-2024, The ROOT Team; conception: R. Brun, F. Rademakers |
| Built for linuxx8664gcc on Apr 04 2024, 12:44:07 |
| From tags/v6.30.06@v6.30.06 |
| With c++ (Ubuntu 9.4.0-1ubuntu1~20.04.2) 9.4.0 |
| Try '.help'/'.?', '.demo', '.license', '.credits', '.quit'/'.q' |
------------------------------------------------------------------
root [0]
Processing anonymous.C...
root [1] a
(int) 0
How to unify them?
I naively tried to merge these 2 types:
#if defined(FUNCTION_INTERFACE)
void merged()
#endif
{
int a=0;
}
merged.C (70 Bytes)
But when the macro is not defined, I get an error:
$ root -q merged.C
------------------------------------------------------------------
| Welcome to ROOT 6.30/06 https://root.cern |
| (c) 1995-2024, The ROOT Team; conception: R. Brun, F. Rademakers |
| Built for linuxx8664gcc on Apr 04 2024, 12:44:07 |
| From tags/v6.30.06@v6.30.06 |
| With c++ (Ubuntu 9.4.0-1ubuntu1~20.04.2) 9.4.0 |
| Try '.help'/'.?', '.demo', '.license', '.credits', '.quit'/'.q' |
------------------------------------------------------------------
Processing merged.C...
In file included from input_line_8:1:
/.../merged.C:4:1: error: expected unqualified-id
{
^
Asking for help
How would you combine those 2 types?
Setup
ROOT v6.30/06
Built for linuxx8664gcc on Apr 04 2024, 12:44:07
From tags/v6.30.06@v6.30.06
With c++ (Ubuntu 9.4.0-1ubuntu1~20.04.2) 9.4.0
Compiled from source
Additional context
Calling named script
One solution might be to call a named script from an anonymous script, .e.g.:
library.C
:
void modify(size_t& num_points) {
num_points = 3;
}
void library() {}
main.C
:
{
size_t num_points {0};
modify(num_points);
}
Test:
root {library,main}.C
------------------------------------------------------------------
| Welcome to ROOT 6.24/06 https://root.cern |
| (c) 1995-2021, The ROOT Team; conception: R. Brun, F. Rademakers |
| Built for linuxx8664gcc on Sep 02 2021, 14:20:23 |
| From tags/v6-24-06@v6-24-06 |
| With |
| Try '.help', '.demo', '.license', '.credits', '.quit'/'.q' |
------------------------------------------------------------------
root [0]
Processing .../library.C...
Processing .../main.C...
root [2] num_points
(unsigned long) 3
But it is tedious.
Environment variables
An alternative would be to use environment variables, e.g. uranie/dataserver/show_parallel.C · v6-30_uranie · Uranie CEA / ROOT / roottest · GitLab
{
string figure = gSystem->Getenv("FIGURE");
string input_file = gSystem->Getenv("INPUT_FILE");
string configuration_file = gSystem->Getenv("CONFIGURATION_FILE");
[...]
}
However, I must pay attention to side effects.