Totem command line parser
Using Totem command line parser (totemCLparser.py) one may pass arguments to a configuration file directly from a command line, e.g.:
cmsRun test_cfg.py +nevents=1 +beta=2p5 +energy=3500
This parser is based on python modul argparse (see
http://code.google.com/p/argparse/
) .
The Problem
The problem is that if we try to pass arguments to a configuration file (test_cfg.py) from command line like:
cmsRun test_cfg.py arg1 arg2 ...
then all arguments arg1, arg2, ... are passed by cmsRun to the configuration file test_cfg.py but only
if they don't have dash ('-') in prefix, e.g. -x or --xxx are not aloved,
because options with a dash prefix are recognized by cmsRun and not by
test_cfg.py. So we can't do
something like:
cmsRun test_cfg.py -nevents=10
which would be realy nice... The arg1, arg2, etc. may be accesed in test_cfg.py only using sys.argv:
import sys
for arg in sys.argv:
print arg, "\n"
but then we have to do all the arguments handling on our own which is really tedious...
Elegant Solution
Using purely python modul argparse (see
http://code.google.com/p/argparse/
) we
may avoid many problems because it is posible to configure also prefix '+'
for arg options instead of just dash ('-'). This aproach is used by totemCLparser which predefine some options such as nevents, energy, optics, help (options which are common for all of us). So it is possible from command line to do something like:
cmsRun -p test_cfg.py +nevents=10 +energy=3500 +beta=2.5
these +options are correctly passed to test_cfg.py as options and possible
-options are passed to cmsRun. Definition of the first +option in totemCLparser.py just like:
parser.add_argument(
"+nevents", # option name(s) used from command line
dest = 'nevents', # one common name
default = -1, # dafault value
type = int, # support for types
action = "store",
help = "number of events to be processed, as default all events from a source are processed"
)
so it is really easy to specify default value for this option, type, define help and much more (all one needs...).
Help for all options is automatically generated and may be printed to a
screen also with usage etc - as it is common in unix word...
For details and possible options see documentation of argparse
http://code.google.com/p/argparse/
, mainly the pdf which is there:
http://argparse.googlecode.com/files/argparse-1.1.pdf
Usage for user of totemCLparser
Into your configuration file put:
from Configuration.TotemCommon.totemCLParser import parser
# add new option if you want
parser.add_argument(
"+nplanes",
dest = 'nplanes',
default = 3,
type = int,
action = "store",
help = "minimum number of planes needed for fitting"
)
# set new default energy and optics (overwrite default values in totemCLparser.py) if you want/need
parser.set_defaults(nevents=-1, beta="2p5", energy=3500)
# here the parser reads and sets values passed from command line (or just sets the default values)
args = parser.parser_args()
# prints all arguments and their values handled by the parser
print "Parsed arguments = ", args
...
# now you may set the number of events like this:
process.maxEvents = cms.untracked.PSet(
input = cms.untracked.int32(args.nevents)
)
# print value of newly defined argument "nplanes"
print 'Number of planes', args.nplanes
...
--
JiriProchazka - 25-Mar-2010