Exemple #1
0
def bluemethod(workingpath,zoutrange,whatuse,mcprod,verbose):
	""" ..function:: bluemethod(workingpath,zoutrange,whatuse) --> 
	"""
	from array import array
	from math import sqrt
	from functionspool_mod import getxserrorsrel,getratioerrorsrel,BR
	import ROOT
	
	nchannels = len(IDCHANNEL.keys())
	# Get cross-section and its relative-errors
	if whatuse == 'ratio':
		xs,xserrors = getratioerrorsrel(workingpath)
	else:
		xs,xserrors = getxserrorsrel(workingpath,xstype=whatuse,mcprod=mcprod)
	### ========== INITIALIZATIONS =============================
	### Arrays to be used to fill the matrices: arrayName  
	### --- U: link between observable and measure 
	arrayU = array('d',[1.0 for i in xrange(nchannels)])
	U = ROOT.TMatrixD(nchannels,1)
	U.SetMatrixArray(arrayU)
	U_T = ROOT.TMatrixD(1,nchannels)
	U_T.SetMatrixArray(arrayU)
	### --- E: measure covariance 
	arrayEmain,arrayEsep = geterrorarray(xs,xserrors)
	E = ROOT.TMatrixD(nchannels,nchannels)
	E.SetMatrixArray(arrayEmain["total"])
	E_inv = ROOT.TMatrixD(nchannels,nchannels)
	E_inv.SetMatrixArray(arrayEmain["total"])
	E_inv.Invert()
	### --- w: weights ==> xs_mean = Sum_{i} w_i*xs_i
	arrayW = array('d', [0.0 for i in xrange(nchannels)])
	W = ROOT.TMatrixD(nchannels,1)
	W.SetMatrixArray(arrayW)
	W_T = ROOT.TMatrixD(1,nchannels)
	### --- xs: cross-section measurements
	arrayXS= array('d', [xs[IDCHANNEL[i]] for i in xrange(nchannels)])
	XS = ROOT.TMatrixD(nchannels,1)
	XS.SetMatrixArray(arrayXS)
	XS_T = ROOT.TMatrixD(1,nchannels)
	XS_T.Transpose(XS)
	# ===== END INITIALIZATION

	# Extract the weights (using the BLUE method)
	kinv = (U_T*E_inv*U)(0,0)
	k = 1.0/kinv
	W = E_inv*U
	W *= k
	W_T.Transpose(W)

	# Get the estimated cross-section
	xsmean = (XS_T*W)(0,0)
	S = 0.0
	# Estimating the validity of the estimator
	for i in xrange(nchannels):
		for j in xrange(nchannels):
			S += (xsmean-XS(i,0))*E_inv(i,j)*(xsmean-XS(j,0))
	# variance separated by error type
	E_types = {}
	E_types_inv = {}
	for errname,arrayerr in arrayEmain.iteritems():
		E_types[errname] = ROOT.TMatrixD(nchannels,nchannels)
		E_types[errname].SetMatrixArray(arrayerr)

	variances = {}
	for errname,Ematrix in E_types.iteritems():
		variances[errname] = (W_T*Ematrix*W)(0,0)
	errors = dict(map(lambda (key,x): (key,sqrt(x)), variances.iteritems()))
		
	
	#if whatuse == "inclusive":
	#	print "\033[33;1mbluemethod WARNING\033[m Inclusive cross-section"\
	#			" calculation is not well-understood. See 'bluemethod -v'"

	if verbose:
		newline = "\033[32;2mbluemethod VERBOSE\033[m "
		message = newline+"WEIGHTS:: " 
		for i,channel in IDCHANNEL.iteritems():
			message += "%s:%.4f " % (channel,W(i,0))
		# S estimator
		message += "\n"
		message += newline+"S-estimator:: %.2f, i.e., prob. of %.0f%s that our combination"\
				" is consistent with our measures" % (S,getprobability(S,nchannels-1)*100,"%")
		if whatuse == "exclusive":
			print "\033[33;1mbluemethod WARNING\033[m Exclusive cross-section"\
				" calculation is not well-understood."
			message += "\n"
			BRprompt = ((BR.W2e+BR.W2m+BR.W2tau)/3.0*BR.Z2ll)
			xswz  = xsmean/BRprompt
			syswz = errors["sys"]/BRprompt
			statwz= errors["stat"]/BRprompt
			lumiwz= errors["lumi"]/BRprompt
			totalwz = sqrt(syswz**2.+statwz**2.+lumiwz**2.)
			message += newline+"Inclusive XS using just the exclusive "\
					"values: %.3f+-%.3f(stat)+-%.3f(sys)+-%.3f(lumi)  (total=%.3f)" % \
					(xswz,statwz,syswz,lumiwz,totalwz)
		# Matrices: E y E_sys
		print message
	
	return xsmean,errors
Exemple #2
0
def xscalc(path,zoutrange,format,mcprod,lumi,sysinfolder,verbose):
	"""
	"""
	from functionspool_mod import getxserrorsrel
	import os

	# Check we have the expected folder structure
	if not os.path.isdir(os.path.join(path,"leptonchannel")):
		message =  "\033[31mxscalc ERROR\033[m The '%s' path has not the expected folder structure"
		message += " (leptonchannel SIGNALeee SIGNALeem SIGNALmme SIGNALmmm)"
		raise RuntimeError(message)
	
	xsWZ,xsWZrelerrors = getxserrorsrel(path,xstype="inclusive",mcprod=mcprod,lumi=lumi,sysinfolder=sysinfolder)
	xs,xsrelerrors = getxserrorsrel(path,xstype="exclusive",mcprod=mcprod,lumi=lumi,sysinfolder=sysinfolder)
	# -- 
	hasprint=True
	if not format:
		hasprint=False
	elif format == "html":
		mainstr    = "%.3f±%.3f(stat)±%.3f(sys)±%.3f(lumi)"
		channelline= "| %s | "+mainstr+" | "+mainstr+" |\n"
	elif format == "tex":
		mainstr = "$%.3f\\pm%.3f(\\text{stat})\\pm%.3f(\\text{sys})\\pm%.5f(\\text{lumi})$"
		channelline = " %s & "+mainstr+" & "+mainstr+"\\\\\n "

	xsabserrors = adduperrors(xsrelerrors,xs,verbose)
	xsWZabserrors = adduperrors(xsWZrelerrors,xsWZ,verbose)
	if hasprint:
		print "Legend: value+-(stat)+-(sys_up,sys_down)+-(lumi)"
		print "Cross-section for WZ*BR(channel) || Cross-section for WZ "
	outmessage = ""
	xscalcdict = {}
	# Ordering the channel
	sortedchannels = sorted(xs.keys(),key=lambda x: (x[1],x[-1]))
	for channel in sortedchannels:
		csval = xs[channel]
		cserr = xsabserrors["stat"][channel]
		cssys = xsabserrors["sys"][channel]
		cssys_Lumi = xsabserrors["lumi"][channel]
		csval_WZ = xsWZ[channel] 
		cserr_WZ = xsWZabserrors["stat"][channel]
		cssys_WZ = xsWZabserrors["sys"][channel]
		cssys_Lumi_WZ = xsWZabserrors["lumi"][channel]
		if hasprint:
			mainstr= "%.4f+-%.4f+-%.4f+-%.4f"
			txtout = "| Channel %s : "+mainstr+" || "+mainstr
			print txtout % (channel,csval,cserr,cssys,cssys_Lumi,csval_WZ,cserr_WZ,cssys_WZ,cssys_Lumi_WZ)
			outmessage += channelline % (channel,csval,cserr,cssys,cssys_Lumi,csval_WZ,cserr_WZ,cssys_WZ,cssys_Lumi_WZ)
		xscalcdict[channel] = {'exclusive': (csval,cserr,cssys,cssys_Lumi),
				'inclusive': (csval_WZ,cserr_WZ,cssys_WZ,cssys_Lumi_WZ) }
	
	# tex or html output
	if hasprint:
		if format == "html":
			print "\n= HTLM OUTPUT "+"="*90
			tablestr= "|  | <latex size=\"scriptsize\">\\sigma_{WZ\\rightarrow3\\ell\\nu}\\;(pb^{-1})</latex> | <latex size=\"scriptsize\"> \\sigma_{WZ}\\;(pb_{-1}) </latex> |\n"
			print tablestr+outmessage[:-1]
			print "= HTML OUTPUT "+"="*90
		elif format == "tex":
			print "\n= TEX OUTPUT "+"="*90
			tablestr =  "\\begin{tabular}{l c c }\\hline\\hline\n"
			tablestr += " & $\\sigma_{WZ\\rightarrow3\\ell\\nu}\\;(pb^{-1})$ & $\\sigma_{WZ}$ \\\\ \\hline\\hline\n"
			print tablestr+outmessage+"\\hline\n\\end{tabular}"
			print "= TEX OUTPUT "+"="*90

	return xscalcdict
Exemple #3
0
def bluemethod(workingpath, zoutrange, whatuse, mcprod, verbose):
    """ ..function:: bluemethod(workingpath,zoutrange,whatuse) --> 
	"""
    from array import array
    from math import sqrt
    from functionspool_mod import getxserrorsrel, BR
    import ROOT

    nchannels = len(IDCHANNEL.keys())
    # Get cross-section and its relative-errors
    xs, xserrors = getxserrorsrel(workingpath, xstype=whatuse, mcprod=mcprod)
    ### ========== INITIALIZATIONS =============================
    ### Arrays to be used to fill the matrices: arrayName
    ### --- U: link between observable and measure
    arrayU = array('d', [1.0 for i in xrange(nchannels)])
    U = ROOT.TMatrixD(nchannels, 1)
    U.SetMatrixArray(arrayU)
    U_T = ROOT.TMatrixD(1, nchannels)
    U_T.SetMatrixArray(arrayU)
    ### --- E: measure covariance
    arrayEmain, arrayEsep = geterrorarray(xs, xserrors)
    E = ROOT.TMatrixD(nchannels, nchannels)
    E.SetMatrixArray(arrayEmain["total"])
    E_inv = ROOT.TMatrixD(nchannels, nchannels)
    E_inv.SetMatrixArray(arrayEmain["total"])
    E_inv.Invert()
    ### --- w: weights ==> xs_mean = Sum_{i} w_i*xs_i
    arrayW = array('d', [0.0 for i in xrange(nchannels)])
    W = ROOT.TMatrixD(nchannels, 1)
    W.SetMatrixArray(arrayW)
    W_T = ROOT.TMatrixD(1, nchannels)
    ### --- xs: cross-section measurements
    arrayXS = array('d', [xs[IDCHANNEL[i]] for i in xrange(nchannels)])
    XS = ROOT.TMatrixD(nchannels, 1)
    XS.SetMatrixArray(arrayXS)
    XS_T = ROOT.TMatrixD(1, nchannels)
    XS_T.Transpose(XS)
    # ===== END INITIALIZATION

    # Extract the weights (using the BLUE method)
    kinv = (U_T * E_inv * U)(0, 0)
    k = 1.0 / kinv
    W = E_inv * U
    W *= k
    W_T.Transpose(W)

    # Get the estimated cross-section
    xsmean = (XS_T * W)(0, 0)
    S = 0.0
    # Estimating the validity of the estimator
    for i in xrange(nchannels):
        for j in xrange(nchannels):
            S += (xsmean - XS(i, 0)) * E_inv(i, j) * (xsmean - XS(j, 0))
    # variance separated by error type
    E_types = {}
    E_types_inv = {}
    for errname, arrayerr in arrayEmain.iteritems():
        E_types[errname] = ROOT.TMatrixD(nchannels, nchannels)
        E_types[errname].SetMatrixArray(arrayerr)

    variances = {}
    for errname, Ematrix in E_types.iteritems():
        variances[errname] = (W_T * Ematrix * W)(0, 0)
    errors = dict(map(lambda (key, x): (key, sqrt(x)), variances.iteritems()))

    if whatuse == "inclusive":
        print "\033[33;1mbluemethod WARNING\033[m Inclusive cross-section"\
          " calculation is not well-understood. See 'bluemethod -v'"

    if verbose:
        newline = "\033[32;2mbluemethod VERBOSE\033[m "
        message = newline + "WEIGHTS:: "
        for i, channel in IDCHANNEL.iteritems():
            message += "%s:%.4f " % (channel, W(i, 0))
        # S estimator
        message += "\n"
        message += newline+"S-estimator:: %.2f, i.e., prob. of %.0f%s that our combination"\
          " is consistent with our measures" % (S,getprobability(S,nchannels-1)*100,"%")
        if whatuse == "exclusive":
            message += "\n"
            BRprompt = ((BR.W2e + BR.W2m + BR.W2tau) / 3.0 * BR.Z2ll)
            xswz = xsmean / BRprompt
            syswz = errors["sys"] / BRprompt
            statwz = errors["stat"] / BRprompt
            lumiwz = errors["lumi"] / BRprompt
            totalwz = sqrt(syswz**2. + statwz**2. + lumiwz**2.)
            message += newline+"Inclusive XS using just the exclusive "\
              "values: %.3f+-%.3f(stat)+-%.3f(sys)+-%.3f(lumi)  (total=%.3f)" % \
              (xswz,statwz,syswz,lumiwz,totalwz)
        # Matrices: E y E_sys
        print message

    return xsmean, errors