def render(self, canvas): params = self.request.params names, histograms = zip(*self.resource_to_render.stack) assert len(names) == 2 objs = [h.obj for h in histograms] h1, h2 = objs p1 = R.TPad("pad", "", 0, 0, 1, 1) keepalive(canvas, p1) p1.Draw() p1.cd() h1.SetLineColor(R.kGreen) h1.Draw() # return p2 = R.TPad("overlay", "", 0, 0, 1, 1) keepalive(canvas, p2) p2.SetFillStyle(4000) p2.SetFrameFillStyle(4000) p2.Draw() p2.cd() h2.SetLineColor(R.kRed) h2.Draw("Y+")
def Get(self, name, rootpy=True, **kwargs): """ Return the requested object cast as its corresponding subclass in rootpy if one exists and ``rootpy=True``, otherwise return the unadulterated TObject. """ thing = super(_DirectoryBase, self).Get(name) if not thing: raise DoesNotExist # Ensure that the file we took the object from is alive at least as long # as the object being taken from it. # Note, Python does *not* own `thing`, it is ROOT's responsibility to # delete it in the C++ sense. (SetOwnership is False). However, ROOT # will delete the object when the TFile's destructor is run. # Therefore, when `thing` goes out of scope and the file referred to # by `this` has no references left, the file is destructed and calls # `thing`'s delete. # (this is thanks to the fact that weak referents (used by keepalive) # are notified when they are dead). keepalive(thing, self) if rootpy: return asrootpy(thing, **kwargs) return thing
def line(x): args = x, ymin, x, ymax l = R.TLine(*args) l.SetLineWidth(1) l.SetLineStyle(2) l.Draw() keepalive(canvas, l)
def line(x, yrange): ymin, ymax = yrange args = x, ymin, x, ymax l = R.TLine(*args) l.SetLineWidth(3); l.SetLineStyle(2) l.Draw() keepalive(l)
def render(self, canvas): # TODO(pwaller): Introduce some options here.. g = self.resource_to_render.obj opts = "ACP" if isinstance(g, R.TGraph2D): opts = "SURF" g.Draw(opts) keepalive(canvas, g)
def line(x, yrange): ymin, ymax = yrange args = x, ymin, x, ymax l = R.TLine(*args) l.SetLineWidth(3) l.SetLineStyle(2) l.Draw() keepalive(canvas, l)
def label_plot(pad, template, xaxis, yaxis, ylabel='Events', xlabel=None, units=None, data_info=None, category_label=None, atlas_label=None, extra_label=None, extra_label_position='left', textsize=22): # set the axis labels binw = list(template.xwidth()) binwidths = list(set(['%.2g' % w for w in binw])) if units is not None: if xlabel is not None: xlabel = '%s [%s]' % (xlabel, units) if ylabel and len(binwidths) == 1 and binwidths[0] != '1': # constant width bins ylabel = '%s / %s %s' % (ylabel, binwidths[0], units) elif ylabel and len(binwidths) == 1 and binwidths[0] != '1': ylabel = '%s / %s' % (ylabel, binwidths[0]) if ylabel: yaxis.SetTitle(ylabel) if xlabel: xaxis.SetTitle(xlabel) left, right, bottom, top = pad.margin_pixels height = float(pad.height_pixels) category_lumi_atlas(pad, category_label, data_info, atlas_label) # draw the extra label if extra_label is not None: if extra_label_position == 'left': label = ROOT.TLatex(pad.GetLeftMargin() + 0.03, 1. - (top + 2 * (textsize + 15)) / height, extra_label) else: # right label = ROOT.TLatex(1. - pad.GetRightMargin() - 0.03, 1. - (top + 2 * (textsize + 15)) / height, extra_label) label.SetTextAlign(31) label.SetNDC() label.SetTextFont(43) label.SetTextSize(textsize) with pad: label.Draw() keepalive(pad, label) pad.Update() pad.Modified()
def render(self, canvas): params = self.request.params names, histograms = zip(*self.resource_to_render.stack) assert len(names) == 2 h1, h2 = [h.obj for h in histograms] h = h2.Clone() keepalive(h) h.Add(h1, -1) h.Draw()
def render(self, canvas): params = self.request.params names, histograms = zip(*self.resource_to_render.stack) assert len(names) == 2 h1, h2 = sorted([h.obj for h in histograms], key=lambda x: x.GetEntries()) eff = R.TEfficiency(h1, h2) keepalive(canvas, eff) eff.SetFillColor(R.kRed) eff.Draw("AP")
def Draw(self, *args): pad = ROOT.gPad.func() if not pad: pad = Canvas() if self.visible: if self.drawstyle: self.__class__.__bases__[-1].Draw(self, " ".join((self.drawstyle, ) + args)) else: self.__class__.__bases__[-1].Draw(self, " ".join(args)) pad.Modified() pad.Update() keepalive(self, pad)
def render(self, canvas): params = self.request.params h = self.resource_to_render.obj if "unit_fixup" in params: h = fixup_hist_units(h) if "nostat" in params: h.SetStats(False) if "notitle" in params: h.SetTitle("") h.Draw(build_draw_params(h, params)) keepalive(canvas, h)
def category_lumi_atlas(pad, category_label=None, data_info=None, atlas_label=None, textsize=22): left, right, bottom, top = pad.margin_pixels height = float(pad.height_pixels) # draw the category label if category_label: label = ROOT.TLatex(1. - pad.GetRightMargin(), 1. - (textsize - 2) / height, category_label) label.SetNDC() label.SetTextFont(43) label.SetTextSize(textsize) label.SetTextAlign(31) with pad: label.Draw() keepalive(pad, label) # draw the luminosity label if data_info is not None: plabel = ROOT.TLatex(1. - pad.GetRightMargin() - 0.03, 1. - (top + textsize + 15) / height, str(data_info)) plabel.SetNDC() plabel.SetTextFont(43) plabel.SetTextSize(textsize) plabel.SetTextAlign(31) with pad: plabel.Draw() keepalive(pad, plabel) # draw the ATLAS label if atlas_label is not False: label = atlas_label or ATLAS_LABEL ATLAS_label(pad.GetLeftMargin() + 0.03, 1. - (top + textsize + 15) / height, sep=0.132, pad=pad, sqrts=None, text=label, textsize=textsize) pad.Update() pad.Modified()
def category_lumi_atlas(pad, category_label=None, data_info=None, atlas_label=None, textsize=22): left, right, bottom, top = pad.margin_pixels height = float(pad.height_pixels) # draw the category label if category_label: label = ROOT.TLatex( 1. - pad.GetRightMargin(), 1. - (textsize - 2) / height, category_label) label.SetNDC() label.SetTextFont(43) label.SetTextSize(textsize) label.SetTextAlign(31) with pad: label.Draw() keepalive(pad, label) # draw the luminosity label if data_info is not None: plabel = ROOT.TLatex( 1. - pad.GetRightMargin() - 0.03, 1. - (top + textsize + 15) / height, str(data_info)) plabel.SetNDC() plabel.SetTextFont(43) plabel.SetTextSize(textsize) plabel.SetTextAlign(31) with pad: plabel.Draw() keepalive(pad, plabel) # draw the ATLAS label if atlas_label is not False: label = atlas_label or ATLAS_LABEL ATLAS_label(pad.GetLeftMargin() + 0.03, 1. - (top + textsize + 15) / height, sep=0.132, pad=pad, sqrts=None, text=label, textsize=textsize) pad.Update() pad.Modified()
def __init__(self, workspace, category): self.ws = workspace self.cat = category self.mc = self.ws.obj('ModelConfig') self.index_cat = self.mc.GetPdf().index_category self.obsData = self.ws.data('obsData') self.pdf = self.mc.GetPdf().pdf(self.cat) self._obs = self.pdf.getObservables(self.mc.GetObservables()).first() self._frame = self._obs.frame() self._frame.SetName('{0}'.format(self.cat.name)) keepalive(self._frame, self._frame.GetName()) self._components = [Component(comp) for comp in self.iter_pdf_components()] self._signal = Component(RooAddition('sum_sig_{0}'.format(self.cat.name), 'sum_sig_{0}'.format(self.cat.name), self.signal_pdf_components())) self._background = Component(RooAddition('sum_bkg_{0}'.format(self.cat.name), 'sum_bkg_{0}'.format(self.cat.name), self.background_pdf_components()))
def __init__(self, workspace, category): self.ws = workspace self.cat = category self.mc = self.ws.obj('ModelConfig') self.index_cat = self.mc.GetPdf().index_category self.obsData = self.ws.data('obsData') self.pdf = self.mc.GetPdf().pdf(self.cat) self._obs = self.pdf.getObservables(self.mc.GetObservables()).first() self._frame = self._obs.frame() self._frame.SetName('{0}'.format(self.cat.name)) keepalive(self._frame, self._frame.GetName()) self._components = [ Component(comp) for comp in self.iter_pdf_components() ] self._signal = Component( RooAddition('sum_sig_{0}'.format(self.cat.name), 'sum_sig_{0}'.format(self.cat.name), self.signal_pdf_components())) self._background = Component( RooAddition('sum_bkg_{0}'.format(self.cat.name), 'sum_bkg_{0}'.format(self.cat.name), self.background_pdf_components()))
def render(self, canvas): params = self.request.params names, histograms = zip(*self.resource_to_render.stack) # print "Rendering stack with {0} histograms".format(len(histograms)) objs = [h.obj for h in histograms] colordict = { "all": R.kBlue, "signal": R.kGreen, "fake": R.kRed, } from ROOT import (kAzure, kBlue, kWhite, kRed, kBlack, kGray, kGreen, kYellow, kTeal, kCyan, kMagenta, kSpring) clrs = [ kWhite, kGreen, kYellow, kBlue, kCyan, kMagenta, kBlack, kGray, kRed, kAzure ] mc = [] signals = [] data = [] name_of = {} def is_data(h, name): # log.info("Got histogram: {0} - {1}".format(h.GetName(), name)) # return h.GetTitle().lower().startswith("data") return "data" in name def is_signal(h): return h.GetTitle().lower().startswith("higgs") for name, obj in zip(names, objs): name_of[obj] = name obj.SetStats(False) if not obj.GetTitle(): obj.SetTitle(name.replace(".root", "")) if is_data(obj, name): data.append(obj) elif is_signal(obj): signals.append(obj) else: mc.append(obj) # obj.SetFillStyle(1001) for h in data: h.SetMarkerStyle(20) h.SetMarkerSize(1.2) h.SetFillStyle(0) mc.sort(key=lambda h: h.GetMaximum()) for h, col in zip(reversed(mc), clrs): if name_of[h] in colordict: col = colordict[name_of[h]] log.warning("Giving %s the color %s" % (name_of[h], col)) h.SetMarkerColor(col) h.SetLineColor(R.kBlack) h.SetLineWidth(2) h.SetFillColor(col) h.SetFillStyle(1001) for h, col in zip(reversed(signals), (R.kRed, R.kBlue, R.kGreen)): h.SetLineColor(col) h.SetLineWidth(2) h.SetLineStyle(2) # get min/max ymax = sum(h.GetMaximum() for h in mc) ymin = sum(h.GetMinimum() for h in mc) if data or signals: ymax = max(ymax, max(h.GetMaximum() for h in data + signals)) ymin = min(ymin, min(h.GetMinimum() for h in data + signals)) ymax += 1 ymax *= (1.5 if not canvas.GetLogy() else 120) ymin = max(5e-1, ymin) # Create Stack of MC mcstack = R.THStack() for h in mc: mcstack.Add(h) keepalive(canvas, mcstack) axis = None mc_sum_line, mc_sum_error = None, None if mc: axis = mcstack mcstack.Draw("Hist") mc_sum_line, mc_sum_error = create_mc_sum(mc) keepalive(canvas, mc_sum_line) keepalive(canvas, mc_sum_error) for signal in signals: if mc: signal.Add(mc_sum_line) if not axis: axis = signal signal.Draw("hist") else: signal.Draw("hist same") if mc: mc_sum_error.Draw("e2same") mc_sum_line.Draw("hist same") for d in data: if not axis: axis = d d.Draw("pe") else: d.Draw("pe same") axis.SetMaximum(ymax) axis.SetMinimum(ymin) axis.GetXaxis().SetRange(objs[0].GetXaxis().GetFirst(), objs[0].GetXaxis().GetLast()) axis.GetXaxis().SetTitle(objs[0].GetXaxis().GetTitle()) if not self.request.params.get("xlabel", None) is None: axis.GetXaxis().SetTitle(self.request.params["xlabel"]) if not self.request.params.get("ylabel", None) is None: axis.GetYaxis().SetTitle(self.request.params["ylabel"]) logy = canvas.GetLogy() canvas.SetLogy(False) canvas.Update() ymin, ymax = canvas.GetUymin(), canvas.GetUymax() canvas.SetLogy(logy) canvas.Update() def line(x): args = x, ymin, x, ymax l = R.TLine(*args) l.SetLineWidth(1) l.SetLineStyle(2) l.Draw() keepalive(canvas, l) # Draw cuts slot = self.request.params.get("slot", None) if not slot: # Determine slot from path for p in lineage(self): if p.__name__ in cuts: slot = p.__name__ if slot: for x in set(cuts[slot]): if canvas.GetUxmin() < x < canvas.GetUxmax(): line(x) if not self.request.params.get("legend", None) is None: legend = get_legend(mc=mc, data=data, signal=signals, mc_sum=mc_sum_error) legend.Draw() if not self.request.params.get("lumi", None) is None: label = get_lumi_label(lumi=self.request.params["lumi"]) label.Draw() p = preliminary() p.Draw("hist e0x0")
def render(self, canvas): params = self.request.params names, histograms = zip(*self.resource_to_render.stack) # print "Rendering stack with {0} histograms".format(len(histograms)) objs = [h.obj for h in histograms] colordict = { "all": R.kBlue, "signal": R.kGreen, "fake": R.kRed, } from ROOT import (kAzure, kBlue, kWhite, kRed, kBlack, kGray, kGreen, kYellow, kTeal, kCyan, kMagenta, kSpring) clrs = [kWhite, kGreen, kYellow, kBlue, kCyan, kMagenta, kBlack, kGray, kRed, kAzure] mc = [] signals = [] data = [] name_of = {} def is_data(h, name): # log.info("Got histogram: {0} - {1}".format(h.GetName(), name)) # return h.GetTitle().lower().startswith("data") return "data" in name def is_signal(h): return h.GetTitle().lower().startswith("higgs") for name, obj in zip(names, objs): name_of[obj] = name obj.SetStats(False) if not obj.GetTitle(): obj.SetTitle(name.replace(".root", "")) if is_data(obj, name): data.append(obj) elif is_signal(obj): signals.append(obj) else: mc.append(obj) # obj.SetFillStyle(1001) for h in data: h.SetMarkerStyle(20) h.SetMarkerSize(1.2) h.SetFillStyle(0) mc.sort(key=lambda h: h.GetMaximum()) for h, col in zip(reversed(mc), clrs): if name_of[h] in colordict: col = colordict[name_of[h]] log.warning("Giving %s the color %s" % (name_of[h], col)) h.SetMarkerColor(col) h.SetLineColor(R.kBlack) h.SetLineWidth(2) h.SetFillColor(col) h.SetFillStyle(1001) for h, col in zip(reversed(signals), (R.kRed, R.kBlue, R.kGreen)): h.SetLineColor(col) h.SetLineWidth(2) h.SetLineStyle(2) # get min/max ymax = sum(h.GetMaximum() for h in mc) ymin = sum(h.GetMinimum() for h in mc) if data or signals: ymax = max(ymax, max(h.GetMaximum() for h in data + signals)) ymin = min(ymin, min(h.GetMinimum() for h in data + signals)) ymax += 1 ymax *= (1.5 if not canvas.GetLogy() else 120) ymin = max(5e-1, ymin) # Create Stack of MC mcstack = R.THStack() for h in mc: mcstack.Add(h) keepalive(canvas, mcstack) axis = None mc_sum_line, mc_sum_error = None, None if mc: axis = mcstack mcstack.Draw("Hist") mc_sum_line, mc_sum_error = create_mc_sum(mc) keepalive(canvas, mc_sum_line) keepalive(canvas, mc_sum_error) for signal in signals: if mc: signal.Add(mc_sum_line) if not axis: axis = signal signal.Draw("hist") else: signal.Draw("hist same") if mc: mc_sum_error.Draw("e2same") mc_sum_line.Draw("hist same") for d in data: if not axis: axis = d d.Draw("pe") else: d.Draw("pe same") axis.SetMaximum(ymax) axis.SetMinimum(ymin) axis.GetXaxis().SetRange(objs[0].GetXaxis().GetFirst(), objs[0].GetXaxis().GetLast()) axis.GetXaxis().SetTitle(objs[0].GetXaxis().GetTitle()) if not self.request.params.get("xlabel", None) is None: axis.GetXaxis().SetTitle(self.request.params["xlabel"]) if not self.request.params.get("ylabel", None) is None: axis.GetYaxis().SetTitle(self.request.params["ylabel"]) logy = canvas.GetLogy() canvas.SetLogy(False) canvas.Update() ymin, ymax = canvas.GetUymin(), canvas.GetUymax() canvas.SetLogy(logy) canvas.Update() def line(x): args = x, ymin, x, ymax l = R.TLine(*args) l.SetLineWidth(1) l.SetLineStyle(2) l.Draw() keepalive(canvas, l) # Draw cuts slot = self.request.params.get("slot", None) if not slot: # Determine slot from path for p in lineage(self): if p.__name__ in cuts: slot = p.__name__ if slot: for x in set(cuts[slot]): if canvas.GetUxmin() < x < canvas.GetUxmax(): line(x) if not self.request.params.get("legend", None) is None: legend = get_legend(mc=mc, data=data, signal=signals, mc_sum=mc_sum_error) legend.Draw() if not self.request.params.get("lumi", None) is None: label = get_lumi_label(lumi=self.request.params["lumi"]) label.Draw() p = preliminary() p.Draw("hist e0x0")
def Draw(self, *args, **kwargs): keepalive(ROOT.gPad.func(), self) return super(DrawableKeepAlive, self).Draw(*args, **kwargs)
def render(self, canvas): params = self.request.params names, histograms = zip(*self.resource_to_render.stack) # print "Rendering stack with {0} histograms".format(len(histograms)) names = [n for n in names] histograms = [h for h in histograms] objs = [h.obj for h in histograms] if "sum" in params: hsum = objs[0].Clone("sum") keepalive(canvas, hsum) hsum.SetTitle("sum") for h in objs[1:]: hsum.Add(h) names.append("sum") objs.append(hsum) colordict = { "all": R.kBlue, "signal": R.kGreen, "fake": R.kRed, } cols = [R.kBlue, R.kRed, R.kGreen, R.kViolet, R.kAzure + 6, R.kOrange] for name, obj, col in zip(names, objs, cols): col = col + 1 # obj.SetTitle(""); obj.SetStats(False) if name in colordict: obj.SetLineColor(colordict[name]) else: obj.SetLineColor(col) obj.SetMarkerColor(col) obj.SetLineWidth(2) if "shape" in params: for obj in objs: if obj.Integral(): obj.Scale(1. / obj.Integral()) max_value = max(o.GetMaximum() for o in objs) * 1.1 min_value = min(o.GetMinimum() for o in objs) if min_value != objs[0].GetMinimum(): old_minvalue = min_value # Apply a correction to include a bit more than just the lower bound min_value = min_value - (max_value - min_value) * 0.1 # If this takes us below zero but the old minimum value was close # to zero, just use zero. if min_value < 0 and abs(old_minvalue) < 1e-8: min_value = 0 obj = objs[0] # .pop(0) from root.histogram import build_draw_params dp = "hist e0x0" # build_draw_params(obj, self.request.params, True) obj.Draw(dp) obj.SetMaximum(max_value) # obj.SetMinimum(0) for obj in objs[1:]: obj.Draw(dp + " same") logy = canvas.GetLogy() canvas.SetLogy(False) canvas.Update() canvas_yrange = ymin, ymax = canvas.GetUymin(), canvas.GetUymax() canvas.SetLogy(logy) canvas.Update() def line(x, yrange): ymin, ymax = yrange args = x, ymin, x, ymax l = R.TLine(*args) l.SetLineWidth(3) l.SetLineStyle(2) l.Draw() keepalive(canvas, l) # Draw cuts slot = self.request.params.get("slot", None) if not slot: # Determine slot from path for p in lineage(self): if p.__name__ in cuts: slot = p.__name__ if slot: for x, yrange in zip(cuts[slot], zip(etabins, etabins[1:])): if canvas.GetUxmin() < x < canvas.GetUxmax(): line(x, canvas_yrange if obj.GetDimension() != 2 else yrange) if self.request.params.get("legend", None) is not None: log.debug("Drawing legend: {0}".format(objs)) for n, o in zip(names, objs): o.SetTitle(n) legend = get_legend(mc=objs) legend.Draw()