# Root Classes and Inheritance

**URL:** <https://root-forum.cern.ch/t/root-classes-and-inheritance/16217>\
**Category:** ROOT\
**Created:** [June 11, 2013, 8:03pm UTC](https://root-forum.cern.ch/t/root-classes-and-inheritance/16217 "2013-06-11T20:03:29Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![soam](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/e0b2c6/32.png) [@soam](https://root-forum.cern.ch/u/soam)\
**Post date:** [June 11, 2013, 8:03pm UTC](https://root-forum.cern.ch/t/root-classes-and-inheritance/16217/1 "2013-06-11T20:03:29Z")

</div>

Hello,

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+;  
#endif

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [June 11, 2013, 8:08pm UTC](https://root-forum.cern.ch/t/root-classes-and-inheritance/16217/2 "2013-06-11T20:08:38Z")

</div>

[url][Simple way to create and merge shared libraries?](https://root-forum.cern.ch/t/simple-way-to-create-and-merge-shared-libraries/11652/6)  
[url][Adding a class: hand holding requested](https://root-forum.cern.ch/t/adding-a-class-hand-holding-requested/13893/3)

[url][Storing 2D vector [--\> vector\<vector\<...\> \>] into a TTree](https://root-forum.cern.ch/t/storing-2d-vector-vector-vector-into-a-ttree/14331/2)  
[url][Problem in getting the vector \< vector \<string\> \> branch](https://root-forum.cern.ch/t/problem-in-getting-the-vector-vector-string-branch/14608/4)

---

<div class="post-metadata">

**Author:** ![soam](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/e0b2c6/32.png) [@soam](https://root-forum.cern.ch/u/soam)\
**Post date:** [June 12, 2013, 9:39pm UTC](https://root-forum.cern.ch/t/root-classes-and-inheritance/16217/3 "2013-06-12T21:39:38Z")

</div>

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  
root [0] LendaEvent e  
root [1] TFile f(“temp.root”,“recreate”)  
root [2] e.Write()  
(Int\_t)329  
root [3] .q  
\<hake:Introspective \>root temp.root  
root [0]  
Attaching file temp.root as \_file0…  
gDirectory-\>Get(“LendaEvent”)

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+;  
#endif

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [June 13, 2013, 6:42am UTC](https://root-forum.cern.ch/t/root-classes-and-inheritance/16217/4 "2013-06-13T06:42:17Z")

</div>

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 [SomeLibraryWithDictionaries.so](http://SomeLibraryWithDictionaries.so)”), before using them.

---

<div class="post-metadata">

**Author:** ![soam](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/e0b2c6/32.png) [@soam](https://root-forum.cern.ch/u/soam)\
**Post date:** [June 13, 2013, 1:08pm UTC](https://root-forum.cern.ch/t/root-classes-and-inheritance/16217/5 "2013-06-13T13:08:00Z")

</div>

Sorry. .L [libLendaEvent.so](http://libLendaEvent.so) is called in my start up file.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [June 13, 2013, 1:14pm UTC](https://root-forum.cern.ch/t/root-classes-and-inheritance/16217/6 "2013-06-13T13:14:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![soam](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/e0b2c6/32.png) [@soam](https://root-forum.cern.ch/u/soam)\
**Post date:** [June 13, 2013, 1:23pm UTC](https://root-forum.cern.ch/t/root-classes-and-inheritance/16217/7 "2013-06-13T13:23:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [June 13, 2013, 1:28pm UTC](https://root-forum.cern.ch/t/root-classes-and-inheritance/16217/8 "2013-06-13T13:28:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![soam](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/e0b2c6/32.png) [@soam](https://root-forum.cern.ch/u/soam)\
**Post date:** [June 13, 2013, 1:32pm UTC](https://root-forum.cern.ch/t/root-classes-and-inheritance/16217/9 "2013-06-13T13:32:17Z")

</div>

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: =========  
/lib/libc.so.6(+0x78bb6)[0x7f6449a77bb6]  
/lib/libc.so.6(cfree+0x73)[0x7f6449a7e483]  
/soft/x86\_64/lucid/root/5.26.00/lib/root/libRIO.so(\_ZN19TGenCollectionProxy5ClearEPKc+0xc6)[0x7f64489817a6]  
/soft/x86\_64/lucid/root/5.26.00/lib/root/libRIO.so(\_ZN22TGenCollectionStreamer8StreamerER7TBuffer+0x106)[0x7f6448989326]  
/soft/x86\_64/lucid/root/5.26.00/lib/root/libRIO.so(\_ZN19TCollectionStreamer8StreamerER7TBufferPviP6TClass+0x56)[0x7f64489660b6]  
/soft/x86\_64/lucid/root/5.26.00/lib/root/libRIO.so(_ZN11TBufferFile13ReadFastArrayEPvPK6TClassiP15TMemberStreamerS3_+0xc7)[0x7f6448962ef7]  
/soft/x86\_64/lucid/root/5.26.00/lib/root/libRIO.so(\_ZN13TStreamerInfo10ReadBufferIPPcEEiR7TBufferRKT\_iiii+0x5585)[0x7f6448a0da95]  
/soft/x86\_64/lucid/root/5.26.00/lib/root/libRIO.so(_ZN11TBufferFile15ReadClassBufferEPK6TClassPvS2_+0xe1)[0x7f64489600c1]  
/user/lipschut/Introspective/libLendaEvent-m64.so(\_ZN13Introspective8StreamerER7TBuffer+0x57)[0x7f6448d08987]  
/user/lipschut/Introspective/libLendaEvent-m64.so(\_ZN13Introspective16StreamerNVirtualER7TBuffer+0x29)[0x7f6448d1f429]  
/user/lipschut/Introspective/libLendaEvent-m64.so(+0x5ae4c)[0x7f6448d0ee4c]  
/soft/x86\_64/lucid/root/5.26.00/lib/root/libCint.so.5.26(\_ZN4Cint11G\_\_CallFunc7ExecuteEPv+0xcf)[0x7f644b0004af]  
/soft/x86\_64/lucid/root/5.26.00/lib/root/libCore.so.5.26(_ZNK5TCint13CallFunc\_ExecEPvS0_+0xc)[0x7f644bb8ed2c]  
/soft/x86\_64/lucid/root/5.26.00/lib/root/libCore.so.5.26(\_ZN11TMethodCall7ExecuteEPv+0xfb)[0x7f644bb8031b]  
/soft/x86\_64/lucid/root/5.26.00/lib/root/libCore.so.5.26(\_ZN13TStreamerBase10ReadBufferER7TBufferPc+0x94)[0x7f644bb87514]  
/soft/x86\_64/lucid/root/5.26.00/lib/root/libRIO.so(\_ZN13TStreamerInfo10ReadBufferIPPcEEiR7TBufferRKT\_iiii+0x45aa)[0x7f6448a0caba]  
/soft/x86\_64/lucid/root/5.26.00/lib/root/libRIO.so(_ZN11TBufferFile15ReadClassBufferEPK6TClassPvS2_+0xe1)[0x7f64489600c1]  
/user/lipschut/Introspective/libLendaEvent-m64.so(\_ZN10LendaEvent8StreamerER7TBuffer+0x57)[0x7f6448d08dc5]  
/soft/x86\_64/lucid/root/5.26.00/lib/root/libRIO.so(\_ZN4TKey13ReadObjectAnyEPK6TClass+0x33b)[0x7f6448989f2b]  
/soft/x86\_64/lucid/root/5.26.00/lib/root/libRIO.so(\_ZN14TDirectoryFile16GetObjectCheckedEPKcPK6TClass+0x1f4)[0x7f644896e1a4]  
/soft/x86\_64/lucid/root/5.26.00/lib/root/libCore.so.5.26(+0x1bd533)[0x7f644bac8533]  
/soft/x86\_64/lucid/root/5.26.00/lib/root/libCint.so.5.26(\_ZN4Cint19G\_\_ExceptionWrapperEPFiP8G\_\_valuePKcP8G\_\_paramiES1\_PcS5\_i+0x3e)[0x7f644afd1e1e]  
/soft/x86\_64/lucid/root/5.26.00/lib/root/libCint.so.5.26(G\_\_execute\_call+0x61)[0x7f644b070a31]  
/soft/x86\_64/lucid/root/5.26.00/lib/root/libCint.so.5.26(G\_\_call\_cppfunc+0x282)[0x7f644b072222]  
/soft/x86\_64/lucid/root/5.26.00/lib/root/libCint.so.5.26(G\_\_interpret\_func+0x1cb8)[0x7f644b0501e8]  
/soft/x86\_64/lucid/root/5.26.00/lib/root/libCint.so.5.26(G\_\_getfunction+0x1959)[0x7f644b03fbb9]  
/soft/x86\_64/lucid/root/5.26.00/lib/root/libCint.so.5.26(G\_\_getstructmem+0x6ff)[0x7f644b126d0f]  
/soft/x86\_64/lucid/root/5.26.00/lib/root/libCint.so.5.26(G\_\_getvariable+0x9b7)[0x7f644b11c7e7]  
/soft/x86\_64/lucid/root/5.26.00/lib/root/libCint.so.5.26(G\_\_getitem+0x81)[0x7f644b01bdc1]  
/soft/x86\_64/lucid/root/5.26.00/lib/root/libCint.so.5.26(G\_\_getexpr+0x4d69)[0x7f644b021e49]  
/soft/x86\_64/lucid/root/5.26.00/lib/root/libCint.so.5.26(G\_\_exec\_statement+0x62ea)[0x7f644b09e4ca]  
/soft/x86\_64/lucid/root/5.26.00/lib/root/libCint.so.5.26(+0x88b66)[0x7f644b009b66]  
/soft/x86\_64/lucid/root/5.26.00/lib/root/libCint.so.5.26(G\_\_exec\_tempfile\_fp+0xe)[0x7f644b009e1e]  
/soft/x86\_64/lucid/root/5.26.00/lib/root/libCint.so.5.26(G\_\_process\_cmd+0x15c3)[0x7f644b0a9b33]  
/soft/x86\_64/lucid/root/5.26.00/lib/root/libCore.so.5.26(\_ZN5TCint11ProcessLineEPKcPN12TInterpreter10EErrorCodeE+0x406)[0x7f644bb94cd6]  
/soft/x86\_64/lucid/root/5.26.00/lib/root/libCore.so.5.26(\_ZN12TApplication11ProcessLineEPKcbPi+0x301)[0x7f644bacb971]  
/soft/x86\_64/lucid/root/5.26.00/lib/root/libRint.so.5.26(\_ZN5TRint15HandleTermInputEv+0x1a8)[0x7f644a966e58]  
/soft/x86\_64/lucid/root/5.26.00/lib/root/libCore.so.5.26(\_ZN11TUnixSystem16CheckDescriptorsEv+0x14e)[0x7f644bba6cae]  
/soft/x86\_64/lucid/root/5.26.00/lib/root/libCore.so.5.26(\_ZN11TUnixSystem16DispatchOneEventEb+0xd1)[0x7f644bba6fa1]  
/soft/x86\_64/lucid/root/5.26.00/lib/root/libCore.so.5.26(\_ZN7TSystem9InnerLoopEv+0x16)[0x7f644bb26536]  
/soft/x86\_64/lucid/root/5.26.00/lib/root/libCore.so.5.26(\_ZN7TSystem3RunEv+0x7b)[0x7f644bb2874b]  
/soft/x86\_64/lucid/root/5.26.00/lib/root/libCore.so.5.26(\_ZN12TApplication3RunEb+0x1f)[0x7f644bac9dcf]  
/soft/x86\_64/lucid/root/5.26.00/lib/root/libRint.so.5.26(\_ZN5TRint3RunEb+0x189)[0x7f644a967979]  
/soft/x86\_64/lucid/root/5.26.00/bin/root.exe(main+0x4c)[0x4011fc]  
/lib/libc.so.6(\_\_libc\_start\_main+0xfd)[0x7f6449a1dc4d]  
/soft/x86\_64/lucid/root/5.26.00/bin/root.exe[0x4010c9]  
======= 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/libvectorDict.so.5.26  
7f6447e45000-7f6448045000 —p 00024000 00:2a 26722300 /soft/x86\_64/lucid/root/5.26.00/lib/root/libvectorDict.so.5.26  
7f6448045000-7f6448047000 r–p 00024000 00:2a 26722300 /soft/x86\_64/lucid/root/5.26.00/lib/root/libvectorDict.so.5.26  
7f6448047000-7f6448048000 rw-p 00026000 00:2a 26722300 /soft/x86\_64/lucid/root/5.26.00/lib/root/libvectorDict.so.5.26  
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/vector.so.5.26  
7f6448205000-7f6448404000 —p 001bc000 00:2a 5081446 /soft/x86\_64/lucid/root/5.26.00/lib/root/cint/cint/stl/vector.so.5.26  
7f6448404000-7f6448405000 r–p 001bb000 00:2a 5081446 /soft/x86\_64/lucid/root/5.26.00/lib/root/cint/cint/stl/vector.so.5.26  
7f6448405000-7f644840c000 rw-p 001bc000 00:2a 5081446 /soft/x86\_64/lucid/root/5.26.00/lib/root/cint/cint/stl/vector.so.5.26  
7f644840c000-7f6448428000 r-xp 00000000 00:2a 13516424 /soft/x86\_64/lucid/root/5.26.00/lib/root/libmapDict.so.5.26

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [June 13, 2013, 2:45pm UTC](https://root-forum.cern.ch/t/root-classes-and-inheritance/16217/10 "2013-06-13T14:45:45Z")

</div>

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.

[code]/\*

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”);  
e.Write();  
.q

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

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

\*/

#include “TObject.h”

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

---

<div class="post-metadata">

**Author:** ![soam](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/e0b2c6/32.png) [@soam](https://root-forum.cern.ch/u/soam)\
**Post date:** [June 13, 2013, 6:06pm UTC](https://root-forum.cern.ch/t/root-classes-and-inheritance/16217/11 "2013-06-13T18:06:56Z")

</div>

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[“TOF”]=&TOF;  
theMap[“Dt”]=&Dt;  
…  
}  
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)
