Adding custom objects to Trees

Hi all,
A beginners question I guess:
I want to create some custom objects (for the moment just representing rectangles, but in the future they will represent objects in physics events) and add these to a root tree. Once in the root tree I want to use a macro to plot histograms of the different rectangles area (calling the Rectangle::area() method, again this will eventually be more complex).
Am I right in thinking I need to have Rectangle inherit from TObject and then TObject::Write() the object into a tree? And then for the macro add the Rectangle to root/cint? Are there any examples of how to do this? I’ve spent the last 3 days looking for some example code but have yet to find any, perhaps I’m looking in the wrong places :unamused:
Thanks for any and all help
Simon

Hi Simon,
look at http://root.cern.ch/root/CintGenerator.html. Note that there is no reason to have a standalone program, or a Makefile. You can simply create the library (including the dictionary) and load it by running “.L Track.C+” in the above example. But the example shows you what the code should look like.
Axel.

Ok I now have working code adding in objects. (Woo hooo!) I’d like to have these objects in branches of my TTree a la root.cern.ch/root/HowtoWriteTree.html so I’ve copied the code from the example changing the relevant lines, eg:

However when I run the code I get:

Now my program knows about CMyRectangle objects (it makes them and saves them into the Tree), but the Branch method doesn’t know about them. How do I get the Branch method to find my CMyRectangle class?
Cheers
Simon

Ahh posted before I saw your reply :slight_smile: I guess I need to do something similar to solve this problem Axel?
Cheers
Simon

Hi Simon,
yepp, create a dictionary. That will be done automatically if you create your lib with “.L MyRectangle.C+” (or .cxx+,…) and if the header file declaring CMyRectangle has the same name as the source file you’re loading, only with a .h / .hxx / .hpp extension (e.g. MyRectangle.h) . Otherwise you’ll need a linkdef.h; see http://root.cern.ch/root/RootCintMan.html on that.
Axel.

Hello. The link you provided is now defunct - would you be able to provide an updated one? With many thanks!

I think this might be it! Chapter: Addinga Class