00001
00002 import re, os
00003 from sys import argv, stdout, stderr, exit
00004 from optparse import OptionParser
00005 from math import *
00006
00007
00008 argv.append( '-b-' )
00009 import ROOT
00010 ROOT.gROOT.SetBatch(True)
00011 argv.remove( '-b-' )
00012 parser = OptionParser(usage="usage: %prog [options] mass \nrun with --help to get list of options")
00013 parser.add_option("--xsbr", dest="xsbr", action="store_true", default=False, help="Use correct XS*BR for Higgs (if not enabled, just makes a flat copy)")
00014 parser.add_option("--ddir", dest="ddir", type="string", default=".", help="Path to the datacards")
00015 parser.add_option("--refmasse", dest="refmass", type="float", default="0", help="Reference mass to start from (default = nearest one, left in case of ties)")
00016 parser.add_option("--postfix", dest="postfix", type="string", default="", help="Postfix to add to datacard name")
00017 parser.add_option("--flavour", dest="flavour", type="string", default="BDT", help="flavour of datacard (vhbb_DC_ALL_<FLAVOUR>.<MASS.DECIMAL>.txt)")
00018 (options, args) = parser.parse_args()
00019 options.bin = True; options.stat = False
00020 if len(args) not in [1]:
00021 parser.print_usage()
00022 exit(1)
00023
00024 from HiggsAnalysis.CombinedLimit.DatacardParser import *
00025
00026 refmasses = range(110,135+1,5)
00027 mass = float(args[0])
00028
00029 if options.refmass == 0:
00030 options.refmass = refmasses[0]
00031 for m in refmasses[1:]:
00032 if abs(mass-m) < abs(mass-options.refmass):
00033 options.refmass = m
00034
00035 if mass in refmasses and options.postfix == "": raise RuntimeError, "Will not overwrite the reference masses"
00036
00037 xsbrR = { 'WH':1.0, 'ZH':1.0 }
00038 xsbr = { 'WH':1.0, 'ZH':1.0 }
00039 if options.xsbr:
00040 def file2map(x):
00041 ret = {}; headers = []
00042 for x in open(x,"r"):
00043 cols = x.split()
00044 if len(cols) < 2: continue
00045 if "mH" in x:
00046 headers = [i.strip() for i in cols[1:]]
00047 else:
00048 fields = [ float(i) for i in cols ]
00049 ret[fields[0]] = dict(zip(headers,fields[1:]))
00050 return ret
00051 path = os.environ['CMSSW_BASE']+"/src/HiggsAnalysis/CombinedLimit/data/";
00052 whXS = file2map(path+"YR-XS-WH.txt")
00053 zhXS = file2map(path+"YR-XS-ZH.txt")
00054 br = file2map(path+"YR-BR1.txt")
00055 xsbrR['WH'] = whXS[options.refmass]['XS_pb'] * br[options.refmass]['H_bb']
00056 xsbr ['WH'] = whXS[mass ]['XS_pb'] * br[mass ]['H_bb']
00057 xsbrR['ZH'] = zhXS[options.refmass]['XS_pb'] * br[options.refmass]['H_bb']
00058 xsbr ['ZH'] = zhXS[mass ]['XS_pb'] * br[mass ]['H_bb']
00059 print "Will interpolate %g from %g (XS*BR ratio: %.3f for WH, %.3f for ZH)" % (mass, options.refmass, xsbr['WH']/xsbrR['WH'], xsbr['ZH']/xsbrR['ZH'])
00060 else:
00061 print "Will copy %g from %g" % (mass, options.refmass)
00062
00063 fileR = options.ddir+"/%g/vhbb_DC_ALL_%s.%.1f.txt" % (options.refmass, options.flavour, options.refmass)
00064 options.fileName = fileR; options.mass = options.refmass;
00065 DCR = parseCard(open(fileR,"r"), options)
00066
00067 obskeyline = DCR.bins;
00068 obsline = [str(DCR.obs[b]) for b in DCR.bins];
00069 cmax = 5;
00070 keyline = []; expline = []; systlines = {}; systlines2 = {}
00071 signals = []; backgrounds = []; shapeLines = [];
00072 paramSysts = {}; flatParamNuisances = {}
00073 for (name,nf,pdf,args,errline) in DCR.systs:
00074 systlines[name] = [ pdf, args, errline, nf ]
00075
00076 for b,p,sig in DCR.keyline:
00077 rate = DCR.exp[b][p]
00078 if p == "VH":
00079 pTrue = "WH" if b[0] == "W" else "ZH";
00080 rate = rate * xsbr[pTrue]/xsbrR[pTrue]
00081 keyline.append( (b, p, DCR.isSignal[p]) )
00082 expline.append( "%.4f" % rate )
00083
00084 xfile = open(options.ddir+"/%g/vhbb_DC_ALL_%s%s.%.1f.txt" % (mass, options.flavour, options.postfix, mass), "w")
00085 xfile.write(" ".join(["imax %d number of bins" % len(DCR.bins)])+"\n")
00086 xfile.write(" ".join(["jmax * number of processes minus 1"])+"\n")
00087 xfile.write(" ".join(["kmax * number of nuisance parameters"])+"\n")
00088 xfile.write(" ".join(["-" * 130])+"\n")
00089
00090 cmax = max([cmax]+[len(l) for l in obskeyline]+[len(x) for x in obsline])
00091 cfmt = "%-"+str(cmax)+"s";
00092 xfile.write(" ".join(["bin ", " ".join([cfmt % x for x in obskeyline])])+"\n")
00093 xfile.write(" ".join(["observation ", " ".join([cfmt % x for x in obsline])])+"\n")
00094
00095 xfile.write(" ".join(["-" * 150])+"\n")
00096
00097 pidline = []; signals = []; backgrounds = []
00098 for (b,p,s) in keyline:
00099 if s:
00100 if p not in signals: signals.append(p)
00101 pidline.append(-len(DCR.signals)+signals.index(p)+1)
00102 else:
00103 if p not in backgrounds: backgrounds.append(p)
00104 pidline.append(1+backgrounds.index(p))
00105 cmax = max([cmax]+[max(len(p),len(b)) for p,b,s in keyline]+[len(e) for e in expline])
00106 hmax = max([10] + [len("%-12s %s %s" % (l,p,a)) for l,(p,a,e,nf) in systlines.items()])
00107 cfmt = "%-"+str(cmax)+"s"; hfmt = "%-"+str(hmax)+"s ";
00108 xfile.write(" ".join([hfmt % "bin", " ".join([cfmt % p for p,b,s in keyline])])+"\n")
00109 xfile.write(" ".join([hfmt % "process", " ".join([cfmt % b for p,b,s in keyline])])+"\n")
00110 xfile.write(" ".join([hfmt % "process", " ".join([cfmt % x for x in pidline])])+"\n")
00111 xfile.write(" ".join([hfmt % "rate", " ".join([cfmt % x for x in expline])])+"\n")
00112 xfile.write(" ".join(["-" * 150])+"\n")
00113 sysnamesSorted = systlines.keys(); sysnamesSorted.sort()
00114 for name in sysnamesSorted:
00115 (pdf,pdfargs,effect,nofloat) = systlines[name]
00116 if nofloat: name += "[nofloat]"
00117 systline = []
00118 for b,p,s in keyline:
00119 try:
00120 systline.append(effect[b][p] if (effect[b][p] != 1.0 or pdf != 'lnN') else "-")
00121 except KeyError:
00122 systline.append("-");
00123 xfile.write(" ".join([hfmt % ("%-28s %s %s" % (name, pdf, " ".join(pdfargs))), " ".join([cfmt % x for x in systline])])+"\n")
00124