def main():
    style = tdrstyle.TDRStyle()
    # Object for selecting data eras, search modes, and optimization modes
    myModuleSelector = analysisModuleSelector.AnalysisModuleSelector()

    parser = OptionParser(usage="Usage: %prog [options]",add_help_option=True,conflict_handler="resolve")
    myModuleSelector.addParserOptions(parser)
    parser.add_option("-m", "--mcrab", dest="mcrab", action="store", help="Path to the multicrab directory for input")
    parser.add_option("-d", "--dataset", dest="dataset", action="store", help="Name of the dataset to be plotted")
    parser.add_option("-e", "--error", dest="errorlevel", default=0.10, action="store", help="Maximum relative uncertainty per bin (default=10%%)")
    (opts, args) = parser.parse_args()

    if opts.mcrab == None:
        raise Exception("Please provide input multicrab directory with -m")
    if not os.path.exists(opts.mcrab):
        raise Exception("The input root file '%s' does not exist!"%opts.mcrab)
    if opts.dataset == None:
        raise Exception("Please provide dataset name with -d")

    # Find module names
    #myNames = findModuleNames(opts.mcrab, "BTagEfficiency")

    # Get dataset managers
    # Obtain dsetMgrCreator and register it to module selector
    dsetMgrCreator = dataset.readFromMulticrabCfg(directory=opts.mcrab)
    myModuleSelector.setPrimarySource("analysis", dsetMgrCreator)
    # Select modules
    myModuleSelector.doSelect(opts)
    myModuleSelector.printSelectedCombinationCount()
    #for n in myNames:
    results = []
    #for era in myModuleSelector.getSelectedEras(): #degug
    for era in ["Run2016"]:
        msg = "=== FIXME:\n\tRunning only over era \"%s\". The function myModuleSelector.getSelectedEras does not seem to work. Press any key to proceed" % (era)
        raw_input(msg)
        for searchMode in myModuleSelector.getSelectedSearchModes():
            for optimizationMode in myModuleSelector.getSelectedOptimizationModes():
                dsetMgr = dsetMgrCreator.createDatasetManager(dataEra=era,searchMode=searchMode,optimizationMode=optimizationMode)
                #datasets = dataset.getDatasetsFromMulticrabDirs(dirs=[opts.mcrab], analysisName=n)
                dsetMgr.loadLuminosities()
                dsetMgr.updateNAllEventsToPUWeighted()
                plots.mergeRenameReorderForDataMC(dsetMgr)
                lumi = dsetMgr.getDataset("Data").getLuminosity()
                for dset in dsetMgr.getMCDatasets():
                    print dset.name
                    if dset.name.startswith(opts.dataset):
                        results.extend(doPlot("btageff_%s_%s_%s"%(era, searchMode, optimizationMode), dset, opts.errorlevel, optimizationMode, lumi))
                  #dsetMgr.close()
    print "\nFigures generated"
    
    for item in results:
        print item
    
    # Write results to a json file
    filename = "btageff_%s.json"%opts.dataset
    with open(filename, 'w') as outfile:
        json.dump(results, outfile)
    print "Written results to %s"%filename
                qcdMt, ewkFakeTausMt)

        #===== Save normalization
        outFileName = "QCDNormalizationFactors_%s_%s.py" % (HISTONAME,
                                                            moduleInfoString)
        print argv[1], outFileName
        outFileFullName = os.path.join(argv[1], outFileName)
        manager.writeScaleFactorFile(outFileFullName, moduleInfoString)


if __name__ == "__main__":
    # Check parameters
    if len(sys.argv) < 2:
        usage()
    # Find out the era/search mode/optimization mode combinations and run each of them
    myModuleSelector = analysisModuleSelector.AnalysisModuleSelector()
    dsetMgrCreator = dataset.readFromMulticrabCfg(directory=sys.argv[1])
    myModuleSelector.setPrimarySource("analysis", dsetMgrCreator)
    myModuleSelector.doSelect(None)
    #myModuleSelector.printSelectedCombinationCount()
    for era in myModuleSelector.getSelectedEras():
        for searchMode in myModuleSelector.getSelectedSearchModes():
            for optimizationMode in myModuleSelector.getSelectedOptimizationModes(
            ):
                print ShellStyles.HighlightStyle(
                ) + "\nCalculating normalization for module %s/%s/%s%s" % (
                    era, searchMode, optimizationMode,
                    ShellStyles.NormalStyle())
                # Construct info string for the module
                moduleInfoString = "%s_%s" % (era, searchMode)
                if len(optimizationMode) > 0:
def main(opts):

    # Object for selecting data eras, search modes, and optimization modes
    myModuleSelector = analysisModuleSelector.AnalysisModuleSelector()

    # Obtain dsetMgrCreator and register it to module selector
    dsetMgrCreator = dataset.readFromMulticrabCfg(directory=opts.mcrab)

    # Obtain systematics names
    mySystematicsNamesRaw = dsetMgrCreator.getSystematicVariationSources()
    mySystematicsNames = getSystematicsNames(mySystematicsNamesRaw, opts)

    # Set the primary source
    Verbose(
        "Setting the primary source (label=%s)" %
        (ShellStyles.NoteStyle() + opts.analysisName +
         ShellStyles.NormalStyle()), True)
    myModuleSelector.setPrimarySource(label=opts.analysisName,
                                      dsetMgrCreator=dsetMgrCreator)

    # Select modules
    myModuleSelector.doSelect(opts=None)

    # Loop over era/searchMode/optimizationMode combos
    myTotalModules = myModuleSelector.getSelectedCombinationCount() * (
        len(mySystematicsNames) + 1)
    count, nEras, nSearchModes, nOptModes, nSysVars = myModuleSelector.getSelectedCombinationCountIndividually(
    )
    if nSysVars > 0:
        msg = "Running over %d modules (%d eras x %d searchModes x %d optimizationModes x %d systematic variations)" % (
            count, nEras, nSearchModes, nOptModes, nSysVars)
    else:
        msg = "Running over %d modules (%d eras x %d searchModes x %d optimizationModes)" % (
            count, nEras, nSearchModes, nOptModes)
    Verbose(msg, True)

    # Create pseudo-multicrab creator
    msg = "Creating pseudo-dataset \"%s\" inside the pseudo-multicrab directory \"%s\"" % (
        opts.newDsetName, opts.mcrab)
    Verbose(msg, True)
    myOutputCreator = pseudoMultiCrabCreator.PseudoMultiCrabCreator(
        opts.newDsetName, opts.mcrab, verbose=opts.verbose)

    # Make time stamp for start time
    myGlobalStartTime = time.time()

    # Initialize
    myOutputCreator.initialize(subTitle="", prefix="")

    # Get lists of settings
    erasList = myModuleSelector.getSelectedEras()
    modesList = myModuleSelector.getSelectedSearchModes()
    optList = myModuleSelector.getSelectedOptimizationModes()
    if 0:
        optList.append(
            "")  #append the default opt mode iff more optimization modes exist
    Verbose(
        "Found %d eras, %d modes, %d optimisations" %
        (len(erasList), len(modesList), len(optList)), True)

    iModule = 0
    # For-loop: All eras
    for era in erasList:
        # For-loop: All searchModes
        for searchMode in modesList:
            # For-loop: AlloptimizationModes
            for optimizationMode in optList:
                Verbose(
                    "era = %s, searchMode = %s, optMode = %s" %
                    (era, searchMode, optimizationMode), True)

                # If an optimization mode is defined in options skip the rest
                if opts.optMode != None:
                    if optimizationMode != opts.optMode:
                        continue

                # Nominal module
                myModuleInfoString = getModuleInfoString(
                    opts.analysisName, era, searchMode, optimizationMode)
                iModule += 1

                # Inform user of what is being processes
                msg = "{:<10} {:>3} {:^1} {:<3}: {:<20}".format(
                    "Module", iModule, "/", myTotalModules, myModuleInfoString)
                Print(hs + msg + ns, iModule == 1)

                Verbose("Creating dataset manager for nominal module", True)
                myStartTime = time.time()

                nominalModule = ModuleBuilder(opts, myOutputCreator,
                                              opts.verbose)
                nominalModule.createDsetMgr(opts.mcrab, opts.analysisName, era,
                                            searchMode, optimizationMode)

                # Build the module
                nominalModule.buildModule(opts.dsetSrc)

                Verbose("Deleting nominal module", True)
                nominalModule.delete()

                Verbose("Printing time estimate", True)
                printTimeEstimate(myGlobalStartTime, myStartTime, iModule,
                                  myTotalModules)

                Verbose("Now do the rest of systematics variations", True)
                for syst in mySystematicsNames:

                    myModuleInfoString = getModuleInfoString(
                        opts.analysisName, era, searchMode, optimizationMode,
                        syst)

                    iModule += 1
                    msg = "{:<10} {:>3} {:^1} {:<3}: {:<20}".format(
                        "Module", iModule, "/", myTotalModules,
                        myModuleInfoString)
                    Print(hs + msg + ns, False)

                    Verbose(
                        "Creating dataset manager for systematics module %s" %
                        (syst), True)
                    myStartTime = time.time()
                    systModule = ModuleBuilder(opts, myOutputCreator,
                                               opts.verbose)
                    systModule.createDsetMgr(opts.mcrab,
                                             opts.analysisName,
                                             era,
                                             searchMode,
                                             optimizationMode,
                                             systematicVariation=syst)

                    # Build the module
                    systModule.buildModule(opts.dsetSrc)

                    Verbose("Deleting nominal module", True)
                    systModule.delete()

                    Verbose("Printing time estimate", True)
                    printTimeEstimate(myGlobalStartTime, myStartTime, iModule,
                                      myTotalModules)

        Verbose(
            "New dataset %s added to pseud-multicrab %s" %
            (hs + opts.newDsetName + ns, hs + opts.mcrab + ns), True)

    # Print some timing statistics
    Verbose(
        "Average processing time per module was %.1f seconds" %
        getAvgProcessTimeForOneModule(myGlobalStartTime, myTotalModules), True)
    Verbose(
        "Total elapsed time was %.1f seconds" %
        getTotalElapsedTime(myGlobalStartTime), True)

    # Create rest of pseudo multicrab directory
    myOutputCreator.finalize(silent=False)

    # Update the multicrab.cfg file
    updateMulticrabCfg()

    return
def main(opts):

    # Object for selecting data eras, search modes, and optimization modes
    myModuleSelector = analysisModuleSelector.AnalysisModuleSelector()

    # Obtain dsetMgrCreator and register it to module selector
    dsetMgrCreator = dataset.readFromMulticrabCfg(directory=opts.mcrab)

    # Obtain systematics names
    mySystematicsNamesRaw = dsetMgrCreator.getSystematicVariationSources()
    mySystematicsNames = getSystematicsNames(mySystematicsNamesRaw, opts)

    # Set the primary source
    Verbose(
        "Setting the primary source (label=%s)" %
        (ShellStyles.NoteStyle() + opts.analysisName +
         ShellStyles.NormalStyle()), True)
    myModuleSelector.setPrimarySource(label=opts.analysisName,
                                      dsetMgrCreator=dsetMgrCreator)

    # Select modules
    myModuleSelector.doSelect(opts=None)  #fixme: (opts=opts)?

    # Loop over era/searchMode/optimizationMode combos
    myTotalModules = myModuleSelector.getSelectedCombinationCount() * (
        len(mySystematicsNames) + 1) * len(opts.shape)
    Verbose("Found %s modules in total" % (myTotalModules), True)

    count, nEras, nSearchModes, nOptModes, nSysVars = myModuleSelector.getSelectedCombinationCountIndividually(
    )
    if nSysVars > 0:
        msg = "Running over %d modules (%d eras x %d searchModes x %d optimizationModes x %d systematic variations)" % (
            count, nEras, nSearchModes, nOptModes, nSysVars)
    else:
        msg = "Running over %d modules (%d eras x %d searchModes x %d optimizationModes)" % (
            count, nEras, nSearchModes, nOptModes)
    Verbose(msg, True)

    # Create pseudo-multicrab creator
    msg = "Will create pseudo-dataset %s inside the pseudo-multicrab directory" % (
        ShellStyles.NoteStyle() + opts.analysisName +
        ShellStyles.NormalStyle())
    Verbose(msg, True)
    myOutputCreator = pseudoMultiCrabCreator.PseudoMultiCrabCreator(
        opts.analysisName, opts.mcrab, verbose=opts.verbose)

    # Make time stamp for start time
    myGlobalStartTime = time.time()

    iModule = 0
    # For-loop: All Shapes
    for iShape, shapeType in enumerate(opts.shape, 1):

        msg = "Shape %d/%d:%s %s" % (iShape, len(
            opts.shape), ShellStyles.NormalStyle(), shapeType)
        Verbose(ShellStyles.HighlightAltStyle() + msg, True)

        # Initialize
        myOutputCreator.initialize(
            subTitle=shapeType,
            prefix="")  #fixme: remove shapeType from sub-directory name?

        # Get lists of settings
        erasList = myModuleSelector.getSelectedEras()
        modesList = myModuleSelector.getSelectedSearchModes()
        optList = myModuleSelector.getSelectedOptimizationModes()
        if 0:
            optList.append(
                ""
            )  #append the default opt mode iff more optimization modes exist

        # For-loop: All eras
        for era in erasList:
            # For-loop: All searchModes
            for searchMode in modesList:
                # For-loop: AlloptimizationModes
                for optimizationMode in optList:
                    Verbose(
                        "era = %s, searchMode = %s, optMode = %s" %
                        (era, searchMode, optimizationMode), True)

                    # If an optimization mode is defined in options skip the rest
                    if opts.optMode != None:
                        if optimizationMode != opts.optMode:
                            continue

                    # Obtain normalization factors for given Era, SearchMode, and OptimizationMode!
                    myNormFactors = importNormFactors(era, searchMode,
                                                      optimizationMode,
                                                      opts.mcrab)

                    # Nominal module
                    myModuleInfoString = getModuleInfoString(
                        era, searchMode, optimizationMode)
                    iModule += 1

                    # Inform user of what is being processes
                    if optimizationMode != "":
                        msg = "Module %d/%d: %s_%s_%s_%s" % (
                            iModule, myTotalModules, opts.analysisName,
                            searchMode, era, optimizationMode)
                    else:
                        msg = "Module %d/%d: %s_%s_%s" % (
                            iModule, myTotalModules, opts.analysisName,
                            searchMode, era)
                    Print(
                        ShellStyles.HighlightAltStyle() + msg +
                        ShellStyles.NormalStyle(), iModule == 1)

                    # Keep time
                    myStartTime = time.time()

                    Verbose("Create dataset manager with given settings", True)
                    nominalModule = ModuleBuilder(opts, myOutputCreator,
                                                  opts.verbose)
                    nominalModule.createDsetMgr(opts.mcrab, era, searchMode,
                                                optimizationMode)

                    if (iModule == 1):
                        if opts.verbose:
                            nominalModule.debug()

                    # Build the module
                    doFakeBNormalisationSyst = False
                    nominalModule.buildModule(
                        opts.dataSrc, opts.ewkSrc,
                        myNormFactors[opts.normFactorKey],
                        doFakeBNormalisationSyst, opts.normDataSrc,
                        opts.normEwkSrc)

                    # Do TF variations named "SystVarUp" and "SystVarDown" (i.e. (Get results using TF+Error and TF-Error instead of TF)
                    if len(mySystematicsNames) > 0:
                        Verbose(
                            "Adding FakeB normalization systematics (iff also other systematics  present) ",
                            True)
                        #nominalModule.buildTransferFactorVarSystModule(opts.dataSrc, opts.ewkSrc, myNormFactors["SystVarUp"], myNormFactors["SystVarDown"])
                        nominalModule.buildTransferFactorVarSystModule(
                            opts.dataSrc, opts.ewkSrc, myNormFactors)

                    Verbose("Deleting nominal module", True)
                    nominalModule.delete()

                    Verbose("Printing time estimate", True)
                    printTimeEstimate(myGlobalStartTime, myStartTime, iModule,
                                      myTotalModules)

                    Verbose("Now do the rest of systematics variations", True)
                    for syst in mySystematicsNames:
                        iModule += 1
                        msg = "Module %d/%d: %s/%s" % (
                            iModule, myTotalModules, myModuleInfoString, syst)
                        print
                        Print(
                            ShellStyles.HighlightAltStyle() + msg +
                            ShellStyles.NormalStyle(), False)
                        myStartTime = time.time()
                        systModule = ModuleBuilder(opts, myOutputCreator)
                        # Create dataset manager with given settings
                        systModule.createDsetMgr(opts.mcrab,
                                                 era,
                                                 searchMode,
                                                 optimizationMode,
                                                 systematicVariation=syst)

                        # Build systematics module
                        Verbose(
                            "Building systematics module (opts.normFactorKey = %s)"
                            % (opts.normFactorKey), True)
                        systModule.buildModule(
                            opts.dataSrc, opts.ewkSrc,
                            myNormFactors[opts.normFactorKey], False,
                            opts.normDataSrc, opts.normEwkSrc)
                        printTimeEstimate(myGlobalStartTime, myStartTime,
                                          iModule, myTotalModules)
                        systModule.delete()
        print
        Verbose("Pseudo-multicrab ready for %s" % shapeType, True)

    # Print some timing statistics
    Verbose(
        "Average processing time per module was %.1f seconds" %
        getAvgProcessTimeForOneModule(myGlobalStartTime, myTotalModules), True)
    Print(
        "Total elapsed time was %.1f seconds" %
        getTotalElapsedTime(myGlobalStartTime), True)

    # Create rest of pseudo multicrab directory
    myOutputCreator.finalize(silent=False)

    return
Exemple #5
0
def main():

    # Object for selecting data eras, search modes, and optimization modes
    myModuleSelector = analysisModuleSelector.AnalysisModuleSelector()

    # Obtain multicrab directory
    myMulticrabDir = "."
    if opts.mcrab != None:
        myMulticrabDir = opts.mcrab
    if not os.path.exists("%s/multicrab.cfg" % myMulticrabDir):
        msg = "No multicrab directory found at path '%s'! Please check path or specify it with --mcrab!" % (
            myMulticrabDir)
        raise Exception(ShellStyles.ErrorLabel() + msg +
                        ShellStyles.NormalStyle())
    if len(opts.shape) == 0:
        raise Exception(
            ShellStyles.ErrorLabel() +
            "Provide a shape identifierwith --shape (for example MT)!" +
            ShellStyles.NormalStyle())

    # Obtain dsetMgrCreator and register it to module selector
    dsetMgrCreator = dataset.readFromMulticrabCfg(directory=myMulticrabDir)

    # Obtain systematics names
    mySystematicsNamesRaw = dsetMgrCreator.getSystematicVariationSources()
    mySystematicsNames = []
    for item in mySystematicsNamesRaw:
        mySystematicsNames.append("%sPlus" % item)
        mySystematicsNames.append("%sMinus" % item)
    if opts.test:
        mySystematicsNames = []  #[mySystematicsNames[0]] #FIXME

    # Set the primary source
    myModuleSelector.setPrimarySource(label=opts.analysisName,
                                      dsetMgrCreator=dsetMgrCreator)

    # Select modules
    myModuleSelector.doSelect(opts=None)  #FIXME: (opts=opts)

    # Loop over era/searchMode/optimizationMode combos
    myDisplayStatus = True
    myTotalModules = myModuleSelector.getSelectedCombinationCount() * (
        len(mySystematicsNames) + 1) * len(opts.shape)
    Verbose("Found %s modules in total" % (myTotalModules), True)

    count, nEras, nSearchModes, nOptModes, nSysVars = myModuleSelector.getSelectedCombinationCountIndividually(
    )
    if nSysVars > 0:
        msg = "Will run over %d modules (%d eras x %d searchModes x %d optimizationModes x %d systematic variations)" % (
            count, nEras, nSearchModes, nOptModes, nSysVars)
    else:
        msg = "Will run over %d modules (%d eras x %d searchModes x %d optimizationModes)" % (
            count, nEras, nSearchModes, nOptModes)
    Print(msg, True)

    # Create pseudo-multicrab creator
    myOutputCreator = pseudoMultiCrabCreator.PseudoMultiCrabCreator(
        opts.analysisName, myMulticrabDir)

    # Make time stamp for start time
    myGlobalStartTime = time.time()

    iModule = 0
    # For-loop: All Shapes
    for shapeType in opts.shape:

        # Initialize
        myOutputCreator.initialize(shapeType, prefix="")

        msg = "Creating dataset for shape \"%s\"%s" % (
            shapeType, ShellStyles.NormalStyle())
        Verbose(ShellStyles.HighlightStyle() + msg, True)

        # Get lists of settings
        erasList = myModuleSelector.getSelectedEras()
        modesList = myModuleSelector.getSelectedSearchModes()
        optList = myModuleSelector.getSelectedOptimizationModes()
        optList.append("")  #append the default opt mode!

        # For-Loop over era, searchMode, and optimizationMode options
        for era in erasList:
            for searchMode in modesList:
                for optimizationMode in optList:

                    Verbose(
                        "era = %s, searchMode = %s, optMode = %s" %
                        (era, searchMode, optimizationMode), True)
                    # If an optimization mode is defined in options skip the rest
                    if opts.optMode != None:
                        if optimizationMode != opts.optMode:
                            continue

                    # Obtain normalization factors
                    myNormFactors = importNormFactors(era, searchMode,
                                                      optimizationMode,
                                                      opts.mcrab)

                    # Nominal module
                    myModuleInfoString = getModuleInfoString(
                        era, searchMode, optimizationMode)
                    iModule += 1

                    # Inform user of what is being processes
                    msg = "Module %d/%d:%s %s/%s" % (
                        iModule, myTotalModules, ShellStyles.NormalStyle(),
                        myModuleInfoString, shapeType)
                    Print(ShellStyles.CaptionStyle() + msg, True)

                    # Keep time
                    myStartTime = time.time()

                    Verbose("Create dataset manager with given settings", True)
                    nominalModule = ModuleBuilder(opts, myOutputCreator)
                    nominalModule.createDsetMgr(myMulticrabDir, era,
                                                searchMode, optimizationMode)

                    if (iModule == 1):
                        if opts.verbose:
                            nominalModule.debug()

                    doQCDNormalizationSyst = False  #FIXME
                    if not doQCDNormalizationSyst:
                        msg = "Disabling systematics"
                        Print(ShellStyles.WarningLabel() + msg, True)
                    nominalModule.buildModule(opts.dataSrc, opts.ewkSrc,
                                              myNormFactors["nominal"],
                                              doQCDNormalizationSyst,
                                              opts.normDataSrc,
                                              opts.normEwkSrc)

                    if len(mySystematicsNames) > 0:
                        Print(
                            "Adding QCD normalization systematics (iff also other systematics  present) ",
                            True)
                        nominalModule.buildQCDNormalizationSystModule(
                            opts.dataSrc, opts.ewkSrc)

                    # FIXME: add quark gluon weighting systematics!
                    if 0:
                        Print("Adding Quark/Gluon weighting systematics", True)
                        nominalModule.buildQCDQuarkGluonWeightingSystModule(
                            opts.dataSrc, opts.ewkSrc,
                            myNormFactors["FakeWeightingUp"],
                            myNormFactors["FakeWeightingDown"], False,
                            opts.normDataSrc, opts.normEwkSrc)

                    Verbose("Deleting nominal module", True)
                    nominalModule.delete()

                    Verbose("Printing time estimate", True)
                    printTimeEstimate(myGlobalStartTime, myStartTime, iModule,
                                      myTotalModules)

                    Verbose("Now do the rest of systematics variations", True)
                    for syst in mySystematicsNames:
                        iModule += 1
                        msg = "Analyzing systematics variations %d/%d: %s/%s/%s" % (
                            iModule, myTotalModules, myModuleInfoString, syst,
                            shapeType)
                        Print(
                            ShellStyles.CaptionStyle() + msg +
                            ShellStyles.NormalStyle(), True)
                        myStartTime = time.time()
                        systModule = ModuleBuilder(opts, myOutputCreator)
                        # Create dataset manager with given settings
                        systModule.createDsetMgr(myMulticrabDir,
                                                 era,
                                                 searchMode,
                                                 optimizationMode,
                                                 systematicVariation=syst)

                        # Build asystematics module
                        systModule.buildModule(opts.dataSrc, opts.ewkSrc,
                                               myNormFactors["nominal"], False,
                                               opts.normDataSrc,
                                               opts.normEwkSrc)
                        printTimeEstimate(myGlobalStartTime, myStartTime,
                                          iModule, myTotalModules)
                        systModule.delete()

        Verbose("Pseudo-multicrab ready for %s" % shapeType, True)

    # Create rest of pseudo multicrab directory
    myOutputCreator.silentFinalize()

    # Print some timing statistics
    Print(
        "Average processing time per module was %.1f s" %
        getAvgProcessTimeForOneModule(myGlobalStartTime, myTotalModules), True)
    Print(
        "Total elapsed time was %.1f s" %
        getTotalElapsedTime(myGlobalStartTime), False)

    msg = "Created pseudo-multicrab %s for shape type \"%s\"" % (
        myOutputCreator.getDirName(), shapeType)
    Print(ShellStyles.SuccessLabel() + msg, True)
    return
Exemple #6
0
    #ROOT.SetMemoryPolicy( ROOT.kMemoryHeuristics)
    #memoryDump()

def memoryDump():
    dump = open("memory_pickle.txt", 'w')
    for obj in gc.get_objects():
        i = id(obj)
        size = sys.getsizeof(obj, 0)
        #    referrers = [id(o) for o in gc.get_referrers(obj) if hasattr(o, '__class__')]
        referents = [id(o) for o in gc.get_referents(obj) if hasattr(o, '__class__')]
        if hasattr(obj, '__class__'):
            cls = str(obj.__class__)
            cPickle.dump({'id': i, 'class': cls, 'size': size, 'referents': referents}, dump)

if __name__ == "__main__":
    myModuleSelector = analysisModuleSelector.AnalysisModuleSelector() # Object for selecting data eras, search modes, and optimization modes

    parser = OptionParser(usage="Usage: %prog [options]",add_help_option=False,conflict_handler="resolve")
    parser.add_option("-h", "--help", dest="helpStatus", action="store_true", default=False, help="Show this help message and exit")
    parser.add_option("-x", "--datacard", dest="datacard", action="store", help="Name (incl. path) of the datacard to be used as an input")
    myModuleSelector.addParserOptions(parser)
    parser.add_option("--lands", dest="lands", action="store_true", default=False, help="Generate datacards for LandS (not supported)")
    parser.add_option("--combine", dest="combine", action="store_true", default=True, help="Generate datacards for Combine (default=True)")
    parser.add_option("-d", "--dir", dest="directories", action="append", help="Name of directories for creating datacards for multiple directories")
    parser.add_option("--systAnalysis", dest="systAnalysis", action="store_true", default=False, help="Runs the macro for generating systematic uncertainties plots")
    parser.add_option("--testShapeSensitivity", dest="testShapeSensitivity", action="store_true", default=False, help="Creates datacards for varying each shape nuisance up and down by 1 sigma")
    parser.add_option("--showcard", dest="showDatacard", action="store_true", default=False, help="Print datacards also to screen")
    parser.add_option("--tailfit", dest="dotailfit", action="store_true", default=False, help="Runs the tail fitter for heavy H+ after the cards are done")
    #parser.add_option("--QCDfactorised", dest="useQCDfactorised", action="store_true", default=False, help="Use factorised method for QCD measurement")
    #parser.add_option("--QCDinverted", dest="useQCDinverted", action="store_true", default=False, help="Use inverted method for QCD measurement")
    parser.add_option("--debugDatasets", dest="debugDatasets", action="store_true", default=False, help="Enable debugging print for datasetMgr contents")
Exemple #7
0
def main(opts):

    # Object for selecting data eras, search modes, and optimization modes
    myModuleSelector = analysisModuleSelector.AnalysisModuleSelector()

    # Obtain dsetMgrCreator and register it to module selector
    dsetMgrCreator = dataset.readFromMulticrabCfg(directory=opts.mcrab)

    # Obtain systematics names
    mySystematicsNamesRaw = dsetMgrCreator.getSystematicVariationSources()
    mySystematicsNames = []
    for i, item in enumerate(mySystematicsNamesRaw, 0):
        Print(
            "Using systematic %s" %
            (ShellStyles.NoteStyle() + item + ShellStyles.NormalStyle()),
            i == 0)
        mySystematicsNames.append("%sPlus" % item)
        mySystematicsNames.append("%sMinus" % item)
    if opts.test:
        mySystematicsNames = []

    # Set the primary source
    Verbose(
        "Setting the primary source (label=%s)" %
        (ShellStyles.NoteStyle() + opts.analysisName +
         ShellStyles.NormalStyle()), True)
    myModuleSelector.setPrimarySource(
        label=opts.analysisName,
        dsetMgrCreator=dsetMgrCreator)  #fixme: what is label for?

    # Select modules
    myModuleSelector.doSelect(opts=None)  #fixme: (opts=opts)

    # Loop over era/searchMode/optimizationMode combos
    myTotalModules = myModuleSelector.getSelectedCombinationCount() * (
        len(mySystematicsNames) + 1) * len(opts.shape)
    Verbose("Found %s modules in total" % (myTotalModules), True)

    count, nEras, nSearchModes, nOptModes, nSysVars = myModuleSelector.getSelectedCombinationCountIndividually(
    )
    if nSysVars > 0:
        msg = "Running  over %d modules (%d eras x %d searchModes x %d optimizationModes x %d systematic variations)" % (
            count, nEras, nSearchModes, nOptModes, nSysVars)
    else:
        msg = "Running over %d modules (%d eras x %d searchModes x %d optimizationModes)" % (
            count, nEras, nSearchModes, nOptModes)
    Verbose(msg, True)

    # Create pseudo-multicrab creator
    msg = "Will create pseudo-dataset %s inside the pseudo-multicrab directory" % (
        ShellStyles.NoteStyle() + opts.analysisName +
        ShellStyles.NormalStyle())
    Verbose(msg, True)
    myOutputCreator = pseudoMultiCrabCreator.PseudoMultiCrabCreator(
        opts.analysisName, opts.mcrab, verbose=opts.verbose)

    # Make time stamp for start time
    myGlobalStartTime = time.time()

    iModule = 0
    # For-loop: All Shapes
    for iShape, shapeType in enumerate(opts.shape, 1):

        msg = "Shape %d/%d:%s %s" % (iShape, len(
            opts.shape), ShellStyles.NormalStyle(), shapeType)
        Print(ShellStyles.CaptionStyle() + msg, True)

        # Initialize
        myOutputCreator.initialize(
            subTitle=shapeType,
            prefix="")  #fixeme: remove shapeType from sub-directory name?

        # Get lists of settings
        erasList = myModuleSelector.getSelectedEras()
        modesList = myModuleSelector.getSelectedSearchModes()
        optList = myModuleSelector.getSelectedOptimizationModes()
        if 0:
            optList.append(
                ""
            )  #append the default opt mode iff more optimization modes exist

        # For-Loop over era, searchMode, and optimizationMode options
        for era in erasList:
            for searchMode in modesList:
                for optimizationMode in optList:

                    Verbose(
                        "era = %s, searchMode = %s, optMode = %s" %
                        (era, searchMode, optimizationMode), True)
                    # If an optimization mode is defined in options skip the rest
                    if opts.optMode != None:
                        if optimizationMode != opts.optMode:
                            continue

                    # Obtain normalization factors
                    myNormFactors = importNormFactors(era, searchMode,
                                                      optimizationMode,
                                                      opts.mcrab)

                    # Nominal module
                    myModuleInfoString = getModuleInfoString(
                        era, searchMode, optimizationMode)
                    iModule += 1

                    # Inform user of what is being processes
                    msg = "Module %d/%d:%s %s/%s" % (
                        iModule, myTotalModules, ShellStyles.NormalStyle(),
                        myModuleInfoString, shapeType)
                    Print(ShellStyles.CaptionStyle() + msg, True)

                    # Keep time
                    myStartTime = time.time()

                    Verbose("Create dataset manager with given settings", True)
                    nominalModule = ModuleBuilder(opts, myOutputCreator,
                                                  opts.verbose)
                    nominalModule.createDsetMgr(opts.mcrab, era, searchMode,
                                                optimizationMode)

                    if (iModule == 1):
                        if opts.verbose:
                            nominalModule.debug()

                    doQCDNormalizationSyst = False  #FIXME
                    if not doQCDNormalizationSyst:
                        msg = "Disabling systematics"
                        Verbose(ShellStyles.WarningLabel() + msg, True)  #fixme

                    # Build the module
                    nominalModule.buildModule(
                        opts.dataSrc, opts.ewkSrc,
                        myNormFactors[opts.normFactorKey],
                        doQCDNormalizationSyst, opts.normDataSrc,
                        opts.normEwkSrc)

                    if len(mySystematicsNames) > 0:
                        Print(
                            "Adding QCD normalization systematics (iff also other systematics  present) ",
                            True)
                        nominalModule.buildQCDNormalizationSystModule(
                            opts.dataSrc, opts.ewkSrc)

                    # FIXME: add quark gluon weighting systematics!
                    if 0:
                        Print("Adding Quark/Gluon weighting systematics", True)
                        nominalModule.buildQCDQuarkGluonWeightingSystModule(
                            opts.dataSrc, opts.ewkSrc,
                            myNormFactors["FakeWeightingUp"],
                            myNormFactors["FakeWeightingDown"], False,
                            opts.normDataSrc, opts.normEwkSrc)

                    Verbose("Deleting nominal module", True)
                    nominalModule.delete()

                    Verbose("Printing time estimate", True)
                    printTimeEstimate(myGlobalStartTime, myStartTime, iModule,
                                      myTotalModules)

                    Verbose("Now do the rest of systematics variations", True)
                    for syst in mySystematicsNames:
                        iModule += 1
                        msg = "Analyzing systematics variations %d/%d: %s/%s/%s" % (
                            iModule, myTotalModules, myModuleInfoString, syst,
                            shapeType)
                        Print(
                            ShellStyles.CaptionStyle() + msg +
                            ShellStyles.NormalStyle(), True)
                        myStartTime = time.time()
                        systModule = ModuleBuilder(opts, myOutputCreator)
                        # Create dataset manager with given settings
                        systModule.createDsetMgr(opts.mcrab,
                                                 era,
                                                 searchMode,
                                                 optimizationMode,
                                                 systematicVariation=syst)

                        # Build asystematics module
                        systModule.buildModule(
                            opts.dataSrc, opts.ewkSrc,
                            myNormFactors[opts.normFactorKey], False,
                            opts.normDataSrc, opts.normEwkSrc)
                        printTimeEstimate(myGlobalStartTime, myStartTime,
                                          iModule, myTotalModules)
                        systModule.delete()

        Verbose("Pseudo-multicrab ready for %s" % shapeType, True)

    # Print some timing statistics
    Print(
        "Average processing time per module was %.1f seconds" %
        getAvgProcessTimeForOneModule(myGlobalStartTime, myTotalModules), True)
    Print(
        "Total elapsed time was %.1f seconds" %
        getTotalElapsedTime(myGlobalStartTime), False)

    # Create rest of pseudo multicrab directory
    myOutputCreator.finalize(silent=False)

    return