Beispiel #1
0
def get_list_of_syst_to_fill(opts):
    systematics = ['NOM']
    sysOption = opts.syst
    excludedSyst = opts.exclude
    anySys = sysOption == None
    if sysOption == 'fake' or anySys:
        systematics += systUtils.fakeSystVariations()
    if sysOption == 'object' or anySys:
        systematics += systUtils.mcObjectVariations()
    if sysOption == 'weight' or anySys:
        systematics += systUtils.mcWeightVariations()
    if sysOption and sysOption.count(','):
        systematics = [
            s for s in systUtils.getAllVariations()
            if s in sysOption.split(',')
        ]
    elif sysOption in systUtils.getAllVariations():
        systematics = [sysOption]
    elif not anySys and len(systematics) == 1 and sysOption != 'NOM':
        raise ValueError("Invalid syst %s" % str(sysOption))
    if excludedSyst:
        systematics = [
            s for s in systematics
            if s not in filterWithRegexp(systematics, excludedSyst)
        ]
    return systematics
def runFill(opts) :
    batchMode    = opts.batch
    inputFakeDir = opts.input_fake
    inputGenDir  = opts.input_gen
    outputDir    = opts.output_dir
    sysOption    = opts.syst
    excludedSyst = opts.exclude
    verbose      = opts.verbose

    if verbose : print "filling histos"
    mkdirIfNeeded(outputDir)
    systematics = ['NOM']
    anySys = sysOption==None
    if sysOption=='fake'   or anySys : systematics += systUtils.fakeSystVariations()
    if sysOption=='object' or anySys : systematics += systUtils.mcObjectVariations()
    if sysOption=='weight' or anySys : systematics += systUtils.mcWeightVariations()
    if sysOption and sysOption.count(',') : systematics = [s for s in systUtils.getAllVariations() if s in sysOption.split(',')]
    elif sysOption in systUtils.getAllVariations() : systematics = [sysOption]
    elif not anySys and len(systematics)==1 and sysOption!='NOM' : raise ValueError("Invalid syst %s"%str(sysOption))
    if excludedSyst : systematics = [s for s in systematics if s not in filterWithRegexp(systematics, excludedSyst)]

    if verbose : print "about to loop over these systematics:\n %s"%str(systematics)
    for syst in systematics :
        if batchMode :
            newOptions  = " --input-gen %s" % opts.input_gen
            newOptions += " --input-fake %s" % opts.input_fake
            newOptions += " --output-dir %s" % opts.output_dir
            newOptions += " --verbose %s" % opts.verbose
            newOptions += " --syst %s" % syst
            template = 'batch/templates/check_hft_fill.sh.template'
            script = "batch/hft_%s.sh"%syst
            scriptFile = open(script, 'w')
            scriptFile.write(open(template).read()
                             .replace('%(opt)s', newOptions)
                             .replace('%(logfile)s', 'log/hft/fill_'+syst+'.log')
                             .replace('%(jobname)s', 'fill_'+syst))
            scriptFile.close()
            cmd = "sbatch %s"%script
            if verbose : print cmd
            out = getCommandOutput(cmd)
            if verbose : print out['stdout']
            if out['stderr'] : print  out['stderr']
            continue
        if verbose : print '---- filling ',syst
        samplesPerGroup = allSamplesAllGroups()
        [s.setSyst(syst) for g, samples in samplesPerGroup.iteritems() for s in samples]
        counters, histos = countAndFillHistos(samplesPerGroup=samplesPerGroup, syst=syst, verbose=verbose, outdir=outputDir)
        printCounters(counters)
        saveHistos(samplesPerGroup, histos, outputDir, verbose)
Beispiel #3
0
def get_list_of_syst_to_fill(opts):
    systematics = ['NOM']
    sysOption    = opts.syst
    excludedSyst = opts.exclude
    anySys       = sysOption==None
    if sysOption=='fake'   or anySys : systematics += systUtils.fakeSystVariations()
    if sysOption=='object' or anySys : systematics += systUtils.mcObjectVariations()
    if sysOption=='weight' or anySys : systematics += systUtils.mcWeightVariations()
    if sysOption and sysOption.count(','):
        systematics = [s for s in systUtils.getAllVariations() if s in sysOption.split(',')]
    elif sysOption in systUtils.getAllVariations(): systematics = [sysOption]
    elif not anySys and len(systematics)==1 and sysOption!='NOM':
        raise ValueError("Invalid syst %s"%str(sysOption))
    if excludedSyst:
        systematics = [s for s in systematics if s not in filterWithRegexp(systematics, excludedSyst)]
    return systematics
def main() :
    parser = optparse.OptionParser(usage=usage)
    parser.add_option('-b', '--batch',  action='store_true', default=False, help='submit to batch (used in fill mode)')
    parser.add_option('-f', '--input-fake', help='location hft trees for fake')
    parser.add_option('-g', '--input-gen', help='location hft trees for everything else')
    parser.add_option('-i', '--input-dir')
    parser.add_option('-o', '--output-dir')
    parser.add_option('-s', '--syst', help="variations to process (default all). Give a comma-sep list or say 'weight', 'object', or 'fake'")
    parser.add_option('-e', '--exclude', help="skip some systematics, example 'EL_FR_.*'")
    parser.add_option('-v', '--verbose', action='store_true', default=False)
    parser.add_option('-l', '--list-systematics', action='store_true', default=False, help='list what is already in output_dir')
    parser.add_option('-L', '--list-all-systematics', action='store_true', default=False, help='list all possible systematics')

    (opts, args) = parser.parse_args()
    if opts.list_all_systematics :
        print "All systematics:\n\t%s"%'\n\t'.join(systUtils.getAllVariations())
        return
    if opts.list_systematics :
        print listExistingSyst(opts.input_dir)
        return
    inGenSpecified, inDirSpecified = opts.input_gen!=None, opts.input_dir!=None
    eitherMode = inGenSpecified != inDirSpecified
    if not eitherMode : parser.error("Run either in 'fill' or 'plot' mode")
    mode = 'fill' if inGenSpecified else 'plot' if inDirSpecified else None
    requiredOptions = (['input_fake', 'input_gen', 'output_dir'] if mode=='fill' else ['input_dir', 'output_dir'])
    allOptions = [x.dest for x in parser._get_all_options()[1:]]
    def optIsNotSpecified(o) : return not hasattr(opts, o) or getattr(opts,o) is None
    if any(optIsNotSpecified(o) for o in requiredOptions) :
        parser.error('Missing required option\n'+'\n'.join(["%s : %s"%(o, getattr(opts, o)) for o in requiredOptions]))
    if opts.verbose : print '\nUsing the following options:\n'+'\n'.join("%s : %s"%(o, str(getattr(opts, o))) for o in allOptions)

    if   mode=='fill' : runFill(opts)
    elif mode=='plot' : runPlot(opts)
 def variationsSummary(self) :
     summaries = {} # one summary for each selection
     for selection, sysCounts in self.varCounts.iteritems() :
         nominalCount = sysCounts['NOM']
         summaries[selection] = [(sys, sysCount, (100.0*(sysCount-nominalCount)/nominalCount) if nominalCount else None)
                                 for sys, sysCount in sortedAs(sysCounts, systUtils.getAllVariations())]
     return summaries
Beispiel #6
0
def main() :
    parser = optparse.OptionParser(usage=usage)
    parser.add_option('-g', '--group', help='group to be processed (used only in fill mode)')
    parser.add_option('--exclude-group', help='exclude group from processing (used only in fill mode)')
    parser.add_option('-f', '--input-fake', help='location of fake trees')
    parser.add_option('-O', '--input-other', help='location other trees')
    parser.add_option('-i', '--input-dir')
    parser.add_option('-o', '--output-dir')
    parser.add_option('--samples-dir', default='samples/',
                      help='directory with the list of samples; default ./samples/')
    parser.add_option('-s', '--syst', help="variations to process (default all)."
                      " Give a comma-sep list or say 'weight', 'object', or 'fake'")
    parser.add_option('--log-dir', help='directory where the batch logs will be (default log/...)')
    parser.add_option('-e', '--exclude', help="skip some systematics, example 'EL_FR_.*'")
    parser.add_option('-q', '--queue', default='atlas_all', help="batch queue, default atlas_all")
    parser.add_option('-T', '--tight-def', help='on-the-fly tight def, one of defs in fakeUtils.py: fakeu.lepIsTight_std, etc.')
    parser.add_option('--regions', default=None, help='comma-separated list of regions to consider')
    parser.add_option('--include-regions', default='.*', help='regexp to filter regions')
    parser.add_option('--exclude-regions', default=None, help='regext to exclude regions')
    # reminder: submit_batch_fill_job_per_group expects argument-less opt to default to False
    parser.add_option('--debug', action='store_true')
    parser.add_option('--verbose', action='store_true')
    parser.add_option('--unblind', action='store_true')
    parser.add_option('-b', '--batch',  action='store_true', help='submit to batch (used in fill mode)')
    parser.add_option('-l', '--list-systematics', action='store_true', help='list what is already in output_dir')
    parser.add_option('-L', '--list-all-systematics', action='store_true', help='list all possible systematics')
    parser.add_option('--list-all-regions', action='store_true', help='list all possible regions')
    parser.add_option('--require-tight-tight', action='store_true', help='fill histos only when both leps are tight')
    parser.add_option('--quick-test', action='store_true', help='run a quick test and fill only 1% of the events')
    parser.add_option('--disable-cache', action='store_true', help='disable the entry cache')
    parser.add_option('--format-aux', action='store_true', help='format plots for paper aux material')

    (opts, args) = parser.parse_args()
    if opts.list_all_systematics :
        print "All systematics:\n\t%s"%'\n\t'.join(systUtils.getAllVariations())
        return
    if opts.list_systematics :
        print listExistingSyst(opts.input_dir)
        return
    if opts.list_all_regions:
        print "All regions:\n\t%s"%'\n\t'.join(sorted(selection_formulas().keys()))
        return

    inOtherSpecified, inDirSpecified = opts.input_other!=None, opts.input_dir!=None
    eitherMode = inOtherSpecified != inDirSpecified
    if not eitherMode : parser.error("Run either in 'fill' or 'plot' mode")
    mode = 'fill' if inOtherSpecified else 'plot' if inDirSpecified else None
    if opts.quick_test : opts.disable_cache = True # don't write bogus entrylists
    requiredOptions = (['input_fake', 'input_other', 'output_dir'] if mode=='fill'
                       else ['input_dir', 'output_dir'])
    def optIsNotSpecified(o) : return not hasattr(opts, o) or getattr(opts,o) is None
    if any(optIsNotSpecified(o) for o in requiredOptions):
        parser.error('Missing required option\n'
                     +'\n'.join(["%s : %s"%(o, getattr(opts, o)) for o in requiredOptions]))
    if opts.verbose : utils.print_running_conditions(parser, opts)

    if   mode=='fill' : runFill(opts)
    elif mode=='plot' : runPlot(opts)
Beispiel #7
0
def main():
    parser = optparse.OptionParser(usage=usage)
    parser.add_option('-g',
                      '--group',
                      help='group to be processed (used only in fill mode)')
    parser.add_option(
        '--exclude-group',
        help='exclude group from processing (used only in fill mode)')
    parser.add_option('-f', '--input-fake', help='location of fake trees')
    parser.add_option('-O', '--input-other', help='location other trees')
    parser.add_option('-i', '--input-dir')
    parser.add_option('-o', '--output-dir')
    parser.add_option(
        '--samples-dir',
        default='samples/',
        help='directory with the list of samples; default ./samples/')
    parser.add_option(
        '-s',
        '--syst',
        help="variations to process (default all)."
        " Give a comma-sep list or say 'weight', 'object', or 'fake'")
    parser.add_option(
        '--log-dir',
        help='directory where the batch logs will be (default log/...)')
    parser.add_option('-e',
                      '--exclude',
                      help="skip some systematics, example 'EL_FR_.*'")
    parser.add_option('-q',
                      '--queue',
                      default='atlas_all',
                      help="batch queue, default atlas_all")
    parser.add_option(
        '-T',
        '--tight-def',
        help=
        'on-the-fly tight def, one of defs in fakeUtils.py: fakeu.lepIsTight_std, etc.'
    )
    parser.add_option('--regions',
                      default=None,
                      help='comma-separated list of regions to consider')
    parser.add_option('--include-regions',
                      default='.*',
                      help='regexp to filter regions')
    parser.add_option('--exclude-regions',
                      default=None,
                      help='regext to exclude regions')
    # reminder: submit_batch_fill_job_per_group expects argument-less opt to default to False
    parser.add_option('--debug', action='store_true')
    parser.add_option('--verbose', action='store_true')
    parser.add_option('--unblind', action='store_true')
    parser.add_option('-b',
                      '--batch',
                      action='store_true',
                      help='submit to batch (used in fill mode)')
    parser.add_option('-l',
                      '--list-systematics',
                      action='store_true',
                      help='list what is already in output_dir')
    parser.add_option('-L',
                      '--list-all-systematics',
                      action='store_true',
                      help='list all possible systematics')
    parser.add_option('--list-all-regions',
                      action='store_true',
                      help='list all possible regions')
    parser.add_option('--require-tight-tight',
                      action='store_true',
                      help='fill histos only when both leps are tight')
    parser.add_option('--quick-test',
                      action='store_true',
                      help='run a quick test and fill only 1% of the events')
    parser.add_option('--disable-cache',
                      action='store_true',
                      help='disable the entry cache')
    parser.add_option('--format-aux',
                      action='store_true',
                      help='format plots for paper aux material')

    (opts, args) = parser.parse_args()
    if opts.list_all_systematics:
        print "All systematics:\n\t%s" % '\n\t'.join(
            systUtils.getAllVariations())
        return
    if opts.list_systematics:
        print listExistingSyst(opts.input_dir)
        return
    if opts.list_all_regions:
        print "All regions:\n\t%s" % '\n\t'.join(
            sorted(selection_formulas().keys()))
        return

    inOtherSpecified, inDirSpecified = opts.input_other != None, opts.input_dir != None
    eitherMode = inOtherSpecified != inDirSpecified
    if not eitherMode: parser.error("Run either in 'fill' or 'plot' mode")
    mode = 'fill' if inOtherSpecified else 'plot' if inDirSpecified else None
    if opts.quick_test:
        opts.disable_cache = True  # don't write bogus entrylists
    requiredOptions = (['input_fake', 'input_other', 'output_dir']
                       if mode == 'fill' else ['input_dir', 'output_dir'])

    def optIsNotSpecified(o):
        return not hasattr(opts, o) or getattr(opts, o) is None

    if any(optIsNotSpecified(o) for o in requiredOptions):
        parser.error('Missing required option\n' + '\n'.join(
            ["%s : %s" % (o, getattr(opts, o)) for o in requiredOptions]))
    if opts.verbose: utils.print_running_conditions(parser, opts)

    if mode == 'fill': runFill(opts)
    elif mode == 'plot': runPlot(opts)
def runPlot(opts) :
    inputDir     = opts.input_dir
    outputDir    = opts.output_dir
    sysOption    = opts.syst
    excludedSyst = opts.exclude
    verbose      = opts.verbose
    mkdirIfNeeded(outputDir)
    buildTotBkg = systUtils.buildTotBackgroundHisto
    buildStat = systUtils.buildStatisticalErrorBand
    buildSyst = systUtils.buildSystematicErrorBand

    groups = allGroups()
    selections = allRegions()
    variables = variablesToPlot()
    for group in groups :
        group.setHistosDir(inputDir)
        group.exploreAvailableSystematics(verbose)
        group.filterAndDropSystematics(sysOption, excludedSyst, verbose)

    mkdirIfNeeded(outputDir)
    systematics = ['NOM']
    anySys = sysOption==None
    if sysOption=='fake'   or anySys : systematics += systUtils.fakeSystVariations()
    if sysOption=='object' or anySys : systematics += systUtils.mcObjectVariations()
    if sysOption=='weight' or anySys : systematics += systUtils.mcWeightVariations()
    if sysOption and sysOption.count(',') : systematics = [s for s in systUtils.getAllVariations() if s in sysOption.split(',')]
    elif sysOption in systUtils.getAllVariations() : systematics = [sysOption]
    if not anySys and len(systematics)==1 and sysOption!='NOM' : raise ValueError("Invalid syst %s"%str(sysOption))
    if excludedSyst : systematics = [s for s in systematics if s not in filterWithRegexp(systematics, excludedSyst)]
    if verbose : print "using the following systematics : %s"%str(systematics)

    fakeSystematics = [s for s in systematics if s in systUtils.fakeSystVariations()]
    mcSystematics = [s for s in systematics if s in systUtils.mcObjectVariations() + systUtils.mcWeightVariations()]

    simBkgs = [g for g in groups if g.isMcBkg]
    data, fake, signal = findByName(groups, 'data'), findByName(groups, 'fake'), findByName(groups, 'signal')

    for sel in selections :
        if verbose : print '-- plotting ',sel
        for var in variables :
            if verbose : print '---- plotting ',var
            for g in groups : g.setSystNominal()
            nominalHistoData    = data.getHistogram(variable=var, selection=sel, cacheIt=True)
            nominalHistoSign    = signal.getHistogram(variable=var, selection=sel, cacheIt=True)
            nominalHistoFakeBkg = fake.getHistogram(variable=var, selection=sel, cacheIt=True)
            nominalHistosSimBkg = dict([(g.name, g.getHistogram(variable=var, selection=sel, cacheIt=True)) for g in simBkgs])
            nominalHistosBkg    = dict([('fake', nominalHistoFakeBkg)] + [(g, h) for g, h in nominalHistosSimBkg.iteritems()])
            nominalHistoTotBkg  = buildTotBkg(histoFakeBkg=nominalHistoFakeBkg, histosSimBkgs=nominalHistosSimBkg)
            statErrBand = buildStat(nominalHistoTotBkg)
            systErrBand = buildSyst(fake=fake, simBkgs=simBkgs, variable=var, selection=sel,
                                    fakeVariations=fakeSystematics, mcVariations=mcSystematics, verbose=verbose)

            plotHistos(histoData=nominalHistoData, histoSignal=nominalHistoSign, histoTotBkg=nominalHistoTotBkg,
                       histosBkg=nominalHistosBkg,
                       statErrBand=statErrBand, systErrBand=systErrBand,
                       canvasName=(sel+'_'+var), outdir=outputDir, verbose=verbose)
    for group in groups :
        summary = group.variationsSummary()
        for selection, summarySel in summary.iteritems() :
            colW = str(12)
            header = ' '.join([('%'+colW+'s')%colName for colName in ['variation', 'yield', 'delta[%]']])
            lineTemplate = '%(sys)'+colW+'s'+'%(counts)'+colW+'s'+'%(delta)'+colW+'s'
            print "---- summary of variations for %s ----" % group.name
            print "---             %s                 ---" % selection
            print header
            print '\n'.join(lineTemplate%{'sys':s,
                                          'counts':(("%.3f"%c) if type(c) is float else (str(c)+str(type(c)))),
                                          'delta' :(("%.3f"%d) if type(d) is float else '--' if d==None else (str(d)+str(type(d)))) }
                            for s,c,d in summarySel)