Пример #1
0
def main():
  CMSStyle.setCMSEra(2018)
  xtitle = "p_{T}^{MET} [GeV]"
  hists = createhists(2)
  for ratio in [True,False]: # True
    plothist(xtitle,hists,ratio=ratio,logy=True,norm=False)
    for hor in ['','L','LL','R','RR','C','CL','CR']:
      for ver in ['','T','TT','B','BB','M']:
        pos = (hor+'-'+ver).strip('-')
        plothist(xtitle,hists,ratio=ratio,logy=True,norm=False,pos=pos)
  deletehist(hists)
Пример #2
0
def testposition():
    """Test positioning of drawlegend."""
    xtitle = "p_{T}^{MET} [GeV]"
    hists = createhists(2)
    for ratio in [False]:  # True
        plothist(xtitle, hists, ratio=ratio, logy=True, norm=False)
        for hor in ['', 'L', 'LL', 'R', 'RR', 'C', 'CL', 'CR']:
            for ver in ['', 'T', 'TT', 'B', 'BB', 'M']:
                pos = (hor + '-' + ver).strip('-')
                plothist(xtitle,
                         hists,
                         ratio=ratio,
                         logy=True,
                         norm=False,
                         pos=pos)
    deletehist(hists)
Пример #3
0
def testwidth():
    """Test automatic width of drawlegend."""
    xtitle = "p_{T}^{MET} [GeV]"
    entries = [
        "short",
        "Z -> tautau",
        "Z #rightarrow #tau_{h}tau_{h}",
        "p_{T} > 20 GeV, |#eta| < 2.5",
        "p_{T} > 20 GeV, 2.5 < |#eta| < 4.7",
        "this is medium long",
        "this is a very long legend entry",
        "this is a very long Z -> tautau",
        "line one\nline two",
    ]
    hists = createhists(2)
    for i, entry in enumerate(entries):
        tag = "_width%d" % (i)
        hists[0].SetTitle(entry)
        plothist(xtitle, hists, ratio=False, logy=True, norm=False,
                 tag=tag)  #,pos='R'
    deletehist(hists)
Пример #4
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()