示例#1
0
    def getHistograms( self ):
        relIsoHists = [self.relIsoHistogramPrefix + jetbin for jetbin in self.jetBins]
        pfIsoHists = [self.pfIsoHistogramPrefix + jetbin for jetbin in self.jetBins]
        pfIsoControlHists = [self.pfIsoControlRegionHistogramPrefix + jetbin for jetbin in self.jetBins]
        allHists = relIsoHists
        allHists.extend( pfIsoHists )
        allHists.extend( pfIsoControlHists )
        #print allHists
#        HistGetter.hists = allHists

        self.histograms = HistGetter.getHistsFromFiles(allHists, self.files);
        self.histograms = HistGetter.addSampleSum( self.histograms )
def compareShapesTwoData(dataOld, dataNew):
    hists = [
        'topReconstruction/mttbar_withMETAndAsymJets',
        'topReconstruction/mttbar_3jets_withMETAndAsymJets',
    ]
    histsOld = HistGetter.getHistsFromFiles(hists, {'data': dataOld},
                                            bJetBins=HistPlotter.allBjetBins)
    histsNew = HistGetter.getHistsFromFiles(hists, {'data': dataNew},
                                            bJetBins=HistPlotter.allBjetBins)

    leg = TLegend(0.696, 0.35, 0.94, 0.92)
    leg.SetBorderSize(0)
    leg.SetLineStyle(0)
    leg.SetTextFont(42)
    leg.SetFillStyle(0)

    AddLegendEntry = leg.AddEntry
    alreadyAdded = False

    for bin in HistPlotter.allBjetBins:
        for hist in hists:
            current = hist + '_' + bin
            currentOld = histsOld['data'][current]
            currentNew = histsNew['data'][current]
            currentOld.Rebin(50)
            currentNew.Rebin(50)
            if currentOld.Integral() > 0:
                currentOld.Scale(1000 / currentOld.Integral())
            if currentNew.Integral():
                currentNew.Scale(1000 / currentNew.Integral())

            c = TCanvas("cname3", 'cname3', 1200, 900)
            c.SetLogy(1)
            currentOld.SetFillColor(2)
            currentOld.SetFillStyle(3004)

            currentNew.SetFillColor(4)
            currentNew.SetFillStyle(3005)

            currentOld.GetXaxis().SetRangeUser(250, 3000)
            currentOld.Draw('hist')
            currentNew.Draw('hist same')

            if not alreadyAdded:
                AddLegendEntry(currentOld, "old JEC", "f")
                AddLegendEntry(currentNew, "new JEC", "f")
            alreadyAdded = True

            leg.Draw()
            saveAs(c, current + '_shape_comparison', outputFormats)
            del c
def compareShapesTwoData(dataOld, dataNew):
    hists = ['topReconstruction/mttbar_withMETAndAsymJets',
             'topReconstruction/mttbar_3jets_withMETAndAsymJets',
             ]
    histsOld = HistGetter.getHistsFromFiles(hists, {'data': dataOld}, bJetBins=HistPlotter.allBjetBins)
    histsNew = HistGetter.getHistsFromFiles(hists, {'data': dataNew}, bJetBins=HistPlotter.allBjetBins)
    
    
    leg = TLegend(0.696, 0.35, 0.94, 0.92);
    leg.SetBorderSize(0);
    leg.SetLineStyle(0);
    leg.SetTextFont(42);
    leg.SetFillStyle(0);

    AddLegendEntry = leg.AddEntry 
    alreadyAdded = False
    
    for bin in HistPlotter.allBjetBins:
        for hist in hists:
            current = hist + '_' + bin
            currentOld = histsOld['data'][current]
            currentNew = histsNew['data'][current]
            currentOld.Rebin(50)
            currentNew.Rebin(50)
            if currentOld.Integral() > 0:
                currentOld.Scale(1000/currentOld.Integral())
            if currentNew.Integral():
                currentNew.Scale(1000/currentNew.Integral())
            
            c = TCanvas("cname3", 'cname3', 1200, 900)
            c.SetLogy(1)
            currentOld.SetFillColor(2)
            currentOld.SetFillStyle(3004)
        
            currentNew.SetFillColor(4)
            currentNew.SetFillStyle(3005)
    
            currentOld.GetXaxis().SetRangeUser(250, 3000);
            currentOld.Draw('hist')
            currentNew.Draw('hist same')
    
            if not alreadyAdded:
                AddLegendEntry(currentOld, "old JEC", "f");
                AddLegendEntry(currentNew, "new JEC", "f");
            alreadyAdded = True
        
            leg.Draw()
            saveAs(c, current + '_shape_comparison' , outputFormats)
            del c
示例#4
0
def printCutFlow(hist, analysis):
    files = inputFiles.files
    hist_1mBtag =  'TTbarPlusMetAnalysis/' + analysis + '/Ref selection/MET/patMETsPFlow/MET_1orMoreBtag'
    hist_2mBtag = 'TTbarPlusMetAnalysis/' + analysis + '/Ref selection/MET/patMETsPFlow/MET_2orMoreBtags'
    hists = [hist, #due to b-tag scale factors these are not as simple any more
             hist_1mBtag,
             hist_2mBtag
             ]
    hists = HistGetter.getHistsFromFiles(hists, files, bJetBins=[])
    hists = HistGetter.addSampleSum(hists)
#    histname3 = 'EventCount/TTbarEplusJetsSelection_2orMoreBtags'
    
    header = "| Step | TTJet | W+jets | DY + Jets | single top | QCD | Sum MC | Data |"
    row = " | %s  |  %d |  %d |  %d |  %d |  %d|  %d |  %d | "
    print header
    
    numbers = getEventNumbers(hists, hist, hist_1mBtag, hist_2mBtag)# + '_0orMoreBtag')
    for step in range(len(cuts)):
        nums = numbers[step]
        sumMC = nums['ttbar'] + nums['wjets'] + nums['zjets'] + nums['singleTop'] + nums['qcd']
        print row % (cuts[step], nums['ttbar'], nums['wjets'], nums['zjets'], nums['singleTop'], nums['qcd'], sumMC, nums['data'])
示例#5
0
    def __init__( self, files ):
        self.files = files
        HistGetter.samplefiles = files
        self.histograms = {}
        self.histGetter = HistGetter()
        self.histGetter.setStyle()
        self.getHistograms()
        self.applyStyleAndCreateStack()

        for bin in self.jetBins:
            self.pfIsoResults[bin] = {'actualNumberOfQCDEvents': 0, 'estimatedNumberOfQCDEvents':0,
                                      'fitFunction': None, 'fitParameters': {}, 'numberOfAllDataEvents':0,
                                      'numberOfAllMCEvents':0}
            self.relIsoResults[bin] = {'actualNumberOfQCDEvents': 0, 'estimatedNumberOfQCDEvents':0,
                                       'fitFunction': None, 'fitParameters': {}, 'numberOfAllDataEvents':0,
                                       'numberOfAllMCEvents':0}
    leg.SetTextFont(42);
    leg.SetFillStyle(0);
    leg.AddEntry(dataPlot, 'data', 'P')
    leg.AddEntry(mcPlot, 'MC', 'P')
    return leg

def getCaption():
    tex = TLatex(0.18,1,"CMS Preliminary 2011,  #sqrt{s} = 7 TeV, L = 4.69 fb^{-1}");
    tex.SetNDC();
    tex.SetTextAlign(13);
    tex.SetTextFont(42);
    tex.SetTextSize(0.04);
    tex.SetLineWidth(2);
    return tex

if __name__ == '__main__':
    gROOT.SetBatch(True)
    gROOT.ProcessLine('gErrorIgnoreLevel = 1001;')
    
    files = inputFiles.files
    
    hltFiles = {}
    
    hltFiles['data'] = inputFiles.files['data']
    hltFiles['ttbar'] = inputFiles.files['ttbar']

    triggerPlots = ['HLTStudy/' + trigger + '/' + variable + '_' + modifier for trigger in triggers for variable in triggerVariables for modifier in triggerModifiers]
    HistPlotter.setStyle()
    hists = HistGetter.getHistsFromFiles(triggerPlots, hltFiles, jetBins=HistPlotter.allJetBins)
    makeHLTPlots(hists)
def plotMttbar():
    global used_data
    saveAs = HistPlotter.saveAs
    savePath = "/storage/TopQuarkGroup/results/plots/2012_v9a"
    
    performRescale = False
    HistPlotter.setStyle()
    lumi = 5814#5050.0#1959.75#1611.95
    oldLumi = 10000#5050#/(0.95**2)
#    scale = 0.15#lumi / oldLumi;
    
    outputFormats = [
                     'pdf'
                    ]
    reverseMCOrder = False
#    qcdFromData = 'topReconstruction/backgroundShape/mttbar_conversions_withMETAndAsymJets_0orMoreBtag'
    
    hists = [];
    ttbarUncertainty = 0.1
    

    hists.append('TTbarPlusMetAnalysis/EPlusJets/Ref selection/Electron/electron_AbsEta')
    hists.append('TTbarPlusMetAnalysis/EPlusJets/QCDConversions/Electron/electron_AbsEta')
    hists.append('TTbarPlusMetAnalysis/EPlusJets/QCD non iso e+jets/Electron/electron_AbsEta')
#    hists.append('TTbarPlusMetAnalysis/EPlusJets/QCD non iso e+jets, non iso trigger/Electron/electron_AbsEta')
#    hists.append('TTbarPlusMetAnalysis/EPlusJets/QCDAntiID/Electron/electron_AbsEta')
#    hists.append('TTbarPlusMetAnalysis/EPlusJets/QCDNoIsoNoID/Electron/electron_AbsEta')
#    
    hists.append('TTbarPlusMetAnalysis/EPlusJets/Ref selection/Electron/electron_pT')
    hists.append('TTbarPlusMetAnalysis/EPlusJets/QCDConversions/Electron/electron_pT')
    hists.append('TTbarPlusMetAnalysis/EPlusJets/QCD non iso e+jets/Electron/electron_pT')
#
#    hists.append('TTbarPlusMetAnalysis/EPlusJets/QCD non iso e+jets/Electron/electron_pfIsolation_03')
#    hists.append('TTbarPlusMetAnalysis/EPlusJets/QCD e+jets PFRelIso/Electron/electron_pfIsolation_03')
#    
#    hists.append('TTbarPlusMetAnalysis/EPlusJets/Ref selection/BinnedMETAnalysis/Electron_patType1CorrectedPFMet_bin_0-25/electron_AbsEta')
#    hists.append('TTbarPlusMetAnalysis/EPlusJets/Ref selection/BinnedMETAnalysis/Electron_patType1CorrectedPFMet_bin_25-45/electron_AbsEta')
#    hists.append('TTbarPlusMetAnalysis/EPlusJets/Ref selection/BinnedMETAnalysis/Electron_patType1CorrectedPFMet_bin_45-70/electron_AbsEta')
#    hists.append('TTbarPlusMetAnalysis/EPlusJets/Ref selection/BinnedMETAnalysis/Electron_patType1CorrectedPFMet_bin_70-100/electron_AbsEta')
#    hists.append('TTbarPlusMetAnalysis/EPlusJets/Ref selection/BinnedMETAnalysis/Electron_patType1CorrectedPFMet_bin_100-inf/electron_AbsEta')
#    
#
#    hists.append('TTbarPlusMetAnalysis/EPlusJets/Ref selection/bjet_invariant_mass')
#    hists.append('TTbarPlusMetAnalysis/EPlusJets/Ref selection/MET/patType1CorrectedPFMet/MET')
#    hists.append('TTbarPlusMetAnalysis/EPlusJets/Ref selection/MET/patType1CorrectedPFMet/MET_phi')
#
    hists.append('TTbarPlusMetAnalysis/EPlusJets/Ref selection/MET/patMETsPFlow/MET')
    hists.append('TTbarPlusMetAnalysis/EPlusJets/Ref selection/MET/patMETsPFlow/MET_phi')
#    hists.append('TTbarPlusMetAnalysis/EPlusJets/Ref selection/MET/patType1p2CorrectedPFMet/MET')
#    hists.append('TTbarPlusMetAnalysis/EPlusJets/Ref selection/MET/patType1p2CorrectedPFMet/MET_phi')
#
#    hists.append('TTbarPlusMetAnalysis/EPlusJets/QCDConversions/MET/patType1CorrectedPFMet/MET')
#    hists.append('TTbarPlusMetAnalysis/EPlusJets/QCD non iso e+jets/MET/patType1CorrectedPFMet/MET')
##    
#    hists.append('TTbarPlusMetAnalysis/EPlusJets/Ref selection/MET/patType1CorrectedPFMet/Transverse_Mass')
#    hists.append('TTbarPlusMetAnalysis/EPlusJets/Ref selection/MET/patType1CorrectedPFMet/Angle_lepton_MET')
#    
#    hists.append('MttbarAnalysis/ElectronPlusJets/Ref selection/FourJetChi2/m3')
#    hists.append('MttbarAnalysis/ElectronPlusJets/Ref selection/FourJetChi2/hadronicTopMass')
#    hists.append('MttbarAnalysis/ElectronPlusJets/Ref selection/FourJetChi2/hadronicWMass')
    
    hists.append('TTbarPlusMetAnalysis/EPlusJets/Ref selection/Jets/N_Jets')
    
    hists.append('TTbarPlusMetAnalysis/MuPlusJets/Ref selection/Muon/muon_AbsEta')
    hists.append('TTbarPlusMetAnalysis/MuPlusJets/QCD non iso mu+jets/Muon/muon_AbsEta')
##
#    hists.append('TTbarPlusMetAnalysis/MuPlusJets/Ref selection/BinnedMETAnalysis/Muon_patType1CorrectedPFMet_bin_0-25/muon_AbsEta')
#    hists.append('TTbarPlusMetAnalysis/MuPlusJets/Ref selection/BinnedMETAnalysis/Muon_patType1CorrectedPFMet_bin_25-45/muon_AbsEta')
#    hists.append('TTbarPlusMetAnalysis/MuPlusJets/Ref selection/BinnedMETAnalysis/Muon_patType1CorrectedPFMet_bin_45-70/muon_AbsEta')
#    hists.append('TTbarPlusMetAnalysis/MuPlusJets/Ref selection/BinnedMETAnalysis/Muon_patType1CorrectedPFMet_bin_70-100/muon_AbsEta')
#    hists.append('TTbarPlusMetAnalysis/MuPlusJets/Ref selection/BinnedMETAnalysis/Muon_patType1CorrectedPFMet_bin_100-inf/muon_AbsEta')
###    
#    hists.append('TTbarPlusMetAnalysis/MuPlusJets/Ref selection/bjet_invariant_mass')
    hists.append('TTbarPlusMetAnalysis/MuPlusJets/Ref selection/MET/patMETsPFlow/MET')
#    hists.append('TTbarPlusMetAnalysis/MuPlusJets/Ref selection/MET/patType1CorrectedPFMet/MET')
#    hists.append('TTbarPlusMetAnalysis/MuPlusJets/Ref selection/MET/patType1p2CorrectedPFMet/MET')
    hists.append('TTbarPlusMetAnalysis/MuPlusJets/Ref selection/MET/patMETsPFlow/MET_phi')
#    hists.append('TTbarPlusMetAnalysis/MuPlusJets/Ref selection/MET/patType1CorrectedPFMet/MET_phi')
#    hists.append('TTbarPlusMetAnalysis/MuPlusJets/Ref selection/MET/patType1p2CorrectedPFMet/MET_phi')
#    
#    hists.append('TTbarPlusMetAnalysis/MuPlusJets/QCD non iso mu+jets/MET/patMETsPFlow/MET')
#    hists.append('TTbarPlusMetAnalysis/MuPlusJets/QCD non iso mu+jets/MET/patType1CorrectedPFMet/MET')
#    hists.append('TTbarPlusMetAnalysis/MuPlusJets/QCD non iso mu+jets/MET/patType1p2CorrectedPFMet/MET')
##    
#    hists.append('TTbarPlusMetAnalysis/MuPlusJets/QCD non iso mu+jets/Muon/muon_pfIsolation_04')
#    hists.append('TTbarPlusMetAnalysis/MuPlusJets/QCD mu+jets PFRelIso/Muon/muon_pfIsolation_04')
#    hists.append('TTbarPlusMetAnalysis/MuPlusJets/Ref selection/MET/patType1CorrectedPFMet/Transverse_Mass')
#    hists.append('TTbarPlusMetAnalysis/MuPlusJets/Ref selection/MET/patType1CorrectedPFMet/Angle_lepton_MET')
#    
#    hists.append('MttbarAnalysis/MuonPlusJets/Ref selection/FourJetChi2/m3')
#    hists.append('MttbarAnalysis/MuonPlusJets/Ref selection/FourJetChi2/hadronicTopMass')
#    hists.append('MttbarAnalysis/MuonPlusJets/Ref selection/FourJetChi2/hadronicWMass')
    hists.append('TTbarPlusMetAnalysis/MuPlusJets/Ref selection/Jets/N_Jets')
    hists.append('TTbarPlusMetAnalysis/MuPlusJets/Ref selection/Muon/muon_pT')
    
    
    files = inputFiles.files
    hists = HistGetter.getHistsFromFiles(hists, files, bJetBins=[
#                                                                 '0orMoreBtag',
#                                                                 '0btag', 
#                                                                 '1btag', 
                                                                 '2orMoreBtags',
#                                                                 '4orMoreBtags'
                                                                 ])
    otherHists = []
    otherHists.append('EventCount/TTbarMuPlusJetsRefSelection')
    otherHists.append('EventCount/TTbarEplusJetsRefSelection')
    otherHists.append('EventCount/TTbarMuPlusJetsRefSelectionUnweighted')
    otherHists.append('EventCount/TTbarEplusJetsRefSelectionUnweighted')
#    otherHists.append('TTbarPlusMetAnalysis/EPlusJets/Ref selection/Vertices/nVertex')
#    otherHists.append('TTbarPlusMetAnalysis/EPlusJets/Ref selection/Vertices/nVertex_reweighted')
#    
#    otherHists.append('TTbarPlusMetAnalysis/EPlusJets/Ref selection/N_BJets')
#    otherHists.append('TTbarPlusMetAnalysis/EPlusJets/Ref selection/N_BJets_reweighted')
    
    
#    otherHists.append('TTbarPlusMetAnalysis/MuPlusJets/Ref selection/Vertices/nVertex')
#    otherHists.append('TTbarPlusMetAnalysis/MuPlusJets/Ref selection/Vertices/nVertex_reweighted')
#    
#    otherHists.append('TTbarPlusMetAnalysis/MuPlusJets/Ref selection/N_BJets')
#    otherHists.append('TTbarPlusMetAnalysis/MuPlusJets/Ref selection/N_BJets_reweighted')

    
    if len(otherHists) > 0:
        otherHists = HistGetter.getHistsFromFiles(otherHists, files)
        hists = HistGetter.joinHistogramDictionaries([hists,
                                                  otherHists])
    gcd = gROOT.cd

    reverseMCOrdertmp = reverseMCOrder
    for histname in hists[hists.keys()[0]]:
        reverseMCOrder = reverseMCOrdertmp
        print '=' * 70
        print 'Plotting:', histname
        gcd()
        if 'MuPlusJets' in histname or 'Muon' in histname:
            used_data = 'SingleMu'
        else:
            used_data = 'SingleElectron'
        hist_data = hists[used_data][histname]
        
        for dataset in hists:
            if not 'Single' in dataset:
                scale = lumi * datasetInfo[dataset]['cross-section']/datasetInfo[dataset]['NumberOfProcessedEvents']
#                print dataset, '[', histname, '] is scaled by ', scale
                hists[dataset][histname].Scale(scale)

        hist_ttbar = hists['TTJet'][histname]
        #hist_wjets = hists['WJetsToLNu'][histname]
        hist_wjets = hists['W1Jet'][histname]
        hist_wjets += hists['W2Jets'][histname] 
        hist_wjets += hists['W3Jets'][histname] 
        hist_wjets += hists['W4Jets'][histname]
        #hist_zjets = hists['DYJetsToLL'][histname]
        hist_zjets = hists['DY1JetsToLL'][histname]
        hist_zjets += hists['DY2JetsToLL'][histname]
        hist_zjets += hists['DY3JetsToLL'][histname]
        hist_zjets += hists['DY4JetsToLL'][histname]

#        hist_muQCD = hists['QCD_Pt-20_MuEnrichedPt-15'][histname]
        hist_muQCD = hists['QCD_Pt-15to20_MuEnrichedPt5'][histname]
        hist_muQCD += hists['QCD_Pt-20to30_MuEnrichedPt5'][histname]
        hist_muQCD += hists['QCD_Pt-30to50_MuEnrichedPt5'][histname]
        hist_muQCD += hists['QCD_Pt-50to80_MuEnrichedPt5'][histname]
        hist_muQCD += hists['QCD_Pt-80to120_MuEnrichedPt5'][histname]
        hist_muQCD += hists['QCD_Pt-120to170_MuEnrichedPt5'][histname]
        hist_muQCD += hists['QCD_Pt-170to300_MuEnrichedPt5'][histname]
        hist_muQCD += hists['QCD_Pt-300to470_MuEnrichedPt5'][histname]
        hist_muQCD += hists['QCD_Pt-470to600_MuEnrichedPt5'][histname]
#        hist_muQCD += hists['QCD_Pt-600to800_MuEnrichedPt5'][histname]
        hist_muQCD += hists['QCD_Pt-800to1000_MuEnrichedPt5'][histname]
        hist_muQCD += hists['QCD_Pt-1000_MuEnrichedPt5'][histname]
        hist_bce1 = hists['QCD_Pt_20_30_BCtoE'][histname]
        hist_bce2 = hists['QCD_Pt_30_80_BCtoE'][histname]
        hist_bce3 = hists['QCD_Pt_80_170_BCtoE'][histname]
        hist_bce4 = hists['QCD_Pt_170_250_BCtoE'][histname]
        hist_bce5 = hists['QCD_Pt_250_350_BCtoE'][histname]
        hist_bce6 = hists['QCD_Pt_350_BCtoE'][histname]        
        hist_enri1 = hists['QCD_Pt_20_30_EMEnriched'][histname]
        hist_enri2 = hists['QCD_Pt_30_80_EMEnriched'][histname]
        hist_enri3 = hists['QCD_Pt_80_170_EMEnriched'][histname]
        hist_enri4 = hists['QCD_Pt_170_250_EMEnriched'][histname]
        hist_enri5 = hists['QCD_Pt_250_350_EMEnriched'][histname]
        hist_enri6 = hists['QCD_Pt_350_EMEnriched'][histname]        
        hist_pj4 = hists['GJets_HT-200To400'][histname]
        hist_pj5 = hists['GJets_HT-400ToInf'][histname]
        hist_singleTop = hists['T_tW-channel'][histname] + hists['T_t-channel'][histname] + hists['T_s-channel'][histname]
        hist_singleTop += hists['Tbar_t-channel'][histname] + hists['Tbar_s-channel'][histname] + hists['Tbar_tW-channel'][histname]
        
        #hist_diboson = hists['ww'][histname] + hists['wz'][histname] + hists['zz'][histname]
        
#        hist_ttbarZ = hists['TTbarZIncl'][histname]
#        hist_ttbarW = hists['TTbarInclWIncl'][histname]

        hist_qcd = hist_bce1 + hist_bce2 + hist_bce3 + hist_bce4 + hist_bce5 + hist_bce6
        hist_qcd += hist_enri1 + hist_enri2 + hist_enri3 + hist_enri4 + hist_enri5 + hist_enri6
        hist_qcd += hist_pj4 + hist_pj5
                
        nqcd = hist_qcd.Integral();
        shapeErrorHist = None 
        errorHist = None
        relativeQCDEstimationError = 0
        #TODO: fix this for muon+jets
        if 'EPlusJets' in histname or 'Electron' in histname:
            qcdFromMCPlots = 'QCD' in histname or 'Vertices' in histname or 'jets' in histname.split('/')[-1].lower() or  'bjet_invariant_mass' in histname      
            if ('MttbarAnalysis' in histname or 'TTbarPlusMetAnalysis' in histname):
                qcdRateEstimate = 'TTbarPlusMetAnalysis/EPlusJets/QCD e+jets PFRelIso/Electron/electron_pfIsolation_03'
                currentBin = HistPlotter.getBjetBin(histname)
                estimate, err = QCDEstimation.getQCDEstimate(files[used_data],
                                                             bjetBin=currentBin,
                                                             histogramForEstimation=qcdRateEstimate,
                                                             function='expo')
                if not estimate == 0:
                    relativeQCDEstimationError = err / estimate
                print 'Estimated QCD background: %.1f +- %.1f' % (estimate, err)
                if(hist_qcd.Integral() > 0 and estimate >= 0):
                    hist_qcd.Scale(estimate / hist_qcd.Integral())

                if not qcdFromMCPlots:#take QCD shape from data
                    currentBin = HistPlotter.getBjetBin(histname)
                    qcdShapeFromData = histname.replace('Ref selection', 'QCDConversions')
                    qcdShapeFromData = qcdShapeFromData.replace(currentBin, '0btag')
                    qcdShapeComparison = histname.replace('Ref selection', 'QCD non iso e+jets')
                    qcdShapeComparison = qcdShapeComparison.replace(currentBin, '0btag')
                    
                    print "Taking QCD shape from DATA (%s)" % qcdShapeFromData
                    qcdHists = HistGetter.getHistsFromFiles([qcdShapeFromData], {'data':files[used_data]})
                    
                    nQCD = hist_qcd.Integral()
                    hist_qcd = qcdHists['data'][qcdShapeFromData]
                    nShape = hist_qcd.Integral()
                    if nShape > 0:
                        hist_qcd.Scale(nQCD / nShape)
                    files['data'] = files['SingleElectron']
                    shapeErrorHist = QCDEstimation.getShapeErrorHistogram(files,
                                                                      histogramForShape=qcdShapeFromData,
                                                                      histogramForComparison=qcdShapeComparison)
                    
            
                
        if 'MuPlusJets' in histname or 'Muon' in histname:
            nQCD = hist_muQCD.Integral()
            #scale by factor, TODO: implement on-the-fly scale factor
            nMuQCD = nQCD*1.21
            print 'Estimated QCD background: %.1f' % nMuQCD
            #get template from anti-isolated region
            qcdFromMCPlots = 'QCD' in histname or 'Vertices' in histname or 'jets' in histname.split('/')[-1].lower() or 'bjet_invariant_mass' in histname 
                    
            if ('MttbarAnalysis' in histname or 'TTbarPlusMetAnalysis' in histname) and not qcdFromMCPlots:
                qcdShapeFromData = histname.replace('Ref selection', 'QCD non iso mu+jets')
                currentBin = HistPlotter.getBjetBin(histname)
                qcdShapeFromData = qcdShapeFromData.replace(currentBin, '0btag')
                print 'QCD shape from Data:', qcdShapeFromData
                qcdHists = HistGetter.getHistsFromFiles([qcdShapeFromData], {'data':files[used_data]})
                hist_muQCD = qcdHists['data'][qcdShapeFromData]
                
            nShape = hist_muQCD.Integral()
            if nShape > 0:
                hist_muQCD.Scale(nMuQCD / nShape)
                
        if performRescale:        
            print '=' * 100
            print '"best" rescaling for', histname
            rescales = rescaleMC(hist_data, hist_ttbar, hist_wjets, hist_qcd)
            print rescales
            print '=' * 100
            topScale = rescales[1]['ttbar']
            wjetScale = rescales[1]['wjets']
            qcdScale2 = rescales[1]['qcd']

            hist_ttbar.Scale(topScale)
            hist_wjets.Scale(wjetScale)
            hist_qcd.Scale(qcdScale2) 
        
        

        rebin = 1;
        Urange = (0, 5000)
        if ("mttbar" in histname):
            hist_data.SetXTitle("m(t#bar{t}) [GeV]");
            rebin = 50;
            hist_data.SetYTitle("Events/(%f GeV)" % hist_data.GetBinWidth(1) * rebin);
            if setLogY:
                Urange = (300, 3000)
            else:
                Urange = (300, 1500)
        if "All_Electron_mvaTrigV0" in histname:
            hist_data.SetXTitle("mva disc");
            hist_data.SetYTitle("Events/(0.01)");
            rebin = 1;
            Urange = (-1.1, 1.1)
        elif ("m3" in histname):
            hist_data.SetXTitle("M3 [GeV]");
            hist_data.SetYTitle("Events/(5 GeV)");
            rebin = 5;
            Urange = (0, 500)
        elif (histname == "electron_et"):
            hist_data.SetXTitle("electron p_{T} [GeV]");
            hist_data.SetYTitle("Events/(5 GeV)");
            rebin = 5;
        elif ("ttbar_pt" in histname):
            hist_data.SetXTitle("p_{T} of t#bar{t} system [GeV]");
            hist_data.SetYTitle("Events/(10 GeV)");
            rebin = 10;
            if setLogY:
                Urange = (0, 700)
            else:
                Urange = (0, 300)
        elif ("ttbar_px" in histname):
            hist_data.SetXTitle("p_{x} of t#bar{t} system [GeV]");
            hist_data.SetYTitle("Events/(10 GeV)");
            rebin = 10;
            Urange = (0, 1000)
        elif ("ttbar_py" in histname):
            hist_data.SetXTitle("p_{y} of t#bar{t} system [GeV]");
            hist_data.SetYTitle("Events/(10 GeV)");
            rebin = 10;
            Urange = (0, 1000)
        elif ("ttbar_pz" in histname):
            hist_data.SetXTitle("p_{z} of t#bar{t} system [GeV]");
            hist_data.SetYTitle("Events/(50 GeV)");
            rebin = 50;
            Urange = (0, 2500)
        elif ("HT" in histname):
            hist_data.SetXTitle("#Sigma p_{T} [GeV]");
            hist_data.SetYTitle("Events/(50 GeV)");
            rebin = 50;
            Urange = (0, 2500)
        elif (histname == "numberOfJets"):
            hist_data.SetXTitle("number of jets");
            hist_data.SetYTitle("Events");
        elif (histname == "numberOfBJets"):
            hist_data.SetXTitle("number of b-tagged jets (SSVHE medium)");
            hist_data.SetYTitle("Events");
        elif ('MET_' in histname and not 'phi' in histname and not 'Angle_lepton_MET' in histname):
            hist_data.SetXTitle("E_{T}^{miss} [GeV]");
            hist_data.SetYTitle("Events/(5 GeV)");
            rebin = 5
            if setLogY:
                Urange = (200, 590)
                rebin = 10
                hist_data.SetYTitle("Events/(10 GeV)")
            else:
                Urange = (0, 195)
        elif ('MET_phi' in histname):
            hist_data.SetXTitle("#phi(E_{T}^{miss})");
            hist_data.SetYTitle("Events/(0.1)");
            rebin = 1;
            Urange = (-4, 4)
        elif 'Angle_lepton_MET' in histname:
            hist_data.SetXTitle("angle(l,E_{T}^{miss})");
            hist_data.SetYTitle("Events/0.05");
            rebin = 5;
            Urange = (0, 3.2)
        elif 'METsignificance' in histname:
            hist_data.SetXTitle("METsignificance [GeV]");
            hist_data.SetYTitle("Events/(5 GeV)");
            rebin = 5;
            Urange = (0, 200)
        elif ("mtW" in histname or 'Transverse_Mass' in histname):
            hist_data.SetXTitle("transverse W-boson mass [GeV]");
            
            if setLogY:
                Urange = (0, 500)
                hist_data.SetYTitle("Events/(10 GeV)");
                rebin = 10;
            else:
                Urange = (0, 200)
                hist_data.SetYTitle("Events/(5 GeV)");
                rebin = 5;
        elif ("electronD0" in histname):
            hist_data.SetXTitle("electron d_{0} / cm");
            hist_data.SetYTitle("Events/(0.001 cm)");
            rebin = 10;
        elif ("angleTops" in histname):
            hist_data.SetXTitle("angle between top quarks");
            hist_data.SetYTitle("Events/(0.1 rad)");
            rebin = 20;
        elif ("neutrino_pz" in histname):
            hist_data.SetXTitle("neutrino p_{Z} [GeV]");
            hist_data.SetYTitle("Events/(10 GeV)");
            rebin = 10;
            Urange = (-500, 500)
        elif ('hadronicTopMass' in histname or 'leptonicTopMass' in histname or 'mAllTop' in histname):
            hist_data.SetXTitle("top mass [GeV]");
            if setLogY:
                Urange = (0, 1000)
                rebin = 10
                hist_data.SetYTitle("Events/(10 GeV)")
            else:
                rebin = 5
                hist_data.SetYTitle("Events/(5 GeV)")
                Urange = (0, 500)
        elif 'hadronicWMass' in histname:
            hist_data.SetXTitle("W mass [GeV]");
            hist_data.SetYTitle("Events/(5 GeV)");
            rebin = 5;
            if setLogY:
                Urange = (0, 350)
            else:
                Urange = (0, 200)
        elif ('pt_leadingTop' in histname or 'pt_NextToLeadingTop' in histname):
            hist_data.SetXTitle("top p_{T} [GeV]");
            hist_data.SetYTitle("Events/(20 GeV)");
            rebin = 20;
        elif('QCDest_CombRelIso' in histname):
            hist_data.SetXTitle("relative isolation");
            hist_data.SetYTitle("Events/(0.1)");
            rebin = 10;
            Urange = (0, 2)
        elif('pfisolation' in histname.lower()):
            hist_data.SetXTitle("Relative isolation");
            hist_data.SetYTitle("Events/(0.1)");
            rebin = 10
            Urange = (0, 2)
            reverseMCOrder = False
        elif 'DirectionalIsolation' in histname:
            hist_data.SetXTitle("directional isolation");
            hist_data.SetYTitle("Events/(0.01)");
            rebin = 10
            Urange = (0, 2)
        elif 'DirectionalIsolationWithGaussianFallOff' in histname:
            hist_data.SetXTitle("directional isolation");
            hist_data.SetYTitle("Events/(0.01)");
            rebin = 10
            Urange = (0, 2)
        elif('diElectron' in histname):
            hist_data.SetXTitle("m(ee)");
            hist_data.SetYTitle("Events/(10 GeV)");
            rebin = 10
            Urange = (50, 2000)
        elif 'JetMass' in histname:
            hist_data.SetXTitle("m(jet)");
            hist_data.SetYTitle("Events/(5 GeV)");
            rebin = 5
            Urange = (0, 200)
        elif 'nVertex' in histname:
            Urange = (0, 21)
        elif 'electron_pT' in histname:
            hist_data.SetXTitle("p_{T}(e)");
            hist_data.SetYTitle("Events/(5 GeV)");
            rebin = 5
            if setLogY:
                Urange = (0, 500)
            else:
                print 'electron_pt, 200'
                Urange = (0, 200)
        elif 'muon_pT' in histname:
            hist_data.SetXTitle("p_{T}(#mu)");
            hist_data.SetYTitle("Events/(5 GeV)");
            rebin = 5
            if setLogY:
                Urange = (0, 500)
            else:
                print 'electron_pt, 200'
                Urange = (0, 200)
        elif 'electron_eta' in histname:
            hist_data.SetYTitle("Events/(0.2)");
            rebin = 10
            Urange = (-3, 3)
        elif 'N_BJets' in histname:
            Urange = (0, 5)
            hist_data.SetXTitle("B-tag Multiplicity");
            hist_data.SetYTitle("Events")
        elif 'N_Jets' in histname:
            hist_data.SetYTitle("Events");
            hist_data.SetXTitle("Jet Multiplicity");
            Urange = (4, 14)
        elif 'bjet_invariant_mass' in histname:
            Urange = (0, 600)
            rebin = 20
            hist_data.SetYTitle("Events/(20 GeV)");
            hist_data.SetXTitle("m(b,b)");
        if 'electron_AbsEta' in histname:
            hist_data.SetYTitle("Events/(0.1)");
            hist_data.SetXTitle("|#eta(e)|");
            rebin = 10
            Urange = (0, 2.5)
        if 'muon_AbsEta' in histname:
            hist_data.SetYTitle("Events/(0.1)");
            hist_data.SetXTitle("|#eta(#mu)|");
            rebin = 10
            Urange = (0, 2.5)
        if normalise:
            title = hist_data.GetYaxis().GetTitle()
            title = title.replace('Events', 'normalised to unit area')
            title = title.replace('events', 'normalised to unit area')
            hist_data.SetYTitle(title);
        hist_data.Rebin(rebin);
        hist_ttbar.Rebin(rebin);
        hist_wjets.Rebin(rebin);
        hist_zjets.Rebin(rebin);
        hist_qcd.Rebin(rebin);
#        hist_ttbarW.Rebin(rebin)
#        hist_ttbarZ.Rebin(rebin)
        hist_muQCD.Rebin(rebin);
        hist_singleTop.Rebin(rebin)
#        hist_diboson.Rebin(rebin)
        hist_data.SetAxisRange(Urange[0], Urange[1]);
        hist_ttbar.SetAxisRange(Urange[0], Urange[1]);
        hist_wjets.SetAxisRange(Urange[0], Urange[1]);
        hist_zjets.SetAxisRange(Urange[0], Urange[1]);
        hist_qcd.SetAxisRange(Urange[0], Urange[1]);
        hist_muQCD.SetAxisRange(Urange[0], Urange[1]);
        hist_singleTop.SetAxisRange(Urange[0], Urange[1]);
#        hist_diboson.SetAxisRange(Urange[0], Urange[1]);

        if shapeErrorHist:
            shapeErrorHist.Rebin(rebin)
            shapeErrorHist.SetAxisRange(Urange[0], Urange[1])
        hist_data.SetMarkerStyle(8);
        
        hist_ttbar.SetFillStyle(1001);
        hist_ttbar.SetFillColor(kRed + 1);
#        hist_ttbar.SetLineColor(kRed + 1);
        hist_wjets.SetFillStyle(1001);
        hist_wjets.SetFillColor(kGreen - 3);
#        hist_wjets.SetLineColor(kGreen - 3);
        hist_zjets.SetFillStyle(1001);
        hist_zjets.SetFillColor(kAzure - 2);
#        hist_zjets.SetLineColor(kAzure - 2);
        hist_qcd.SetFillStyle(1001);
        hist_qcd.SetFillColor(kYellow);
#        hist_qcd.SetLineColor(kYellow);
        hist_muQCD.SetFillStyle(1001);
        hist_muQCD.SetFillColor(kYellow);
        hist_singleTop.SetFillStyle(1001);
        hist_singleTop.SetFillColor(kMagenta)
#        hist_singleTop.SetLineColor(kMagenta)
        
#        hist_diboson.SetFillStyle(1001);
#        hist_diboson.SetFillColor(kWhite)
#        hist_diboson.SetLineColor(kWhite)
        
#        hist_ttbarZ.SetLineColor(kCyan-4)
#        hist_ttbarZ.SetFillStyle(4000)
#        hist_ttbarZ.SetLineWidth(3)
#        hist_ttbarZ.SetLineStyle(4);
#        hist_ttbarW.SetLineColor(kBlue +2)
#        hist_ttbarW.SetFillStyle(4000)
#        hist_ttbarW.SetLineWidth(3)
#        hist_ttbarW.SetLineStyle(4);
        allMC = hist_ttbar.Clone('allMC')
        allMC += hist_wjets + hist_zjets + hist_singleTop# + hist_diboson
        if 'MuPlusJets' in histname or 'Muon' in histname:
            allMC += hist_muQCD
        else:
            allMC += hist_qcd
            
#        ttbarErr = allMC.Clone('ttbar_err')
#        for bin_i in range(1, ttbarErr.GetNbinsX()):
#            value = hist_ttbar.GetBinContent(bin_i)
#            error = value * ttbarUncertainty
#            ttbarErr.SetBinError(bin_i, error)
#        ttbarErr.SetFillColor(kGray +2)
#        ttbarErr.SetMarkerStyle(0)
#        ttbarErr.SetFillStyle(3001);

        leg = TLegend(0.696, 0.95, 0.94, 0.55)
        leg.SetBorderSize(0)
        leg.SetLineStyle(0)
        leg.SetTextFont(42)
        leg.SetFillStyle(0)

        leg.AddEntry(hist_data, "data", "P")
        #        leg.AddEntry(hist_data2, "data(no HLT)", "P");
        leg.AddEntry(hist_ttbar, "t#bar{t}", "f")
        leg.AddEntry(hist_singleTop, "Single-Top", "f")
        leg.AddEntry(hist_wjets, "W#rightarrowl#nu", "f")
        leg.AddEntry(hist_zjets, "Z/#gamma*#rightarrowl^{+}l^{-}", "f")
        if 'MuPlusJets' in histname or 'Muon' in histname:
            leg.AddEntry(hist_muQCD, "QCD #mu Enriched", "f")
        else:
            leg.AddEntry(hist_qcd, "QCD/#gamma + jets", "f")
#        leg.AddEntry(hist_diboson, "VV + X", "f")
#        leg.AddEntry(hist_ttbarW, "t#bar{t} + W x 100", "f")
#        leg.AddEntry(hist_ttbarZ, "t#bar{t} + Z x 100", "f")
        #leg.AddEntry(ttbarErr, "t#bar{t} uncertainty", 'F')
        
        if normalise:
            if 'EPlusJets' in histname or 'Electron' in histname:
                normalisePlotsToUnitArea(hist_data, hist_ttbar, hist_wjets, hist_zjets, hist_singleTop, hist_qcd)#, hist_diboson)
            else:
                normalisePlotsToUnitArea(hist_data, hist_ttbar, hist_wjets, hist_zjets, hist_singleTop, hist_muQCD)#, hist_diboson)
            

        
        canvases.append(TCanvas("cname" + histname, histname, 1600, 1200))
        canvases[-1].cd().SetRightMargin(0.04);
        if 'TTbarPlusMetAnalysis' in histname and not 'QCD' in histname and 'EPlusJets' in histname:
#            relativeQCDEstimationError = 0
            errorHist = QCDEstimation.createErrorHistogram([hist_ttbar, hist_wjets, hist_zjets, hist_singleTop, hist_qcd],
                                                       hist_qcd, relativeQCDEstimationError, shapeErrorHist)
        
        hs = THStack("MC", "MC");
        if reverseMCOrder:
            hs.Add(hist_ttbar);
            hs.Add(hist_wjets);
            hs.Add(hist_zjets);
            hs.Add(hist_singleTop);
            if 'MuPlusJets' in histname or 'Muon' in histname:
                hs.Add(hist_muQCD);
            else:
                hs.Add(hist_qcd);
#            hs.Add(hist_diboson);
        
        else:
 #           hs.Add(hist_diboson);
            if 'MuPlusJets' in histname or 'Muon' in histname:
                hs.Add(hist_muQCD);
            else:
                hs.Add(hist_qcd);
            hs.Add(hist_zjets);
            hs.Add(hist_wjets);
            hs.Add(hist_singleTop);
            hs.Add(hist_ttbar);
        max = 0
        if hs.GetMaximum() > hist_data.GetMaximum():
            max = hs.GetMaximum() * 1.4
        else:
            max = hist_data.GetMaximum() * 1.4
        hist_data.GetYaxis().SetRangeUser(0, max);
        if setLogY:
            hist_data.GetYaxis().SetRangeUser(0.1, max);
        hist_data.Draw('error');
        hs.Draw("hist same");
#        hist_ttbarW.Draw("same")
#        hist_ttbarZ.Draw("same")
        setErrorX = TExec('ex1', 'gStyle->SetErrorX(0.5);')
        unsetErrorX = TExec('ex2', 'gStyle->SetErrorX(0.);')
#        gStyle.SetErrorX(0.5);
        setErrorX.Draw()
#        ttbarErr.Draw('e2 same')
        unsetErrorX.Draw()
#        gStyle.SetErrorX(0);
        if errorHist and drawQCDError:
            gStyle.SetErrorX(0.5);
            errorHist.SetFillColor(kGray + 3)
            errorHist.SetMarkerStyle(0)
            errorHist.SetFillStyle(3001);
            leg.AddEntry(errorHist, "QCD uncertainty")
            errorHist.Draw('e2 same')
        else:
            gStyle.SetErrorX(0);
            
        hist_data.Draw("error same");
        leg.Draw();
        text1, text2 = HistPlotter.get_cms_label(lumiInInvPb=lumi, njet=HistPlotter.getJetBin(histname),
                                          nbjet=HistPlotter.getBjetBin(histname), channel=HistPlotter.getChannel(histname))
        text1.Draw();
        text2.Draw();
#
        postfix = ''
        if setLogY:
            canvases[-1].SetLogy(1)
            postfix = '_log'
        if normalise:
            postfix = postfix + '_norm'
        if custom_suffix:
            postfix += '_' + custom_suffix
            
        prefix = 'EPlusJets_'
        if 'MuPlusJets' in histname or 'Muon' in histname:
            prefix = 'MuPlusJets_'
        selectionLabel = ''
        if 'Ref' in histname:
            selectionLabel = 'Ref_'
        if 'QCDConversions' in histname:
            selectionLabel = 'QCDConversions_'
        if 'QCD non iso e+jets' in histname:
            selectionLabel = 'AntiIsolated_'
            
        name = ''.join(histname[:histname.rfind('/') + 1]) + prefix + selectionLabel + ''.join(histname[histname.rfind('/') + 1:]) + postfix
        #specific selections
        
        
#        fullName = name.replace('EPlusJets', 'EPlusJets' + selectionLabel) + postfix
        saveAs(canvas=canvases[-1], name=name, outputFormats=outputFormats, outputFolder=savePath)
        del canvases[-1]
    #    hltFiles['data'] = inputFiles.files['data']
    #    hltFiles['ttbar'] = inputFiles.files['ttbar']

    qcdPlots = [  #MET
        #                'TTbarEplusJetsPlusMetAnalysis/Ref selection/QCDConversions/MET/patType1CorrectedPFMet/MET',
        #                'TTbarEplusJetsPlusMetAnalysis/Ref selection/QCD non iso e+jets/MET/patType1CorrectedPFMet/MET',
        'TTbarPlusMetAnalysis/EPlusJets/QCDConversions/Electron/electron_AbsEta',
        'TTbarPlusMetAnalysis/EPlusJets/QCD non iso e+jets/Electron/electron_AbsEta',
        #                'TTbarEplusJetsPlusMetAnalysis/Ref + AsymJets selection/QCD non iso e+jets, non iso trigger/MET/patMETsPFlow/MET',
        #                'TTbarEplusJetsPlusMetAnalysis/Ref + AsymJets selection/QCD e+jets PFRelIso/Electron/electron_pfIsolation_03',
        #                'TTbarEplusJetsPlusMetAnalysis/Ref + AsymJets selection/QCD e+jets PFRelIso, non iso trigger/Electron/electron_pfIsolation_03'
    ]
    #    triggerPlots = ['HLTStudy/' + trigger + '/' + variable + '_' + modifier for trigger in triggers for variable in triggerVariables for modifier in triggerModifiers]

    HistPlotter.setStyle()
    hists = HistGetter.getHistsFromFiles(qcdPlots, files, bJetBins=['0btag'])
    #    hists = HistGetter.getHistsFromFiles(triggerPlots, hltFiles, jetBins=HistPlotter.allJetBins)
    #    hists = HistGetter.addSampleSum( hists )
    #
    #    hists = HistPlotter.applyDefaultStylesAndColors( hists )
    #
    #    hists = HistPlotter.rebin(hists, 50, 'mttbar*')
    #    hists = HistPlotter.setXRange(hists, (300, 1800), 'mttbar*')
    ###
    #    hists = HistPlotter.setXTitle(hists, 'm(t#bar{t})/GeV', 'mttbar*')
    #    hists = HistPlotter.setYTitle(hists, 'Events/(50 GeV)', 'mttbar*')
    #
    #    hists = HistGetter.makeMCStack( hists )
    #    hists = HistGetter.makeDetailedMCStack( hists )

    #    hists = HistPlotter.rebin(hists, 5, 'MET*')
        'BJetAnalysis/SoftMuonByIP3d_udsQuarkJets',
        'BJetAnalysis/SoftMuonByPt_allJets',
        'BJetAnalysis/SoftMuonByPt_bQuarkJets',
        'BJetAnalysis/SoftMuonByPt_cQuarkJets',
        'BJetAnalysis/SoftMuonByPt_gluonJets',
        'BJetAnalysis/SoftMuonByPt_udsQuarkJets',
        'BJetAnalysis/TrackCountingHighEfficiency_allJets',
        'BJetAnalysis/TrackCountingHighEfficiency_bQuarkJets',
        'BJetAnalysis/TrackCountingHighEfficiency_cQuarkJets',
        'BJetAnalysis/TrackCountingHighEfficiency_gluonJets',
        'BJetAnalysis/TrackCountingHighEfficiency_udsQuarkJets',
        'BJetAnalysis/TrackCountingHighPurity_allJets',
        'BJetAnalysis/TrackCountingHighPurity_bQuarkJets',
        'BJetAnalysis/TrackCountingHighPurity_cQuarkJets',
        'BJetAnalysis/TrackCountingHighPurity_gluonJets',
        'BJetAnalysis/TrackCountingHighPurity_udsQuarkJets',
    ]

    bTagFiles = {}

    #bTagFiles['data'] = inputFiles.files['data']
    bTagFiles[
        'ttbar'] = '/storage/results/histogramfiles/TTJet_50000pb_PFElectron_PFMuon_PF2PATJets_PFMET.root'

    #histNames = ['BJetAnalysis/' + histName for histName in histNames]
    HistPlotter.setStyle()
    hists = HistGetter.getHistsFromFiles(
        histNames, bTagFiles)  #returns a dictionary of form {histname:hist}
    makeBTagPlots(hists['ttbar'])
    makeEfficiencyPlots()
示例#10
0
class QCDEstimator:
    luminosity = 36.145#pb-1
    mc_luminosity = 36.145#pb-1
    luminosity_unit = 'pb-1'
    scale = luminosity / mc_luminosity
    jetBins = ['0jet', 'allJets', '1jet', '1orMoreJets', '2jets', '2orMoreJets', '3jets', '3orMoreJets', '4orMoreJets']
    jetBinsLatex = {'0jet':'0 jet', 'allJets':'#geq 0 jets', '1jet':'1 jet', '1orMoreJets':'#geq 1 jet',
                    '2jets':'2 jets', '2orMoreJets':'#geq 2 jets', '3jets':'3 jets', '3orMoreJets':'#geq 3 jets',
                    '4orMoreJets':'#geq 4 jets'}
    binWidth = 0.01
    rebin = 10
    fitRangesClosureTest = [ ( 0.1, 0.9 ), ( 0.1, 1.0 ), ( 0.1, 1.1 ),
                  ( 0.2, 0.9 ), ( 0.2, 1.0 ), ( 0.2, 1.1 ),
                  ( 0.3, 0.9 ), ( 0.3, 1.0 ), ( 0.3, 1.1 )]

    fitRangesEstimation = [ ( 0.1, 1.1 ), ( 0.2, 1.1 )]#, ( 0.3, 1.1 )]
    signalRegion = ( 0, 0.1 )
    maxValue = 1.6
    pfIsoHistogramPrefix = 'QCDest_PFIsolation_WithMETCutAndAsymJetCuts_'
    pfIsoControlRegionHistogramPrefix = 'QCDest_PFIsolation_controlRegion2_WithMETCutAndAsymJetCuts_'
    relIsoHistogramPrefix = 'QCDest_CombRelIso_'
    pfIsoResults = {}
    relIsoResults = {}
    allPfIsoResults = {}
    allRelIsoResults = {}

    useEntryAsData = 'data'
    constrainFit = False
    numberOfFreeParameters = 0

    currentFitRange = ( 0.1, 1.6 )
    currentFitFuntion = 'gaus'
    currentJetBin = jetBins[-1]

    outputFormat = 'pdf'
    outputFolder = ''

    def __init__( self, files ):
        self.files = files
        HistGetter.samplefiles = files
        self.histograms = {}
        self.histGetter = HistGetter()
        self.histGetter.setStyle()
        self.getHistograms()
        self.applyStyleAndCreateStack()

        for bin in self.jetBins:
            self.pfIsoResults[bin] = {'actualNumberOfQCDEvents': 0, 'estimatedNumberOfQCDEvents':0,
                                      'fitFunction': None, 'fitParameters': {}, 'numberOfAllDataEvents':0,
                                      'numberOfAllMCEvents':0}
            self.relIsoResults[bin] = {'actualNumberOfQCDEvents': 0, 'estimatedNumberOfQCDEvents':0,
                                       'fitFunction': None, 'fitParameters': {}, 'numberOfAllDataEvents':0,
                                       'numberOfAllMCEvents':0}

    def getHistograms( self ):
        relIsoHists = [self.relIsoHistogramPrefix + jetbin for jetbin in self.jetBins]
        pfIsoHists = [self.pfIsoHistogramPrefix + jetbin for jetbin in self.jetBins]
        pfIsoControlHists = [self.pfIsoControlRegionHistogramPrefix + jetbin for jetbin in self.jetBins]
        allHists = relIsoHists
        allHists.extend( pfIsoHists )
        allHists.extend( pfIsoControlHists )
        HistGetter.hists = allHists

        self.histograms = self.histGetter.getHistsFromFiles()
        self.histograms = self.histGetter.addSampleSum( self.histograms )

    def applyStyleAndCreateStack( self ):
        samplesOfInterest = ['data', 'qcd', 'zjets', 'wjets', 'singleTop', 'ttbar', 'allMC']
        colors = {'ttbar' :  kRed + 1,
                  'wjets' :  kGreen - 3,
                  'zjets' :  kAzure - 2,
                  'qcd' :  kYellow,
                  'singleTop' :  kMagenta}

        mcStack = {}

        for sample in samplesOfInterest:#sample
            for histname in self.histograms[sample].keys():
                self.histograms[sample][histname].Rebin( self.rebin )
                if not sample in ['data', 'allMC']:
                    self.histograms[sample][histname].Scale( self.scale )
                    self.histograms[sample][histname].SetFillStyle( 1001 )
                    self.histograms[sample][histname].SetFillColor( colors[sample] )
                    if not mcStack.has_key( histname ):
                        mcStack[histname] = THStack( "MC_" + histname, "MC_" + histname );
                    mcStack[histname].Add( self.histograms[sample][histname] )
                else:
                    self.histograms[sample][histname].SetMarkerStyle( 8 );


        self.histograms['MCStack'] = mcStack
        self.setStyle()
        print "=" * 40
        print "data integrated luminosity:", self.luminosity, self.luminosity_unit
        print "MC integrated luminosity:", self.mc_luminosity, self.luminosity_unit
        print "MC scale factor: ", self.scale
        print '=' * 40


    def setStyle( self ):
        tdrstyle = setTDRStyle();
        tdrstyle.SetPadRightMargin( 0.05 )#originally was 0.02, too narrow!
        tdrStyle.SetStatH( 0.2 );
        tdrStyle.SetOptStat( 0 );#off title
        tdrStyle.SetOptFit( 0 );#off title
        tdrStyle.cd();
        gROOT.ForceStyle();
        gStyle.SetTitleH( 0.1 );
        gStyle.SetStatH( 0.22 ); #0.24);
        gStyle.SetStatW( 0.22 ); #0.26);
        gStyle.SetOptStat( 1 ); #on stat
        gStyle.SetLineScalePS( 2 ); #D=3
        gStyle.SetOptFit( 112 );


    def doClosureTests( self, function = 'gaus' ):
        self.useEntryAsData = 'allMC'
        self.currentFitFuntion = function

        for fitRange in self.fitRangesClosureTest:
            self.currentFitRange = fitRange
            for bin in self.jetBins:
                self.currentJetBin = bin
                self.EstimateJetBin( bin )
                self.plot( self.pfIsoHistogramPrefix + bin, self.pfIsoResults[bin] )
                self.plot( self.relIsoHistogramPrefix + bin, self.relIsoResults[bin] )
                self.allPfIsoResults['%1.1f-%1.1f' % fitRange] = deepcopy( self.pfIsoResults )
                self.allRelIsoResults['%1.1f-%1.1f' % fitRange] = deepcopy( self.relIsoResults )
        self.plotClosureTest( self.pfIsoHistogramPrefix, self.allPfIsoResults )
        self.plotClosureTest( self.relIsoHistogramPrefix, self.allRelIsoResults )


    def doEstimate( self, function = 'gaus' ):
        self.useEntryAsData = 'data'
        self.currentFitFuntion = function

        for fitRange in self.fitRangesEstimation:
            self.currentFitRange = fitRange
            for bin in self.jetBins:
                self.currentJetBin = bin
                self.EstimateJetBin( bin )
                self.plot( self.pfIsoHistogramPrefix + bin, self.pfIsoResults[bin] )
                self.plot( self.relIsoHistogramPrefix + bin, self.relIsoResults[bin] )
                self.allPfIsoResults['%1.1f-%1.1f' % fitRange] = deepcopy( self.pfIsoResults )
                self.allRelIsoResults['%1.1f-%1.1f' % fitRange] = deepcopy( self.relIsoResults )

    def EstimateJetBin( self, jetbin ):
#        fitRange = self.currentFitRange
        function = self.currentFitFuntion

        QCD = self.histograms['qcd']
        data = self.histograms[self.useEntryAsData]
        allMC = self.histograms['allMC']
        pfIsoHist = self.pfIsoHistogramPrefix + jetbin
        relIsoHist = self.relIsoHistogramPrefix + jetbin

        self.pfIsoResults[jetbin]['actualNumberOfQCDEvents'] = QCD[pfIsoHist].GetBinContent(1)
        self.pfIsoResults[jetbin]['numberOfAllDataEvents'] = data[pfIsoHist].GetBinContent(1)
        self.pfIsoResults[jetbin]['numberOfAllMCEvents'] = allMC[pfIsoHist].GetBinContent(1)

        pfIsoFit = self.doFit( data[pfIsoHist] )
        self.pfIsoResults[jetbin]['fitFunction'] = pfIsoFit
        self.pfIsoResults[jetbin]['fitParameters'] = self.getFitParameters( pfIsoFit )
        estimate = 0
        if pfIsoFit:
            estimate = pfIsoFit.Integral( self.signalRegion[0], self.signalRegion[1] ) / ( self.binWidth * self.rebin )

        self.pfIsoResults[jetbin]['estimatedNumberOfQCDEvents'] = estimate

#---------------------------------------------------------------------------------------------------------------------- 
        self.relIsoResults[jetbin]['actualNumberOfQCDEvents'] = QCD[relIsoHist].GetBinContent(1)
        self.relIsoResults[jetbin]['numberOfAllDataEvents'] = data[relIsoHist].GetBinContent(1)
        self.relIsoResults[jetbin]['numberOfAllMCEvents'] = allMC[relIsoHist].GetBinContent(1)

        relIsoFit = self.doFit( data[relIsoHist] )
        self.relIsoResults[jetbin]['fitFunction'] = relIsoFit
        self.relIsoResults[jetbin]['fitParameters'] = self.getFitParameters( relIsoFit )

        estimate = relIsoFit.Integral( self.signalRegion[0], self.signalRegion[1] ) / ( self.binWidth * self.rebin )
        self.relIsoResults[jetbin]['estimatedNumberOfQCDEvents'] = estimate



    def doFit( self, histogram ):
        function = self.currentFitFuntion
        fitRange = self.currentFitRange

        if not self.constrainFit:
            histogram.Fit( function, "Q0", "ah", fitRange[0], fitRange[1] )

        else:
             ff = TF1( function, function, 0, 1 );
             self.numberOfFreeParameters = ff.GetNumberFreeParameters();

        return histogram.GetFunction( function )

    def plot( self, histname, results ):
        data = self.histograms[self.useEntryAsData][histname]
        mcStack = self.histograms['MCStack'][histname]
        fitFunction = results['fitFunction']
        if not fitFunction:
            print 'no fitfunction found'
            return;

        data.GetXaxis().SetRangeUser( 0, self.maxValue - 0.01 );
        fitFunction.SetLineColor( kRed );
        fitFunction.SetLineWidth( 2 )

        fitFunction2 = fitFunction.Clone()
        fitFunction2.SetLineColor( kBlue );
        fitFunction2.SetRange( self.signalRegion[0], self.signalRegion[1] );

        fitFunction3 = fitFunction.Clone()
        fitFunction3.SetLineColor( kBlue );
        fitFunction3.SetLineStyle( kDashed );
        fitFunction3.SetRange( self.signalRegion[1], self.currentFitRange[0] );

        canvas = TCanvas( "c1", "Iso fit", 1920, 1080 )
        data.Draw();

        max = 0
        if mcStack.GetMaximum() > data.GetBinContent( 1 ):
            max = mcStack.GetMaximum()*1.1
        else:
            max = data.GetBinContent( 1 ) * 1.1

        data.GetYaxis().SetRangeUser( 0, max );
        data.SetXTitle( "Relative Isolation" );
        data.SetYTitle( "Events/0.1" );
        # draw mc
        mcStack.Draw( "hist same" );
        data.Draw( "ae same" );

        fitFunction.Draw( "same" );
        fitFunction2.Draw( "same" );
        fitFunction3.Draw( "same" );

        label = self.add_cms_label( self.currentJetBin )
        label.Draw()

        legend = self.add_legend( histname )
#        legend.Draw()

        if self.currentFitFuntion == "pol1":
            out = "%s_fit_linear_from_0%.0f_%s" % ( histname, self.currentFitRange[0] * 10.0, self.useEntryAsData );
        else:
            out = "%s_fit_%s_from_%1.1f_to_%1.1f_%s" % ( histname, self.currentFitFuntion, self.currentFitRange[0],
                                            self.currentFitRange[1], self.useEntryAsData );
#        if self.outputFormat == 'pdf':
#            canvas.SaveAs( '%s.eps' % out );
#            gROOT.ProcessLine( ".!ps2pdf -dEPSCrop %s.eps" % out );
#            gROOT.ProcessLine( ".!rm -f %s.eps" % out );
#        else:
        canvas.SaveAs( '%s/%s.%s' % ( self.outputFolder, out, self.outputFormat ) )

        canvas.Close(); #crucial!


    def plotClosureTest( self, histname, results ):
        c2 = TCanvas( "c2", "QCD estimates", 1080, 1080 );
        x = array( 'd', [2, 3, 4] )
        jetBinsOfInterest = [#'1jet', 
                             '2jets', '3jets', '4orMoreJets']
        function = self.currentFitFuntion

        gStyle.SetMarkerSize( 1.7 );
        gStyle.SetMarkerStyle( 20 );
        c2.SetTopMargin( 0.1 );
#        c2.SetLeftMargin( 0.12 );
        c2.SetRightMargin( 0.35 );

        y = {}
        for fitRange in self.fitRangesClosureTest:
            range = '%1.1f-%1.1f' % fitRange
            y[range] = []
            for bin in jetBinsOfInterest:

                est = results[range][bin]['estimatedNumberOfQCDEvents']
                true = results[range][bin]['actualNumberOfQCDEvents']
                variation = 0
                if not true == 0:
                    variation = ( est - true ) / true
                y[range].append( variation )
        nbins = 3
        gr1 = TGraph( nbins, x, array( 'd', y['%1.1f-%1.1f' % self.fitRangesClosureTest[0]] ) )
        gr2 = TGraph( nbins, x, array( 'd', y['%1.1f-%1.1f' % self.fitRangesClosureTest[1]] ) )
        gr3 = TGraph( nbins, x, array( 'd', y['%1.1f-%1.1f' % self.fitRangesClosureTest[2]] ) )
        gr4 = TGraph( nbins, x, array( 'd', y['%1.1f-%1.1f' % self.fitRangesClosureTest[3]] ) )
        gr5 = TGraph( nbins, x, array( 'd', y['%1.1f-%1.1f' % self.fitRangesClosureTest[4]] ) )
        gr6 = TGraph( nbins, x, array( 'd', y['%1.1f-%1.1f' % self.fitRangesClosureTest[5]] ) )
        gr7 = TGraph( nbins, x, array( 'd', y['%1.1f-%1.1f' % self.fitRangesClosureTest[6]] ) )
        gr8 = TGraph( nbins, x, array( 'd', y['%1.1f-%1.1f' % self.fitRangesClosureTest[7]] ) )
        gr9 = TGraph( nbins, x, array( 'd', y['%1.1f-%1.1f' % self.fitRangesClosureTest[8]] ) )

        gr1.SetMarkerColor( kGreen + 1 );
        gr2.SetMarkerColor( kGreen + 2 );
        gr3.SetMarkerColor( kGreen + 3 );
        gr4.SetMarkerColor( kAzure + 7 );
        gr5.SetMarkerColor( kAzure - 3 );
        gr6.SetMarkerColor( kBlue );
        gr7.SetMarkerColor( kOrange );
        gr8.SetMarkerColor( kOrange - 1 );
        gr9.SetMarkerColor( kOrange - 6 );

        gStyle.SetTitleW( 0.9 );
        gStyle.SetTitleH( 0.05 )

        h = None
        if function == "gaus":
            h = TH1D( "h", "Variation of QCD estimates with fit range (Gaussian)", 4, 0.5, 4.5 );
        elif function == "pol3":
            h = TH1D( "h", "Variation of QCD estimates with fit range (Pol3)", 4, 0.5, 4.5 );
        elif function == "pol1":
            h = TH1D( "h", "Variation of QCD estimates with fit range (Pol1)", 4, 0.5, 4.5 );
        elif function == "landau":
            h = TH1D( "h", "Variation of QCD estimates with fit range (Landau)", 4, 0.5, 4.5 );

        h.SetStats( kFALSE ); # no statistics
        h.Draw();
        h.SetYTitle( "Deviation = (N_{QCD,est}-N_{QCD,true})/N_{QCD,true}" );
        h.GetYaxis().SetRangeUser( -1, 1 );
        h.GetXaxis().SetRangeUser( 1.5, 5.5 );
#        h.GetXaxis().SetBinLabel( 1, "1j" );
        h.GetXaxis().SetBinLabel( 2, "2j" );
        h.GetXaxis().SetBinLabel( 3, "3j" );
        h.GetXaxis().SetBinLabel( 4, "#geq4j" );
        h.GetXaxis().SetLabelSize( 0.07 );
        h.GetYaxis().SetTitleOffset( 1.3 );

        gr1.Draw( "P" );
        gr2.Draw( "P" ); #to superimpose graphs, do not re-draw axis
        gr3.Draw( "P" );
        gr4.Draw( "P" );
        gr5.Draw( "P" );
        gr6.Draw( "P" );
        gr7.Draw( "P" );
        gr8.Draw( "P" );
        gr9.Draw( "P" );

        c2.SetGrid( 1, 1 );

        leg = TLegend( 0.65, 0.1, 0.98, 0.9 );
        leg.SetFillColor( 0 );
        leg.AddEntry( gr1, "Free: 0.1-0.9", "p" );
        leg.AddEntry( gr2, "Free: 0.1-1.0", "p" );
        leg.AddEntry( gr3, "Free: 0.1-1.1", "p" );
        leg.AddEntry( gr4, "Free: 0.2-0.9", "p" );
        leg.AddEntry( gr5, "Free: 0.2-1.0", "p" );
        leg.AddEntry( gr6, "Free: 0.2-1.1", "p" );
        leg.AddEntry( gr7, "Free: 0.3-0.9", "p" );
        leg.AddEntry( gr8, "Free: 0.3-1.0", "p" );
        leg.AddEntry( gr9, "Free: 0.3-1.1", "p" );

        leg.Draw();

        c2.SaveAs( "%s/%s_qcd_estimate_%s.%s" % ( self.outputFolder, histname, function, self.outputFormat ) )

        setRange = h.GetYaxis().SetRangeUser
        saveAs = c2.SaveAs
        for limit in [1, 2, 3, 6, 8]:
            setRange( -limit, limit );
            saveAs( "%s/%s_qcd_estimate_%s_zoom_%d.%s" % ( self.outputFolder, histname, function, limit, self.outputFormat ) );

    def getFitParameters( self, fitFunction ):
        fitParameters = {'chi2':-1, 'numberOfdegreesOfFreedom': 0, 'constrain1': 0, 'constrain2': 0,
                         'constrain3': 0, 'constrain4': 0}
        if fitFunction:
            fitParameters['chi2'] = fitFunction.GetChisquare()
            fitParameters['numberOfdegreesOfFreedom'] = fitFunction.GetNDF()
        return fitParameters

    def doConstrainedFit( self, histogram, function = 'gaus', limits = ( 0.1, 1.6 ) ):
        fitFunction = None
        if function == 'gaus':
            fitFunction = TF1( "gaus", "gaus", 0, 2 );
        elif function == 'pol3':
            fitFunction = TF1( "pol3", "[0] * ( 1 + [1]*x + [2]*x^2 + [3]*x^3 )", 0, 2 );
        elif function == 'landau':
            fitFunction = TF1( "landau", "landau", 0, 2 )


        myFitResult = data.Fit( function, "Q0", "ah", limits[0], limits[1] );

    def add_cms_label( self, njet = "" ):

        mytext = TPaveText( 0.3, 0.8, 0.6, 0.93, "NDC" );
        mytext.AddText( "CMS Preliminary" );
        mytext.AddText( "%.1f pb^{-1} at  #sqrt{s} = 7 TeV" % self.luminosity );
        if njet != "":
            mytext.AddText( "e+jets, %s" % self.jetBinsLatex[njet] )
        mytext.SetFillStyle( 0 );
        mytext.SetBorderSize( 0 );
        mytext.SetTextFont( 42 );
        mytext.SetTextAlign( 13 );
        return mytext

    def add_legend( self, histname ):
        function = self.currentFitFuntion

        tt = self.histograms['ttbar'][histname]
        wj = self.histograms['wjets'][histname]
        zj = self.histograms['zjets'][histname]
        data = self.histograms['data'][histname]
        QCD = self.histograms['qcd'][histname]
        stop = self.histograms['singleTop'][histname]

        leg = TLegend( 0.64, 0.4, 0.9, 0.9 );
        leg.SetFillStyle( 0 );
        leg.SetBorderSize( 0 );
        leg.SetTextFont( 42 );

        # Here I define coloured lines for use in the legend
        blue = TF1( "blue", "pol0", 0, 1 );
        red = TF1( "red", "pol0", 0, 1 );

        blue.SetLineColor( kBlue );
        red.SetLineColor( kRed );

        red.SetLineWidth( 2 );
        blue.SetLineWidth( 2 );

        blue.SetLineStyle( kDashed );

        # Add entry to legend
        if self.useEntryAsData == 'data':
            leg.AddEntry( data, "Data", "LP" );
        else:
            leg.AddEntry( data, "All MC events", "LP" );
        if function == "pol1":
            leg.AddEntry( red, "Linear Fit", "l" );
        elif function == "expo":
            leg.AddEntry( red, "Exponenetial Fit", "l" );
        elif function == "gaus":
            leg.AddEntry( red, "Gaussian Fit", "l" );

        leg.AddEntry( blue, "Extrapolation", "l" );
        leg.AddEntry( tt, "t#bar{t}", "F" );
        leg.AddEntry( stop, "Single-Top", "F" );
        leg.AddEntry( wj, "W#rightarrowl#nu", "F" );
        leg.AddEntry( zj, "Z/#gamma*#rightarrowl^{+}l^{-}", "F" );
        leg.AddEntry( QCD, "QCD & #gamma+jets", "F" );
        leg.Draw()
        return ( leg, red, blue )

    def printResults( self, results ):
        self.printJetBinResults( results, '3jets' )
        print '=' * 60
        self.printJetBinResults( results, '3orMoreJets' )
        print '=' * 60
        self.printJetBinResults( results, '4orMoreJets' )
        print '=' * 60


    def printJetBinResults( self, results, jetBin ):
        estimate = 0
        estimate2 = 0
        predicted = results[results.keys()[0]][jetBin]['actualNumberOfQCDEvents']
        allData = results[results.keys()[0]][jetBin]['numberOfAllDataEvents']
        allMC = results[results.keys()[0]][jetBin]['numberOfAllMCEvents']

        if jetBin == '4orMoreJets':
            print 'Estimation for >= 4 jets'
        elif jetBin == '3orMoreJets':
            print 'Estimation for >= 3 jets'
        elif jetBin == '3jets':
            print 'Estimation for == 3 jets'
        print 'predicted number of QCD events', predicted
        for fitRange in self.fitRangesEstimation:
            range = '%1.1f-%1.1f' % fitRange
            est = results[range][jetBin]['estimatedNumberOfQCDEvents']
            true = results[range][jetBin]['actualNumberOfQCDEvents']
            variation = ( est - true ) / true
            estimate += est
            estimate2 += est * est


            print
            print 'estimated number of QCD events'
            print 'for range', range, ': ',
            print est
        print
        mean = estimate / len( self.fitRangesEstimation )
        mean2 = estimate2 / len( self.fitRangesEstimation )
        error = sqrt( ( mean2 - mean * mean ) / len( self.fitRangesEstimation ) )
        print 'average estimate', mean, '+-', error
        weight = estimate / len( self.fitRangesEstimation ) / predicted
        print 'average weight factor', weight
        print 'Total number of data in signal bin(<0.1)', allData
        print 'Total number of MC in signal bin(<0.1) before reweighting QCD', allMC
        print 'Total number of MC in signal bin(<0.1) after reweighting QCD', ( allMC - predicted ) + predicted * weight

    def plotControlRegionComparison( self ):
        for bin in self.jetBins:
            hist = self.pfIsoHistogramPrefix + bin
            histControl = self.pfIsoControlRegionHistogramPrefix + bin
            QCD_control = self.histograms['qcd'][histControl]
            QCD = self.histograms['qcd'][hist]
            nQCD_Control = QCD_control.Integral()
            nQCD = QCD.Integral()
            if nQCD_Control > 0:
                QCD_control.Scale( 1 / nQCD_Control )
            if nQCD > 0:
                QCD.Scale( 1 / nQCD )
            QCD_control.SetFillStyle( 3004 )
            QCD.GetXaxis().SetRangeUser( 0., self.maxValue - 0.01 )

            max = 0
            if QCD.GetMaximum() > QCD_control.GetMaximum():
                max = QCD.GetMaximum()*1.1
            else:
                max = QCD_control.GetMaximum()*1.1

            QCD.GetYaxis().SetRangeUser( 0., max )

            canvas = TCanvas( "c1", "Shape comparision", 1920, 1080 )
            QCD.Draw()
            QCD_control.Draw( 'same' )
            label = self.add_cms_label( bin )
            label.Draw()

            leg = TLegend( 0.64, 0.4, 0.9, 0.9 );
            leg.SetFillStyle( 0 );
            leg.SetBorderSize( 0 );
            leg.SetTextFont( 42 );
            leg.AddEntry( QCD_control, 'QCD control region' )
            leg.AddEntry( QCD, 'QCD standard selection' )
            leg.Draw()

            canvas.SaveAs( '%s/%s_shapeComparison.%s' % ( self.outputFolder, hist, self.outputFormat ) )
#    hltFiles['data'] = inputFiles.files['data']
#    hltFiles['ttbar'] = inputFiles.files['ttbar']

    qcdPlots = [#MET
#                'TTbarEplusJetsPlusMetAnalysis/Ref selection/QCDConversions/MET/patType1CorrectedPFMet/MET',
#                'TTbarEplusJetsPlusMetAnalysis/Ref selection/QCD non iso e+jets/MET/patType1CorrectedPFMet/MET',
                'TTbarPlusMetAnalysis/EPlusJets/QCDConversions/Electron/electron_AbsEta',
                'TTbarPlusMetAnalysis/EPlusJets/QCD non iso e+jets/Electron/electron_AbsEta',
#                'TTbarEplusJetsPlusMetAnalysis/Ref + AsymJets selection/QCD non iso e+jets, non iso trigger/MET/patMETsPFlow/MET',
#                'TTbarEplusJetsPlusMetAnalysis/Ref + AsymJets selection/QCD e+jets PFRelIso/Electron/electron_pfIsolation_03',
#                'TTbarEplusJetsPlusMetAnalysis/Ref + AsymJets selection/QCD e+jets PFRelIso, non iso trigger/Electron/electron_pfIsolation_03'
                ]
#    triggerPlots = ['HLTStudy/' + trigger + '/' + variable + '_' + modifier for trigger in triggers for variable in triggerVariables for modifier in triggerModifiers]
    
    HistPlotter.setStyle()
    hists = HistGetter.getHistsFromFiles(qcdPlots, files, bJetBins=['0btag'])
#    hists = HistGetter.getHistsFromFiles(triggerPlots, hltFiles, jetBins=HistPlotter.allJetBins)
#    hists = HistGetter.addSampleSum( hists )
#    
#    hists = HistPlotter.applyDefaultStylesAndColors( hists )
#    
#    hists = HistPlotter.rebin(hists, 50, 'mttbar*')
#    hists = HistPlotter.setXRange(hists, (300, 1800), 'mttbar*')
###    
#    hists = HistPlotter.setXTitle(hists, 'm(t#bar{t})/GeV', 'mttbar*')
#    hists = HistPlotter.setYTitle(hists, 'Events/(50 GeV)', 'mttbar*')
#    
#    hists = HistGetter.makeMCStack( hists )
#    hists = HistGetter.makeDetailedMCStack( hists )
    
#    hists = HistPlotter.rebin(hists, 5, 'MET*')