Changes

Jump to navigation Jump to search
|}
== ROOT ==
This example shot how to literally embed
original python source code .
<table>
<tr>
<td><texcode>
\startluacode
function test_ROOT(filename)
require("python")
pg = python.globals()
 
python.execute([[
def run(filename):
from ROOT import TCanvas, TGraph
from ROOT import gROOT
from math import sin
from array import array
 
 
gROOT.Reset()
 
c1 = TCanvas( 'c1', 'A Simple Graph Example', 200, 10, 700, 500 )
 
c1.SetFillColor( 42 )
c1.SetGrid()
 
n = 20
x, y = array( 'd' ), array( 'd' )
 
for i in range( n ):
x.append( 0.1*i )
y.append( 10*sin( x[i]+0.2 ) )
 
gr = TGraph( n, x, y )
gr.SetLineColor( 2 )
gr.SetLineWidth( 4 )
gr.SetMarkerColor( 4 )
gr.SetMarkerStyle( 21 )
gr.SetTitle( 'a simple graph' )
gr.GetXaxis().SetTitle( 'X title' )
gr.GetYaxis().SetTitle( 'Y title' )
gr.Draw( 'ACP' )
c1.Update()
c1.Print(filename)
]])
run = pg.run
run(filename)
end
\stopluacode
 
\starttext
\startTEXpage
\ctxlua{test_ROOT("testsin.pdf")}
\rotate[rotation=90]{\externalfigure[testsin.pdf][width=5cm]}
\stopTEXpage
\stoptext
</texcode> </td>
<td> [[Image:Testsin.jpg|512px]] </td>
</tr>
</table>
 
We can do a bit better: separate python code from lua code .<br/>
Save this in <tt>test-ROOT1.py</tt> (so it's also easy to test) :
<pre>
from ROOT import TCanvas, TGraph ,TGraphErrors,TMultiGraph
from ROOT import gROOT
from math import sin
from array import array
 
def run(filename):
c1 = TCanvas("c1","multigraph",200,10,700,500)
c1.SetGrid()
 
# draw a frame to define the range
mg = TMultiGraph()
# create first graph
n = 24;
x = array('d',range(24))
data = file('data').readlines()
for line in data:
line = line.strip()
y = array('d',[float(d) for d in line.split()])
gr = TGraph(n,x,y)
gr.Fit("pol6","q")
mg.Add(gr)
 
mg.Draw("ap")
 
#force drawing of canvas to generate the fit TPaveStats
c1.Update()
c1.Print(filename)
 
</pre>
Here file 'data' is a 110 lines file with 24 floats values space separated,
ie <br/><tt> 20.6000 19.4000 19.4000 18.3000 17.8000 16.1000 16.7000 21.1000 23.3000 26.1000 26.1000 27.2000 27.8000 28.3000 28.3000 27.2000 25.6000 22.8000 21.7000 21.7000 21.7000 21.7000 21.7000 21.7000 </tt>.<br/>
Now a tex file, with a simple layer in lua as interface for python:
{|
|-
|<texcode>
\startluacode
function test_ROOT(filename)
require("python")
test = python.import('test-ROOT1')
test.run(filename)
end
\stopluacode
 
\starttext
\startTEXpage
\ctxlua{test_ROOT("data.pdf")}
\rotate[rotation=90]{\externalfigure[data.pdf]}
\stopTEXpage
\stoptext
</texcode>
| [[Image:Test-ROOT1.jpg|300px]]
|}
== ImageMagick ==

Navigation menu