How to deserialize a TTree?

I want to serialize a TTree and then deserialize it. The code is like this:

#include <iostream>
#include <assert.h>
#include <string.h>
#include <leveldb/db.h>
#include "TROOT.h"
#include "TH1F.h"
#include "TFile.h"
#include "TTree.h"
#include "TCanvas.h"
#include "TBrowser.h"
#include "TMessage.h"
#include "TApplication.h"

using namespace std;

class MyTMessage : public TMessage {
    MyTMessage(void *buf, Int_t bufsize) : TMessage(buf, bufsize) { }

void serialize(){
    TFile *f = new TFile("2.root");
    TTree *t2 = (TTree*)f->Get("Event");

    TMessage *tm = new TMessage(kMESS_OBJECT);
    cout<<tm->Length()<<endl;    //The length changed here

    //Here I want to deserialize the TTree
    MyTMessage *myTM = new MyTMessage(tm->Buffer(),tm->Length());
    TTree *dtree = (TTree *)myTM->ReadObject(myTM->GetClass());


int main()
    return 0;

But the result is: 
*    Row   * TEvtHeade * TEvtHeade * TEvtHeade * TEvtHeade * TEvtHeade * TEvtHeade * TEvtHeade *
*        0 *         0 *         0 *         0 *         0 *         0 *         0 *         0 *
*        1 *         0 *         0 *         0 *         0 *         0 *         0 *         0 *
*        2 *         0 *         0 *         0 *         0 *         0 *         0 *         0 *
*        3 *         0 *         0 *         0 *         0 *         0 *         0 *         0 *
*        4 *         0 *         0 *         0 *         0 *         0 *         0 *         0 *
*        5 *         0 *         0 *         0 *         0 *         0 *         0 *         0 *

Everything is 0. But the meta data of the TTree is right,Why?

That’s because the TTree object you pass through the message is only the header. Its data is in separate chunks; @pcanal might have an idea how to transfer those, too.


Hi, Axel !!Thank you for replying!!! Did you mean pcanal can solve this problem? How can I ask him about this problem? I‘ve been troubled by this problem for a long time.


@pcanal got notified by mentioning him - he’ll reply when he finds the time.


There are a few ways to implement this, however they really depends on the circumstances.

Could you remind us what is your overload goal (that you are trying to accomplish by passing a TTree through a TMessage). What is the organization of your processes? What is the reason to pass the TTree? etc…


I want to serialize a TTree and then save the binary value into LevelDB. When I need it I read the binary value from LevelDB and deserialize it into a TTree.
Thank you for replying!!

Hi Wei,

I still do no understand the context and so I am likely to give sub-optimal advice.

What is the advantage of storing a TTree in LevelDB?
What is the purposed of storing a TTree in LevelDB?
What is the semantic of the data stored in this TTree?
When/How will you need to retrieve the data out of the TTree which in the LevelDB?
With which granularity?
What are the performance requirement and access pattern?
What is the total datasize in a TTree?
Does LevelDB support single entries that are that large?

I.e. eventhough you do what you requested it may or may not be the ‘right’ solution to your overall problem?



I’m working on a project as follows:
There are some physical events and they can’t be serialized. So I put them on a TTree and serialize the TTree. When someone wants to find an event on the disk, it’s too slow. So I want to make a cache based on SSD and LevelDB to speed up the process of searching. That’s why I want to save a TTree to Leveldb and when I need an event I read the binary then deserialize it into a TTree to find the event. Now I have the problem in deserializing a binary.

[quote]When someone wants to find an event on the disk, it’s too slow. So I want to make a cache based on SSD[/quote]What about simply storing the file(s) on the SSD?

[quote]a cache based on SSD and LevelDB[/quote]The simplest way is to simply store all the bytes from the file into the database and then retrieve those bytes and attach them to a TMemFile.

[quote]to speed up the process of searching. [/quote]But this implies, that you actually need to go through the data to do some other of selection. As you described it, the solution you are pursuing is unlikely to be the most performance as it requires, dowload the TTree meta and data and all its data, then select base on that (the non-performant part is 'download all the data, most of which your probably don’t need).

An alternative (is LevelDB has an sql interface) would be to store a (partial) copy of the TTree natively in the database by using TTreeSQL (i.e. have a column per branch).


1 Like

Thank you very much!!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.