9 """Helpers for drawing ROOT objects.
10 The main function in here is draw_obj(obj [, options]).
11 This is similar to obj->Draw(options), but some extra
12 functionality is added:
14 - Automatic zone handling. You can split the canvas into
15 subpads (zones) with zone(). Each call to draw_obj() will
16 plot into the next sequential pad (unless SAME was specified).
17 Selecting a pad by clicking with button 2 will change
18 the sequence so that the next objects is drawn in the
19 selected pad. draw_obj also takes an optional padnum
20 argument to explicitly specify the pad number to use.
22 - If the SAME option is given, then the new object will be
23 superimposed on an existing one in the current pad.
24 In the case of 1D histograms, we change the line
25 type of the new histogram, and also rescale the vertical
26 axis, if needed to make the new histogram fit.
28 - The option MERGE is like SAME, except that we step through
29 the pads as normal. The new plot will be made on top of
30 whatever happens to be on that pad. By default, the line
31 type used will be the same as one would get the first time
32 one overlays a plot with SAME; to override this, put a number
33 directly after the option, like `MERGE2'.
35 - The option NORM draws the histogram normalized to 1.
37 - The option LOGY draws the histogram using a logarithmic
40 - The line type will recycle after four histograms have
41 been superimposed. If the LINECOLORS option is given, then
42 the new histograms will be drawn in a different color.
44 - draw_obj takes optional min and max parameters to set the
45 minimum and maximum values for the y-axis of the histogram
48 Besides draw_obj, a few other functions are available.
49 zone(nx,ny) was already mentioned.
50 printeps(fname) is a shortcut to print an eps file from the current canvas.
51 get_canvas() returns the canvas currently being used for drawing.
52 get_pad() returns the pad currently being used for drawing.
54 from __future__
import division
57 from ROOT
import gROOT, TCanvas, TVirtualPad, TH1, TH2, TObject
76 """Helper class for parsing options."""
88 options = options.replace (
',',
' ')
89 for o
in options.split():
91 if lo
in [
"merge",
"same",
"norm",
"logy",
'linecolors']:
103 if lo.startswith (pat+
'='):
104 setattr (self, pat, int (lo[len (pat)+1:]))
109 def draw_obj (obj, options = "", padnum = -1, pad = None, min=None, max=None):
110 """Draw the root object OBJ in the next available pad.
113 obj - The object to draw.
114 options - Drawing options.
115 These are passed through to the root Draw
116 method, except that we have special
117 handling for the SAME option, and add a new MERGE option.
118 See the header for details.
119 padnum - If this is a non-negative integer, then this specifies
120 the pad in which to draw, overriding
121 other specifications except for PAD. Note: subpad numbers
123 pad - Explicitly specify the pad to use for drawing.
124 Overrides all other specifications.
127 The object that we drew (may not be the same as OBJ if we
137 op = _options (options)
146 if op.same
or op.merge:
168 pad = get_pad (advance_p, padnum)
170 if not op.merge
and not op.same:
171 pad.SetLogy (
not not op.logy)
173 if isinstance (obj, TH1)
and not isinstance (obj, TH2):
186 if rescale_p
and max
is None:
189 prims = pad.GetListOfPrimitives()
191 prims.ResetBit(TObject.kMustCleanup)
193 if isinstance (obj, TH1):
199 if hfirst
and h.GetMaximum() > hfirst.GetMaximum():
200 hfirst.SetMaximum (h.GetMaximum() * 1.1)
202 if hfirst
and h.GetMinimum() < hfirst.GetMinimum():
203 hfirst.SetMinimum (h.GetMinimum())
207 hh = h.DrawCopy (op.other)
209 hh.SetLineStyle (op.linetype)
211 hh.SetLineStyle ((_samecount%4)+1)
213 hh.SetLineColor (op.color)
214 elif op.linecolors
and _samecount >= 4:
215 hh.SetLineColor (_samecount//4 + 1)
217 hh.SetFillColor (op.fill)
227 """Advance to the next pad, if requested.
228 Allow clicking on the pads (button 2) to change the next pad.
230 global _lastpad, _nextpad, _npads
233 pad = TVirtualPad.Pad()
237 pad.GetCanvasID() == c1.GetCanvasID()
and
238 pad.GetNumber() != _lastpad
and pad.GetNumber() != 0):
239 _nextpad = pad.GetNumber()
240 if _nextpad > _npads:
248 if _nextpad > _npads:
260 _nextpad = _lastpad + 1
261 if _nextpad > _npads:
269 return TVirtualPad.Pad()
274 """Return the canvas named CNAME.
275 Create it if it doesn't exist.
278 _canvas = gROOT.FindObject (cname)
280 _canvas = TCanvas (cname, cname, 700, 600)
281 _canvas.SetLeftMargin (0.15)
282 _canvas.SetBottomMargin (0.15)
289 """Divide the canvas into subpads.
290 NX and NY are the number of subpads in x and y, respectively.
292 global _npads, _nextpad, _lastpad
303 """Print the current canvas as an eps file."""
306 c1.Print (s,
"eps,Portrait")