CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
text2workspace.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 import re
3 from sys import argv, stdout, stderr, exit, modules
4 from optparse import OptionParser
5 
6 # import ROOT with a fix to get batch mode (http://root.cern.ch/phpBB3/viewtopic.php?t=3198)
7 argv.append( '-b-' )
8 import ROOT
9 ROOT.gROOT.SetBatch(True)
10 argv.remove( '-b-' )
11 
16 
17 parser = OptionParser(usage="usage: %prog [options] datacard.txt -o output \nrun with --help to get list of options")
19 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)")
20 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)")
21 (options, args) = parser.parse_args()
22 
23 if len(args) == 0:
24  parser.print_usage()
25  exit(1)
26 
27 options.fileName = args[0]
28 if options.fileName.endswith(".gz"):
29  import gzip
30  file = gzip.open(options.fileName, "rb")
31  options.fileName = options.fileName[:-3]
32 else:
33  file = open(options.fileName, "r")
34 
35 ## Parse text file
36 DC = parseCard(file, options)
37 
38 ## Load tools to build workspace
39 MB = None
40 if DC.hasShapes:
41  MB = ShapeBuilder(DC, options)
42 else:
43  MB = CountingModelBuilder(DC, options)
44 
45 ## Load physics model
46 (physModMod, physModName) = options.physModel.split(":")
47 __import__(physModMod)
48 mod = modules[physModMod]
49 physics = getattr(mod, physModName)
50 if mod == None: raise RuntimeError, "Physics model module %s not found" % physModMod
51 if physics == None or not isinstance(physics, PhysicsModel):
52  raise RuntimeError, "Physics model %s in module %s not found, or not inheriting from PhysicsModel" % (physModName, physModMod)
53 physics.setPhysicsOptions(options.physOpt)
54 ## Attach to the tools, and run
55 MB.setPhysics(physics)
56 MB.doModel()
def addDatacardParserOptions