Variable size character array

Hi,

I have a tree that contains some compressed data as a character buffer. The leaf with the data has the structure: “size/I:buf[size]/b”. How can I put this buffer into a string?

Thanks,
Vyacheslav

[quote]How can I put this buffer into a string? [/quote]What kind of strings?
Typically you should use:struct Holder { Int_t size; Char_t buf[MAX_BUFFER_SIZE]; }; ... Holder holder; tree->SetBranchAddress(name,&holder); tree->GetEntry(some_entry);and buf would contain the information you need (however the contain may or may not be null terminated depending on how it was filled).

Cheers,
Philippe.

Hi,

character buffers are automatically converted to python strings, and so it is assumed that they have a terminating \0. If not, then the approach that Philippe lays out needs to be followed. The Holder class can be made available to python by having CINT interpret it:[code]import ROOT

ROOT.gROOT.ProcessLine( “const int MAX_BUFFER_SIZE = 42;” )
ROOT.gROOT.ProcessLine( “struct Holder {
Int_t size;
Char_t buf[MAX_BUFFER_SIZE];
};” )

holder = ROOT.Holder()[/code]
Cheers,
Wim

The buffer in question is a png image stored as a char array, so not null terminated.

I followed the approach you suggested. After creating the holder struct and making it available in python, I set the branch address as follows:

mytree.SetBranchAddress(bname,AddressOf(holder,'buf'))
mytree.SetBranchAddress(bname,AddressOf(holder,'size'))

mytree.GetEntry(some_entry)

#save the image to a file
fil = open("image.png","wb")
fil.write(buffer(holder.buf,0,holder.size))
fil.close()  

The problem is only the first 8 bytes from the buffer are saved. Is it not possible to handle raw binary data this way?

Thanks,
Vyachelsav

Vyachelsav,

well, the obvious first questions are: what is holder.size, and what is the (static) size of buf?

Cheers,
Wim

Hi Wim,

Checked those, no obvious problem there. The buffer size is set to 130000, while the the holder.size value prints to 123040.

Vyachelsav,

too many string conversions … and with that the Holder is not going to work. That leaves the python array:from array import array buf = array('c',['\0']*bufsize) mytree.SetBranchAddress(bname,buf)and you can then either write the array as you intended with the buffer object, or use the array.tofile() function (which should be a lot faster).

Cheers,
Wim