Execute a shell command (calling a launcher) from ROOT

Holà,

ROOT 6.10/02, MAcOS

Inside a macro, I would like to execute a function from my .bashrc but ROOT write “command not found” .
(This function import libraries for executing GEANT4)

In root I’ve written:

gSystem->(“source ~/.bashrc”)
gSystem->(“Geant4104”)

//Where Geant4104 is the aforementioned function.

I hope I’ve been clear enough, thanks for helping me.

Hi Pierre,

which macro? A C++ one?

May we see the function please? What kind of function is it?

– none of these are expected to work, the syntax is incorrect. For the first one, it should be rather

gSystem->Exec("source ~/.bashrc")

at the very least (though it does not make much sense to me… ~/.bashrc gets executed by default anyway - why would you want to source it yourself?). The second one is meaningless - what are you trying to do here? Let’s forget about your incorrect syntax and suppose you already sourced your ~/.bashrc - why would you need to call some function from this file then?

Hi @yus, thanks for your answer.

Yes a C++ macro. Ok I will try to be more concise, my idea is to launch several time the “GEANT4” software, a simulation software, using a macro compiled and read by ROOT in C++.
For that I use :
gSystem->Exec("./geantEXE ./…/geant/Options/benchmark_cube_attenuation.txt");

'./geantEXE ’ is the command I use in the shell to call the launcher.
‘./…/geant/Options/benchmark_cube_attenuation.txt’ is a data set.

My main is then :

int main(){
    gSystem->Exec("./geantEXE ./../geant4/Options/benchmark_cube_attenuation.txt"); 
    return 0;
}

Now on from ROOT I type :

root [0] .L ~/Desktop/Pierre/ATTE/test.cpp
root [1] main()

And I get that error message :

sh: -c: line 1: syntax error: unexpected end of file
dyld: Library not loaded: @rpath/libCore.so
  Referenced from: /Users/Pierre/research/geant4/./geantEXE
  Reason: image not found

My question : why the command line works when directly written in the Shell, and not when written in the ROOT interpreter ?

Thanks

PS :

My bad, I did a bad copy/paste but I did use gSystem->Exec().

Well I think I understood my problem, I should have looked for “dyld: Library not loaded: @rpath/libCore.so” on internet.

Instead of doing

, why not simply name the function anything but main(), e.g.

$ cat callGeant.cxx
int callGeant(){
    gSystem->Exec("./geantEXE ./../geant4/Options/benchmark_cube_attenuation.txt"); 
    return 0;
}

and execute it like

root -l -b -q callGeant.cxx

? Isn’t it simpler?
The other question is why you chose C++ for it. This is a job a simple bash script will accomplish perfectly (and faster).

1 Like

I have the same error message, but thanks for the tip, that is for sure something that I will recall (root -l -b -q x.cxx).

You are totally right, I would better write a bash script, I will do that.

Why C++, it’s because my global idea is to do that :

for(several times){
modify my data set
launch Geant4
read result file
}

In other terms, I want to repeat an experiment, changing my initial conditions.

I coded a C++ function to modify conveniently my data set, and I was thinking to continue by writing a launch() function and a read_result) function.

Isn’t that the better way to do so ?

Maybe I should write a bash script for launching Geant4, and then call it from my C++ main function ?
Or the inverse, in the bash script to call my modify_data() function and my read_data() function ?

Thanks

Can you try the following:

  1. create a file aaa.cxx with the following content:
int aaa()
{
        gSystem->Exec("ls");
        return 0;
}

  1. Execute its code with
root -l -b -q aaa.cxx

– does it work? If not, then there is probably something wrong with your ROOT installation.

It heavily depends on how you have to modify your data set and to read the resulting file. If it’s possible to do with bash, I would go for it and forget about the C++ way.

1 Like

Just my 2 cents:
I would consider either your first (calling GEANT from ROOT, with just one C++ macro) or your last (calling ROOT and GEANT from a shell script) options. As for which one, for me it would depend on whether you want speed, convenience, etc.
The first way may be faster, as you would only be running ROOT one time, instead of 2 times for every iteration (but if you compile the functions and use a shell script they should run faster, so again, it depends!). On the other hand, using a shell script may be easier to read and maintain if you have to call many different programmes (ROOT, GEANT, others…) and/or functions.
In the end, if both ways work, it’s up to you :slight_smile:

1 Like

Well sadly it works…
I will investigate if it’s not about my OS version.

Okok I will go for bash, thanks a lot for your help !

Ok @dastudillo, duly noted.

I didn’t saw it that way but it make sense, thanks !

Last comment I guess :

All in all, as recommanded, I made a bash script and I had the same issue : dyld: Library not loaded: @rpath/libCore.so

It turns out that executing either a bash script or a ROOT macro, the shell creates a sub shell to run the code, a subshell where no librairies are imported.

Luckily from the bash script it has been quite easy to import the missing librairies.
Thanks.