示例#1
0
def colvar(string):
    for var in [
            'm_vis',
            'pt_1',
            'njets',
    ]:
        var = repr(var)
        string = string.replace(var, color(var))
    for classname in ['Variable', 'Selection']:
        string = string.replace(classname, color(classname, 'grey'))
    return string
示例#2
0
def main_set(args):
    """Set variables in the config file."""
    if args.verbosity >= 1:
        print ">>> main_set", args
    variable = args.variable
    key = args.key  # 'channel' or 'era'
    value = args.value
    verbosity = args.verbosity
    cfgname = CONFIG._path
    if key:  # redirect 'channel' and 'era' keys to main_link
        args.subcommand = variable
        return main_link(args)
    elif variable in ['channel', 'era']:
        LOG.throw(IOError,
                  "Variable '%s' is reserved for dictionaries!" % (variable))
    if verbosity >= 1:
        print '-' * 80
    print ">>> Setting variable '%s' to '%s' config" % (color(variable), value)
    if verbosity >= 1:
        print ">>> %-14s = %s" % ('cfgname', cfgname)
        print ">>> %-14s = %s" % ('config', CONFIG)
        print '-' * 80
    if variable == 'all':
        if 'default' in value:
            GLOB.setdefaultconfig(verb=verb)
        else:
            LOG.warning(
                "Did not recognize value '%s'. Did you mean 'default'?" %
                (value))
    elif variable in ['nfilesperjob', 'maxevtsperjob']:
        CONFIG[variable] = int(value)
        CONFIG.write()
    else:
        CONFIG[variable] = value
        CONFIG.write()
示例#3
0
def main():

    #### Test several initializations of Variable object.
    #### Note that key-word arguments starting with 'c' are used for context-dependent attributes
    ###mvisbins  = [0,30,40,50,60,70,75,80,90,100,120,200]
    ###variables = [
    ###  Variable('m_vis',     "m_{vis} [GeV]", 40, 0,200),
    ###  Variable('njets',     "Number of jets", 8, 0,  8),
    ###  Variable('m_vis',     40, 0,200),
    ###  Variable('njets',      8, 0,  8),
    ###  Variable('njets',      8, 0,  8, veto='njets'),
    ###  Variable('st',        20, 0,800, title="S_{T}",only="njets",blind="st>600"),
    ###  Variable('m_vis',     40, 0,200, cbins={'njets':mvisbins},blind=(70,90)),
    ###  Variable('m_vis',      mvisbins, cbins={"njets[^&]*2":(40,0,200)},),
    ###  Variable('pt_1+pt_2', 20, 0,800, title="S_{T}^{tautau}",ctitle={"njets":"S_{T}"}),
    ###]
    selections = [
        Selection(""),
        Selection("baseline", "pt_1>50 && pt_2>50"),
        Selection("njets>=1", "pt_1>50 && pt_2>50 && njets>=1"),
        Selection("1 b tags",
                  "pt_1>50 && pt_2>50 && njets>=2 && nbtags>=1",
                  weight="btagweight"),
    ]

    for selection in selections:
        LOG.header(selection.name)
        print ">>> name='%s', filename='%s', title='%s', cut='%s'" % (color(
            selection.name), color(selection.filename), color(
                selection.title), color(selection.selection))
        print ">>> weight=%r, drawcmd=%r" % (selection.weight,
                                             selection.drawcmd())
        sum1 = selection + "dzeta>-40"
        print '>>> sum1 = selection + "dzeta>-40"'
        print ">>>   name=%r, filename=%r, title=%r" % (
            sum1.name, sum1.filename, sum1.title)
        print ">>>   cut=%r" % (sum1.selection)
        sum2 = selection + Selection("dzeta", "dzeta>-40")
        print '>>> sum2 = selection + Selection("dzeta","dzeta>-40")'
        print ">>>   name=%r, filename=%r, title=%r" % (
            sum2.name, sum2.filename, sum2.title)
        print ">>>   cut=%r" % (sum2.selection)
示例#4
0
def printfamily(leaf, family, depth=0, mark=[], verb=0):
    """Recursively print family starting from common ancestor 'root'."""
    indent = ("  " * depth)
    line = color(leaf) if leaf in mark else leaf
    print indent + line
    kids = family.get(leaf, None)
    if kids == None:
        print "Warning! %r not in family!" % (leaf)
    else:
        for kid in kids:
            printfamily(kid, family, depth=depth + 1, mark=mark, verb=verb)
示例#5
0
def main_rm(args):
    """Remove variable from the config file."""
    if args.verbosity >= 1:
        print ">>> main_rm", args
    variable = args.variable
    key = args.key  # 'channel' or 'era'
    verbosity = args.verbosity
    cfgname = CONFIG._path
    if verbosity >= 1:
        print '-' * 80
    if key:
        print ">>> Removing %s '%s' from the configuration..." % (variable,
                                                                  color(key))
    else:
        print ">>> Removing variable '%s' from the configuration..." % (
            color(variable))
    if verbosity >= 1:
        print ">>> %-14s = %s" % ('variable', variable)
        print ">>> %-14s = %s" % ('key', key)
        print ">>> %-14s = %s" % ('cfgname', cfgname)
        print ">>> %-14s = %s" % ('config', CONFIG)
        print '-' * 80
    if key:  # redirect 'channel' and 'era' keys to main_link
        variable = variable + 's'
        if variable in CONFIG:
            if key in CONFIG[variable]:
                CONFIG[variable].pop(key, None)
                CONFIG.write()
            else:
                print ">>> %s '%s' not in the configuration. Nothing to remove..." % (
                    variable.capitalize(), key)
        else:
            print ">>> Variable '%s' not in the configuration. Nothing to remove..." % (
                variable)
    else:
        if variable in CONFIG:
            CONFIG.pop(variable)
            CONFIG.write()
        else:
            print ">>> Variable '%s' not in the configuration. Nothing to remove..." % (
                variable)
示例#6
0
def printfamily(leaf,family,depth=0,mark=[ ],evts=None,verb=0):
  """Recursively print family starting from common ancestor 'root'."""
  indent = ("  "*depth)
  line   = color(leaf) if leaf in mark else leaf
  if isinstance(evts,dict):
    if leaf in evts:
      nevts = evts[leaf]
    else:
      nevts = getdasnevents(leaf)
      evts[leaf] = nevts # cache to save time
    line += ", %d"%(nevts)
  print indent+line
  kids = family.get(leaf,None)
  if kids==None:
    print "Warning! %r not in family!"%(leaf)
  else:
    for kid in kids:
      printfamily(kid,family,depth=depth+1,mark=mark,evts=evts,verb=verb)
示例#7
0
def main_list(args):
    """List contents of configuration for those lazy to do 'cat config/config.json'."""
    if args.verbosity >= 1:
        print ">>> main_list", args
    verbosity = args.verbosity
    cfgname = CONFIG._path
    if verbosity >= 1:
        print '-' * 80
        print ">>> %-14s = %s" % ('cfgname', cfgname)
        print ">>> %-14s = %s" % ('config', CONFIG)
        print '-' * 80

    print ">>> Configuration %s:" % (cfgname)
    for variable, value in CONFIG.iteritems():
        variable = "'" + color(variable) + "'"
        if isinstance(value, dict):
            print ">>>  %s:" % (variable)
            for key, item in value.iteritems():
                if isinstance(item, basestring): item = str(item)
                print ">>>    %-12r -> %r" % (str(key), str(item))
        else:
            if isinstance(value, basestring): value = str(value)
            print ">>>  %-24s = %r" % (variable, value)
示例#8
0
def createinputs(fname, sampleset, observables, bins, **kwargs):
    """Create histogram inputs in ROOT file for datacards.
       fname:       filename pattern of ROOT file
       sampleset:   SampleSet object
       observables: list of Variables objects
       bins:        list of Selection objects
  """
    #LOG.header("createinputs")
    outdir = kwargs.get('outdir', "")
    tag = kwargs.get('tag', "")  # file tag
    htag = kwargs.get('htag', "")  # hist tag for systematic
    filters = kwargs.get('filter',
                         None)  # only create histograms for these processes
    vetoes = kwargs.get('veto', None)  # veto these processes
    parallel = kwargs.get('parallel', True)  # MultiDraw histograms in parallel
    recreate = kwargs.get('recreate', False)  # recreate ROOT file
    replaceweight = kwargs.get('replaceweight', None)  # replace weight
    extraweight = kwargs.get('weight', "")  # extraweight
    shiftQCD = kwargs.get('shiftQCD', 0)  # e.g 0.30 for 30%
    verbosity = kwargs.get('verb', 0)
    option = 'RECREATE' if recreate else 'UPDATE'
    method = 'QCD_OSSS' if filters == None or 'QCD' in filters else None
    method = kwargs.get('method', method)

    # FILE LOGISTICS: prepare file and directories
    files = {}
    ensuredir(outdir)
    fname = os.path.join(outdir, fname)
    for obs in observables:
        obsname = obs.filename
        ftag = tag + obs.tag
        fname_ = repkey(fname, OBS=obsname, TAG=tag)
        file = TFile.Open(fname_, option)
        if recreate:
            print ">>> created file %s" % (fname_)
        for selection in bins:
            if not obs.plotfor(selection): continue
            obs.changecontext(selection)
            ensureTDirectory(file, selection.filename, cd=True, verb=verbosity)
            if recreate:
                string = joincuts(selection.selection, obs.cut)
                TNamed("selection", string).Write(
                )  # write exact selection string to ROOT file for the record / debugging
                #TNamed("weight",sampleset.weight).Write()
                LOG.verb(
                    "%s selection %r: %r" % (obsname, selection.name, string),
                    verbosity, 1)
        files[obs] = file

    # GET HISTS
    for selection in bins:
        bin = selection.filename  # bin name
        print ">>>\n>>> " + color(
            " %s " % (bin), 'magenta', bold=True, ul=True)
        if htag:
            print ">>> systematic uncertainty: %s" % (color(
                htag.lstrip('_'), 'grey'))
        if recreate or verbosity >= 1:
            print ">>> %r" % (selection.selection)
        hists = sampleset.gethists(observables,
                                   selection,
                                   method=method,
                                   split=True,
                                   parallel=parallel,
                                   filter=filters,
                                   veto=vetoes)

        # SAVE HIST
        ljust = 4 + max(11, len(htag))  # extra space
        TAB = LOG.table("%10.1f %10d  %-18s  %s")
        TAB.printheader('events', 'entries', 'variable',
                        'process'.ljust(ljust))
        for obs, hist in hists.iterhists():
            name = lreplace(hist.GetName(), obs.filename).strip(
                '_')  # histname = $VAR_$NAME (see Sample.gethist)
            if not name.endswith(htag):
                name += htag  # HIST = $PROCESS_$SYSTEMATIC
            name = repkey(name, BIN=bin)
            drawopt = 'E1' if 'data' in name else 'EHIST'
            lcolor = kBlack if any(
                s in name
                for s in ['data', 'ST', 'VV']) else hist.GetFillColor()
            hist.SetOption(drawopt)
            hist.SetLineColor(lcolor)
            hist.SetFillStyle(0)  # no fill in ROOT file
            hist.SetName(name)
            hist.GetXaxis().SetTitle(obs.title)
            for i, yval in enumerate(hist):
                if yval < 0:
                    print ">>> replace bin %d (%.3f<0) of %r" % (
                        i, yval, hist.GetName())
                    hist.SetBinContent(i, 0)
            files[obs].cd(bin)  # $FILE:$BIN/$PROCESS_$SYSTEMATC
            hist.Write(name, TH1.kOverwrite)
            TAB.printrow(hist.GetSumOfWeights(), hist.GetEntries(),
                         obs.printbins(), name)
            deletehist(hist)  # clean memory

    # CLOSE
    for obs, file in files.iteritems():
        file.Close()
示例#9
0
def plotinputs(fname, varprocs, observables, bins, **kwargs):
    """Plot histogram inputs from ROOT file for datacards, and write to ROOT file.
       fname:       filename pattern of ROOT file
       varprocs:    dictionary for systematic variation to list of processes,
                    e.g. { 'Nom':   ['ZTT','TTT','W','QCD','data_obs'],
                           'TESUp': ['ZTT','TTT'], 'TESDown': ['ZTT','TTT'] }
       observables: list of Variables objects
       bins:        list of Selection objects
  """
    #LOG.header("plotinputs")
    tag = kwargs.get('tag', "")
    pname = kwargs.get('pname', "$OBS_$BIN$TAG.png")
    outdir = kwargs.get('outdir', 'plots')
    text = kwargs.get('text', "$BIN")
    groups = kwargs.get('group',
                        [])  # add processes together into one histogram
    verbosity = kwargs.get('verb', 0)
    ensuredir(outdir)
    print ">>>\n>>> " + color(" plotting... ", 'magenta', bold=True, ul=True)
    for obs in observables:
        obsname = obs.filename
        ftag = tag + obs.tag
        fname_ = repkey(fname, OBS=obsname, TAG=ftag)
        file = ensureTFile(fname_, 'UPDATE')
        for set, procs in varprocs.iteritems(
        ):  # loop over processes with variation
            if set == 'Nom':
                systag = ""  # no systematics tag for nominal
                procs_ = procs[:]
            else:
                systag = '_' + set  # systematics tag for variation, e.g. '_TESUp'
                procs_ = [
                    (p + systag if p in procs else p) for p in varprocs['Nom']
                ]  # add tag to varied processes
            for selection in bins:
                if not obs.plotfor(selection): continue
                obs.changecontext(selection)
                bin = selection.filename
                text_ = repkey(
                    text, BIN=selection.title)  # extra text in plot corner
                tdir = ensureTDirectory(file, bin,
                                        cd=True)  # directory with histograms
                if set == 'Nom':
                    gStyle.Write(
                        'style', TH1.kOverwrite
                    )  # write current TStyle object to reproduce plots

                # STACKS
                pname_ = repkey(pname, OBS=obsname, BIN=bin,
                                TAG=ftag + systag)  # image file name
                wname = "stack" + systag  # name in ROOT file
                stackinputs(tdir,
                            obs,
                            procs_,
                            group=groups,
                            save=pname_,
                            write=wname,
                            text=text_)

                # VARIATIONS
                if 'Down' in set:
                    systag_ = systag.replace(
                        'Down', '')  # e.g.'_TES' without 'Up' or 'Down' suffix
                    pname_ = repkey(pname,
                                    OBS=obsname,
                                    BIN=bin,
                                    TAG=ftag + "_$PROC" +
                                    systag)  # image file name
                    wname = "plot_$PROC" + systag  # name in ROOT file
                    comparevars(tdir,
                                obs,
                                procs,
                                systag_,
                                save=pname_,
                                write=wname,
                                text=text_)

        file.Close()
示例#10
0
def testSample(args):
  
  era       = "2016"
  storage   = None #"/eos/user/i/ineuteli/samples/nano/$ERA/$PATH"
  url       = None #"root://cms-xrd-global.cern.ch/"
  filelist  = None #"samples/files/2016/$SAMPLE.txt"
  verbosity = args.verbosity
  samples   = [
    M('DY','DYJetsToLL_M-50',
      "/DYJetsToLL_M-50_TuneCUETP8M1_13TeV-madgraphMLM-pythia8/RunIISummer16NanoAODv6-PUMoriond17_Nano25Oct2019_102X_mcRun2_asymptotic_v7_ext1-v1/NANOAODSIM",
      "/DYJetsToLL_M-50_TuneCUETP8M1_13TeV-madgraphMLM-pythia8/RunIISummer16NanoAODv6-PUMoriond17_Nano25Oct2019_102X_mcRun2_asymptotic_v7_ext2-v1/NANOAODSIM",
      store=storage,url=url,files=filelist,opts='zpt=True',
    ),
    M('TT','TT',
      "/TT_TuneCUETP8M2T4_13TeV-powheg-pythia8/RunIISummer16NanoAODv6-PUMoriond17_Nano25Oct2019_102X_mcRun2_asymptotic_v7-v2/NANOAODSIM",
      store=storage,url=url,files=filelist,opts='toppt=True',
    ),
    M('WJ','W4JetsToLNu',
      "/W4JetsToLNu_TuneCP5_13TeV-madgraphMLM-pythia8/lathomas-NANOAODv2Step_UL16nonAPV-ae987f664fa23fba95446c7ef5426a19/USER",
      store=storage,url=url,files=filelist, #instance='prod/phys03',
    ),
    D('Data','SingleMuon_Run2016C', "/SingleMuon/Run2016C-Nano25Oct2019-v1/NANOAOD",
      store=storage,url=url,files=filelist,
    ),
  ]
  filters = args.filters
  terms = [
    'DY',
    'DY*Jets',
    'DY?Jets',
    'DY[1J]',
    'DY*pythia8',
  ] + filters
  
  # PRINT
  listname = "test/files/$ERA/$SAMPLE.txt"
  for sample in samples:
    LOG.header(sample.name)
    print ">>> %-14s = %r"%("group",sample.group)
    print ">>> %-14s = %r"%("name",sample.name)
    print ">>> %-14s = %r"%("paths",sample.paths)
    print ">>> %-14s = %r"%("url",sample.url)
    print ">>> %-14s = %r"%("era",sample.era)
    print ">>> %-14s = %r"%("channels",sample.channels)
    print ">>> %-14s = %r"%("storage",sample.storage)
    print ">>> %-14s = %r"%("extraopts",sample.extraopts)
    print ">>> %-14s = %r"%("nfilesperjob",sample.nfilesperjob)
    print ">>> %-14s = %r"%("files",sample.files)
    print ">>> %-14s = %r"%("nevents",sample.nevents)
    
    # MATCH
    print ">>> Testing matching:"
    for term in terms:
      match = sample.match(term,verb=verbosity)
      match = color('YES','green',b=True) if match else color('NO','red',b=True)
      print ">>> %r matches to %r: %s"%(sample.name,term,match)
    if filters and not all(sample.match(f,verb=verbosity) for f in filters):
      continue
    
    # WRITE
    fname = repkey(listname,ERA=era)
    print ">>>\n>>> Write..."
    sample.writefiles(fname,nevts=True) # write Sample.files to txt file
    print ">>> %-14s = %r"%("listname",fname)
    #print ">>> %-14s = %r"%("files",sample.files)
    print ">>> %-14s = %r"%("nfiles",len(sample.files))
    print ">>> %-14s = %r"%("nevents",sample.nevents)
    
    # LOAD
    print ">>>\n>>> Reset..."
    newsample = Sample(sample.group,sample.name,*sample.paths,
                       store=storage,url=url,files=fname,opts=sample.extraopts)
    print ">>> %-14s = %r"%("listname",fname)
    print ">>> %-14s = %r"%("files",newsample.files)
    print ">>> %-14s = %r"%("nfiles",len(newsample.files))
    print ">>> %-14s = %r"%("nevents",newsample.nevents)
    print ">>> Call Sample.getfiles..." # load Sample.files from txt file
    files = newsample.getfiles()
    print ">>> %-14s = %r"%("files",newsample.files)
    print ">>> %-14s = %r"%("nfiles",len(newsample.files))
    print ">>> %-14s = %r"%("nevents",newsample.nevents)
    print ">>> %-14s = %r"%("filenevts",newsample.filenevts)
示例#11
0
def main_get(args):
    """Get information of given variable in configuration or samples."""
    if args.verbosity >= 1:
        print ">>> main_get", args
    variable = args.variable
    eras = args.eras  # eras to loop over and run
    channels = args.channels or [""]  # channels to loop over and run
    dtypes = args.dtypes  # filter (only include) these sample types ('data','mc','embed')
    filters = args.samples  # filter (only include) these samples (glob patterns)
    dasfilters = args.dasfilters  # filter (only include) these das paths (glob patterns)
    vetoes = args.vetoes  # exclude these sample (glob patterns)
    dasvetoes = args.dasvetoes  # exclude these DAS paths (glob patterns)
    inclurl = args.inclurl  # include URL in filelist
    checkdas = args.checkdas or args.dasfiles  # check file list in DAS
    checklocal = args.checklocal  # check nevents in local files
    split = args.split  # split samples with multiple DAS dataset paths
    limit = args.limit
    writedir = args.write  # write sample file list to text file
    tag = args.tag
    ncores = args.ncores  # number of cores to get nevents in parallel
    verbosity = args.verbosity
    getnevts = variable in ['nevents', 'nevts']
    cfgname = CONFIG._path
    if verbosity >= 1:
        print '-' * 80
        print ">>> %-14s = %s" % ('variable', variable)
        print ">>> %-14s = %s" % ('eras', eras)
        print ">>> %-14s = %s" % ('channels', channels)
        print ">>> %-14s = %s" % ('cfgname', cfgname)
        print ">>> %-14s = %s" % ('config', CONFIG)
        print ">>> %-14s = %s" % ('checkdas', checkdas)
        print ">>> %-14s = %s" % ('checklocal', checklocal)
        print ">>> %-14s = %s" % ('split', split)
        print ">>> %-14s = %s" % ('limit', limit)
        print ">>> %-14s = %s" % ('writedir', writedir)
        print ">>> %-14s = %s" % ('ncores', ncores)
        print '-' * 80

    # LIST SAMPLES
    if variable == 'samples':
        if not eras:
            LOG.warning("Please specify an era to get a sample for.")
        for era in eras:
            for channel in channels:
                if channel:
                    print ">>> Getting file list for era %r, channel %r" % (
                        era, channel)
                else:
                    print ">>> Getting file list for era %r" % (era)
                samples = getsamples(era,
                                     channel=channel,
                                     dtype=dtypes,
                                     filter=filters,
                                     veto=vetoes,
                                     dasfilter=dasfilters,
                                     dasveto=dasvetoes,
                                     verb=verbosity)
                if not samples:
                    LOG.warning("No samples found for era %r." % (era))
                for sample in samples:
                    print ">>> %s" % (bold(sample.name))
                    for path in sample.paths:
                        print ">>>   %s" % (path)

    # LIST SAMPLE FILES
    elif variable in ['files', 'nevents', 'nevts']:

        # LOOP over ERAS & CHANNELS
        if not eras:
            LOG.warning("Please specify an era to get a sample for.")
        for era in eras:
            for channel in channels:
                target = "file list" if variable == 'files' else "nevents"
                if channel:
                    print ">>> Getting %s for era %r, channel %r" % (
                        target, era, channel)
                else:
                    print ">>> Getting %s for era %r" % (target, era)
                print ">>> "

                # GET SAMPLES
                LOG.insist(
                    era in CONFIG.eras,
                    "Era '%s' not found in the configuration file. Available: %s"
                    % (era, CONFIG.eras))
                samples = getsamples(era,
                                     channel=channel,
                                     dtype=dtypes,
                                     filter=filters,
                                     veto=vetoes,
                                     dasfilter=dasfilters,
                                     dasveto=dasvetoes,
                                     split=split,
                                     verb=verbosity)

                # LOOP over SAMPLES
                for sample in samples:
                    print ">>> %s" % (bold(sample.name))
                    for path in sample.paths:
                        print ">>> %s" % (bold(path))
                    if getnevts or checkdas or checklocal:
                        das = checkdas and not checklocal  # checklocal overrides checkdas
                        refresh = das  # (not sample.storage and all('/store' in f for f in sample.files)
                        nevents = sample.getnevents(das=das,
                                                    refresh=refresh,
                                                    verb=verbosity + 1)
                        storage = "(%s)" % sample.storage.__class__.__name__ if checklocal else "(DAS)" if checkdas else ""
                        print ">>>   %-7s = %s %s" % ('nevents', nevents,
                                                      storage)
                    if variable == 'files':
                        infiles = sample.getfiles(das=checkdas,
                                                  url=inclurl,
                                                  limit=limit,
                                                  verb=verbosity + 1)
                        print ">>>   %-7s = %r" % ('channel', channel)
                        print ">>>   %-7s = %r" % ('url', sample.url)
                        print ">>>   %-7s = %r" % ('postfix', sample.postfix)
                        print ">>>   %-7s = %s" % ('nfiles', len(infiles))
                        print ">>>   %-7s = [ " % ('infiles')
                        for file in infiles:
                            print ">>>     %r" % file
                        print ">>>   ]"
                    print ">>> "
                    if writedir:  # write files to text files
                        flistname = repkey(writedir,
                                           ERA=era,
                                           GROUP=sample.group,
                                           SAMPLE=sample.name,
                                           TAG=tag)
                        print ">>> Write list to %r..." % (flistname)
                        sample.writefiles(flistname,
                                          nevts=getnevts,
                                          ncores=ncores)

    # CONFIGURATION
    else:
        if variable in CONFIG:
            print ">>> Configuration of %r: %s" % (variable,
                                                   color(CONFIG[variable]))
        else:
            print ">>> Did not find %r in the configuration" % (variable)
示例#12
0
def main_link(args):
    """Link channels or eras in the config file."""
    if args.verbosity >= 1:
        print ">>> main_link", args
    variable = args.subcommand
    varkey = variable + 's'
    key = args.key
    value = args.value
    verbosity = args.verbosity
    cfgname = CONFIG._path
    if verbosity >= 1:
        print '-' * 80
    print ">>> Linking %s '%s' to '%s' in the configuration..." % (
        variable, color(key), value)
    if verbosity >= 1:
        print ">>> %-14s = %s" % ('cfgname', cfgname)
        print ">>> %-14s = %s" % ('key', key)
        print ">>> %-14s = %s" % ('value', value)
        print ">>> %-14s = %s" % ('config', CONFIG)
        print '-' * 80

    # SANITY CHECKS
    if varkey not in CONFIG:
        CONFIG[varkey] = {}
    LOG.insist(isinstance(CONFIG[varkey], dict),
               "%s in %s has to be a dictionary" % (varkey, cfgname))
    oldval = value
    for char in '/\,:;!?\'" ':
        if char in key:
            LOG.throw(
                IOError,
                "Given key '%s', but keys cannot contain any of these characters: %s"
                % (key, char))
    if varkey == 'channels':
        if 'skim' in key.lower():  #or 'test' in key:
            parts = value.split(' ')  # "PROCESSOR [--FLAG[=VALUE] ...]"
            script = os.path.basename(parts[0])  # separate script from options
            ensurefile("python/processors", script)
            value = ' '.join([script] + parts[1:])
        else:
            parts = value.split(' ')  # "MODULE [KEY=VALUE ...]"
            module = parts[0]
            LOG.insist(
                all('=' in o for o in parts[1:]),
                "All extra module options should be of format KEY=VALUE!")
            if 'python/analysis/' in module:  # useful for tab completion
                module = module.split('python/analysis/')[-1].replace('/', '.')
            module = rreplace(module, '.py')
            path = os.path.join('python/analysis/',
                                '/'.join(module.split('.')[:-1]))
            ensureinit(path, by="pico.py")
            ensuremodule(module)
            value = ' '.join([module] + parts[1:])
    elif varkey == 'eras':
        if 'samples/' in value:  # useful for tab completion
            value = ''.join(value.split('samples/')[1:])
        path = os.path.join("samples",
                            repkey(value, ERA='*', CHANNEL='*', TAG='*'))
        LOG.insist(glob.glob(path),
                   "Did not find any sample lists '%s'" % (path))
        ensureinit(os.path.dirname(path), by="pico.py")
    if value != oldval:
        print ">>> Converted '%s' to '%s'" % (oldval, value)
    CONFIG[varkey][key] = value
    CONFIG.write()
示例#13
0
def main():

    mvisbins = [0, 30, 40, 50, 60, 70, 75, 80, 90, 100, 120, 200]

    # Test several initializations of Variable object.
    # Note that key-word arguments starting with 'c' are used for context-dependent attributes
    variables = [
        Variable('m_vis', "m_{vis} [GeV]", 40, 0, 200),
        Variable('njets', "Number of jets", 8, 0, 8),
        Variable('m_vis', 40, 0, 200),
        Variable('njets', 8, 0, 8),
        Variable('njets', 8, 0, 8, veto='njets'),
        Variable('st', 20, 0, 800, title="S_{T}", only="njets",
                 blind="st>600"),
        Variable('m_vis',
                 40,
                 0,
                 200,
                 cbins={'njets': mvisbins},
                 blind=(70, 90)),
        Variable(
            'm_vis',
            mvisbins,
            cbins={"njets[^&]*2": (40, 0, 200)},
        ),
        Variable('pt_1+pt_2',
                 20,
                 0,
                 800,
                 title="S_{T}^{tautau}",
                 ctitle={"njets": "S_{T}"}),
    ]
    selections = [
        "pt_1>50 && pt_2>50",
        "pt_1>50 && pt_2>50 && njets>=1",
        "pt_1>50 && pt_2>50 && njets>=2 && nbtags>=1",
        "pt_1>50 && pt_2>50 && njets==0",
        "pt_1>50 && pt_2>50 && dzeta>-40",
    ]

    for var in variables:
        LOG.header(var.name)
        print ">>> string=%s, repr=%r" % (var, var)
        print ">>> name='%s', filename='%s', title='%s'" % (color(
            var.name), color(var.filename), color(var.title))
        print ">>> (nbins,xmin,xmax)=(%s,%s,%s), bins=%s" % (
            var.nbins, var.xmin, var.xmax, var.bins)
        print ">>> hasintbins=%s, hasvariablebins=%s" % (var.hasintbins(),
                                                         var.hasvariablebins())
        print ">>> cut=%r, blindcuts=%r, blind(50,60)=%r" % (
            var.cut, var.blindcuts, var.blind(50, 60))
        hist = var.gethist()
        print ">>> hist=%s, (nbins,xmin,xmax)=(%s,%s,%s), variable=%s" % (
            hist, hist.GetXaxis().GetNbins(), hist.GetXaxis().GetXmin(),
            hist.GetXaxis().GetXmax(), hist.GetXaxis().IsVariableBinSize())
        gDirectory.Delete(hist.GetName())
        for sel in selections:  # context-dependent attributes
            var.changecontext(sel)
            print ">>> context: '%s'" % color(sel, 'grey')
            print ">>>   plotfor=%s, name='%s', title='%s'" % (
                var.plotfor(sel), color(var.name), color(var.title))
            print ">>>   (nbins,xmin,xmax)=(%s,%s,%s), bins=%s" % (
                var.nbins, var.xmin, var.xmax, var.bins)
        print