Char ** from pyroot?

Hi,

I’m trying to using TImage::GetImageBuffer(char** buffer, int* size, TImage::EImageFileTypes type = TImage::kPng) from pyroot. It’s just the first parameter giving me problems.

Do I have to use gROOT.ProcessLine(…)?

I’ve tried this but I don’t seem to get the whole image back. It only fills a little bit of the buffer.

Regards,
Karol.

ROOT 5.14/00
Python 2.5
Mac OSX 10.4
gcc 4.0.1

Karol,

putting a buffer through a char** is easy (even if fugly):import array a = array.array( 'b', [0]*9 ) addr = array.array( 'l', [ a.buffer_info()[0] ] )where ‘addr’ can now be used for a function that takes a char** argument, and fills it with up to 9 chars (or 3 strings of length 2).

However, what TImage::GetImageBuffer() does, is using the char** as an out-argument. That will work, too, like with:addr = array.array( 'l', [0] )and after the GetImageBuffer() call, ‘addr[0]’ will contain the address of the TImage buffer.

But then where does it have to go? Pretty much anything you do with a char* in python gets sooner or later copied into a str object. I don’t think that’s what you want?

Cheers,
Wim

Here’s some code:

[code]#!/usr/bin/env python
from ROOT import *
from array import array
from ctypes import *

c=TCanvas(‘c’,‘c’)

f1=TF1(‘f1’, ‘sin(x)/x’, 0, 10)
f1.Draw()

x=array(‘l’, [0])
y=array(‘i’, [0])

img=TImage.Create()
img.FromPad©
img.GetImageBuffer(x,y, TImage.kPng)

s=string_at(x[0], y[0])

of=file(‘boo.png’, ‘wb’)
for i in s:
of.write(s)
of.close()[/code]

This produces a valid png but there’s no content unfortunately. The PNG header is there when I look at the file in vi, but it’s not putting in the actual plot.

I think the canvas is not being passed correctly or something.

Note: the ctypes modules is in Python 2.5 only I think.

Karol,

yes, ctypes is 2.5 only. Thanks for pointing out the existence of string_at!

With release 5.14.00, I don’t see anything in my file either. However, it works with CVS HEAD (on Linux, anyway). I tried again with an upgraded pyroot in 5.14.00-patches, which still left the .png empty, so the fix didn’t seem to come from anything I did.

What did help, is to add a line ‘c.Update()’ in your script, just after the f1.Draw() call.

Note that the display of the function in the canvas on the screen doesn’t mean that much, because canvases are updated by the displayhook (called by the python interpreter at the end of your script) and by a separate GUI thread (called intermittently). As such, I’d add the ‘c.Update()’ call always, even for CVS HEAD.

HTH,
Wim

It works :smiley:

Hooray for c.Update() !

Thank you Wim.