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 {
public:
    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;
    tm->WriteObject(t2);
    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());
    dtree->Scan();

}

int main()
{
    serialize();
    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.

Axel

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.

Wei

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

Hi,

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ā€¦

Thanks,
Philippe

Hiļ¼
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!!
Wei

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?

Cheers,
Philippe.

HIļ¼ŒPhilippe!!

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).

Cheers,
Philippe.

1 Like

Thank you very much!!

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