Ejemplo n.º 1
0
def compareMCProfiles(samples, channel, era, tag=""):
    """Compare MC profiles."""
    print ">>> compareMCProfiles()"

    hname = 'pileup'
    htitle = 'MC average'
    outdir = ensuredir("plots")
    avehist = None
    hists = []
    if tag and tag[0] != '_':
        tag = '_' + tag
    if 'pmx' in tag:
        htitle += " %s pre-mixing" % ("old" if "old" in tag else "new")

    # GET histograms
    for sample, fname in samples:
        print ">>>   %s" % (fname)
        file, hist = gethist(fname, hname, retfile=True)
        hist.SetName(sample)
        hist.SetTitle(sample)
        hist.SetDirectory(0)
        if avehist == None:
            avehist = hist.Clone('average%s' % tag)
            avehist.SetTitle(htitle)
            avehist.SetDirectory(0)
        else:
            avehist.Add(hist)
        hist.Scale(1. / hist.Integral())
        hists.append(hist)
        file.Close()

    # PLOT
    hists = [avehist] + hists
    colors = [kBlack] + linecolors
    avehist.Scale(1. / avehist.Integral())
    pname = "%s/pileup_MC_%s%s" % (outdir, era, tag)
    xtitle = "Number of true interactions"
    plot = Plot(hists, ratio=True)
    plot.draw(xtitle=xtitle,
              ytitle="A.U.",
              rtitle="MC / Ave.",
              textsize=0.032,
              rmin=0.45,
              rmax=1.55,
              denom=2,
              colors=colors)
    plot.drawlegend('TTR', tsize=0.04, latex=False)
    plot.saveas(pname + ".png")
    plot.saveas(pname + ".pdf")
    plot.close(keep=True)
    for hist in hists:  # clean memory
        if hist == avehist:
            continue
        if hist.GetDirectory():
            gDirectory.Delete(hist.GetName())
        else:
            hist.Delete()

    return avehist
Ejemplo n.º 2
0
def compareDataMCProfiles(datahists,mchist,era="",minbiases=0.0,tag="",rmin=0.6,rmax=1.4,delete=False):
  """Compare data/MC profiles."""
  print ">>> compareDataMCProfiles()"
  mctitle = "MC average"
  outdir  = ensuredir("plots")
  if islist(datahists): # multiple datahists
    if all(islist(x) for x in datahists): # datahists = [(minbias1,datahist1),...]
      minbiases = [m for m,h in datahists]
      datahists = [h for m,h in datahists]
  else: # is single datahist histogram
    minbiases = [minbiases]
    datahists = [datahists]
  hists  = datahists+[mchist]
  styles = [kSolid]*len(datahists)+[kDashed]
  colors = [kBlack]+linecolors if len(datahists)==1 else linecolors[:len(datahists)]+[kBlack]
  if 'pmx' in tag:
    width  = 0.36
    position = 'TCR'
  else:
    width  = 0.39
    position = 'TR'
  if era and isinstance(era,str) and any(s in era for s in ["Run","VFP"]):
    width =  max(width,0.26+(len(era)-5)*0.018)
    position = 'TCR'
  if tag and tag[0]!='_':
    tag = '_'+tag
  if 'pmx' in tag:
    mctitle += " (%s pre-mixing)"%("old" if "old" in tag else "new")
  if len(minbiases)==1 and minbiases[0]>0:
    tag = "_"+str(minbiases[0]).replace('.','p')
  
  for datahist, minbias in zip(datahists,minbiases):
    title = "Data"
    if era:
      title += " %s"%(era)
    if minbias>0:
      title += ", %.1f pb"%(minbias)
    if 'VFP' in era:
      title = title.replace("_"," ").replace("VFP","-VFP")
    datahist.SetTitle(title)
    datahist.Scale(1./datahist.Integral())
  mchist.SetTitle(mctitle)
  mchist.Scale(1./mchist.Integral())
  
  xtitle = "Number of interactions"
  pname  = "%s/pileup_Data-MC_%s%s"%(outdir,era,tag)
  plot   = Plot(hists,ratio=True)
  plot.draw(xtitle=xtitle,ytitle="A.U.",rtitle="Data / MC",
            textsize=0.045,rmin=rmin,rmax=rmax,denom=-1,colors=colors,styles=styles)
  plot.drawlegend(position,width=width)
  plot.saveas(pname+".png")
  plot.saveas(pname+".pdf")
  plot.close(keep=True)
  if delete:
    deletehist(datahists) # clean memory
Ejemplo n.º 3
0
def compareDataMCProfiles(datahist,
                          mchist,
                          era,
                          minbias,
                          tag="",
                          rmin=0.75,
                          rmax=1.25):
    """Compare data/MC profiles."""
    print ">>> compareDataMCProfiles()"
    mctitle = "MC average"
    outdir = ensuredir("plots")
    hists = [datahist, mchist]
    colors = [kBlack] + linecolors
    width = 0.36 if 'pmx' in tag else 0.26
    if isinstance(era, str) and "Run" in era:
        width = max(width, 0.26 + (len(era) - 5) * 0.02)
    if tag and tag[0] != '_':
        tag = '_' + tag
    if 'pmx' in tag:
        mctitle += " (%s pre-mixing)" % ("old" if "old" in tag else "new")

    datahist.SetTitle("Data %s, %.1f pb" % (era, minbias))
    mchist.SetTitle(mctitle)
    datahist.Scale(1. / datahist.Integral())
    mchist.Scale(1. / mchist.Integral())

    xtitle = "Number of interactions"
    pname = "%s/pileup_Data-MC_%s_%s%s" % (outdir, era, str(minbias).replace(
        '.', 'p'), tag)
    plot = Plot(hists, ratio=True)
    plot.draw(xtitle=xtitle,
              ytitle="A.U.",
              rtitle="Data / MC",
              textsize=0.045,
              rmin=rmin,
              rmax=rmax,
              denom=2,
              colors=colors)
    plot.drawlegend('TCR', width=width)
    plot.saveas(pname + ".png")
    plot.saveas(pname + ".pdf")
    plot.close(keep=True)
    deletehist(datahist)  # clean memory
Ejemplo n.º 4
0
def compare_eras(eras, samplesets, channel, tag="", **kwargs):
    """Test plotting of SampleSet class for data/MC comparison."""
    LOG.header("compare_eras", pre=">>>")
    if 'mu' in channel:
        snames = ['TT', 'DY', 'WJ', 'SingleMuon']
    else:
        snames = ['TT', 'DY', 'WJ', 'Tau']
    snames = kwargs.get('samples', snames)  # samples to compare
    outdir = kwargs.get('outdir', "plots")
    parallel = kwargs.get('parallel', True)  #and False
    norms = kwargs.get('norm', [True])
    entries = kwargs.get('entries', [str(e) for e in eras])  # for legend
    exts = kwargs.get('exts', ['png'])  # figure file extensions
    ensuredir(outdir)
    norms = ensurelist(norms)

    # SELECTIONS
    if 'mutau' in channel:
        idiso1 = "iso_1<0.15"
        idiso2 = "idDecayModeNewDMs_2 && idDeepTau2017v2p1VSjet_2>=16 && idDeepTau2017v2p1VSe_2>=2 && idDeepTau2017v2p1VSmu_2>=8"
        baseline = "q_1*q_2<0 && %s && %s && !lepton_vetoes_notau && metfilter" % (
            idiso1, idiso2)
    else:
        cuts_iso = "idDeepTau2017v2VSjet_1>=16 && idDeepTau2017v2VSjet_2>=16"
        antilep = "idDeepTau2017v2VSe_1>=2 && idDeepTau2017v2VSmu_1>=1 && idDeepTau2017v2VSe_2>=2 && idDeepTau2017v2VSmu_2>=1"
        baseline = "q_1*q_2<0 && idDecayModeNewDMs_1 && idDecayModeNewDMs_2 && %s && %s && !lepton_vetos_notau && metfilter" % (
            antilep, cuts_iso)
    selections = [
        Sel('baseline', baseline),
        Sel('baseline, muon pt > 24 GeV',
            baseline + " && pt_1>24",
            fname="baseline-ptgt24"),
    ]

    # VARIABLES
    variables = [
        Var('m_vis', 23, 0, 460),
        Var('pt_1', "Leading tau_h pt", 18, 0, 270),
        Var('pt_2', "Subleading tau_h pt", 18, 0, 270),
        #Var('jpt_1',  18, 0, 270),
        #Var('jpt_2',  18, 0, 270),
        #Var('met',    20, 0, 300),
        Var('rawDeepTau2017v2p1VSe_2',
            "rawDeepTau2017v2p1VSe",
            30,
            0.70,
            1,
            fname="$VAR_zoom",
            logy=True,
            pos='L;y=0.85'),
        Var('rawDeepTau2017v2p1VSmu_2',
            "rawDeepTau2017v2p1VSmu",
            20,
            0.80,
            1,
            fname="$VAR_zoom",
            logy=True,
            logyrange=4,
            pos='L;y=0.85'),
        Var('rawDeepTau2017v2p1VSjet_2',
            "rawDeepTau2017v2p1VSjet",
            100,
            0.0,
            1,
            pos='L;y=0.85',
            logy=True,
            ymargin=2.5),
        Var('rawDeepTau2017v2p1VSjet_2',
            "rawDeepTau2017v2p1VSjet",
            20,
            0.80,
            1,
            fname="$VAR_zoom",
            pos='L;y=0.85'),
    ]

    # PLOT
    for sname in snames:
        #LOG.header(sname)
        print ">>> %s" % (sname)
        samples = [samplesets[e].get(sname, unique=True) for e in eras]
        header = samples[0].title
        for selection in selections:
            print ">>> %s: %r" % (selection, selection.selection)
            hdict = {}
            text = "%s: %s" % (channel.replace("tau",
                                               "tau_{h}"), selection.title)
            fname = "%s/compare_eras_$VAR_%s_%s%s$TAG" % (
                outdir, sname, selection.filename, tag)
            for sample in samples:
                vars = [v for v in variables if v.data or not sample.isdata]
                hists = sample.gethist(vars, selection, parallel=parallel)
                for variable, hist in zip(variables, hists):
                    hdict.setdefault(variable, []).append(hist)
            for variable, hists in hdict.iteritems():
                for norm in norms:
                    ntag = '_norm' if norm else ""  #_lumi"
                    plot = Plot(variable, hists, norm=norm)
                    plot.draw(ratio=True, lstyle=1)
                    plot.drawlegend(header=header, entries=entries)
                    plot.drawtext(text)
                    plot.saveas(fname, ext=['png'], tag=ntag)  #,'pdf'
                    plot.close(keep=True)
                deletehist(hists)
        print ">>> "
Ejemplo n.º 5
0
def compare_cuts(sampleset, channel, tag="", outdir="plots"):
    """Compare different selections."""
    LOG.header("compare_eras")

    # SELECTIONS & VARIABLES
    if 'tautau' in channel:
        cuts_iso = "idDeepTau2017v2VSjet_1>=16 && idDeepTau2017v2VSjet_2>=16"
        antilep = "idDeepTau2017v2VSe_1>=2 && idDeepTau2017v2VSmu_1>=1 && idDeepTau2017v2VSe_2>=2 && idDeepTau2017v2VSmu_2>=1"
        baseline = "q_1*q_2<0 && idDecayModeNewDMs_1 && idDecayModeNewDMs_2 && %s && %s && !lepton_vetos_noTau && metfilter" % (
            antilep, cuts_iso)
    else:
        cuts_iso = "idDeepTau2017v2VSjet_2>=16"
        antilep = "idDeepTau2017v2VSe_2>=2 && idDeepTau2017v2VSmu_2>=8"
        baseline = "q_1*q_2<0 && idMedium_1 && pfRelIso04_all_1<0.15 && idDecayModeNewDMs_2 && %s && %s && !lepton_vetos && metfilter" % (
            antilep, cuts_iso)
    selections = [
        (
            'baseline-medium-tight-muon',
            'Baseline, Medium vs. Tight muon ID',
            Sel('Medium', baseline),
            Sel('Tight', baseline.replace('idMedium_1', 'idTight_1')),
        ),
        (
            '0b-medium-tight-muon',
            '0b, Medium vs. Tight muon ID',
            Sel(
                'Medium', baseline +
                " && pt_1>50 && pt_2>50 && njets50>=1 && m_vis>100 && nbtag50_loose==0"
            ),
            Sel(
                'Tight',
                baseline.replace(
                    'idMedium_1', 'idTight_1' +
                    " && pt_1>50 && pt_2>50 && njets50>=1 && m_vis>100 && nbtag50_loose==0"
                )),
        ),
        (
            'geq1b-medium-tight-muon',
            'geq1b, Medium vs. Tight muon ID',
            Sel(
                'Medium', baseline +
                " && pt_1>50 && pt_2>50 && njets50>=1 && m_vis>100 && nbtag50_loose>=1"
            ),
            Sel(
                'Tight',
                baseline.replace(
                    'idMedium_1', 'idTight_1' +
                    " && pt_1>50 && pt_2>50 && njets50>=1 && m_vis>100 && nbtag50_loose>=1"
                )),
        ),
    ]
    variables = [
        Var('m_vis', 23, 0, 460),
        Var('pt_1', "Leading tau_h pt", 18, 0, 270),
        Var('pt_2', "Subleading tau_h pt", 18, 0, 270),
        Var('jpt_1', 18, 0, 270),
        Var('jpt_2', 18, 0, 270),
        Var('met', 20, 0, 300),
        Var('pt_1+pt_2+jpt_1', 30, 0, 600, fname='ST', cut="jpt_1>50"),
    ]

    # SAMPLESETS
    sampleset = makesamples(channel, year)
    snames = [
        'SingleMuon',
        'TT',
    ]  #'DY','WJ']

    # PLOT
    outdir = ensuredir(outdir)
    parallel = True and False
    for sname in snames:
        if 'Tau' and 'tautau' not in channel: continue
        if 'SingleMuon' and 'mutau' not in channel: continue
        LOG.header(sname)
        sample = sampleset.get(sname, unique=True)
        header = samples[0].title
        for setname, settitle, selection1, selection2 in selections:
            hdict = {}
            text = "%s: %s" % (channel.replace("tau", "tau_{h}"), settitle)
            fname = "%s/compareSels_$VAR_%s_%s%s$TAG" % (outdir, sname,
                                                         setname, tag)
            for sample in samples:
                vars = [v for v in variables if v.data or not sample.isdata]
                hists = sample.gethist(vars, selection, parallel=parallel)
                for variable, hist in zip(variables, hists):
                    hdict.setdefault(variable, []).append(hist)
            #entries = [str(y) for y in eras] # for legend
            for variable, hists in hdict.iteritems():
                for norm in [False, True]:
                    #print norm, hists
                    ntag = '_norm' if norm else "_lumi"
                    plot = Plot(variable, hists, norm=norm)
                    plot.draw(ratio=True, lstyle=1)
                    plot.drawlegend(header=header, entries=entries)
                    plot.drawtext(text)
                    plot.saveas(fname, ext=['png'], tag=ntag)  #,'pdf'
                    plot.close(keep=True)
                deletehist(hists)
Ejemplo n.º 6
0
def comparevars(file, variable, processes, systag, **kwargs):
    """Compare up/down variations of input histograms from ROOT file.
       file:      TFile or TDirectory object
       variable:  Variables object
       processes: list of strings (name of processes)
       systag:    string of systematic (file must contain up/down variation)
     e.g.
       comparevars(file,variable,['ZTT','TTT'],'TES')
  """
    text = kwargs.get('text', None)
    pname = kwargs.get('pname', "stack.png")
    tag = kwargs.get('tag', "")
    groups = kwargs.get('group', [])  # e.g. [(['^TT','ST'],'Top')]
    dname = kwargs.get('dname', None)  # directory ('bin') name
    pname = kwargs.get('save', "plot_$PROC$SYST$TAG.png")  # save as image file
    wname = kwargs.get('write', "plot_$PROC$SYST$TAG")  # write to file
    processes = ensurelist(processes)
    uptag = systag + "Up"
    downtag = systag + "Down"
    tdir = ensureTDirectory(file, dname, cd=True) if dname else file
    for process in processes:
        hists = []
        skip = False
        for var in [uptag, "", downtag]:
            hname = process + var
            hist = gethist(tdir, hname)
            if not hist:
                skip = True
                break
            hists.append(hist)
        if skip:
            LOG.warning("Skipping %r variation for %r..." % (systag, process))
            continue
        plot = Plot(variable, hists)
        plot.draw(ratio=2, lstyle=1)
        plot.drawlegend()
        if text:
            plot.drawtext(text)
        if pname:
            pname_ = repkey(pname, PROC=process, TAG=tag)
            plot.saveas(pname_, ext=['png'])
        if wname:
            wname_ = repkey(wname, PROC=process, TAG=tag)
            plot.canvas.Write(wname_, TH1.kOverwrite)
        plot.close()
Ejemplo n.º 7
0
def compare(fnames, variables, **kwargs):

    # SETTING
    tnames = kwargs.get('tree', [])
    entries = kwargs.get('entries', [])  # entries of files
    outdir = kwargs.get('outdir', "compare")
    tag = kwargs.get('tag', "")
    cut = kwargs.get('cut', "")
    norm = kwargs.get('norm', False)
    ensuredir(outdir)
    if isinstance(tnames, str):
        tnames = [tnames] * len(fnames)
    else:
        while len(tnames) < len(fnames):
            tnames.append(tnames[-1])
    if norm:
        tag += "_norm"  # normalize each histogram

    # GET FILES & TREES
    files = []
    trees = []
    for fname, tname in zip(fnames, tnames):
        file = TFile.Open(fname, 'READ')
        tree = file.Get(tname)
        files.append(file)
        trees.append(tree)
        for variable in variables[:]:
            if not hasattr(tree, variable.name):
                LOG.warning(
                    "compare: tree %s:%s does not contain branch %r! Skipping..."
                    % (fname, tname, variable.name))
                variables.remove(variable)
    while len(entries) < len(trees):
        i = len(entries)
        entry = "%s %d" % (tnames[i], i + 1)
        entries.append(entry)

    # PLOT
    for variable in variables:
        fname = "%s/%s%s.png" % (outdir, variable, tag)
        rrange = 0.5
        header = "Compare"  # legend header
        text = ""  # corner text
        ratio = True  #and False
        logy = True and False
        grid = True  #and False
        staterr = True  #and False # add uncertainty band to first histogram
        lstyle = 1  # solid lines
        LOG.header(fname)

        # GET HISOGRAMS
        hists = []
        for i, (tree, htitle) in enumerate(zip(trees, entries)):
            hname = "%s_%d" % (variable.filename, i)
            #hist   = variable.gethist(hname,htitle)
            #dcmd   = variable.drawcmd(hname)
            hist = variable.draw(
                tree, cut, name=hname,
                title=htitle)  # create and fill hist from tree
            evts = hist.GetEntries()
            hist.SetTitle("%s (%d)" % (htitle, evts))
            hists.append(hist)
            print ">>>   %r: entries=%d, integral=%s, mean=%#.6g, s.d.=%#.6g" % (
                htitle, evts, hist.Integral(), hist.GetMean(),
                hist.GetStdDev())

        # DRAW PLOT
        plot = Plot(variable, hists, norm=norm)
        plot.draw(ratio=True,
                  logy=logy,
                  ratiorange=rrange,
                  lstyle=lstyle,
                  grid=grid,
                  staterr=staterr)
        plot.drawlegend(header=header)
        plot.drawtext(text)
        plot.saveas(fname)
        plot.close()
        print
Ejemplo n.º 8
0
def compare(fnames, varsets, **kwargs):

    # SETTING
    tname = kwargs.get('tree', 'Events')
    entries = kwargs.get('entries', [])  # entries of files
    outdir = kwargs.get('outdir', "compare")
    tag = kwargs.get('tag', "")
    cut = kwargs.get('cut', "")
    norm = kwargs.get('norm', False)
    ensuredir(outdir)
    if norm:
        tag += "_norm"  # normalize each histogram
    tree = TChain(tname)

    # GET FILES & TREES
    for fname in fnames:
        tree.Add(fname)

    # PLOT
    for varset in varsets:
        fname = "%s/%s%s.png" % (outdir, '-'.join(v.filename
                                                  for v in varset), tag)
        rrange = 0.5
        header = "Compare"  # legend header
        text = ""  # corner text
        ratio = True  #and False
        logy = True and False
        grid = True  #and False
        staterr = True  #and False # add uncertainty band to first histogram
        lstyle = 1  # solid lines
        xvar = varset[0]
        LOG.header(fname)

        # GET HISOGRAMS
        hists = []
        for i, variable in enumerate(varset):
            hname = "%s_%d" % (variable.filename, i)
            htitle = variable.name
            #hist   = variable.gethist(hname,htitle)
            #dcmd   = variable.drawcmd(hname)
            hist = variable.draw(
                tree, cut, name=hname,
                title=htitle)  # create and fill hist from tree
            evts = hist.GetEntries()
            hist.SetTitle(htitle)  #"%s (%d)"%(htitle,evts)
            hists.append(hist)
            print ">>>   %r: entries=%d, integral=%s, mean=%#.6g, s.d.=%#.6g" % (
                htitle, evts, hist.Integral(), hist.GetMean(),
                hist.GetStdDev())

        # DRAW PLOT
        plot = Plot(xvar, hists, norm=norm)
        plot.draw(ratio=True,
                  logy=logy,
                  ratiorange=rrange,
                  lstyle=lstyle,
                  grid=grid,
                  staterr=staterr)
        plot.drawlegend(header=header, latex=False)
        plot.drawtext(text)
        plot.saveas(fname)
        plot.close()
        print
Ejemplo n.º 9
0
def plothist(xtitle,hists,ratio=False,logy=False,norm=False):
  
  # SETTING
  outdir   = ensuredir("plots/")
  fname    = outdir+"testPlot"
  if ratio:
    fname += "_ratio"
  if logy:
    fname += "_logy"
  if norm:
    fname += "_norm" # normalize each histogram
  rrange   = 0.5
  width    = 0.2             # legend width
  position = 'topright'      # legend position
  header   = "Gaussians"     # legend header
  text     = "#mu#tau_{h}"   # corner text
  grid     = True #and False
  staterr  = True and False  # add uncertainty band to first histogram
  lstyle   = 1               # solid lines
  
  # PLOT
  LOG.header(fname)
  plot = Plot(xtitle,hists,norm=norm)
  plot.draw(ratio=ratio,logy=logy,ratiorange=rrange,lstyle=lstyle,grid=grid,staterr=staterr)
  plot.drawlegend(position,header=header,width=width)
  plot.drawtext(text)
  plot.saveas(fname+".png")
  plot.saveas(fname+".pdf")
  #plot.saveas(fname+".C")
  #plot.saveas(fname+".png",fname+".C")
  #plot.saveas(fname,ext=['png','pdf'])
  plot.close()
  print
Ejemplo n.º 10
0
def plothist(xtitle,hists,ratio=False,logy=False,norm=False,**kwargs):
  
  # SETTING
  outdir   = ensuredir("plots/")
  fname    = outdir+"testLegend"
  rrange   = 0.5
  header   = "Gaussians"     # legend header
  text     = "#mu#tau_{h}"   # corner text
  grid     = True and False
  staterr  = True and False  # add uncertainty band to first histogram
  lstyle   = 1               # solid lines
  panel    = 1               # 1 = main (top) panel, 2 = ratio (bottom) panel
  hists    = [h.Clone("%s_clone%d"%(h.GetName(),i)) for i, h in enumerate(hists)]
  if kwargs.get('pos',False):
    fname += "_"+kwargs['pos'].replace(';','')
    header = kwargs['pos']
  if ratio:
    fname += "_ratio"
  
  # PLOT
  LOG.header(fname)
  plot = Plot(xtitle,hists,norm=norm)
  plot.draw(ratio=ratio,logy=logy,ratiorange=rrange,lstyle=lstyle,grid=grid,staterr=staterr)
  plot.drawlegend(border=True,header=header,panel=panel,**kwargs)
  plot.drawtext(text)
  plot.saveas(fname+".png")
  #plot.saveas(fname+".pdf")
  plot.close()
  print