Serializing histogram objects

Dear Rooters,

Currently I am working on parallel application which uses MPI. My problem is to serialize an object TH3D in slavers nodes to send it to master. In master node I use TH3D:Add to sum every object received. I guess serializing should be done by TStreamer and TBuffer, but I do not know how!

Does anybody could help?

Luís A. Perles
Medical Physics PhD. Student
USP - Brazil

see tutorials hserv.C and hclient.C


Hi Rene,

Thank you for quick answer. Those examples was almost what I needed.
I am using the Object Oriented implementation of MPI (OOMPI), which seens to be the best approach when building a full object oriented application. With MPI/OOMPI I can send only basic types or vectors (int, double, char, and so on). It is possible to serialize an object by TMessage::Buffer() method, inherited from TBuffer class. This is my program to a slave node (it is working):

  TH1D *h1sl  = new TH1D ("h1sl","h1sl", 100, -3, 3);
  h1sl->FillRandom ("gaus", 100000);
  msg = new TMessage (kMESS_OBJECT);
  msg->WriteObject (h1sl);
  int j = (int)msg->Length();
  char *k = msg->Buffer();
  OOMPI_Message array_msg (k, j);

My problem now is how to fill a TMessage/TBuffer with a received message and get the object. I have tried this program on master node without success:

  for(Int_t i = 1; i < size; i++)
  int lg;
  OOMPI_COMM_WORLD[i].Recv(lg); //buffer length

  char *kg = new char [lg];	//buffer 

  msg = new TMessage ();
  msg->SetBuffer (kg, lg);	//filling message buffer

  TH1D *h1s = (TH1D *) msg->ReadObject(msg->GetClass());


Where ‘i’ run over my all nodes.

I can compile this program, but when I run I get this error message:
*** Break *** segmentation violation
Generating stack trace…
0x4139cca8 in from /lib/i686/
0x41389c57 in __libc_start_main + 0xc7 from /lib/i686/
0x08066d41 in strcpy + 0x3d from testprogram

Could you help me?

Luís A. Perles

Medical Physics PhD. student
USP - Brazil

Does anybody could explain me why my code above does not work? ](*,)

I am trying to serialize a ROOT object to send it through MPI interface. It should not be very dificult, but the lack of information about it compromises a lot the development. Its almost done but I can not get back the serialized ROOT object. Where is the mistake?


Hi Luis,

do the following:

#include <TMessage.h>

class MyMessage : public TMessage {
   MyMessage(void *buf, Int_t len) : TMessage(buf, len) { }

for(Int_t i = 1; i < size; i++) {
   int lg;
   OOMPI_COMM_WORLD[i].Recv(lg); //buffer length

   char *kg = new char [lg]; //buffer

   msg = new MyMessage(kg, lg);

   TH1D *h1s = (TH1D *) msg->ReadObject(msg->GetClass());


MyMessage is needed since the TMessage(void*,Int_t) method is protected.

Cheers, Fons.

1 Like

Hi Luis,

I do not know if this is really revelant in your case but, TBuffer::SetBuffer takes 3 arguments. The last argument (adopt) default to true.

In particular this means that in your code the buffer you allocate with:

and attach to the buffer with

will be deleted if you do

and will cause a problem if you also do

delete [] kg;.


Hello all!

Thank you very much for your helpfull hints! I have adopted the Rademakers suggestion, but I plan to test Phillipe suggestion .
These hints helped me to build a simple library, which I will describe in “My Root App” section.


Luís A. Perles