CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_14/src/HiggsAnalysis/CombinedLimit/scripts/text2workspace.py

Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 import re
00003 from sys import argv, stdout, stderr, exit, modules
00004 from optparse import OptionParser
00005 
00006 # import ROOT with a fix to get batch mode (http://root.cern.ch/phpBB3/viewtopic.php?t=3198)
00007 argv.append( '-b-' )
00008 import ROOT
00009 ROOT.gROOT.SetBatch(True)
00010 argv.remove( '-b-' )
00011 
00012 from HiggsAnalysis.CombinedLimit.DatacardParser import *
00013 from HiggsAnalysis.CombinedLimit.ModelTools import *
00014 from HiggsAnalysis.CombinedLimit.ShapeTools import *
00015 from HiggsAnalysis.CombinedLimit.PhysicsModel import *
00016 
00017 parser = OptionParser(usage="usage: %prog [options] datacard.txt -o output \nrun with --help to get list of options")
00018 addDatacardParserOptions(parser)
00019 parser.add_option("-P", "--physics-model", dest="physModel", default="HiggsAnalysis.CombinedLimit.PhysicsModel:defaultModel",  type="string", help="Physics model to use. It should be in the form (module name):(object name)")
00020 parser.add_option("--PO", "--physics-option", dest="physOpt", default=[],  type="string", action="append", help="Pass a given option to the physics model (can specify multiple times)")
00021 (options, args) = parser.parse_args()
00022 
00023 if len(args) == 0:
00024     parser.print_usage()
00025     exit(1)
00026 
00027 options.fileName = args[0]
00028 if options.fileName.endswith(".gz"):
00029     import gzip
00030     file = gzip.open(options.fileName, "rb")
00031     options.fileName = options.fileName[:-3]
00032 else:
00033     file = open(options.fileName, "r")
00034 
00035 ## Parse text file 
00036 DC = parseCard(file, options)
00037 
00038 ## Load tools to build workspace
00039 MB = None
00040 if DC.hasShapes:
00041     MB = ShapeBuilder(DC, options)
00042 else:
00043     MB = CountingModelBuilder(DC, options)
00044 
00045 ## Load physics model
00046 (physModMod, physModName) = options.physModel.split(":")
00047 __import__(physModMod)
00048 mod = modules[physModMod]
00049 physics = getattr(mod, physModName)
00050 if mod     == None: raise RuntimeError, "Physics model module %s not found" % physModMod
00051 if physics == None or not isinstance(physics, PhysicsModel): 
00052     raise RuntimeError, "Physics model %s in module %s not found, or not inheriting from PhysicsModel" % (physModName, physModMod)
00053 physics.setPhysicsOptions(options.physOpt)
00054 ## Attach to the tools, and run
00055 MB.setPhysics(physics)
00056 MB.doModel()