Generally, there is a problem: how to run the ‘root’ from shell command line,
and pass args to the application invoked via ‘root’ - usually it is more
convenient then typing-in these args as answers to questions if the shell
allows recalling few previous commands and making corrections in them.
At least some versions of ‘root’ (I guess sufficiently new ones)
have functions Argc(), Argv() in TApplication class. Example:
{ int argc, ai;
argc=gApplication->Argc();
for(ai=0; ai<argc; ai++) printf("a[%d]=%s\n", ai,
gApplication->Argv(ai));
}
Or use const char **argv=gApplication->Argv();
to have argv like in usual C.
But, to make things harder, the output of the example code is like:
$ root -l -n -b -q args_n1.C This is a nice cat.
Warning in <TApplication::GetOptions>: macro This not found
Warning in <TApplication::GetOptions>: macro is not found
Warning in <TApplication::GetOptions>: macro a not found
Warning in <TApplication::GetOptions>: macro nice not found
Warning in <TApplication::GetOptions>: macro cat. not found
root [0]
Processing args_n1.C...
a[0]=/home/mkk/root/bin/root.exe
a[1]=-splash
a[2]=-l
a[3]=-n
a[4]=-b
a[5]=-q
a[6]=args_n1.C
a[7]=This
a[8]=is
a[9]=a
a[10]=nice
a[11]=cat.
So one needs to guess where is ‘argv[0]’ - the ‘args_n1.C’ in this case.
Also, these args specified are attempted to recognize as macro names.
In older versions of ‘root’ one may get these args from /proc/self/cmdline
(in Linux only), using the following code produces the same result:
{ char argbuf[2048], *argv[64];
FILE *cfd=fopen("/proc/self/cmdline","r");
int ai, ao, argc, abl=fread(argbuf,1,sizeof(argbuf),cfd);
fclose(cfd);
for(ao=argc=0; ao<abl && argc<64; argc++, ao++)
ao+=strlen(argv[argc]=argbuf+ao);
if(argc<64) argv[argc]=0;
for(ai=0; ai<argc; ai++) printf("a[%d]=%s\n", ai, argv[ai]);
}
Names of files processed by ‘root’, like the ‘args_n1.C’ in the example,
can be obtained using the following code:
{ Int_t i,f,l; TObjArray *ifl; TObject *obj;
ifl=gApplication->InputFiles();
f=ifl->LowerBound(); l=ifl->GetLast();
printf("low:%d, last:%d; %d entries\n", f, l, ifl->GetEntries());
for(i=f; i<=l; i++) {
obj=ifl->At(i); printf("if[%d]=%s\n", i, obj->GetName());
}
}
However, I found passing arguments via environment variables to be
more convenient, as it does not interfere with TApplication::GetOptions
gSystem->Getenv(name);
gets a variable value as a string. If the shell is ‘bash’, these arguments
can be specified in the command line used to invoke root, like: a=1 b=2 f=datafile.txt root my_root_app.C
to pass ‘a’, ‘b’ and ‘f’ arguments to the application (the bash is commonly used on Linux,
and some antiquted version is available for Windows, to be used in terminal window).
img_view.C (4.79 KB)