def printTPad(canvas): print "\n>>> canvas.cd(1)" pad = canvas.cd(1) print ">>> canvas.cd(1).GetName() = %s" % (pad.GetName()) print ">>> gPad.GetName() = %s" % (gPad.GetName()) print "\n>>> canvas.cd(2)" pad = canvas.cd(2) print ">>> canvas.cd(2).GetName() = %s" % (pad.GetName()) print ">>> gPad.GetName() = %s" % (gPad.GetName())
def addTPads(canvas): print "\n>>> gPad.GetName() = %s" % (gPad.GetName()) print ">>> canvas.ls()" canvas.ls() pad1 = TPad("pad1", "pad1", 0, 0.33, 1, 0.95) pad2 = TPad("pad2", "pad2", 0, 0.05, 1, 0.30) pad1.Draw() pad2.Draw() ROOT.SetOwnership(pad1, False) ROOT.SetOwnership(pad2, False) print "\n>>> gPad.GetName() = %s" % (gPad.GetName()) print ">>> canvas.ls()" canvas.ls() printTPad(canvas)
def savePlot(name): if gPad is None: print 'no active canvas' return fileName = '%s/%s' % (anaDir, name) print 'pad', gPad.GetName(), 'saved to', fileName gPad.SaveAs( fileName )
def nextpad(): pn = gPad.GetName() cn = gPad.GetCanvas().GetName() pad = pn.replace(cn, "").replace("_", "") if pad == "": gPad.cd(1) else: gPad.GetCanvas().cd(int(pad) + 1)
def compare(objs, add_leg_title=True, normalize=True): print("Comparing") cols = ['#e41a1c', '#377eb8', '#4daf4a'] colors = {} drawn = {} for i in objs: print("Entry", len(colors), i) colors[i] = TColor.GetColor(cols[len(colors)]) # Drawing objects for i in objs: for j in objs[i]: obj = objs[i][j] opt = "" if drawn.setdefault(j, None) is None: drawn[j] = [TCanvas(j, j)] else: opt += "same" drawn[j][0].cd() print("Drawing", obj, "with opt", opt, "on canvas", gPad.GetName()) obj.SetLineColor(colors[i]) obj.SetBit(TH1.kNoTitle) obj.SetBit(TH1.kNoStats) obj.SetTitle(i) if normalize: drawn[j].append(obj.DrawNormalized(opt)) else: drawn[j].append(obj.DrawClone(opt)) for i in drawn: d = drawn[i] can = d[0] can.cd() gPad.SetLogy() leg = TLegend(.1, .9, .9, .99, can.GetName()) leg.SetNColumns(2) d.append(leg) for j in can.GetListOfPrimitives(): leg.AddEntry(j) leg.Draw() return drawn
def create(self): """User interface to create the final figure after averything was defined and all plots were added """ # remember if another TPad was active before prev_pad = gPad.cd() if gPad and gPad.GetName() != self.name else None # Only now create the canvas self._canvas = TCanvas(self.name, "", *self.size) # but for now change to this TPad self._canvas.cd() for i, ps in enumerate(self._plot_specs): self._canvas.cd() # only now create all TPads ps.create(f"{self.name}_pad_{i}") if prev_pad: # Don't spoil what the user might want to do afterwards prev_pad.cd()
pad = canvas.cd(2) print ">>> canvas.cd(2).GetName() = %s" % (pad.GetName()) print ">>> gPad.GetName() = %s" % (gPad.GetName()) def addTPads(canvas): print "\n>>> gPad.GetName() = %s" % (gPad.GetName()) print ">>> canvas.ls()" canvas.ls() pad1 = TPad("pad1", "pad1", 0, 0.33, 1, 0.95) pad2 = TPad("pad2", "pad2", 0, 0.05, 1, 0.30) pad1.Draw() pad2.Draw() ROOT.SetOwnership(pad1, False) ROOT.SetOwnership(pad2, False) print "\n>>> gPad.GetName() = %s" % (gPad.GetName()) print ">>> canvas.ls()" canvas.ls() printTPad(canvas) canvas = TCanvas("canvas", "canvas", 100, 100, 800, 600) addTPads(canvas) print "\n>>> gPad.GetName() = %s" % (gPad.GetName()) print ">>> canvas.ls()" canvas.ls() printTPad(canvas) print
def create(self, name, **kwargs): """Create this plot Args: name: str for TPad name kwargs: dict reserve_ndc_top: float to reserve relative space for the legend at the top """ if not self.objects: return # remember if another TPad was active before prev_pad = gPad.cd() if gPad and gPad.GetName() != name else None self.name = name self.pad = TPad(name, "", *self._rel_coordinates) self.pad.Draw() if self._axes[1].is_log: self.pad.SetLogy() if self._axes[0].is_log: self.pad.SetLogx() # but for now change to this TPad self.pad.cd() # This HAS to come before the frame creation self.pad.SetLeftMargin(self.__adjust_column_margin(self._column_margins[0])) self.pad.SetRightMargin(self.__adjust_column_margin(self._column_margins[1])) self.pad.SetBottomMargin(self.__adjust_row_margin(self._row_margins[0])) self.pad.SetTopMargin(self.__adjust_row_margin(self._row_margins[1])) # Set ticks on either side, might be customisable in the future self.pad.SetTickx(1) self.pad.SetTicky(1) # style objects and create legend self.__style_objects(**kwargs) self.__create_legends() if self.root_legend and self._legend_spec.principal_position == "top": kwargs["reserve_ndc_top"] = \ 1 - map_value(self.root_legend.GetY1(), self.__adjust_row_margin(self._row_margins[0]), 1 - self.__adjust_row_margin(self._row_margins[1]), 0, 1) elif self.root_legend and self._legend_spec.principal_position == "bottom": kwargs["reserve_ndc_bottom"] = \ map_value(self.root_legend.GetY2(), self.__adjust_row_margin(self._row_margins[0]), 1 - self.__adjust_row_margin(self._row_margins[1]), 0, 1) # Create the frame now everything is in place self.__create_frame(**kwargs) # adjust some frame axis properties self.__style_frame() # draw objects and legends self.__draw_objects() self.__draw_lines() self.__draw_legends() self.__draw_text() if prev_pad: # Don't spoil what the user might want to do afterwards prev_pad.cd()