Difference between revisions of "Serializing and deserializing root objects"
m |
m |
||
Line 24: | Line 24: | ||
</pre> | </pre> | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
Line 50: | Line 39: | ||
+ | Deserialize using this new class: | ||
+ | |||
+ | <pre> | ||
+ | #include "TMessage.h" | ||
+ | |||
+ | // create special object from byte buffer, length in bytes | ||
+ | MyTMessage *myTM = new MyTMessage(buffer,length); | ||
+ | |||
+ | // reconstruct object, here assumed to be a histogram | ||
+ | TH1 *hist = (TH1*)myTM->ReadObject(myTM->GetClass()); | ||
+ | </pre> | ||
Revision as of 14:32, 30 June 2009
Introduction
ROOT objects can be serialized to a byte array or 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. They are filled via the write() method:
#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 and length char *buffer = tm->Buffer(); int bufferLength = tm->Length()); // in bytes
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:
// class needed to expose protected TMessage constructor class MyTMessage : public TMessage { public: MyTMessage(void *buf, Int_t len) : TMessage(buf, len) { } };
Deserialize using this new class:
#include "TMessage.h" // create special object from byte buffer, length in bytes MyTMessage *myTM = new MyTMessage(buffer,length); // reconstruct object, here assumed to be a histogram TH1 *hist = (TH1*)myTM->ReadObject(myTM->GetClass());
Example Programs
cMsgRootProducer.cc,cMsgRootConsumer.cc,Makefile Examples demonstrate transport via cMsg package.