def doCounters(datasets):
    eventCounter = counter.EventCounter(datasets)
    ewkDatasets = [
        "WJets", "TTJets",
#        "WJets",                                                                                                                      
        "DYJetsToLL", "SingleTop", "Diboson"
        ]

#    if mcOnly:
#        eventCounter.normalizeMCToLuminosity(mcOnlyLumi)
#    else:
    eventCounter.normalizeMCByLuminosity()

    print "============================================================"
    print "Main counter (MC normalized by collision data luminosity)"
    mainTable = eventCounter.getMainCounterTable()
#    mainTable.insertColumn(2, counter.sumColumn("EWKMCsum", [mainTable.getColumn(name=name) for name in ewkDatasets]))
    # Default                                                                                                                                                                                                                                                      
#    cellFormat = counter.TableFormatText()                                                                                                                                                                                                                        
    # No uncertainties                                                                                                                                                                                                                                             
    cellFormat = counter.TableFormatText(cellFormat=counter.CellFormatText(valueOnly=True))
    print mainTable.format(cellFormat)
    print eventCounter.getSubCounterTable("TauSelection").format(cellFormat)
    print eventCounter.getSubCounterTable("e selection").format(cellFormat)
    print eventCounter.getSubCounterTable("mu selection").format(cellFormat)
    print eventCounter.getSubCounterTable("jet selection").format(cellFormat)
    print eventCounter.getSubCounterTable("angular cuts / Collinear").format(cellFormat)
    print eventCounter.getSubCounterTable("bjet selection").format(cellFormat)
    print eventCounter.getSubCounterTable("angular cuts / BackToBack").format(cellFormat)                                                                                                                                
Esempio n. 2
0
def doCounters(datasets):
    eventCounter = counter.EventCounter(datasets)

    eventCounter.normalizeMCByLuminosity()
    #    eventCounter.normalizeMCToLuminosity(73)
    print "============================================================"
    print "Main counter (MC normalized by collision data luminosity)"
    mainTable = eventCounter.getMainCounterTable()
    # No uncertainties
    cellFormat = counter.TableFormatText(cellFormat=counter.CellFormatText(
        valueOnly=True))
    print mainTable.format(cellFormat)

    print eventCounter.getSubCounterTable("b-tagging").format(cellFormat)
    print eventCounter.getSubCounterTable("Jet selection").format(cellFormat)
    print eventCounter.getSubCounterTable("Jet main").format(cellFormat)
Esempio n. 3
0
def main(opts):
    datasets = None
    if len(opts.files) > 0:
        datasets = dataset.getDatasetsFromRootFiles( [(x,x) for x in opts.files], opts=opts, weightedCounters=opts.weighted)
    else:
        datasets = dataset.getDatasetsFromMulticrabCfg(opts=opts, weightedCounters=opts.weighted)

    if os.path.exists(opts.lumifile):
        datasets.loadLuminosities(opts.lumifile)

    if opts.weighted and opts.PUreweight:
        datasets.updateNAllEventsToPUWeighted(era=opts.dataEra)

    if opts.mergeData:
        datasets.mergeData()

    eventCounter = counter.EventCounter(datasets)
    

    print "============================================================"
    if opts.printInfo:
        print "Dataset info: "
        datasets.printInfo()
        print

    quantity = "events"
    if opts.mode == "events":
        pass
    elif opts.mode in ["xsect", "xsection", "crosssection", "crossSection", "eff"]:
        if not opts.PUreweight:
            print "Mode '%s' works only with PU reweighting, which you disabled with --noPUreweight" % opts.mode
            return 1
        eventCounter.normalizeMCByCrossSection()
        quantity = "MC by cross section, data by events"
    else:
        print "Printing mode '%s' doesn't exist! The following ones are available 'events', 'xsect', 'eff'" % opts.mode
        return 1

    cellFormat = counter.CellFormatText(valueOnly=opts.valueOnly, valueFormat=opts.format)
    formatFunc = lambda table: table.format(counter.TableFormatText(cellFormat))
    csvSplitter = counter.TableSplitter([" +- ", " +", " -"])
    if opts.csv:
        formatFunc = lambda table: table.format(counter.TableFormatText(cellFormat, columnSeparator=","), csvSplitter)
    if opts.mode == "eff":
        cellFormat = counter.CellFormatText(valueFormat="%.4f", valueOnly=opts.valueOnly)
        formatFunc = lambda table: counter.counterEfficiency(table).format(counter.TableFormatText(cellFormat))
        quantity = "Cut efficiencies"
        if opts.csv:
            formatFunc = lambda table: counter.counterEfficiency(table).format(counter.TableFormatText(cellFormat, columnSeparator=","), csvSplitter)

    if opts.subCounter is not None:
        print "============================================================"
        print "Subcounter %s %s: " % (opts.subCounter, quantity)
        print formatFunc(eventCounter.getSubCounterTable(opts.subCounter))
        print

        return 0


    print "============================================================"
    print "Main counter %s: " % quantity
    print formatFunc(eventCounter.getMainCounterTable())
    print 

    if not opts.mainCounterOnly:
        names = eventCounter.getSubCounterNames()
        names.sort()
        for name in names:
            print "============================================================"
            print "Subcounter %s %s: " % (name, quantity)
            print formatFunc(eventCounter.getSubCounterTable(name))
            print

    # print

    return 0
Esempio n. 4
0
def doCounters(datasetsMgr):
    '''
    Print values for counters and sub-counters (if -s option is invoked)

    Options:
    --latex (The table formatting is in LaTeX instead of plain text)
    --format (The table value-format of strings)
    --precision (The table value-precision)
    --noError (Don't print statistical errors in tables)
    --mergeEWK (Merge all EWK samples into a single sample called EWK)
    -i (List of datasets in mcrab to include)
    -e (List of datasets in mcrab to exclude)
    '''
    Verbose("Doing the counters")

    # Definitions
    eventCounter = counter.EventCounter(datasetsMgr)
    ewkDatasets = GetListOfEwkDatasets()

    # Normalize MC samples accordingly
    if opts.mcOnly and opts.intLumi > -1.0:
        eventCounter.normalizeMCToLuminosity(opts.intLumi)
    else:
        eventCounter.normalizeMCByLuminosity()
        # eventCounter.normalizeMCByCrossSection()

    # Construct the table
    mainTable = eventCounter.getMainCounterTable()

    # Only keep selected rows
    rows = [
        #"ttree: skimCounterAll",
        #"ttree: skimCounterPassed",
        #"Base::AllEvents",
        #"Base::PUReweighting",
        #"Base::Prescale",
        #"Base::Weighted events with top pT",
        #"Base::Weighted events for exclusive samples",
        "All events",
        "Passed trigger",
        "passed METFilter selection ()",
        "Passed PV",
        "passed e selection (Veto)",
        "passed mu selection (Veto)",
        "Passed tau selection (Veto)",
        #"Passed tau selection and genuine (Veto)",
        "passed jet selection ()",
        #"passed b-jet selection ()",
        #"passed light-jet selection ()",
        "Baseline: passed b-jet selection",
        "Baseline: b tag SF",
        #"passed MET selection (Baseline)",
        "passed topology selection (Baseline)",
        "passed top selection (Baseline)",
        "Baseline: selected events",
        "Inverted: passed b-jet veto",
        "Inverted: b tag SF",
        #"passed MET selection (Inverted)",
        "passed topology selection (Inverted)",
        "passed top selection (Inverted)",
        "Inverted: selected events"
    ]
    mainTable.keepOnlyRows(rows)

    # Get number of rows/columns
    nRows = mainTable.getNrows()
    nColumns = mainTable.getNcolumns()

    # Merge EWK into a new column
    if opts.mergeEWK:
        mainTable.insertColumn(
            nColumns,
            counter.sumColumn(
                "EWK",
                [mainTable.getColumn(name=name) for name in ewkDatasets]))

    # Additional column (through inter-column operations)
    if opts.mergeEWK:
        mainTable.insertColumn(
            mainTable.getNcolumns(),
            counter.subtractColumn("Data-EWK",
                                   mainTable.getColumn(name="Data"),
                                   mainTable.getColumn(name="EWK")))
        mainTable.insertColumn(
            mainTable.getNcolumns(),
            counter.divideColumn("QCD Purity",
                                 mainTable.getColumn(name="Data-EWK"),
                                 mainTable.getColumn(name="Data")))
        # Convert "QCD Purity" from Fraction to Percent (%)
        mainTable.getColumn(name="QCD Purity").multiply(100)

    # Define which columns to keep
    columnsToKeep = ["Data", "EWK", "QCD Purity"]

    if opts.fractionEWK:
        columnsToKeep = []  # columnsToKeep = ["Data", "EWK"]
        for d in GetListOfEwkDatasets():
            columnName = d + "/EWK"
            mainTable.insertColumn(
                mainTable.getNcolumns(),
                counter.divideColumn(columnName, mainTable.getColumn(name=d),
                                     mainTable.getColumn(name="EWK")))
            # Convert EWK Fraction to EWK Percent (%)
            mainTable.getColumn(name=columnName).multiply(100)
            columnsToKeep.append(columnName)

    # Remove all columns that are not needed
    if opts.mergeEWK:
        mainTable.keepOnlyColumns(columnsToKeep)

    # Optional: Produce table in Text or LaTeX format?
    if opts.latex:
        mainTable.renameRows(GetRowNameLaTeXDict())
        cellFormat = counter.TableFormatLaTeX(
            counter.CellFormatTeX(
                valueOnly=opts.valueOnly,
                valueFormat=opts.valueFormat,
                withPrecision=opts.withPrecision,
                uncertaintyFormat=opts.uncertaintyFormat,
                uncertaintyPrecision=opts.uncertaintyPrecision))

    else:
        cellFormat = counter.TableFormatText(cellFormat=counter.CellFormatText(
            valueOnly=opts.valueOnly,
            valueFormat=opts.valueFormat,
            withPrecision=opts.withPrecision,
            uncertaintyFormat=opts.uncertaintyFormat,
            uncertaintyPrecision=opts.uncertaintyPrecision))
    print mainTable.format(cellFormat)

    # Do sub-counters?
    subcounters = [
        "bjet selection ()",
        "e selection (Veto)",
        "jet selection ()",
        #"light-jet selection ()",
        "METFilter selection",
        "METFilter selection ()",
        "mu selection (Veto)",
        "tau selection (Veto)",
        "top selection (Baseline)",
        "top selection (Inverted)",
        "topology selection (Baseline)",
        "topology selection (Inverted)"
    ]

    if opts.subcounters:
        for sc in subcounters:
            Print("\nSub-counter \"%s\"" % (sc), False)
            print eventCounter.getSubCounterTable(sc).format(cellFormat)
    return
def main(opts):

    datasetsMgr = None
    # Fixme: This options is currently invalid
    if len(opts.files) > 0:
        datasetsMgr = dataset.getDatasetsFromRootFiles( [(x,x) for x in opts.files], opts=opts, weightedCounters=opts.weighted)
    else:
        # Define the cwd as the multicrab dir containing the ROOT files with the counters
        multicrabDir = os.getcwd()
        
        # For-loop: All arguments passed during script execution
        for d in sys.argv:
            # Look for a directory that exists
            if os.path.exists(d) and os.path.isdir(d):
                multicrabDir = os.path.abspath(d)

        Print("The multicrab directory to be used is %s" % (multicrabDir), True)
        # Get the datasets    
        datasetsMgr = dataset.getDatasetsFromMulticrabDirs([multicrabDir],opts=opts, weightedCounters=opts.weighted)

    # Optional: Apply include/exclude datasets
    datasetsMgr = FilterDatasets(datasetsMgr, opts)
    Print("The tasks to be included are:\n\t%s" % ("\n\t".join(d.getName() for d in datasetsMgr.getAllDatasets())), True)


    # Optional: Print info on Data and MC samples
    if opts.verbose:
        datasetsMgr.PrintCrossSections()
        datasetsMgr.PrintLuminosities()
    
    # Load the luminosities
    if os.path.exists(opts.lumifile):
        datasetsMgr.loadLuminosities(opts.lumifile)

    # Optional: Apply PU-reweighting
    if opts.weighted and opts.PUreweight:
        Print("Updating all events to PU-weighted (opts.weighted=%s, opts.PUreweight=%s)\n" % (opts.weighted, opts.PUreweight), True)
        datasetsMgr.updateNAllEventsToPUWeighted(era=opts.dataEra)
    
    # Optional: Merge data
    Verbose("Merging Data and/or MC datatets (opts.mergeData=%s, opts.mergeData=%s, opts.mergeData=%s)" % (opts.mergeData, opts.mergeMC, opts.mergeForDataMC), True)
    if opts.mergeData:
        datasetsMgr.mergeData()
    if opts.mergeMC:
        datasetsMgr.mergeMC()
    if opts.mergeForDataMC:
        plots.mergeRenameReorderForDataMC(datasetsMgr)

    # Optional: Print dataset info
    if opts.printInfo:
        datasetsMgr.PrintInfo() #datasetsMgr.printInfo()

    # Create the event counter
    eventCounter = counter.EventCounter(datasetsMgr)

    # Proceed differently depending on operation mode (opts.mode= 'events', 'xsect', 'eff')
    quantity = "events"
    if opts.mode == "events":        
        if opts.mergeForDataMC:
            Print("Normalising the MC histograms to the data luminosity (opts.mergeForDataMC=%s)" % (opts.mergeForDataMC) )
            eventCounter.normalizeMCByLuminosity()
        else:
            pass
    elif opts.mode in ["xsect", "xsection", "crosssection", "crossSection", "eff"]:
        if not opts.PUreweight:
            Print("Mode '%s' works only with PU reweighting, which you disabled with --noPUreweight" % opts.mode)
            return 1
        Print("Normalising MC by cross-section (opt.mode=%s)" % opts.mode)
        eventCounter.normalizeMCByCrossSection()
        quantity = "MC by cross section, data by events"
    else:
        Print("Printing mode '%s' doesn't exist! The following ones are available 'events', 'xsect', 'eff'" % opts.mode)
        return 1

    # Optional: Produce table in Text or LaTeX format?
    if opts.latex:
        cellFormat  = counter.CellFormatTeX(valueOnly=opts.valueOnly, valueFormat=opts.format)
        formatFunc = lambda table: table.format(counter.TableFormatLaTeX(cellFormat))
    else:
        cellFormat  = counter.CellFormatText(valueOnly=opts.valueOnly, valueFormat=opts.format)
        formatFunc = lambda table: table.format(counter.TableFormatText(cellFormat))
    csvSplitter = counter.TableSplitter([" +- ", " +", " -"])

    # Optional: Format as comma-separated-variables (csv), presubambly for exporting to a spreadsheet
    if opts.csv:
        formatFunc = lambda table: table.format(counter.TableFormatText(cellFormat, columnSeparator=","), csvSplitter)

    # Optional: Convert to (relative) efficienies
    if opts.mode == "eff":
        if opts.weighted:
            Print("Cannot operate in \"eff\" mode while using weighted counters (opts.mode=\'%s\', opts.weighted=%s)" % (opts.mode, opts.weighted) )
            return 1
        else:            
            Print("Converting to efficiencies (opts.mode=%s)" % (opts.mode) )

        cellFormat = counter.CellFormatText(valueFormat="%.4f", valueOnly=opts.valueOnly)
        if opts.latex:
            formatFunc = lambda table: counter.counterEfficiency(table).format(counter.TableFormatLaTeX(cellFormat))
        else:
            formatFunc = lambda table: counter.counterEfficiency(table).format(counter.TableFormatText(cellFormat))
        quantity = "Cut efficiencies"
        # Optional: Format as comma-separated-variables (csv), presubambly for exporting to a spreadsheet
        if opts.csv:
            formatFunc = lambda table: counter.counterEfficiency(table).format(counter.TableFormatText(cellFormat, columnSeparator=","), csvSplitter)

    # Optional: Print only this sub-counters
    if opts.subCounter is not None:
        msg = "Subcounter %s %s: " % (opts.subCounter, quantity)
        Print(msg, True)
        print formatFunc(eventCounter.getSubCounterTable(opts.subCounter))
        print
        return 0

    # Print the main counters
    hLine = "="*10
    msg = " Main counter %s: " % quantity
    print "\n" + hLine + msg + hLine
    print formatFunc(eventCounter.getMainCounterTable())
    print 

    # Optional: Print sub-counters (only if --mainCounterOnly is not called)
    if not opts.mainCounterOnly:
        names = eventCounter.getSubCounterNames()
        names.sort()
        for name in names:
            hLine = "="*10
            msg = " Subcounter %s %s: " % (name, quantity)
            print "\n" + hLine + msg + hLine
            print formatFunc(eventCounter.getSubCounterTable(name) )

    return 0