Esempio n. 1
0
def getMainHistogram(filename, column):
    f = ROOT.TFile(
        filename.replace("datacard_", "histograms_").replace(".txt", ".root"),
        "r")
    if f == None:
        sys.exit()
    h = f.Get(column).Clone()
    h.SetDirectory(None)
    f.Close()
    if h == None:
        raise Exception("Error: cannot find histogram '%s' in file %s!" %
                        (column, filename))
    hunc = dataset.RootHistoWithUncertainties(h)
    hunc.makeFlowBinsVisible()

    return hunc
Esempio n. 2
0
 def doPlot(self, opts, myAllShapeNuisances, f, mass, luminosity, signalTable, rebinList=None):
     def rebin(h, rebinList):
         if rebinList == None or h == None:
             return h
         myArray = array.array("d",rebinList)
         hnew = h.Rebin(len(rebinList)-1,h.GetTitle(),myArray)
         h.Delete()
         return hnew
     
     print "Doing plots for:",self._name
     hNominal = f.Get(self._cardReader.getHistoNameForColumn(self._name))
     hNominal = rebin(hNominal, rebinList)
     hNominal.SetFillStyle(0)
     hNominalFine = f.Get(self._cardReader.getHistoNameForColumn(self._name)+"_fineBinning")
     hNominalFine = rebin(hNominalFine, rebinList)
     hNominalHisto = histograms.Histo(hNominal, self._name, drawStyle="HIST")
     # Determine label
     mySignalLabels = ["HH","HW","Hp"]
     mySignalStatus = False
     for l in mySignalLabels:
         if l in self._name:
             mySignalStatus = True
     myMCLabels = ["HH","HW","Hp","MC"]
     myMCStatus = False
     
     for mclab in myMCLabels:
         if mclab in self._name:
             myMCStatus = True
     histograms.cmsTextMode = histograms.CMSMode.PRELIMINARY
     if myMCStatus:
         histograms.cmsTextMode = histograms.CMSMode.SIMULATION_PRELIMINARY
     x = 0.6
     size = 20
     myRatioContainer = RatioPlotContainer(self._label)
     for uncName in self._uncertaintyShapes:
         myShortName = uncName
         print "... uncertainty:",myShortName
         myLongName = self._cardReader.getHistoNameForNuisance(self._name, uncName)
         hup = f.Get("%sUp"%(myLongName))
         hup = rebin(hup, rebinList)
         hup.SetFillStyle(0)
         up = dataset.RootHistoWithUncertainties(hup)
         upFine = f.Get("%sUp_fineBinning"%(myLongName))
         upFine = rebin(upFine, rebinList)
         nom = dataset.RootHistoWithUncertainties(hNominal.Clone())
         nom.makeFlowBinsVisible()
         hdown = f.Get("%sDown"%(myLongName))
         hdown = rebin(hdown, rebinList)
         hdown.SetFillStyle(0)
         down = dataset.RootHistoWithUncertainties(hdown)
         downFine = f.Get("%sDown_fineBinning"%(myLongName))
         downFine = rebin(downFine, rebinList)
         up.getRootHisto().SetLineColor(ROOT.kRed)
         nom.getRootHisto().SetLineColor(ROOT.kBlack)
         down.getRootHisto().SetLineColor(ROOT.kBlue)
         upHisto = histograms.Histo(up, "Up %.1f"%_integral(up.getRootHisto()), drawStyle="HIST", legendStyle="l")
         downHisto = histograms.Histo(down, "Down %.1f"%_integral(down.getRootHisto()), drawStyle="HIST", legendStyle="l")
         nomHisto = histograms.Histo(nom, "Nominal %.1f"%_integral(nom.getRootHisto()), drawStyle="HIST", legendStyle="l")
         # Add fit uncert. as stat uncert.
         for i in range(0,9):
             tailFitUp = f.Get("%s_TailFit_par%dUp"%(myLongName,i))
             tailFitDown = f.Get("%s_TailFit_par%dDown"%(myLongName,i))
             if tailFitUp != None and tailFitDown != None:
                 nom.addShapeUncertaintyFromVariation("%s_TailFit_par%d"%(self._name,i),tailFitUp,tailFitDown)
                 tailFitUp.Delete()
                 tailFitDown.Delete()
         tailfitNames = filter(lambda n: "_TailFit_" in n, nom.getShapeUncertaintyNames())
         nom.setShapeUncertaintiesAsStatistical(tailfitNames)
         # Do plot
         plot = plots.ComparisonManyPlot(nomHisto, [upHisto, downHisto])
         plot.setLuminosity(luminosity)
         plot.histoMgr.forEachHisto(lambda h: h.getRootHisto().SetLineWidth(3))
         myPlotName = "%s/shapeSyst_%s_syst%s" % (_dirname, self._label, uncName.replace("Up",""))
         myParams = {}
         myParams["ylabel"] = "Events"
         myParams["log"] = False
         #myParams["opts2"] = {"ymin": 0.0, "ymax":2.0}
         myParams["opts2"] = {"ymin": 0.7, "ymax":1.3}
         myParams["opts"] = {"ymin": 0.0}
         myParams["ratio"] = True
         myParams["ratioType"] = "errorScale"
         myParams["ratioYlabel"] = "Var./Nom."
         myParams["addLuminosityText"] = True
         myParams["customizeBeforeDraw"] = customizeMarker
         #myParams["ratioErrorOptions"] = {"numeratorStatSyst": False}
         myParams["addMCUncertainty"] = True
         
         plots.drawPlot(plot, myPlotName, **myParams)
         myRatioContainer.addRatioPlot(plot, myShortName)
         # Analyse up and down variation
         if mySignalStatus and upFine != None and downFine != None:
             a = abs(upFine.Integral()/hNominalFine.Integral() - 1.0)
             b = abs(1.0 - downFine.Integral()/hNominalFine.Integral())
             r = a
             if b > a:
                 r = b
             if uncName in signalTable.keys():
                 if r < signalTable[uncName]["min"]:
                     signalTable[uncName]["min"] = r
                 if r > signalTable[uncName]["max"]:
                     signalTable[uncName]["max"] = r
             else:
                 signalTable[uncName] = {}
                 signalTable[uncName]["min"] = r
                 signalTable[uncName]["max"] = r
         
     # Create plots with only the ratio plot
     if opts.individual:
         myRatioContainer.drawIndividually()
     else:
         myRatioContainer.drawAllInOne(myAllShapeNuisances, luminosity)
Esempio n. 3
0
def main():

    if len(sys.argv) < 2 and not os.path.isdir(sys.argv[1]):
        usage()

    multicrabdir = sys.argv[1]

    signalAnalysisDir = ""
    QCDAnalysisDir    = os.path.join(multicrabdir,"pseudoMulticrab_QCDMeasurement")

    candDirs = execute("ls %s"%multicrabdir)
    for d in candDirs:
        if "SignalAnalysis_" in d:
            signalAnalysisDir = os.path.join(multicrabdir,d)

    print "Using",signalAnalysisDir
    print "     ",QCDAnalysisDir

    style    = tdrstyle.TDRStyle()
    ROOT.gROOT.SetBatch(True)
    datasets    = dataset.getDatasetsFromMulticrabDirs([signalAnalysisDir], analysisName=analysis)

    datasetsQCD = dataset.getDatasetsFromMulticrabDirs([QCDAnalysisDir], analysisName="signalAnalysis")
    datasets.extend(datasetsQCD)

    plots.mergeRenameReorderForDataMC(datasets)
    luminosity = datasets.getDataset("Data").getLuminosity()

    # Semi-ugly hack for approval homework, remember improve for the next round
    totalErrorFile = ROOT.TFile("outputLight.root")
    #totalErrorFile = ROOT.TFile("outputHeavy.root")
    ## Remove superfluous shape variation uncertainties                                                                  
    totalErrorHistoUp = totalErrorFile.Get("total_background")
    rebin = [0,20,40,60,80,100,120,140,160,180,200,220,240,260,280,300,320,340,360,380,400,420,440,460,480,500,600,700,800]
    import array
    #totalErrorHistoUp.Sumw2()
    totalErrorHistoUp = totalErrorHistoUp.Rebin(len(rebin)-1,"foo",array.array("d",rebin))
    totalErrorHistoDown = totalErrorHistoUp.Clone("foo2")
    for i in xrange(1, totalErrorHistoUp.GetNbinsX()+1):
        #print i, totalErrorHistoUp.GetBinContent(i), totalErrorHistoUp.GetBinError(i)
        totalErrorHistoUp.SetBinContent(i, totalErrorHistoUp.GetBinError(i))
        totalErrorHistoDown.SetBinContent(i, -totalErrorHistoDown.GetBinError(i))
        #print i, totalErrorHistoUp.GetBinContent(i), totalErrorHistoDown.GetBinContent(i)
    # semi-ugly hack continues below in QCD

    myStackList = []
    h_data = datasets.getDataset("Data").getDatasetRootHisto("ForDataDrivenCtrlPlots/shapeTransverseMass_POSTFIT").getHistogram()
    myRHWU = dataset.RootHistoWithUncertainties(h_data)
    myRHWU.makeFlowBinsVisible()
    myHisto = histograms.Histo(myRHWU, "Data")
    myHisto.setIsDataMC(isData=True, isMC=False)
    myStackList.insert(0, myHisto)

    h_FakeTau = datasets.getDataset("QCDMeasurementMT").getDatasetRootHisto("ForDataDrivenCtrlPlots/shapeTransverseMass_POSTFIT").getHistogram()
    myRHWU = dataset.RootHistoWithUncertainties(h_FakeTau)
    # semi-ugly hack continues
    htemp=myRHWU.getRootHisto()
    myRHWU.addAbsoluteShapeUncertainty("toterr", totalErrorHistoUp, totalErrorHistoDown)
    #myRHWU.addShapeUncertaintyFromVariation("toterr", totalErrorHistoUp, totalErrorHistoDown)
    # semi-ugly hack ends
    myRHWU.makeFlowBinsVisible()
    myHisto = histograms.Histo(myRHWU, "QCDdata")
    myHisto.setIsDataMC(isData=False, isMC=True)
    myStackList.insert(1, myHisto)

    expectedList = []
    expectedList.append("TT")
    expectedList.append("WJets")
    expectedList.append("SingleTop")
    expectedList.append("DYJetsToLL")
    expectedList.append("Diboson")

    for i in range(0,len(expectedList)):
        drh = datasets.getDataset(expectedList[i]).getDatasetRootHisto("ForDataDrivenCtrlPlots/shapeTransverseMass_POSTFIT")
        h_bgr = drh.getHistogram()
        myRHWU = dataset.RootHistoWithUncertainties(h_bgr)
#        myRHWU.addShapeUncertaintyRelative("syst", th1Plus=self._expectedListSystUp[i], th1Minus=self._expectedListSystDown[i])
        myRHWU.makeFlowBinsVisible()
        myHisto = histograms.Histo(myRHWU, expectedList[i])
        myHisto.setIsDataMC(isData=False, isMC=True)
        myStackList.append(myHisto)

    #myStackList = divideByBinWidth(myStackList)

    # no, ugly hack continues here
    histograms.uncertaintyMode.set(histograms.Uncertainty.SystOnly)
    plots._legendLabels["MCSystError"] = "Bkg. stat.#oplus syst. unc."
    plots._legendLabels["BackgroundSystError"] = "Bkg. stat.#oplus syst. unc."
    # and stops again here
    myStackPlot = plots.DataMCPlot2(myStackList)
    myStackPlot.setLuminosity(luminosity)
    myStackPlot.setDefaultStyles()
    myParams = {}   
    myParams["ylabel"] = "Events / bin"
    myParams["ratioYlabel"] = "Data/Bkg."
    myParams["xlabel"] = "m_{T} (GeV)" 

    myParams["log"] = True
    myParams["ratio"] = True
    myParams["cmsTextPosition"] = "outframe"
    myParams["opts"] = {"ymin": 0.0001, "ymax": 3000.0}
    myParams["opts2"] = {"ymin": 0., "ymax":1.99}
    myParams["moveLegend"] = {"dx": -0.15, "dy": 0., "dh":0.05} # for data-driven
    myParams["ratioMoveLegend"] = {"dx": -0.51, "dy": 0.03}
    myParams["stackMCHistograms"] = True
    myParams["divideByBinWidth"] = True
    myParams["addMCUncertainty"] = True
    myParams["ratioType"] = "errorScale"
    myParams["ratioCreateLegend"] = True
    myParams["ratioMoveLegend"] = dict(dy=-0.45, dh=-0.1, dx=-0.5)
    plots.drawPlot(myStackPlot, "TransVerseMassPOSTFIT", **myParams)
Esempio n. 4
0
def validateRootHistoWithUncertainties(codeValidator):
    def createHisto(name, data, dataerr):
        h = ROOT.TH1F(name,name,len(data)-2,0,len(data)-2)
        for i in range(0,len(data)):
            h.SetBinContent(i, data[i])
        for i in range(0,len(dataerr)):
            h.SetBinError(i, dataerr[i])
        return h
    def printHistoContents(h):
        for i in range(0,h.GetNbinsX()+2):
            print i,h.GetBinContent(i),h.GetBinError(i)
    def testHistoContents(name,h,data,dataerr):
        for i in range(0,len(data)):
            codeValidator.test("histoContents(%s) content in bin %d"%(name,i), h.GetBinContent(i), data[i])
        for i in range(0,len(dataerr)):
            codeValidator.test("histoContents(%s) error in bin %d"%(name,i), h.GetBinError(i), dataerr[i])
    def testGraphContents(name,h,data,dataerrup,dataerrdown):
        for i in range(0,len(data)):
            codeValidator.test("graphContents(%s) content in bin %d"%(name,i), g.GetY()[i], data[i])
        for i in range(0,len(dataerrup)):
            codeValidator.test("graphContents(%s) error+ in bin %d"%(name,i), h.GetErrorYhigh(i), dataerrup[i])
            codeValidator.test("graphContents(%s) error- in bin %d"%(name,i), h.GetErrorYlow(i), dataerrdown[i])

    codeValidator.setPackage("tools.dataset.RootHistoWithUncertainties")
    # histogram
    h = createHisto("h",[3.2,6.4,7.6,10.4,5.3],[1.2,2.1,2.3,1.6,2.0])

    rhwu = dataset.RootHistoWithUncertainties(h)
    rhwu.makeFlowBinsVisible()
    testHistoContents("afterFlowBins", rhwu.getRootHisto(), [0.0,9.6,7.6,15.7,0.0], [0.0,2.4186773245,2.3,2.561249695,0.0])
    codeValidator.test("getRate()", rhwu.getRate(), 32.9)
    codeValidator.test("getRateStatUncertainty()", rhwu.getRateStatUncertainty(), 4.2071367936)
    (systUp,systDown) = rhwu.getRateSystUncertainty()
    codeValidator.test("getRateSystUncertainty() up", systUp, 0.0)
    codeValidator.test("getRateSystUncertainty() down", systDown, 0.0)
    # Add a norm. uncertainty
    rhwu.addNormalizationUncertaintyRelative("normA", uncertaintyPlus=0.1, uncertaintyMinus=-0.2)
    testHistoContents("after normA", rhwu.getRootHisto(), [0.0,9.6,7.6,15.7,0.0], [0.0,2.4186773245,2.3,2.561249695,0.0])
    codeValidator.test("getRate()", rhwu.getRate(), 32.9)
    codeValidator.test("getRateStatUncertainty()", rhwu.getRateStatUncertainty(), 4.2071367936)
    (systUp,systDown) = rhwu.getRateSystUncertainty() # this tests also Merge
    codeValidator.test("getRateSystUncertainty() up", systUp, 3.29)
    codeValidator.test("getRateSystUncertainty() down", systDown, 6.58)
    g = rhwu.getSystematicUncertaintyGraph(addStatistical=False)
    testGraphContents("syst.graph,addStat=False", g, [9.6,7.6,15.7], [0.96,0.76,1.57], [1.92,1.52,3.14])
    g = rhwu.getSystematicUncertaintyGraph(addStatistical=True)
    testGraphContents("syst.graph,addStat=True", g, [9.6,7.6,15.7], [2.6022298131,2.4223129443,3.0041471335], [3.0881062158,2.7568822971,4.0521105612])
    # Add shape uncertainty
    htestUp = createHisto("shapeUp", [0,10.5,12.1,13.3,0],[0,0,0,0,0])
    htestDown = createHisto("shapeDown", [0,8.8,8.1,15.5,0],[0,0,0,0,0])
    rhwu.addShapeUncertaintyFromVariation("shape",htestUp,htestDown)
    testHistoContents("after shape", rhwu.getRootHisto(), [0.0,9.6,7.6,15.7,0.0], [0.0,2.4186773245,2.3,2.561249695,0.0])
    codeValidator.test("getRate()", rhwu.getRate(), 32.9)
    codeValidator.test("getRateStatUncertainty()", rhwu.getRateStatUncertainty(), 4.2071367936)
    g = rhwu.getSystematicUncertaintyGraph(addStatistical=False)
    testGraphContents("syst.graph,addStat=False", g, [9.6,7.6,15.7], [1.315902732,4.5637265475,1.57], [2.08,1.52,3.9521639642])
    (systUp,systDown) = rhwu.getRateSystUncertainty() # this tests also Merge
    codeValidator.test("getRateSystUncertainty() up", systUp, 4.4524263048)
    codeValidator.test("getRateSystUncertainty() down", systDown, 6.5989696165)
    # Clone
    rhwuCloned = rhwu.Clone()
    testHistoContents("after Clone", rhwuCloned.getRootHisto(), [0.0,9.6,7.6,15.7,0.0], [0.0,2.4186773245,2.3,2.561249695,0.0])
    g = rhwuCloned.getSystematicUncertaintyGraph(addStatistical=False)
    testGraphContents("syst.graph,addStat=False", g, [9.6,7.6,15.7], [1.315902732,4.5637265475,1.57], [2.08,1.52,3.9521639642])
    # Scale
    rhwuCloned.Scale(2.0)
    testHistoContents("after Scale", rhwuCloned.getRootHisto(), [0.0,9.6*2,7.6*2,15.7*2,0.0], [0.0,2.4186773245*2,2.3*2,2.561249695*2,0.0])
    g = rhwuCloned.getSystematicUncertaintyGraph(addStatistical=False)
    testGraphContents("syst.graph,addStat=False", g, [9.6*2,7.6*2,15.7*2], [1.315902732*2,4.5637265475*2,1.57*2], [2.08*2,1.52*2,3.9521639642*2])
    # ScaleVariationUncertainty
    rhwuCloned.ScaleVariationUncertainty("shape",2.0)
    testHistoContents("after ScaleVariationUncertainty", rhwuCloned.getRootHisto(), [0.0,9.6*2,7.6*2,15.7*2,0.0], [0.0,2.4186773245*2,2.3*2,2.561249695*2,0.0])
    g = rhwuCloned.getSystematicUncertaintyGraph(addStatistical=False)
    testGraphContents("syst.graph,addStat=False", g, [9.6*2,7.6*2,15.7*2], [4.08,18.064063773,1.57*2], [4.9985597926,1.52*2,11.4716345827])
    # Add second shape uncertainty
    htest2Up = createHisto("shape2Up", [0,11.2,13.5,16.3,0],[0,0,0,0,0])
    htest2Down = createHisto("shape2Down", [0,8.2,6.1,13.5,0],[0,0,0,0,0])
    rhwu.addShapeUncertaintyFromVariation("shape2",htest2Up,htest2Down)
    testHistoContents("after shape2", rhwu.getRootHisto(), [0.0,9.6,7.6,15.7,0.0], [0.0,2.4186773245,2.3,2.561249695,0.0])
    g = rhwu.getSystematicUncertaintyGraph(addStatistical=False)
    testGraphContents("syst.graph,addStat=False", g, [9.6,7.6,15.7], [2.0716177254,7.4590616032,1.6807438829], [2.5072694311,2.1355093069,4.5232289352])
    # Add the two rhwu's
    rhwuSum = rhwu.Clone()
    rhwuSum.Add(rhwuCloned)
    testHistoContents("rate after Add", rhwuSum.getRootHisto(), [0,28.8,22.8,47.1,0], [0,5.4083269132,5.1429563482,5.7271284253,0])
    #testHistoContents("shape+ after Add", rhwuSum._shapeUncertaintyAbsolutePlus, [0,2.88,2.28,4.71,0], [])
    #testHistoContents("shape- after Add", rhwuSum._shapeUncertaintyAbsoluteMinus, [0,5.76,4.56,9.42,0], [])
    g = rhwuSum.getSystematicUncertaintyGraph(addStatistical=False)
    testGraphContents("syst.graph,addStat=False", g, [28.8,22.8,47.1], [5.5771318794,23.3721714866,4.7480627629], [7.1510558661,4.8003749854,15.4135135514])
    (systUp,systDown) = rhwuSum.getRateSystUncertainty() # this tests also Merge
    codeValidator.test("getRateSystUncertainty() up", systUp, 19.6983984121)
    codeValidator.test("getRateSystUncertainty() down", systDown, 20.5408763202)
    # Test add in the other direction
    rhwuSum2 = rhwuCloned.Clone()
    rhwuSum2.Add(rhwu)
    testHistoContents("rate after Add2", rhwuSum2.getRootHisto(), [0,28.8,22.8,47.1,0], [0,5.4083269132,5.1429563482,5.7271284253,0])
    #testHistoContents("shape+ after Add2", rhwuSum2._shapeUncertaintyAbsolutePlus, [0,2.88,2.28,4.71,0], [])
    #testHistoContents("shape- after Add2", rhwuSum2._shapeUncertaintyAbsoluteMinus, [0,5.76,4.56,9.42,0], [])
    g = rhwuSum2.getSystematicUncertaintyGraph(addStatistical=False)
    testGraphContents("syst.graph,addStat=False", g, [28.8,22.8,47.1], [5.5771318794,23.3721714866,4.7480627629], [7.1510558661,4.8003749854,15.4135135514])
    (systUp,systDown) = rhwuSum2.getRateSystUncertainty() # this tests also Merge
    codeValidator.test("getRateSystUncertainty() up", systUp, 19.6983984121)
    codeValidator.test("getRateSystUncertainty() down", systDown, 20.5408763202)

    #rhwuSum.Debug()


# Bugs found
# rate stat uncert.: last bin was ignored
# rate syst uncert.: normalization uncertainties in bin content, i.e. linear sum used when combining bins (was squared sum)
# syst. uncert: combining of norm