コード例 #1
0
    def getCounter(self, datasetName, name):
        if not datasetName in self.residualNames:
            return self.datasetsEmb.getCounter(datasetName, name)

        # Ugly hack
        sigName = name
        if isinstance(sigName, basestring):
            sigName = sigName.replace(self.analysisEmb, self.analysisSig)
        else:
            sigName = sigName.clone(tree=sigName.tree.replace(self.analysisEmb, self.analysisSig))

        # Get properly normalized embedded data, embedded DY and normal DY histograms
        (embHisto, tmp) = self.datasetsEmb.getHistogram(datasetName, name)
        sigHisto = self.datasetsSig.getDataset(datasetName).getDatasetRootHisto(sigName) # DatasetRootHisto
        sigHisto.normalizeToLuminosity(self.datasetsEmb.getLuminosity())
        sigHisto = sigHisto.getHistogram() # ROOT.TH1

        table = counter.CounterTable()
        table.appendColumn(counter.HistoCounter("Embedded", embHisto))
        table.appendColumn(counter.HistoCounter("Normal", sigHisto))
        table.removeNonFullRows()

        embColumn = table.getColumn(name="Embedded")
        sigColumn = table.getColumn(name="Normal")
        residual = counter.subtractColumn(datasetName+" residual", sigColumn, embColumn)
        return residual
コード例 #2
0
    def getCounter(self, datasetName, name):
        if not datasetName in self.residualNames:
            return self.datasetsEmb.getCounter(datasetName, name)

        # Ugly hack
        sigName = name
        if isinstance(sigName, basestring):
            sigName = sigName.replace(self.analysisEmb, self.analysisSig)
        else:
            sigName = sigName.clone(
                tree=sigName.tree.replace(self.analysisEmb, self.analysisSig))

        # Get properly normalized embedded data, embedded DY and normal DY histograms
        (embHisto, tmp) = self.datasetsEmb.getHistogram(datasetName, name)
        sigHisto = self.datasetsSig.getDataset(
            datasetName).getDatasetRootHisto(sigName)  # DatasetRootHisto
        sigHisto.normalizeToLuminosity(self.datasetsEmb.getLuminosity())
        sigHisto = sigHisto.getHistogram()  # ROOT.TH1

        table = counter.CounterTable()
        table.appendColumn(counter.HistoCounter("Embedded", embHisto))
        table.appendColumn(counter.HistoCounter("Normal", sigHisto))
        table.removeNonFullRows()

        embColumn = table.getColumn(name="Embedded")
        sigColumn = table.getColumn(name="Normal")
        residual = counter.subtractColumn(datasetName + " residual", sigColumn,
                                          embColumn)
        return residual
コード例 #3
0
ファイル: TreeCounter.py プロジェクト: heitorPB/HiggsAnalysis
def doCounters(datasets, MyCuts, MyCutsName):
    
    ### Define the counters to be used
    #eventCounter = counter.EventCounter(datasets, counters=myAnalysis + myDataEra + "/counters")
    eventCounter = counter.EventCounter(datasets)

    ### Normalise the MC sample to a luminosity before creating a table
    eventCounter.normalizeMCToLuminosity(GetLumi(datasets))
    

    myRowNames = []
    for iCut, iCutName in zip(MyCuts, MyCutsName):
        #print "*** Cut: %s\n*** CutName: %s" % (iCut, iCutName)
        print "*** Processing TCut with:\n    Name = \"%s\" \n    Expr = \"%s\"" % (iCutName, iCut)

        ### Define the event "weight" to be used
        EvtWeight = GetEventWeight(iCut)
    
        ### Define the TTree to be used
        treePath = "tree" # treePath = myAnalysis+"/tree"
        treeDraw = dataset.TreeDraw(treePath, weight=EvtWeight, selection=iCut)

        ### Append custom rows to the event counter. An asterisk denotes that the counter row was added. Informative and makes things easier
        myRowName = iCutName #"*" + iCutName
        myRowNames.append(myRowName)
        eventCounter.getMainCounter().appendRow(myRowName, treeDraw)

    ### Get table with all default rows removed and manage the table format
    myTable = GetCustomTable(eventCounter, myRowNames)

    if getBool("bMergeEwk"):    
        DataMinusEwkMc = counter.subtractColumn("DataMinusEwkMc", myTable.getColumn(name="Data"), myTable.getColumn(name="EWK MC"))
        QcdPurity      = counter.divideColumn("QCD Purity", DataMinusEwkMc, myTable.getColumn(name="Data"))
        myTable.appendColumn(QcdPurity)

    ### See http://docs.python.org/2/library/string.html for string format
    cellTextFormat  = counter.TableFormatText(counter.CellFormatTeX(valueFormat='%.2E', valueOnly=True)) #%.2e
    cellLaTeXFormat = counter.TableFormatLaTeX(counter.CellFormatTeX(valueFormat='%.2E', valueOnly=True)) #%.2e
    purityFormat    = counter.CellFormatTeX(valueFormat='%.2f', valueOnly=True)

    ### Customise the "QCD Purity" column
    cellTextFormat.setColumnFormat(purityFormat, name="QCD Purity") #does nothing
    cellLaTeXFormat.setColumnFormat(purityFormat, name="QCD Purity") #does nothing

    # between construction of table format and table format

    ### Print the final table with the desired format
    print "============================================================"
    print "Data-Era: %s (%s pb-1)" % (myDataEra, GetLumi(datasets))
    print "============================================================"
    print myTable.format(cellTextFormat)

    print "============================================================"
    print "Data-Era: %s (%s pb-1)" % (myDataEra, GetLumi(datasets))
    print "============================================================"
    print myTable.format(cellLaTeXFormat)

    return
コード例 #4
0
 def _calculateResidual(self, table, sigTable):
     columnNames = table.getColumnNames()
     for name in columnNames:
         if name in self.residualNames:
             i = columnNames.index(name)
             col = table.getColumn(index=i)
             table.removeColumn(i)
             col = counter.subtractColumn(name+" residual", sigTable.getColumn(name=name), col)
             table.insertColumn(i, col)
     return table
コード例 #5
0
 def _calculateResidual(self, table, sigTable):
     columnNames = table.getColumnNames()
     for name in columnNames:
         if name in self.residualNames:
             i = columnNames.index(name)
             col = table.getColumn(index=i)
             table.removeColumn(i)
             col = counter.subtractColumn(name + " residual",
                                          sigTable.getColumn(name=name),
                                          col)
             table.insertColumn(i, col)
     return table
コード例 #6
0
    def writeRootHisto(self, directory, dstName, srcEmbName, srcSigName,
                       isCounter):
        if not self.datasetsEmb.hasHistogram(
                self.datasetName,
                srcEmbName) or not self.datasetSig.hasRootHisto(srcSigName):
            return

        histo = None
        # Get properly normalized embedded and normal MC
        (embMcHisto,
         tmp) = self.datasetsEmb.getHistogram(self.datasetName, srcEmbName)
        sigMcHisto = self.datasetSig.getDatasetRootHisto(
            srcSigName).getHistogram()  # ROOT.TH1

        #print embMcHisto, sigMcHisto

        if self.normalOnly:
            histo = sigMcHisto
        elif self.embeddedOnly:
            histo = embMcHisto
        else:

            if isCounter:
                embMcCounter = counter.HistoCounter("EmbMc", embMcHisto)
                sigMcCounter = counter.HistoCounter("SigMc", sigMcHisto)

                table = counter.CounterTable()
                table.appendColumn(embMcCounter)
                table.appendColumn(sigMcCounter)

                table.removeNonFullRows()
                if table.getNrows() == 0:
                    return

                embMcColumn = table.getColumn(name="EmbMc")
                sigMcColumn = table.getColumn(name="SigMc")
                residual = counter.subtractColumn("Correction", sigMcColumn,
                                                  embMcColumn)
                histo = dataset._counterToHisto(dstName,
                                                residual.getPairList())
            else:
                # Residual MC: normal-embedded
                sigMcHisto.Add(embMcHisto, -1)
                histo = sigMcHisto

        histo.SetName(dstName)
        histo.SetDirectory(directory)
        histo.Write()

        histo.Delete()  # could this help???

        print "%s/%s" % (directory.GetPath(), dstName)
コード例 #7
0
    def getSubCounterTable(self, name):
        table = self.eventCounterEmb.getSubCounterTable(name)
        sigTable = self.eventCounterSig.getSubCounterTable(name)
        
        sigDyColumn = sigTable.getColumn(name="DYJetsToLL")
        embDyColumn = table.getColumn(name="DYJetsToLL")
        dyCorrection = counter.subtractColumn("Correction", sigDyColumn, embDyColumn)

        if "Data" in table.getColumnNames():
            self._correctColumn(table, "Data", dyCorrection)
        self._correctColumn(table, "DYJetsToLL", dyCorrection)

        return table
コード例 #8
0
    def getMainCounterTable(self):
        table = self.eventCounterEmb.getMainCounterTable()
        sigTable = self.eventCounterSig.getMainCounterTable()

        sigDyColumn = sigTable.getColumn(name="DYJetsToLL")
        embDyColumn = table.getColumn(name="DYJetsToLL")
        dyCorrection = counter.subtractColumn("Correction", sigDyColumn,
                                              embDyColumn)

        if "Data" in table.getColumnNames():
            self._correctColumn(table, "Data", dyCorrection)
        self._correctColumn(table, "DYJetsToLL", dyCorrection)

        return table
コード例 #9
0
    def writeRootHisto(self, directory, dstName, srcEmbName, srcSigName, isCounter):
        if not self.datasetsEmb.hasHistogram(self.datasetName, srcEmbName) or not self.datasetSig.hasRootHisto(srcSigName):
            return

        histo = None
        # Get properly normalized embedded and normal MC
        (embMcHisto, tmp) = self.datasetsEmb.getHistogram(self.datasetName, srcEmbName)
        sigMcHisto = self.datasetSig.getDatasetRootHisto(srcSigName).getHistogram() # ROOT.TH1

        #print embMcHisto, sigMcHisto

        if self.normalOnly:
            histo = sigMcHisto
        elif self.embeddedOnly:
            histo = embMcHisto
        else:
        
            if isCounter:
                embMcCounter = counter.HistoCounter("EmbMc", embMcHisto)
                sigMcCounter = counter.HistoCounter("SigMc", sigMcHisto)
    
                table = counter.CounterTable()
                table.appendColumn(embMcCounter)
                table.appendColumn(sigMcCounter)
    
                table.removeNonFullRows()
                if table.getNrows() == 0:
                    return
    
                embMcColumn = table.getColumn(name="EmbMc")
                sigMcColumn = table.getColumn(name="SigMc")
                residual = counter.subtractColumn("Correction", sigMcColumn, embMcColumn)
                histo = dataset._counterToHisto(dstName, residual.getPairList())
            else:
                # Residual MC: normal-embedded
                sigMcHisto.Add(embMcHisto, -1)
                histo = sigMcHisto
        
        histo.SetName(dstName)
        histo.SetDirectory(directory)
        histo.Write()
    
        histo.Delete() # could this help???

        print "%s/%s" % (directory.GetPath(), dstName)
コード例 #10
0
    def getCounter(self, datasetName, name):
        if not datasetName in ["Data", "EWKMC", "DYJetsToLL"]:
            (embDataHisto,
             tmp) = self.datasetsEmb.getHistogram(datasetName, name)
            return counter.HistoCounter(datasetName, embDataHisto)

        # Ugly hack
        sigName = name
        if isinstance(sigName, basestring):
            sigName = sigName.replace(self.analysisEmb, self.analysisSig)
        else:
            sigName = sigName.clone(
                tree=sigName.tree.replace(self.analysisEmb, self.analysisSig))

        # Get properly normalized embedded data, embedded DY and normal DY histograms
        (embDataHisto, tmp) = self.datasetsEmb.getHistogram(datasetName, name)
        (embDyHisto, tmp) = self.datasetsEmb.getHistogram("DYJetsToLL", name)
        sigDyHisto = self.datasetsSig.getDataset(
            "DYJetsToLL").getDatasetRootHisto(sigName)  # DatasetRootHisto
        sigDyHisto.normalizeToLuminosity(self.datasetsEmb.getLuminosity())
        sigDyHisto = sigDyHisto.getHistogram()  # ROOT.TH1

        embDataCounter = counter.HistoCounter("EmbData", embDataHisto)
        embDyCounter = counter.HistoCounter("EmbDy", embDyHisto)
        sigDyCounter = counter.HistoCounter("SigDy", sigDyHisto)

        table = counter.CounterTable()
        table.appendColumn(embDataCounter)
        table.appendColumn(embDyCounter)
        table.appendColumn(sigDyCounter)

        table.removeNonFullRows()

        column = table.getColumn(name="EmbData")
        embDyColumn = table.getColumn(name="EmbDy")
        sigDyColumn = table.getColumn(name="SigDy")
        dyCorrection = counter.subtractColumn("Correction", sigDyColumn,
                                              embDyColumn)
        column = counter.sumColumn(datasetName, [column, dyCorrection])

        return column
コード例 #11
0
    def getCounter(self, datasetName, name):
        if not datasetName in ["Data", "EWKMC", "DYJetsToLL"]:
            (embDataHisto, tmp) = self.datasetsEmb.getHistogram(datasetName, name)
            return counter.HistoCounter(datasetName, embDataHisto)

        # Ugly hack
        sigName = name
        if isinstance(sigName, basestring):
            sigName = sigName.replace(self.analysisEmb, self.analysisSig)
        else:
            sigName = sigName.clone(tree=sigName.tree.replace(self.analysisEmb, self.analysisSig))

        # Get properly normalized embedded data, embedded DY and normal DY histograms
        (embDataHisto, tmp) = self.datasetsEmb.getHistogram(datasetName, name)
        (embDyHisto, tmp) = self.datasetsEmb.getHistogram("DYJetsToLL", name)
        sigDyHisto = self.datasetsSig.getDataset("DYJetsToLL").getDatasetRootHisto(sigName) # DatasetRootHisto
        sigDyHisto.normalizeToLuminosity(self.datasetsEmb.getLuminosity())
        sigDyHisto = sigDyHisto.getHistogram() # ROOT.TH1

        embDataCounter = counter.HistoCounter("EmbData", embDataHisto)
        embDyCounter = counter.HistoCounter("EmbDy", embDyHisto)
        sigDyCounter = counter.HistoCounter("SigDy", sigDyHisto)

        table = counter.CounterTable()
        table.appendColumn(embDataCounter)
        table.appendColumn(embDyCounter)
        table.appendColumn(sigDyCounter)

        table.removeNonFullRows()

        column = table.getColumn(name="EmbData")
        embDyColumn = table.getColumn(name="EmbDy")
        sigDyColumn = table.getColumn(name="SigDy")
        dyCorrection = counter.subtractColumn("Correction", sigDyColumn, embDyColumn)
        column = counter.sumColumn(datasetName, [column, dyCorrection])

        return column
コード例 #12
0
def doCounters(datasets, MyCuts, MyCutsName):

    ### Define the counters to be used
    #eventCounter = counter.EventCounter(datasets, counters=myAnalysis + myDataEra + "/counters")
    eventCounter = counter.EventCounter(datasets)

    ### Normalise the MC sample to a luminosity before creating a table
    eventCounter.normalizeMCToLuminosity(GetLumi(datasets))

    myRowNames = []
    for iCut, iCutName in zip(MyCuts, MyCutsName):
        #print "*** Cut: %s\n*** CutName: %s" % (iCut, iCutName)
        print "*** Processing TCut with:\n    Name = \"%s\" \n    Expr = \"%s\"" % (
            iCutName, iCut)

        ### Define the event "weight" to be used
        EvtWeight = GetEventWeight(iCut)

        ### Define the TTree to be used
        treePath = "tree"  # treePath = myAnalysis+"/tree"
        treeDraw = dataset.TreeDraw(treePath, weight=EvtWeight, selection=iCut)

        ### Append custom rows to the event counter. An asterisk denotes that the counter row was added. Informative and makes things easier
        myRowName = iCutName  #"*" + iCutName
        myRowNames.append(myRowName)
        eventCounter.getMainCounter().appendRow(myRowName, treeDraw)

    ### Get table with all default rows removed and manage the table format
    myTable = GetCustomTable(eventCounter, myRowNames)

    if getBool("bMergeEwk"):
        DataMinusEwkMc = counter.subtractColumn(
            "DataMinusEwkMc", myTable.getColumn(name="Data"),
            myTable.getColumn(name="EWK MC"))
        QcdPurity = counter.divideColumn("QCD Purity", DataMinusEwkMc,
                                         myTable.getColumn(name="Data"))
        myTable.appendColumn(QcdPurity)

    ### See http://docs.python.org/2/library/string.html for string format
    cellTextFormat = counter.TableFormatText(
        counter.CellFormatTeX(valueFormat='%.2E', valueOnly=True))  #%.2e
    cellLaTeXFormat = counter.TableFormatLaTeX(
        counter.CellFormatTeX(valueFormat='%.2E', valueOnly=True))  #%.2e
    purityFormat = counter.CellFormatTeX(valueFormat='%.2f', valueOnly=True)

    ### Customise the "QCD Purity" column
    cellTextFormat.setColumnFormat(purityFormat,
                                   name="QCD Purity")  #does nothing
    cellLaTeXFormat.setColumnFormat(purityFormat,
                                    name="QCD Purity")  #does nothing

    # between construction of table format and table format

    ### Print the final table with the desired format
    print "============================================================"
    print "Data-Era: %s (%s pb-1)" % (myDataEra, GetLumi(datasets))
    print "============================================================"
    print myTable.format(cellTextFormat)

    print "============================================================"
    print "Data-Era: %s (%s pb-1)" % (myDataEra, GetLumi(datasets))
    print "============================================================"
    print myTable.format(cellLaTeXFormat)

    return
コード例 #13
0
def processDirectory(dset, srcDirName, dstDir, scaleBy, dsetSubtractFrom):
    # Get directories, recurse to them
    dirs = dset.getDirectoryContent(srcDirName,
                                    lambda o: isinstance(o, ROOT.TDirectory))
    dirs = filter(lambda n: n != "configInfo", dirs)

    for d in dirs:
        newDir = dstDir.mkdir(d)
        processDirectory(dset, os.path.join(srcDirName, d), newDir, scaleBy,
                         dsetSubtractFrom)

    # Then process histograms
    histos = dset.getDirectoryContent(srcDirName,
                                      lambda o: isinstance(o, ROOT.TH1))
    dstDir.cd()
    shouldScale = True
    if srcDirName == "counters":
        # Don't touch unweighted counters
        shouldScale = False
    isCounter = srcDirName in ["counters", "counters/weighted"]
    for hname in histos:
        #        drh = dset.getDatasetRootHisto(os.path.join(srcDirName, hname))
        #        hnew = drh.getHistogram() # TH1
        hnew = dset.getAverageHistogram(
            os.path.join(srcDirName, hname),
            normalizeMCByCrossSection=(dsetSubtractFrom is not None))
        hnew.SetName(hname)
        if shouldScale and hname not in "SplittedBinInfo":
            tauEmbedding.scaleTauBRNormalization(hnew)
            if scaleBy is not None:
                hnew.Scale(scaleBy)
        if dsetSubtractFrom is not None:
            drh = dsetSubtractFrom.getDatasetRootHisto(
                os.path.join(srcDirName, hname))
            if dsetSubtractFrom.isMC():
                drh.normalizeByCrossSection()
            hsub = drh.getHistogram()
            if not isCounter:
                # hnew = hsub-hnew
                hnew.Scale(-1)
                hnew.Add(hsub)
            else:
                cnew = counter.HistoCounter("Emb", hnew)
                csub = counter.HistoCounter("Norm", hsub)

                table = counter.CounterTable()
                table.appendColumn(cnew)
                table.appendColumn(csub)
                cres = counter.subtractColumn("Result",
                                              table.getColumn(name="Norm"),
                                              table.getColumn(name="Emb"))

                hnew2 = dataset._counterToHisto(hnew.GetName(),
                                                cres.getPairList())
                hnew2.SetTitle(hnew.GetTitle())
                hnew = hnew2
                if srcDirName == "counters" and hname == "counter" and hnew.GetBinContent(
                        1) < 0:
                    hnew.SetBinContent(1, 0)

        hnew.SetDirectory(dstDir)

        # # set the first count in main counters to 0 if it is negative,
        # # this is to circumvent certain assumptions made elsewhere in
        # # the code
        # if hname == "counter" and (srcDirName == "counters" or srcDirName == "counters/weighted") and hnew.GetBinContent(1) < 0:
        #     hnew.SetBinContent(1, 0)
        #     hnew.SetBinError(1, 0)

        hnew.Write()
        #        ROOT.gDirectory.Delete(hname)
        hnew.Delete()
コード例 #14
0
def processDirectory(dset, srcDirName, dstDir, scaleBy, dsetSubtractFrom):
    # Get directories, recurse to them
    dirs = dset.getDirectoryContent(srcDirName, lambda o: isinstance(o, ROOT.TDirectory))
    dirs = filter(lambda n: n != "configInfo", dirs)

    for d in dirs:
        newDir = dstDir.mkdir(d)
        processDirectory(dset, os.path.join(srcDirName, d), newDir, scaleBy, dsetSubtractFrom)

    # Then process histograms
    histos = dset.getDirectoryContent(srcDirName, lambda o: isinstance(o, ROOT.TH1))
    dstDir.cd()
    shouldScale = True
    if srcDirName == "counters":
        # Don't touch unweighted counters
        shouldScale = False
    isCounter = srcDirName in ["counters", "counters/weighted"]
    for hname in histos:
#        drh = dset.getDatasetRootHisto(os.path.join(srcDirName, hname))
#        hnew = drh.getHistogram() # TH1
        hnew = dset.getAverageHistogram(os.path.join(srcDirName, hname), normalizeMCByCrossSection = (dsetSubtractFrom is not None))
        hnew.SetName(hname)
        if shouldScale and hname not in "SplittedBinInfo":
            tauEmbedding.scaleTauBRNormalization(hnew)
            if scaleBy is not None:
                hnew.Scale(scaleBy)
        if dsetSubtractFrom is not None:
            drh = dsetSubtractFrom.getDatasetRootHisto(os.path.join(srcDirName, hname))
            if dsetSubtractFrom.isMC():
                drh.normalizeByCrossSection()
            hsub = drh.getHistogram()
            if not isCounter:
                # hnew = hsub-hnew
                hnew.Scale(-1)
                hnew.Add(hsub)
            else:
                cnew = counter.HistoCounter("Emb", hnew)
                csub = counter.HistoCounter("Norm", hsub)

                table = counter.CounterTable()
                table.appendColumn(cnew)
                table.appendColumn(csub)
                cres = counter.subtractColumn("Result", table.getColumn(name="Norm"), table.getColumn(name="Emb"))

                hnew2 = dataset._counterToHisto(hnew.GetName(), cres.getPairList())
                hnew2.SetTitle(hnew.GetTitle())
                hnew = hnew2
                if srcDirName == "counters" and hname == "counter" and hnew.GetBinContent(1) < 0:
                    hnew.SetBinContent(1, 0)


        hnew.SetDirectory(dstDir)

            # # set the first count in main counters to 0 if it is negative,
            # # this is to circumvent certain assumptions made elsewhere in
            # # the code
            # if hname == "counter" and (srcDirName == "counters" or srcDirName == "counters/weighted") and hnew.GetBinContent(1) < 0:
            #     hnew.SetBinContent(1, 0)
            #     hnew.SetBinError(1, 0)

        hnew.Write()
#        ROOT.gDirectory.Delete(hname)
        hnew.Delete()