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).
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
sys.argv += [ ‘-h’ ]
Usage: python [-l] [-b] [-n] [-q] [dir] [file1.C … fileN.C]
-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
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:
from optparse import OptionParser
grootargs = 
def callback_rootargs(option, opt, value, parser):
parser = OptionParser('drawemptycanvas.py [-b] [-n]')
help="run in batch mode wihtout graphics.")
help="do not execute logon and logoff macros as"
"specified in .rootrc")
(options, args) = parser.parse_args()
sys.argv = grootargs
from ROOT import TCanvas
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?
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.