Trouble Trying to Run a Function from Cmdline

Hello all,

I need some help trying to run a script in a backgrounded root. This script has two libraries that it needs to load, and then I need to call a function and give it two arguments. It runs fine in the interpreter but I couldn’t figure out how to get it running in the background.

I’ll try to elaborate below.

So for some ‘myscript.C’

#include <library1>
#include <library2>

void myscript(char arg1, char arg2)
{}

Now, I’ve tried running this in command line like >root -b 'myscript.C("arg1","arg2")'but to no avail, as the libraries didn’t load, so the program doesnt run.

So to circumvent that I tried

void run(){
gROOT->ProcessLine(".L library1");
gROOT->ProcessLine(".L library2");
gROOT->ProcessLine(".L myscript.C");
}

And then running this as > root -b 'run.C' but then that doesn’t really help me run it in the background, as I still need to manually call the myfunc, and give it arguments.

Any gurus care to lend a hand? :smiley:

Instead of “.L myscript.C” try “.x myscript.C” (assuming that the function that you want to call is also named “myscript”):
gROOT->ProcessLine( “.x myscript.C(“arg1”, “arg2”)” );

BTW. I’m almost sure you wanted to say (in “myscript.C”):
void myscript(const char *arg1, const char *arg2)

Awesome, that is a step in the right direction! And yes, I did mean to use const char* in myscript.

So the script doesn’t run with .x, so I have it load a pre-compiled script before hand (myscript.so). Heres what the script now reads:

void run(const char* arg1, const char* arg2){
gROOT->ProcessLine(".L library1");
gROOT->ProcessLine(".L library2");
gROOT->ProcessLine(".L myscript.so");
gROOT->ProcessLine("myscript(\"arg1\",\"arg2\");
}

However, when arg1 and arg2 are passed to the myscript function, myscript returns with a debug statement. Now, as far as I can tell it has something to do with not being able to read arg1 properly. To further specify, arg1 is a string that would be written similarly to “blah_U238_556k_N00.root”. The debug code ends the script if the position of “_N” is at the end of the string or if position<1 or if the length of arg 1<pos+2.

Does this have anything to do with how a string would be passed form command line into root? And yes, I did check for typos!

Instead of:
gROOT->ProcessLine("myscript(“arg1”,“arg2”);
simply try:
myscript(arg1, arg2);

Oh wow, that worked like a charm! Thank you very much for your help!