Root Classes and Inheritance


I am trying to figure out how to make a root class that inherits from another class. I have successfully made my own root class which inherits from TObject, with its own dictionary that I store as a shared object. What I want to do now is have two classes one which inherits from the other but I am unsure exactly what needs to be done. I assume that I need to make dictionaries of both, but what do I need in my linkdef statements for each class? Does each need a ClassDef macro? Does each need to inherit from TObject? For my single class (which is working fine now) my linkdef is below.

#ifdef CINT
#pragma link off all globals;
#pragma link off all classes;
#pragma link off all functions;
#pragma link C++ class vector< vector <Double_t> >+;
#pragma link C++ class vector< vector <UShort_t> >+;
#pragma link C++ class LendaEvent+;

Thanks in advance!

[url]Simple way to create and merge shared libraries?
[url]Adding a class: hand holding requested

[url]Storing 2D vector [--> vector<vector<...> >] into a TTree
[url]Problem in getting the vector < vector <string> > branch

Thank you for the quick reply
I have read the above links, but I am still having trouble with this. Everything compiles fine and I can open root and instatiate my object. It’s members and it’s parents members appear to be all there. I can write the object to file fine as well. The trouble happens when I try to read the object back in from the file. IE

root [0] LendaEvent e
root [1] TFile f(“temp.root”,“recreate”)
root [2] e.Write()
root [3] .q
<hake:Introspective >root temp.root
root [0]
Attaching file temp.root as _file0…

At which points it memory dumps giving this error
*** glibc detected *** /soft/x86_64/lucid/root/5.26.00/bin/root.exe: free(): invalid pointer: 0x00000000025eaec8 **
my linkdef file is
#ifdef CINT
#pragma link off all globals;
#pragma link off all classes;
#pragma link off all functions;
#pragma link C++ class vector< vector <Double_t> >+;
#pragma link C++ class vector< vector <UShort_t> >+;
#pragma link C++ class CorrectionInfo+;
#pragma link C++ class std::vector+;
#pragma link C++ class Introspective+;
#pragma link C++ class LendaEvent+;

If the very first line is:
root [0] LendaEvent e
how is ROOT supposed to know what “LendaEvent” is?
You don’t seem to load dictionaries for your classes (e.g. “.L”), before using them.

Sorry. .L is called in my start up file.

And what do you get when you try:
root [0] TFile *f = TFile::Open(“temp.root”)
root [1] f->ls()

root [0] TFile * f =TFile::Open(“temp.root”)
root [1] f->ls()
TFile** temp.root
TFile* temp.root
KEY: LendaEvent LendaEvent;1
root [2]

Then try:
root [2] LendaEvent *e; f->GetObject(“LendaEvent”, e);
root [3] e

root [0] TFile * f=TFile::Open(“temp.root”) root [1] LendaEvent *e
root [2] f->GetObject(“LendaEvent”,e) *** glibc detected *** /soft/x86_64/lucid/root/5.26.00/bin/root.exe: free(): invalid pointer: 0x00000000023c4548 ***
======= Backtrace: =========
======= Memory map: ========
00400000-00402000 r-xp 00000000 00:2a 5142916 /soft/x86_64/lucid/root/5.26.00/bin/root.exe
00601000-00602000 r–p 00001000 00:2a 5142916 /soft/x86_64/lucid/root/5.26.00/bin/root.exe
00602000-00603000 rw-p 00002000 00:2a 5142916 /soft/x86_64/lucid/root/5.26.00/bin/root.exe
02233000-02b7c000 rw-p 00000000 00:00 0 [heap]
7f6440000000-7f6440021000 rw-p 00000000 00:00 0
7f6440021000-7f6444000000 —p 00000000 00:00 0
7f6447e21000-7f6447e45000 r-xp 00000000 00:2a 26722300 /soft/x86_64/lucid/root/5.26.00/lib/root/
7f6447e45000-7f6448045000 —p 00024000 00:2a 26722300 /soft/x86_64/lucid/root/5.26.00/lib/root/
7f6448045000-7f6448047000 r–p 00024000 00:2a 26722300 /soft/x86_64/lucid/root/5.26.00/lib/root/
7f6448047000-7f6448048000 rw-p 00026000 00:2a 26722300 /soft/x86_64/lucid/root/5.26.00/lib/root/
7f6448048000-7f6448049000 rw-p 00000000 00:00 0
7f6448049000-7f6448205000 r-xp 00000000 00:2a 5081446 /soft/x86_64/lucid/root/5.26.00/lib/root/cint/cint/stl/
7f6448205000-7f6448404000 —p 001bc000 00:2a 5081446 /soft/x86_64/lucid/root/5.26.00/lib/root/cint/cint/stl/
7f6448404000-7f6448405000 r–p 001bb000 00:2a 5081446 /soft/x86_64/lucid/root/5.26.00/lib/root/cint/cint/stl/
7f6448405000-7f644840c000 rw-p 001bc000 00:2a 5081446 /soft/x86_64/lucid/root/5.26.00/lib/root/cint/cint/stl/
7f644840c000-7f6448428000 r-xp 00000000 00:2a 13516424 /soft/x86_64/lucid/root/5.26.00/lib/root/

Well, you could try to create the ROOT file using objects on the heap: LendaEvent *e = new LendaEvent(); TFile *f = TFile::Open("temp.root", "recreate"); e->Write(); delete f; delete e; and if it still dies when you try to “GetObject” afterwards, then you need to carefully inspect (valgrind ?) the “LendaEvent” class, I think.


Note: Save this source code as the “TrialEvent.cxx” file.

// create the ROOT file using objects on the stack
.L TrialEvent.cxx++
TrialEvent e;
TFile f(“temp.root”, “recreate”);

// create the ROOT file using objects on the heap
.L TrialEvent.cxx++
TrialEvent *e = new TrialEvent();
TFile *f = TFile::Open(“temp.root”, “recreate”);
delete f;
delete e;

// read the ROOT file
.L TrialEvent.cxx++
TFile *f = TFile::Open(“temp.root”);
TrialEvent *e;
f->GetObject(“TrialEvent”, e);
if (e) std::cout << "e->v = " << e->v << std::endl;
delete f;
delete e;


#include “TObject.h”

class TrialEvent: public TObject
int v;
TrialEvent(int i = -1) { v = i; }
ClassDef(TrialEvent, 1); // TrialEvent class

I have isolated the error but I am unsure how get rid of it. When LendaEvent constructs it builds a map
void LendaEvent::DefineMap(){

theMap (which is between strings and void*) is inherited froms its parent class. Removing this call in the constructor gets rid of the error (which consistently happens when the events are read back in from a file)