How do I tell PyRoot to ignore command line options?

Hello,

I’m trying to use Python’s optparse. The problem is that Root insists on looking at the command line options as well:

import optparse
import ROOT
import time

wjetsFile = ROOT.TFile.Open ("wjetsAna.root");

# Setup options parser
parser = optparse.OptionParser \
         ("usage: %prog [options]\n" \
          "Converts Guofan's templates to Charles' templates.")
parser.add_option ('--output', dest='output', type='string',
                   default='output.root',
                   help="output file name (%DEFAULT default)")
parser.add_option ('--dontAdd', dest = 'dontAdd', action='store_true',
                   default=False,
                   help="Do not add similar templates together")
options, args = parser.parse_args()

And I get:

cplager@njhu04> ./guofanTemplates.py --help
Usage: python [-l] [-b] [-n] [-q] [dir] [[file:]data.root] [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

usage: guofanTemplates.py [options]
Converts Guofan's templates to Charles' templates.

options:
  -h, --help       show this help message and exit
  --output=OUTPUT  output file name (%DEFAULT default)
  --dontAdd        Do not add similar templates together

Note that if I don’t assign any root objects before optparse, I only get the help option from optparse as I should.

So, how do I tell Root not to look at the command line options?

Cheers,
Charles

I had the same problem before, and I used to manually clean up sys.argv before calling ROOT objects:

import sys ... sys.argv = []

Hi,

best solution is to parse the options before touching anything inside module ROOT (other than gROOT.SetBatch(), that is), then wipe out sys.argv after you’re done, then open the wjetsFile etc. It is TApplication that gets fed sys.argv, and it gets created upon first touching the ROOT module. Something like:[code]import optparse
import ROOT
import time, sys

Setup options parser

done with options

sys.argv = []

wjetsFile = ROOT.TFile.Open (“wjetsAna.root”);[/code]
HTH,
Wim

[quote=“wlav”]Hi,

best solution is to parse the options before touching anything inside module ROOT (other than gROOT.SetBatch(), that is), then wipe out sys.argv after you’re done, then open the wjetsFile etc. It is TApplication that gets fed sys.argv, and it gets created upon first touching the ROOT module. Something like:[code]import optparse
import ROOT
import time, sys

Setup options parser

done with options

sys.argv = []

wjetsFile = ROOT.TFile.Open (“wjetsAna.root”);[/code]
HTH,
Wim[/quote]

Thanks. That is the solution I came to. But this really isn’t ideal. In my case I wanted to have global variables that are Root objects and run the parsing routine only if the script is called as a program (if name == “main”:). Because of this, I have to jump through hoops to get this to work right.

Would it be possible to add some sort of flag telling Root not to look at the command line options in the future?

thanks,
Charles

Charles,

of course. but which one would be preferable, a short flag with risk of clashing with one that the user wants, or a long one that takes more typing?

I’m inclined to go with a long one, as the user can still put the flag inside the script, and so eliminate the typing problem.

Also, ‘-b’ would have to be set explicitly (i.e. gROOT.SetBatch()) if options are ignored otherwise.

Cheers,
Wim

[quote=“wlav”]Charles,

of course. but which one would be preferable, a short flag with risk of clashing with one that the user wants, or a long one that takes more typing?

I’m inclined to go with a long one, as the user can still put the flag inside the script, and so eliminate the typing problem.

Also, ‘-b’ would have to be set explicitly (i.e. gROOT.SetBatch()) if options are ignored otherwise.

Cheers,
Wim[/quote]

I was envisioning something like ‘gROOT.IgnoreCommandLineOptions()’ and that as long as this was called before any Root module was touched everything would be golden.

Thanks!
Charles

Hi,

yes, that won’t interfere, but it doesn’t seem like a too good idea to add a function to gROOT. It did remind me that there is such a thing as PyConfig in module ROOT, which can also be used without anything actually happening on the C++ side, so I opted for:import ROOT ROOT.PyConfig.IgnoreCommandLineOptions = Truewhich is in SVN HEAD now. Internally, it just swaps sys.argv with an empty list before creating the TApplication, and back when it is done with that.

HTH,
Wim

[quote=“wlav”]Hi,

yes, that won’t interfere, but it doesn’t seem like a too good idea to add a function to gROOT. It did remind me that there is such a thing as PyConfig in module ROOT, which can also be used without anything actually happening on the C++ side, so I opted for:import ROOT ROOT.PyConfig.IgnoreCommandLineOptions = Truewhich is in SVN HEAD now. Internally, it just swaps sys.argv with an empty list before creating the TApplication, and back when it is done with that.

HTH,
Wim[/quote]

Beautiful! Thanks!

This posting was accidental and has been removed.

Hi,

no, it still works exactly the same. How does it fail for you?

Also, “IgnoreCommandLineOptions” and “SetBatch” are two completely unrelated things, so I’m not sure how you believe the latter substitutes for the former?

Cheers,
WIm

Sorry. This reply somehow ended up in the wrong thread.
I will edit the above text to remove the misleading information.