ATLAS Offline Software
Loading...
Searching...
No Matches
python.draw_obj Namespace Reference

Classes

class  _options

Functions

 draw_obj (obj, options="", padnum=-1, pad=None, min=None, max=None)
 get_pad (advance_p=1, padnum=-1)
 get_canvas (cname="c1")
 zone (nx, ny)
 printeps (s="out.eps")

Variables

int _samecount = 0
int _lastpad = 0
int _nextpad = 1
int _npads = 1
 _canvas = None

Detailed Description

Helpers for drawing ROOT objects.
The main function in here is draw_obj(obj [, options]).
This is similar to obj->Draw(options), but some extra
functionality is added:

  - Automatic zone handling.  You can split the canvas into
    subpads (zones) with zone().  Each call to draw_obj() will
    plot into the next sequential pad (unless SAME was specified).
    Selecting a pad by clicking with button 2 will change
    the sequence so that the next objects is drawn in the
    selected pad.  draw_obj also takes an optional padnum
    argument to explicitly specify the pad number to use.

  - If the SAME option is given, then the new object will be
    superimposed on an existing one in the current pad.
    In the case of 1D histograms, we change the line
    type of the new histogram, and also rescale the vertical
    axis, if needed to make the new histogram fit.

  - The option MERGE is like SAME, except that we step through
    the pads as normal.  The new plot will be made on top of
    whatever happens to be on that pad.  By default, the line
    type used will be the same as one would get the first time
    one overlays a plot with SAME; to override this, put a number
    directly after the option, like `MERGE2'.

  - The option NORM draws the histogram normalized to 1.

  - The option LOGY draws the histogram using a logarithmic
    y-axis, and LOGX draws it with a logarithmic x-axis.

  - The line type will recycle after four histograms have
    been superimposed.  If the LINECOLORS option is given, then
    the new histograms will be drawn in a different color.

  - draw_obj takes optional min and max parameters to set the
    minimum and maximum values for the y-axis of the histogram
    being drawn.

Besides draw_obj, a few other functions are available.
zone(nx,ny) was already mentioned.
printeps(fname) is a shortcut to print an eps file from the current canvas.
get_canvas() returns the canvas currently being used for drawing.
get_pad() returns the pad currently being used for drawing.

Function Documentation

◆ draw_obj()

python.draw_obj.draw_obj ( obj,
options = "",
padnum = -1,
pad = None,
min = None,
max = None )
Draw the root object OBJ in the next available pad.

Inputs:
obj -         The object to draw.
options -     Drawing options.
            These are passed through to the root Draw
            method, except that we have special
            handling for the SAME option, and add a new MERGE option.
            See the header for details.
padnum -      If this is a non-negative integer, then this specifies
            the pad in which to draw, overriding
            other specifications except for PAD.  Note: subpad numbers
            start with 1.
pad -         Explicitly specify the pad to use for drawing.
            Overrides all other specifications.

Returns:
The object that we drew (may not be the same as OBJ if we
made a copy).

Definition at line 109 of file draw_obj.py.

109def draw_obj (obj, options = "", padnum = -1, pad = None, min=None, max=None):
110 """Draw the root object OBJ in the next available pad.
111
112Inputs:
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
122 start with 1.
123 pad - Explicitly specify the pad to use for drawing.
124 Overrides all other specifications.
125
126Returns:
127 The object that we drew (may not be the same as OBJ if we
128 made a copy).
129"""
130 global _samecount
131
132 if min is not None:
133 obj.SetMinimum (min)
134 if max is not None:
135 obj.SetMaximum (max)
136
137 op = _options (options)
138
139 # Should we advance to the next pad?
140 advance_p = 0
141
142 # Should we do vertical axis rescaling?
143 rescale_p = 0
144
145 # For SAME and MERGE, we have to pass SAME on to root.
146 if op.same or op.merge:
147 op.other += "SAME"
148
149 if not op.same:
150 # No SAME option. Reset the count.
151 _samecount = 0
152
153 # Advance to the next pad.
154 advance_p = 1
155 else:
156 # SAME was specified. Keep count of the number of such.
157 _samecount += 1
158 rescale_p = 1
159
160 # Handle the MERGE option.
161 if op.merge:
162 rescale_p = 1
163 advance_p = 1
164
165 if pad:
166 pad.cd()
167 else:
168 pad = get_pad (advance_p, padnum)
169
170 if not op.merge and not op.same:
171 pad.SetLogx (not not op.logx)
172 pad.SetLogy (not not op.logy)
173
174 if isinstance (obj, TH1) and not isinstance (obj, TH2):
175 h = obj
176 if op.norm:
177 h = h.Clone()
178 intg = h.Integral()
179 if intg == 0:
180 intg = 1
181 h.Scale (1. / intg)
182 if max is not None:
183 h.SetMaximum (max)
184
185 # Special handling for 1D histograms.
186 # If SAME was specified, rescale the vertical axis, if needed.
187 if rescale_p and max is None:
188 # Find the first hist already plotted.
189 hfirst = None
190 prims = pad.GetListOfPrimitives()
191 # Avoids RecursiveRemove crash...
192 prims.ResetBit(TObject.kMustCleanup)
193 for obj in prims:
194 if isinstance (obj, TH1):
195 hfirst = obj
196 break
197
198 # If the new hist's maximum is larger than the first one,
199 # adjust the maximum of the first.
200 if hfirst and h.GetMaximum() > hfirst.GetMaximum():
201 hfirst.SetMaximum (h.GetMaximum() * 1.1)
202
203 if hfirst and h.GetMinimum() < hfirst.GetMinimum():
204 hfirst.SetMinimum (h.GetMinimum())
205
206 # Draw a copy of the histogram.
207 # Adjust the line style.
208 hh = h.DrawCopy (op.other)
209 if op.linetype >= 0:
210 hh.SetLineStyle (op.linetype)
211 else:
212 hh.SetLineStyle ((_samecount%4)+1)
213 if op.color >= 0:
214 hh.SetLineColor (op.color)
215 elif op.linecolors and _samecount >= 4:
216 hh.SetLineColor (_samecount//4 + 1)
217 if op.fill >= 0:
218 hh.SetFillColor (op.fill)
219 obj = hh
220 else:
221 # Not a 1-D histogram. Just draw it.
222 obj.Draw (op.other)
223
224 return obj
225
226

◆ get_canvas()

python.draw_obj.get_canvas ( cname = "c1")
Return the canvas named CNAME.
Create it if it doesn't exist.

Definition at line 274 of file draw_obj.py.

274def get_canvas (cname = "c1"):
275 """Return the canvas named CNAME.
276Create it if it doesn't exist.
277"""
278 global _canvas
279 _canvas = gROOT.FindObject (cname)
280 if not _canvas:
281 _canvas = TCanvas (cname, cname, 700, 600)
282 _canvas.SetLeftMargin (0.15)
283 _canvas.SetBottomMargin (0.15)
284 _canvas.SetLogx (0)
285 _canvas.SetLogy (0)
286 return _canvas
287
288
289

◆ get_pad()

python.draw_obj.get_pad ( advance_p = 1,
padnum = -1 )
Advance to the next pad, if requested.
Allow clicking on the pads (button 2) to change the next pad.

Definition at line 227 of file draw_obj.py.

227def get_pad (advance_p = 1, padnum = -1):
228 """Advance to the next pad, if requested.
229Allow clicking on the pads (button 2) to change the next pad.
230"""
231 global _lastpad, _nextpad
232
233 c1 = get_canvas()
234 pad = TVirtualPad.Pad()
235
236 if advance_p:
237 if (pad and
238 pad.GetCanvasID() == c1.GetCanvasID() and
239 pad.GetNumber() != _lastpad and pad.GetNumber() != 0):
240 _nextpad = pad.GetNumber()
241 if _nextpad > _npads:
242 _nextpad = 1
243
244 # Set up to draw on nextpad.
245 # Bump it for the next go around.
246 if _npads > 1:
247 _lastpad = _nextpad
248 _nextpad += 1
249 if _nextpad > _npads:
250 _nextpad = 1
251 else:
252 _lastpad = 0
253
254 # Select the pad.
255 c1.cd (_lastpad)
256
257 # Handle padnum.
258 if padnum >= 0:
259 _lastpad = padnum
260 if _npads > 0:
261 _nextpad = _lastpad + 1
262 if _nextpad > _npads:
263 _nextpad = 0
264 else:
265 _nextpad = 0
266
267 c1.cd (_lastpad)
268
269 # Refetch the pad.
270 return TVirtualPad.Pad()
271
272

◆ printeps()

python.draw_obj.printeps ( s = "out.eps")
Print the current canvas as an eps file.

Definition at line 304 of file draw_obj.py.

304def printeps (s = "out.eps"):
305 """Print the current canvas as an eps file."""
306
307 c1 = get_canvas()
308 c1.Print (s, "eps,Portrait")
309 return

◆ zone()

python.draw_obj.zone ( nx,
ny )
Divide the canvas into subpads.
NX and NY are the number of subpads in x and y, respectively.

Definition at line 290 of file draw_obj.py.

290def zone (nx, ny):
291 """Divide the canvas into subpads.
292NX and NY are the number of subpads in x and y, respectively.
293"""
294 global _npads, _nextpad, _lastpad
295 c1 = get_canvas()
296 c1.Clear()
297 c1.Divide (nx, ny)
298 _npads = nx*ny
299 _nextpad = 1
300 _lastpad = 0
301 return
302
303

Variable Documentation

◆ _canvas

python.draw_obj._canvas = None
protected

Definition at line 273 of file draw_obj.py.

◆ _lastpad

int python.draw_obj._lastpad = 0
protected

Definition at line 64 of file draw_obj.py.

◆ _nextpad

int python.draw_obj._nextpad = 1
protected

Definition at line 67 of file draw_obj.py.

◆ _npads

int python.draw_obj._npads = 1
protected

Definition at line 70 of file draw_obj.py.

◆ _samecount

int python.draw_obj._samecount = 0
protected

Definition at line 60 of file draw_obj.py.