How to store custom class with pointer private members to ROOT File and read them back

Hi all,
I am trying to figure out an easy way to store a class which has other custom classes as private member to a Root file.
What I would like to achieve is to be able to store the status of those classes in different root files and grab them in a second step.
To give a bit of context:
I have
Class MyClass: public TObject{
private:
Some TString and
Ttree * m_tree ;
RooAbsPdf * pdf ;
ClassDef(MyClass, 1);
}
I am wonder if it is possible to store this kind of object to a root file and how one has to proceed to grab the tuple and the pdf when reading the root file.
Also an example of some complicated project is welcome.
Thanks in advance
Renato

Hi Renato,

it is possible to save complex objects in ROOT but what you propose is rather unusual and probably not the way to go. The way in which RooFit pdfs are persistified is via a RooWorkspaces. TTrees can span several files.
I would rather save the TTree and the workspace in a file to then use them to resume the status of your class. You can hide complexity and make the write and read operations one liners implementing a Read and Write method for your class.

Best,
Danilo

Hi Danilo,
The reason why I asked this is because I wanted to avoid to use rooworkspace and store things spread around different files.
Actually what it could work is to store all private members of the class and in the same file the ttree and the pdf. But in this case since my class has all of them as private members I am not 100% sure how to rewrite the class and if I can still use it.
Notably I can already store the class to a file and read it back but this is valid only if the pointers are not filled.
As soon as ttree * and the rooabspdf* are not null I cannot store it and I get a bunch of errors.
Do you have some suggestions?

Hi,

The reason why I asked this is because I wanted to avoid to use rooworkspace and store things spread around different files.

You can use a single file, you don’t need many. Please note that a TTree is intrinsically bound to a file, it flushes itself automatically on files while being filled, treating it as a data member is a bad idea which will not lead to a successful outcome.

Actually what it could work is to store all private members of the class and in the same file the ttree and the pdf. But in this case since my class has all of them as private members I am not 100% sure how to rewrite the class and if I can still use it.

I do not understand.

Notably I can already store the class to a file and read it back but this is valid only if the pointers are not filled.
As soon as ttree * and the rooabspdf* are not null I cannot store it and I get a bunch of errors.
Do you have some suggestions?

Not use pointers? (the comment about the TTree still holds)

D

Hi Danilo
Thanks for helping me.
I basically have the class with some TString and Doubles as private members and on top of that also the Ttree and the pdf.
I agree with you, I can store the Ttree and the Pdf within a workspace in the same file and read it back but I am not 100 % sure how then restore the other Private members.
What I was saying is that when I do MyClass.SaveAs(“name.root”) with the private members filled but not the ttree* and the pdf* I can restore all private members but this doesn t work when the pointers are full.
For the Ttree i can make it not a pointer while for the 0df it is more tricky since it is an abstract class and it doesn t have a default constructor.
Do you have some suggestions how to store in a single file the ttree the workspace and the set of other private members? Basically in this approach I will have MyClass bound to a single root file and I would be fine with that but it s not clear to me how to store the private members which are not the Ttree and the pdf to the same root file where I will find the tuple and the workspace for the pdf.
Thanks
Renato

Renato,

you cannot store a RooFit pdf as data member: RooFit objects are persistified through the RooWorkspace.
You cannot store a tree as a data member, please store it outside: that is not a normal object.

Cheers,
Danilo

@dpiparo, I guess you are right , but i was wondering if with the \! specification i can still by pass the storage of the pointer to the TTree and let the class have all instruments to re-load it .

Ciao Renato,

I am confused: the //! comment is used for transient data members?

Cheers,
Danilo

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