def getPerformanceOnMC(
    files,
    histogramForEstimation="QCDStudy/PFIsolation_WithMETCutAndAsymJetCuts_DR03",
    bjetBin="",
    function="expo",
    fitRange=(0.3, 1.6),
    additionFitRanges=[(0.2, 1.6), (0.4, 1.6)],
):
    if bjetBin:
        histogramForEstimation = histogramForEstimation + "_" + bjetBin

    hists = [histogramForEstimation]
    hists = getHistsFromFiles(hists, files)
    hists = addSampleSum(hists)

    histogramForComparison = hists["qcd"][histogramForEstimation]
    histogramForEstimation = hists["allMC"][histogramForEstimation]

    estimate, absoluteError = getQCDEstimateFor(
        histogramForEstimation, function, fitRange=fitRange, additionFitRanges=additionFitRanges
    )

    qcdInSignalRegion, qcdError = getIntegral(histogramForComparison, (0, 0.1))

    N_est = ufloat((estimate, absoluteError))
    N_qcd = ufloat((qcdInSignalRegion, qcdError))

    relativeDeviation = N_est / N_qcd

    result = {}
    result["performance"] = (relativeDeviation.nominal_value, relativeDeviation.std_dev())
    result["estimate"] = (estimate, absoluteError)
    result["qcdInSignalRegion"] = (qcdInSignalRegion, qcdError)

    return result
def getQCDEstimate(
        datafile,
        histogramForEstimation='QCDStudy/PFIsolation_WithMETCutAndAsymJetCuts_DR03',
        bjetBin='',
        function='expo',
        fitRange=(0.3, 1.6),
        additionFitRanges=[(0.2, 1.6), (0.4, 1.6)]):
    bias = 0.45
    bias = 0
    reductionFromBias = 1 - bias
    if bjetBin:
        histogramForEstimation = histogramForEstimation + '_' + bjetBin
    files = {'data': datafile}
    hists = [histogramForEstimation]
    hists = getHistsFromFiles(hists, files)
    histogramForEstimation = hists['data'][histogramForEstimation]
    estimate, absoluteError = getQCDEstimateFor(
        histogramForEstimation,
        function,
        fitRange=fitRange,
        additionFitRanges=additionFitRanges)
    estimate = estimate * reductionFromBias
    absoluteError = absoluteError * reductionFromBias
    absoluteError = sqrt(absoluteError**2 + (estimate * bias)**2)
    return estimate, absoluteError
def compareFitFunctions(datafile, histogramForEstimation, functions, fitRange=(0.3, 1.6)):
    files = {"data": datafile}
    hists = [histogramForEstimation]
    hists = getHistsFromFiles(hists, files)
    histogramForEstimation = hists["data"][histogramForEstimation]

    result = {}
    for function in functions:
        fit = doFit(histogramForEstimation, function, fitRange)
        ndof = fit.GetNDF()
        chi2 = fit.GetChisquare()
        result[function] = {"NDOF": ndof, "Chi2": chi2}
    return result
def compareFitFunctions(datafile,
                        histogramForEstimation,
                        functions,
                        fitRange=(0.3, 1.6)):
    files = {'data': datafile}
    hists = [histogramForEstimation]
    hists = getHistsFromFiles(hists, files)
    histogramForEstimation = hists['data'][histogramForEstimation]

    result = {}
    for function in functions:
        fit = doFit(histogramForEstimation, function, fitRange)
        ndof = fit.GetNDF()
        chi2 = fit.GetChisquare()
        result[function] = {'NDOF': ndof, 'Chi2': chi2}
    return result
def getShapeErrorHistogram(
    files,
    histogramForShape="topReconstruction/backgroundShape/mttbar_conversions_withMETAndAsymJets",
    histogramForComparison="topReconstruction/backgroundShape/mttbar_antiIsolated_withMETAndAsymJets",
    rebin=1,
    suffix="",
):
    files = {"data": files["data"]}
    #    histogramForShape = 'topReconstruction/backgroundShape/mttbar_conversions_withMETAndAsymJets'
    #    histogramForComparison = 'topReconstruction/backgroundShape/mttbar_antiIsolated_withMETAndAsymJets'
    #    suffixes = allBjetBins
    #    rebin = 50

    errors = None

    #    for suffix in suffixes:
    #        if suffix in histname:
    if not suffix == "":
        histogramForShape = histogramForShape + "_" + suffix
        histogramForComparison = histogramForComparison + "_" + suffix

    hists = [histogramForShape, histogramForComparison]
    hists = getHistsFromFiles(hists, files)
    histogramForShape = hists["data"][histogramForShape]
    histogramForComparison = hists["data"][histogramForComparison]
    histogramForShape.Sumw2()
    histogramForComparison.Sumw2()

    histogramForShape.Rebin(rebin)
    histogramForComparison.Rebin(rebin)

    nShape = histogramForShape.Integral()
    nCompare = histogramForComparison.Integral()

    if nShape > 0 and nCompare > 0:
        histogramForShape.Scale(1 / nShape)
        histogramForComparison.Scale(1 / nCompare)

    errors = histogramForShape.Clone("ShapeErrors")
    errors.Add(histogramForComparison, -1)  # subtraction
    for bin in range(1, errors.GetNbinsX()):
        errors.SetBinContent(bin, fabs(errors.GetBinContent(bin)))
    errors.Divide(histogramForShape)

    return errors
def getShapeErrorHistogram(
        files,
        histogramForShape='topReconstruction/backgroundShape/mttbar_conversions_withMETAndAsymJets',
        histogramForComparison='topReconstruction/backgroundShape/mttbar_antiIsolated_withMETAndAsymJets',
        rebin=1,
        suffix=''):
    files = {'data': files['data']}
    #    histogramForShape = 'topReconstruction/backgroundShape/mttbar_conversions_withMETAndAsymJets'
    #    histogramForComparison = 'topReconstruction/backgroundShape/mttbar_antiIsolated_withMETAndAsymJets'
    #    suffixes = allBjetBins
    #    rebin = 50

    errors = None

    #    for suffix in suffixes:
    #        if suffix in histname:
    if not suffix == '':
        histogramForShape = histogramForShape + '_' + suffix
        histogramForComparison = histogramForComparison + '_' + suffix

    hists = [histogramForShape, histogramForComparison]
    hists = getHistsFromFiles(hists, files)
    histogramForShape = hists['data'][histogramForShape]
    histogramForComparison = hists['data'][histogramForComparison]
    histogramForShape.Sumw2()
    histogramForComparison.Sumw2()

    histogramForShape.Rebin(rebin)
    histogramForComparison.Rebin(rebin)

    nShape = histogramForShape.Integral()
    nCompare = histogramForComparison.Integral()

    if nShape > 0 and nCompare > 0:
        histogramForShape.Scale(1 / nShape)
        histogramForComparison.Scale(1 / nCompare)

    errors = histogramForShape.Clone('ShapeErrors')
    errors.Add(histogramForComparison, -1)  #subtraction
    for bin in range(1, errors.GetNbinsX()):
        errors.SetBinContent(bin, fabs(errors.GetBinContent(bin)))
    errors.Divide(histogramForShape)

    return errors
def getPerformanceOnMC(
        files,
        histogramForEstimation='QCDStudy/PFIsolation_WithMETCutAndAsymJetCuts_DR03',
        bjetBin='',
        function='expo',
        fitRange=(0.3, 1.6),
        additionFitRanges=[(0.2, 1.6), (0.4, 1.6)]):
    if bjetBin:
        histogramForEstimation = histogramForEstimation + '_' + bjetBin

    hists = [histogramForEstimation]
    hists = getHistsFromFiles(hists, files)
    hists = addSampleSum(hists)

    histogramForComparison = hists['qcd'][histogramForEstimation]
    histogramForEstimation = hists['allMC'][histogramForEstimation]

    estimate, absoluteError = getQCDEstimateFor(
        histogramForEstimation,
        function,
        fitRange=fitRange,
        additionFitRanges=additionFitRanges)

    qcdInSignalRegion, qcdError = getIntegral(histogramForComparison, (0, 0.1))

    N_est = ufloat((estimate, absoluteError))
    N_qcd = ufloat((qcdInSignalRegion, qcdError))

    relativeDeviation = N_est / N_qcd

    result = {}
    result['performance'] = (relativeDeviation.nominal_value,
                             relativeDeviation.std_dev())
    result['estimate'] = (estimate, absoluteError)
    result['qcdInSignalRegion'] = (qcdInSignalRegion, qcdError)

    return result
def getQCDEstimate(
    datafile,
    histogramForEstimation="QCDStudy/PFIsolation_WithMETCutAndAsymJetCuts_DR03",
    bjetBin="",
    function="expo",
    fitRange=(0.3, 1.6),
    additionFitRanges=[(0.2, 1.6), (0.4, 1.6)],
):
    bias = 0.45
    bias = 0
    reductionFromBias = 1 - bias
    if bjetBin:
        histogramForEstimation = histogramForEstimation + "_" + bjetBin
    files = {"data": datafile}
    hists = [histogramForEstimation]
    hists = getHistsFromFiles(hists, files)
    histogramForEstimation = hists["data"][histogramForEstimation]
    estimate, absoluteError = getQCDEstimateFor(
        histogramForEstimation, function, fitRange=fitRange, additionFitRanges=additionFitRanges
    )
    estimate = estimate * reductionFromBias
    absoluteError = absoluteError * reductionFromBias
    absoluteError = sqrt(absoluteError ** 2 + (estimate * bias) ** 2)
    return estimate, absoluteError