Python options and ROOT options

Hi again,

another question: When I use the optionparser module, some options are already defined by ROOT, e.g. -h give always the ROOT help, not the help, that the optionparser normally returns.
Can I change that or switch that off (on the other hand I would loose the -b option from ROOT, so that I would not be able to switch to the batch mode).

Thanks Duc

Duc,

PyROOT propagates the CLI values from python’s sys.argv to ROOT. Hence, if you change that before importing ROOT, those will be the values it sees:[code]$ python

import sys
sys.argv += [ ‘-h’ ]
import ROOT
Usage: python [-l] [-b] [-n] [-q] [dir] [file1.C … fileN.C]
Options:
-b : run in batch mode without graphics
-n : do not execute logon and logoff macros as specified in .rootrc
-q : exit after processing command line macro files
-l : do not show splash screen
dir : if dir is a valid directory cd to it before executing

-? : print usage
-h : print usage
–help : print usage
-config : print ./configure options

$[/code]

Which isn’t exactly pretty, actually. :slight_smile: But you get the idea. Let me know if this suffices for you.

Best regards,
Wim

Hello Duc and Wim,

from Wims last post it is obvious, that one can prevent ROOT from intercepting the command line arguments, by hiding them during importing, like so:

import sys
tmpargv = sys.argv
sys.argv = [ '-b','-n' ]
#sys.argv=[] #also works, of course
import ROOT
sys.argv = tmpargv
from optparse import OptionParser

Then the Optionparser works as expected, and shows its help.

If one wants to have the -b and -n options in a PyROOT script, this can be achieved with a simple callback function for the OptionParser and importing ROOT after dealing with the other command line options. Like in this example script:

#!/usr/bin/env python
import sys
from optparse import OptionParser

grootargs = []
def callback_rootargs(option, opt, value, parser):
    grootargs.append(opt)

parser = OptionParser('drawemptycanvas.py [-b] [-n]')
parser.add_option("-b",action="callback",callback=callback_rootargs,
                  help="run in batch mode wihtout graphics.")
parser.add_option("-n",action="callback",callback=callback_rootargs,
                  help="do not execute logon and logoff macros as"
                  "specified in .rootrc")

(options, args) = parser.parse_args()

sys.argv = grootargs
from ROOT import TCanvas

TCanvas()

Indeed this is not pretty, but works very good.

Cheers
Matthias

Mattias,

thanks! Just a minor issue. This line:tmpargv = sys.argvshould of course be:tmpargv = sys.argv[:] (make full copy, not just take reference).

Cheers,
Wim

I am trying to find a way to run in batch mode (-b) with proper care of the options, and I found this thread. However, I found Matthias’ script (second one) above (with the callback function) doesn’t work. It behaves the same with or without -b option. Is anything about this changed since that post four years ago?

Chih-hsiang

Chih-hsiang,

not sure when the issue came in, but the TPyROOTApplication is expecting that the first part of sys.argv is the name of the program. That special case was made since there are situations when there is no program name (e.g. when run under mod_python in apache) and then a fake program name (“python”) is added for the benefit of TApplication.

Change the example program to init the grootargs with the program name:grootargs = ["drawemptycanvas.py"]and the rest should work as expected.

Cheers,
Wim