-
Notifications
You must be signed in to change notification settings - Fork 0
/
rootnotes.py
66 lines (52 loc) · 1.73 KB
/
rootnotes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
'''
Helper module for displaying ROOT canvases in ipython notebooks
Usage example:
# Save this file as rootnotes.py to your working directory.
import rootnotes
c1 = rootnotes.default_canvas()
fun1 = TF1( 'fun1', 'abs(sin(x)/x)', 0, 10)
c1.SetGridx()
c1.SetGridy()
fun1.Draw()
c1
More examples: http://mazurov.github.io/webfest2013/
@author [email protected]
@author [email protected]
@date 2013-08-09
'''
import ROOT
ROOT.gROOT.SetBatch()
import tempfile
from IPython.core import display
def canvas(name="icanvas", size=(800, 600)):
"""Helper method for creating canvas"""
# Check if icanvas already exists
canvas = ROOT.gROOT.FindObject(name)
assert len(size) == 2
if canvas:
return canvas
else:
return ROOT.TCanvas(name, name, size[0], size[1])
def default_canvas(name="icanvas", size=(800, 600)):
""" depricated """
return canvas(name=name, size=size)
def _display_canvas(canvas):
file = tempfile.NamedTemporaryFile(suffix=".png")
canvas.SaveAs(file.name)
ip_img = display.Image(filename=file.name, format='png', embed=True)
return ip_img._repr_png_()
def _display_any(obj):
file = tempfile.NamedTemporaryFile(suffix=".png")
obj.Draw()
ROOT.gPad.SaveAs(file.name)
ip_img = display.Image(filename=file.name, format='png', embed=True)
return ip_img._repr_png_()
# register display function with PNG formatter:
png_formatter = get_ipython().display_formatter.formatters['image/png'] # noqa
# Register ROOT types in ipython
#
# In [1]: canvas = rootnotes.canvas()
# In [2]: canvas
# Out [2]: [image will be here]
png_formatter.for_type(ROOT.TCanvas, _display_canvas)
png_formatter.for_type(ROOT.TF1, _display_any)