def doPerformanceStudyOnMCOnly(inputFiles,
                               histogramForEstimation=defaultHistogram,
                               function='expo',
                               fitRanges=[(0.2, 1.6), (0.3, 1.6), (0.4, 1.6)]):
    if DEBUG:
        print '*' * 120
        print "Estimating QCD using a fit to RelIso"
        print 'Histogram = ', histogramForEstimation
        print 'Fit functions = ', function
        print 'Fit ranges = ', fitRanges
        print '*' * 120
    #get histograms
    histograms = FileReader.getHistogramDictionary(histogramForEstimation,
                                                   inputFiles)
    global allMC, qcd

    histograms['SumMC'] = plotting.sumSamples(histograms, allMC)

    histograms['QCD'] = plotting.sumSamples(histograms, qcd)

    #    qcdInSignalRegion = histograms['QCD'].Integral()
    #    qcdError = 0
    #    if not qcdInSignalRegion == 0:
    #        qcdError = qcdInSignalRegion / sqrt(qcdInSignalRegion)
    import copy
    results = {}
    qcdInSignalRegion, qcdError = getIntegral(histograms['QCD'], (0, 0.1))
    #        getRelIsoCalibrationCurve(inputFiles, histogramForEstimation, function, fitRanges)
    for fitRange in fitRanges:
        #take all other fit ranges as systematics
        fitRangesForSystematics = copy.deepcopy(fitRanges)
        fitRangesForSystematics.remove(fitRange)
        #instead of data use sum MC
        resultFromMethod = relIsoMethodWithSystematics(
            histograms['SumMC'], function, fitRange, fitRangesForSystematics,
            False)
        estimate, absoluteError = resultFromMethod[
            'estimate'], resultFromMethod['absoluteError']
        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)
        result['fitfunction'] = function
        result['fitRange'] = fitRange
        result['fitRangesForSystematics'] = fitRangesForSystematics
        result['fit'] = resultFromMethod['fit']
        results[str(fitRange)] = result
    return results
def doPerformanceStudyOnMCOnly(inputFiles,
                               histogramForEstimation=defaultHistogram,
                               function='expo',
                   fitRanges=[(0.2, 1.6), (0.3, 1.6), (0.4, 1.6)]):
    if DEBUG:
        print '*' * 120
        print "Estimating QCD using a fit to RelIso"
        print 'Histogram = ', histogramForEstimation
        print 'Fit functions = ', function
        print 'Fit ranges = ', fitRanges
        print '*' * 120
    #get histograms
    histograms = FileReader.getHistogramDictionary(histogramForEstimation, inputFiles)
    global allMC, qcd
    
    histograms['SumMC'] = plotting.sumSamples(histograms, allMC)
    
    histograms['QCD'] = plotting.sumSamples(histograms, qcd)
    
#    qcdInSignalRegion = histograms['QCD'].Integral()
#    qcdError = 0
#    if not qcdInSignalRegion == 0:
#        qcdError = qcdInSignalRegion / sqrt(qcdInSignalRegion) 
    import copy
    results = {}
    qcdInSignalRegion, qcdError = getIntegral(histograms['QCD'], (0, 0.1))
#        getRelIsoCalibrationCurve(inputFiles, histogramForEstimation, function, fitRanges)
    for fitRange in fitRanges:
        #take all other fit ranges as systematics
        fitRangesForSystematics = copy.deepcopy(fitRanges)
        fitRangesForSystematics.remove(fitRange)
        #instead of data use sum MC
        resultFromMethod = relIsoMethodWithSystematics(histograms['SumMC'], function, fitRange, fitRangesForSystematics, False)
        estimate, absoluteError = resultFromMethod['estimate'], resultFromMethod['absoluteError']
        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)
        result['fitfunction'] = function
        result['fitRange'] = fitRange
        result['fitRangesForSystematics'] = fitRangesForSystematics
        result['fit'] = resultFromMethod['fit']
        results[str(fitRange)] = result
    return results
def getStuff(histogramForEstimation, inputFiles):
    histograms = FileReader.getHistogramDictionary(histogramForEstimation, inputFiles)
    global allMC, qcd
    
    histograms['SumMC'] = plotting.sumSamples(histograms, allMC)
    
    histograms['QCD'] = plotting.sumSamples(histograms, qcd)
    qcdInSignalRegion, qcdError = getIntegral(histograms['QCD'], (0, 0.1))
    data, dataError = getIntegral(histograms['SingleElectron'], (0, 0.1))
    sumMC, sumMCError = getIntegral(histograms['SumMC'], (0, 0.1))
    result = {
              'N_data': data,
              'N_QCD': qcdInSignalRegion,
              'N_QCD_Error': qcdError,
              'N_SumMC': sumMC
              }
    return result
def getStuff(histogramForEstimation, inputFiles):
    histograms = FileReader.getHistogramDictionary(histogramForEstimation,
                                                   inputFiles)
    global allMC, qcd

    histograms['SumMC'] = plotting.sumSamples(histograms, allMC)

    histograms['QCD'] = plotting.sumSamples(histograms, qcd)
    qcdInSignalRegion, qcdError = getIntegral(histograms['QCD'], (0, 0.1))
    data, dataError = getIntegral(histograms['SingleElectron'], (0, 0.1))
    sumMC, sumMCError = getIntegral(histograms['SumMC'], (0, 0.1))
    result = {
        'N_data': data,
        'N_QCD': qcdInSignalRegion,
        'N_QCD_Error': qcdError,
        'N_SumMC': sumMC
    }
    return result
def printCutFlow(hist, analysis, outputFormat='Latex'):
    scale_ttbar = 164.4 / 157.5
    used_data = 'ElectronHad'
    lepton = 'Electron/electron'
    if 'Mu' in analysis:
        used_data = 'SingleMu'
        lepton = 'Muon/muon'
    hist_1mBtag = 'TTbarPlusMetAnalysis/' + analysis + '/Ref selection/' + lepton + '_AbsEta_1orMoreBtag'
    hist_2mBtag = 'TTbarPlusMetAnalysis/' + analysis + '/Ref selection/' + lepton + '_AbsEta_2orMoreBtags'
    hist_names = [hist, #due to b-tag scale factors these are not as simple any more
             hist_1mBtag,
             hist_2mBtag
             ]
    inputfiles = {}
    for sample in FILES.samplesToLoad:
        inputfiles[sample] = FILES.files[sample]
    hists = FileReader.getHistogramsFromFiles(hist_names, inputfiles)
    for sample in hists.keys():
        for histname in hists[sample].keys():
            hists[sample][histname].Sumw2()
    if analysis == 'EPlusJets':
        hists['QCD'] = plotting.sumSamples(hists, plotting.qcd_samples)
    else:
        hists['QCD'] = hists['QCD_Pt-20_MuEnrichedPt-15']
    
    hists['SingleTop'] = plotting.sumSamples(hists, plotting.singleTop_samples)
    hists['Di-Boson'] = plotting.sumSamples(hists, plotting.diboson_samples)
    hists['W+Jets'] = plotting.sumSamples(hists, plotting.wplusjets_samples)
#    hists['SumMC'] = plotting.sumSamples(hists, plotting.allMC_samples)
    
    header = "| Step | TTJet | W+jets | DY + Jets | single top | Di-boson | QCD | Sum MC | Data |"
    row = " | %s  |  %d +- %d |  %d +- %d |  %d +- %d |  %d +- %d |  %d +- %d |  %d +- %d |  %d +- %d |  %d | "
    if outputFormat == 'Latex':
        header = "Selection step & \\ttbar & W + Jets & Z + Jets & Single-top & Di-boson & QCD~  & Sum MC & Data\\\\"
        row = " %s  &  $%d \pm %d$ &  $%d \pm %d$ &  $%d \pm %d$ &  $%d \pm %d$ &  $%d \pm %d$ &  $%d \pm %d$ &  $%d \pm %d$ &  %d \\\\ "
    print header
    
    numbers, errors = getEventNumbers(hists, hist, hist_1mBtag, hist_2mBtag)# + '_0orMoreBtag')
    

    for step in range(len(cuts)):
        nums = numbers[step]
        errs = errors[step]
        nums['TTJet'] = nums['TTJet'] * scale_ttbar
        errs['TTJet'] = errs['TTJet'] * scale_ttbar
        if analysis == 'EPlusJets' and step >= len(cuts) - 3:#have only estimates for >= 4 jet and beyond
            histForEstimation = 'TTbarPlusMetAnalysis/EPlusJets/QCD e+jets PFRelIso/Electron/electron_pfIsolation_03_0orMoreBtag'
            if step == len(cuts) - 2:
                histForEstimation = 'TTbarPlusMetAnalysis/EPlusJets/QCD e+jets PFRelIso/Electron/electron_pfIsolation_03_1orMoreBtag'
            if step == len(cuts) - 1:
                histForEstimation = 'TTbarPlusMetAnalysis/EPlusJets/QCD e+jets PFRelIso/Electron/electron_pfIsolation_03_2orMoreBtags'
            estimate = QCDRateEstimation.estimateQCDWithRelIso(FILES.files, histForEstimation)
            nums['QCD'], errs['QCD'] = estimate['estimate'], estimate['absoluteError'] 
        if analysis == 'MuPlusJets' and step >= len(cuts) - 3:#have only estimates for >= 4 jet and beyond
            scale = 1.21
            nums['QCD'], errs['QCD'] = nums['QCD'] * scale, errs['QCD'] * scale
            
        sumMC = nums['TTJet'] + nums['W+Jets'] + nums['DYJetsToLL'] + nums['SingleTop'] + nums['QCD'] + nums['Di-Boson']
        sumMC_err = sqrt(errs['TTJet'] ** 2 + errs['W+Jets'] ** 2 + errs['DYJetsToLL'] ** 2 + errs['SingleTop'] ** 2 + errs['QCD'] ** 2 + errs['Di-Boson'] ** 2)
        print row % (cuts[step], nums['TTJet'], errs['TTJet'], nums['W+Jets'], errs['W+Jets'], nums['DYJetsToLL'], errs['DYJetsToLL'],
                     nums['SingleTop'], errs['SingleTop'], nums['Di-Boson'], errs['Di-Boson'], nums['QCD'], errs['QCD'], sumMC, sumMC_err, nums[used_data])
def printCutFlow(hist, analysis, outputFormat='Latex'):
    scale_ttbar = 164.4 / 157.5
    used_data = 'ElectronHad'
    lepton = 'Electron/electron'
    if 'Mu' in analysis:
        used_data = 'SingleMu'
        lepton = 'Muon/muon'
    hist_1mBtag = 'TTbarPlusMetAnalysis/' + analysis + '/Ref selection/' + lepton + '_AbsEta_1orMoreBtag'
    hist_2mBtag = 'TTbarPlusMetAnalysis/' + analysis + '/Ref selection/' + lepton + '_AbsEta_2orMoreBtags'
    hist_names = [
        hist,  #due to b-tag scale factors these are not as simple any more
        hist_1mBtag,
        hist_2mBtag
    ]
    inputfiles = {}
    for sample in FILES.samplesToLoad:
        inputfiles[sample] = FILES.files[sample]
    hists = FileReader.getHistogramsFromFiles(hist_names, inputfiles)
    for sample in hists.keys():
        for histname in hists[sample].keys():
            hists[sample][histname].Sumw2()
    if analysis == 'EPlusJets':
        hists['QCD'] = plotting.sumSamples(hists, plotting.qcd_samples)
    else:
        hists['QCD'] = hists['QCD_Pt-20_MuEnrichedPt-15']

    hists['SingleTop'] = plotting.sumSamples(hists, plotting.singleTop_samples)
    hists['Di-Boson'] = plotting.sumSamples(hists, plotting.diboson_samples)
    hists['W+Jets'] = plotting.sumSamples(hists, plotting.wplusjets_samples)
    #    hists['SumMC'] = plotting.sumSamples(hists, plotting.allMC_samples)

    header = "| Step | TTJet | W+jets | DY + Jets | single top | Di-boson | QCD | Sum MC | Data |"
    row = " | %s  |  %d +- %d |  %d +- %d |  %d +- %d |  %d +- %d |  %d +- %d |  %d +- %d |  %d +- %d |  %d | "
    if outputFormat == 'Latex':
        header = "Selection step & \\ttbar & W + Jets & Z + Jets & Single-top & Di-boson & QCD~  & Sum MC & Data\\\\"
        row = " %s  &  $%d \pm %d$ &  $%d \pm %d$ &  $%d \pm %d$ &  $%d \pm %d$ &  $%d \pm %d$ &  $%d \pm %d$ &  $%d \pm %d$ &  %d \\\\ "
    print header

    numbers, errors = getEventNumbers(hists, hist, hist_1mBtag,
                                      hist_2mBtag)  # + '_0orMoreBtag')

    for step in range(len(cuts)):
        nums = numbers[step]
        errs = errors[step]
        nums['TTJet'] = nums['TTJet'] * scale_ttbar
        errs['TTJet'] = errs['TTJet'] * scale_ttbar
        if analysis == 'EPlusJets' and step >= len(
                cuts) - 3:  #have only estimates for >= 4 jet and beyond
            histForEstimation = 'TTbarPlusMetAnalysis/EPlusJets/QCD e+jets PFRelIso/Electron/electron_pfIsolation_03_0orMoreBtag'
            if step == len(cuts) - 2:
                histForEstimation = 'TTbarPlusMetAnalysis/EPlusJets/QCD e+jets PFRelIso/Electron/electron_pfIsolation_03_1orMoreBtag'
            if step == len(cuts) - 1:
                histForEstimation = 'TTbarPlusMetAnalysis/EPlusJets/QCD e+jets PFRelIso/Electron/electron_pfIsolation_03_2orMoreBtags'
            estimate = QCDRateEstimation.estimateQCDWithRelIso(
                FILES.files, histForEstimation)
            nums['QCD'], errs['QCD'] = estimate['estimate'], estimate[
                'absoluteError']
        if analysis == 'MuPlusJets' and step >= len(
                cuts) - 3:  #have only estimates for >= 4 jet and beyond
            scale = 1.21
            nums['QCD'], errs['QCD'] = nums['QCD'] * scale, errs['QCD'] * scale

        sumMC = nums['TTJet'] + nums['W+Jets'] + nums['DYJetsToLL'] + nums[
            'SingleTop'] + nums['QCD'] + nums['Di-Boson']
        sumMC_err = sqrt(errs['TTJet']**2 + errs['W+Jets']**2 +
                         errs['DYJetsToLL']**2 + errs['SingleTop']**2 +
                         errs['QCD']**2 + errs['Di-Boson']**2)
        print row % (cuts[step], nums['TTJet'], errs['TTJet'], nums['W+Jets'],
                     errs['W+Jets'], nums['DYJetsToLL'], errs['DYJetsToLL'],
                     nums['SingleTop'], errs['SingleTop'], nums['Di-Boson'],
                     errs['Di-Boson'], nums['QCD'], errs['QCD'], sumMC,
                     sumMC_err, nums[used_data])