Write a root object using an inherited class

Hi,

I have a question which might be the result from my C++ limitations…

I designed a C++ class which inherits from TH2D:

Then, when I’m using this class, I’d like to save the histogram in a file with the traditional Write() method:

MyClass *a = new MyClass(); a->Write();

Something is saved but this is not a TH2D object.
Any idea how I should do this?
Thank you.

Hi adrian,

if you want to persist an object of a custom class you have to create a dictionary for it root.cern.ch/how/how-create-dictionary .

Cheers,
Danilo

Hi,

I did generate a dictionary (using rootcling though). However, a dictionary is only useful for the interpreter, right?

I’m not using the interpreter but a compiled code using my custom derived class. When calling the Write() function, the MyClass object is saved rather than the TH2D object from which it inherits.

How can I save the native TH2D object?

Thanks.

Hi Adrian,

the dictionaries for ROOT6 are used for I/O only: interactivity at the prompt (i.e. interpretation), unlike in the ROOT5 case, can happen without dictionaries.
The fact that you perform I/O operations from the prompt, a macro or compiled code is identical for ROOT. The dictionary has to be available in form of a loaded library.
The moment you ask ROOT to write a MyClass instance, ROOT does so. I see two possibilities:

  1. Change the design of your class and encapsulate the TH2D rather than inheriting from it. You can have a Write method which just forwards to the TH2D::Write method.
  2. Write a custom streamer for your class

I would recommend solution 1).

Cheers,
Danilo