Difference between revisions of "Serializing and deserializing root objects"

From GlueXWiki
Jump to: navigation, search
m
m
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
===Introduction===
 
===Introduction===
  
ROOT objects can be serialized to a byte array or reconstructed from the byte array quite easily. One reason to do this might
+
ROOT objects can be serialized to a byte array and reconstructed from the byte array quite easily.
be to store or transport ROOT objects in a way not supported by ROOT itself.  An example is transport of a ROOT histogram from a producer to a  
+
 
consumer program via the cMsg package, a publish/subscribe messaging system that will be used extensively in the Hall D online monitoring system (see Example Programs below).
+
One reason to do this might be to store or transport ROOT objects in a way not supported by ROOT itself.  An example is transport of a ROOT histogram from a producer to a consumer program via the cMsg package, a publish/subscribe messaging system that will be used extensively in the Hall D online monitoring system (see Example Programs below).
  
  
 
===Serialize===
 
===Serialize===
  
ROOT TMessage objects can hold serialized ROOT objects.
+
ROOT TMessage objects can hold serialized ROOT objects:
  
 
<pre>
 
<pre>
Line 19: Line 19:
 
tm->WriteObject(some_root_object);
 
tm->WriteObject(some_root_object);
  
// get pointer to TMessage internal buffer holding object and its length in bytes
+
// get pointer to TMessage internal buffer holding object and its length (in bytes)
 
char *buffer    = tm->Buffer();
 
char *buffer    = tm->Buffer();
 
int bufferLength = tm->Length());
 
int bufferLength = tm->Length());
 
</pre>
 
</pre>
 
  
  
Line 40: Line 39:
  
  
// create special TMessage object from byte buffer, length in bytes
+
// create special TMessage object from byte buffer, length (in bytes)
 
MyTMessage *myTM = new MyTMessage(buffer,length);
 
MyTMessage *myTM = new MyTMessage(buffer,length);
  
Line 46: Line 45:
 
TH1 *hist = (TH1*)myTM->ReadObject(myTM->GetClass());
 
TH1 *hist = (TH1*)myTM->ReadObject(myTM->GetClass());
 
</pre>
 
</pre>
 
  
  
 
===Example Programs===
 
===Example Programs===
  
[[cMsgRootProducer.cc]],[[cMsgRootConsumer.cc]],[[cMsgRootMakefile|Makefile]]
+
[[cMsgRootProducer.cc]] produces a histogram, serializes it, and ships it over the network via the cMsg package to one or more consumer programs.
Examples demonstrate transport via cMsg package.
+
 
 +
[[cMsgRootConsumer.cc]] receives the cMsg message, extracts the byte buffer and recreates the histogram.  Alternate mechanisms for recreating base objects and casting them to derived objects are shown.
 +
 
 +
[[cMsgRootMakefile|Makefile]] for two example programs.

Latest revision as of 14:48, 30 June 2009

Introduction

ROOT objects can be serialized to a byte array and reconstructed from the byte array quite easily.

One reason to do this might be to store or transport ROOT objects in a way not supported by ROOT itself. An example is transport of a ROOT histogram from a producer to a consumer program via the cMsg package, a publish/subscribe messaging system that will be used extensively in the Hall D online monitoring system (see Example Programs below).


Serialize

ROOT TMessage objects can hold serialized ROOT objects:

#include "TMessage.h"

// create TMessage object capable of holding serialized ROOT object
TMessage *tm = new TMessage(kMESS_OBJECT);

// fill with serialized version of some ROOT object
tm->WriteObject(some_root_object);

// get pointer to TMessage internal buffer holding object and its length (in bytes)
char *buffer     = tm->Buffer();
int bufferLength = tm->Length());


Deserialize

Deserializing involves a trick due to limitations of the TMessage class. The required constructor is protected, so a covering class must be created to expose it:

#include "TMessage.h"

// special class needed to expose protected TMessage constructor
class MyTMessage : public TMessage {
public:
   MyTMessage(void *buf, Int_t len) : TMessage(buf, len) { }
};


// create special TMessage object from byte buffer, length (in bytes)
MyTMessage *myTM = new MyTMessage(buffer,length);

// reconstruct original object, here assumed to be a histogram
TH1 *hist = (TH1*)myTM->ReadObject(myTM->GetClass());


Example Programs

cMsgRootProducer.cc produces a histogram, serializes it, and ships it over the network via the cMsg package to one or more consumer programs.

cMsgRootConsumer.cc receives the cMsg message, extracts the byte buffer and recreates the histogram. Alternate mechanisms for recreating base objects and casting them to derived objects are shown.

Makefile for two example programs.