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
Exemple #5
0
                hist_type0.SetMarkerSize(2)
                hist_sysshift_type0.SetMarkerSize(2)
                
                hist_type0.Draw()
                hist_sysshift.Draw('same')
                hist_sysshift_type0.Draw('same')
                hist_nominal.Draw('same')
                
                if variable == 'MET':
                    hist_nominal.SetAxisRange(0,300, "X")
                    hist_sysshift.SetAxisRange(0,300, "X")
                    hist_type0.SetAxisRange(0,300, "X")
                    hist_sysshift_type0.SetAxisRange(0,300, "X")
                    
                    hist_type0.Draw()
                    hist_sysshift.Draw('same')
                    hist_sysshift_type0.Draw('same')
                    hist_nominal.Draw('same')
                
                legend = plotting.create_legend(x0=0.72, y0 = 0.90, x1=0.84, y1=0.75)
                legend.SetTextSize(0.03)
                legend.AddEntry(hist_nominal, 't#bar{t} nominal', 'l')
                legend.AddEntry(hist_sysshift, 't#bar{t} sys_shift', 'l')
                legend.AddEntry(hist_type0, 't#bar{t} type0', 'l')
                legend.AddEntry(hist_sysshift_type0, 't#bar{t} sys_shift+type0', 'l')
                legend.Draw()
               
                canvas.SaveAs(output_path + met +"_" + variable + "_" + bjet_bin + ".pdf")
                
                
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])
             "electron_pfIsolation_03_0orMoreBtag":'Events/(0.05)',
             "electron_pfIsolation_04_0orMoreBtag":'Events/(0.05)',
             "electron_pfIsolation_05_0orMoreBtag":'Events/(0.05)',
             "electron_dPhi_in":'Events/(0.01)',
             "electron_dEta_in":'Events/(0.001)',
             "electron_HadOverEM":'Events/(0.01)',
             "electron_mvaTrigV0":'Events/(0.05)',
             "electron_mvaNonTrigV0":'Events/(0.05)',
             "electron_dB":'Events/(0.001 cm)',
             'electron_sigma_ietaieta':'Events/(0.001)'
          }

histograms = ['HLTQCDAnalyser_inclusive/' + trigger + '/' + variable for variable in variables for trigger in triggers]

hists = FileReader.getHistogramsFromFiles(histograms, files)
plotting.setStyle()
for variable in variables:
    hists = plotting.rebin(hists, rebins[variable], '*' + variable)
    hists = plotting.setXRange(hists, limits=limits[variable], histname='*' + variable)
    hists = plotting.setYTitle(hists, title=titles[variable], histname='*' + variable)

labels = [
          'CaloIdVT_CaloIsoT_TrkIdT_TrkIsoT',
              'CaloIdVT_CaloIsoVL_TrkIdVL_TrkIsoT',
              'CaloIdVL_CaloIsoT_TrkIdVL_TrkIsoT',
              'CaloIdVT_TrkIdT'
          ]

styles = [
          {'color': kBlack, 'fill': 1001},
          {'color': kRed, 'fill': 3004},
    "electron_dPhi_in": 'Events/(0.01)',
    "electron_dEta_in": 'Events/(0.001)',
    "electron_HadOverEM": 'Events/(0.01)',
    "electron_mvaTrigV0": 'Events/(0.05)',
    "electron_mvaNonTrigV0": 'Events/(0.05)',
    "electron_dB": 'Events/(0.001 cm)',
    'electron_sigma_ietaieta': 'Events/(0.001)'
}

histograms = [
    'HLTQCDAnalyser_inclusive/' + trigger + '/' + variable
    for variable in variables for trigger in triggers
]

hists = FileReader.getHistogramsFromFiles(histograms, files)
plotting.setStyle()
for variable in variables:
    hists = plotting.rebin(hists, rebins[variable], '*' + variable)
    hists = plotting.setXRange(hists,
                               limits=limits[variable],
                               histname='*' + variable)
    hists = plotting.setYTitle(hists,
                               title=titles[variable],
                               histname='*' + variable)

labels = [
    'CaloIdVT_CaloIsoT_TrkIdT_TrkIsoT', 'CaloIdVT_CaloIsoVL_TrkIdVL_TrkIsoT',
    'CaloIdVL_CaloIsoT_TrkIdVL_TrkIsoT', 'CaloIdVT_TrkIdT'
]

styles = [
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])