Deriving from TTree

Hi all,

I’ve written a small test class that derives from TTree (in file TTest.cpp):

[code]#include <TTree.h>

class TTest : public TTree
TTest(const char* ="", const char* ="");
void Init();



TTest::TTest(const char* name, const char* title)
: TTree(name,title)
// empty body

void TTest::Init()
double x1=0.0;
double x2=0.0;

Branch(“x2”,&x2,“x2/D”); // line 26


When I want to test this class, I get the following segmentation fault:

[code]root [0] .L TTest.cpp+
Info in TUnixSystem::ACLiC: creating shared library /home/pieter/./
root [1] TTest *test = new TTest(“test”,“testing”)
root [2] test->Init()
root [3] test->Scan()

  • Row * x1 * x2 *

  •    0 *         0 *         0 *

*** Break *** segmentation violation
Using host libthread_db library “/lib64/”.
Attaching to program: /proc/17779/exe, process 17779
[Thread debugging using libthread_db enabled]
[New Thread 47691895893712 (LWP 17779)]
0x00002b60223488e4 in waitpid () from /lib64/
#1 0x00002b60222ed931 in __gxx_personality_v0 () from /lib64/
#2 0x00002b60203f13e9 in TUnixSystem::StackTrace () from /usr/local/lib/root/
#3 0x00002b60203f1077 in TUnixSystem::DispatchSignals () from /usr/local/lib/root/
#5 0x00002b6020c8c554 in G__call_cppfunc () from /usr/local/lib/root/
#6 0x00002b6020c688e9 in G__interpret_func () from /usr/local/lib/root/
#7 0x00002b6020c59b3c in G__getfunction () from /usr/local/lib/root/
#8 0x00002b6020d1cfe8 in G__getstructmem () from /usr/local/lib/root/
#9 0x00002b6020d13dd2 in G__getvariable () from /usr/local/lib/root/
#10 0x00002b6020c2e25f in G__getitem () from /usr/local/lib/root/
#11 0x00002b6020c35aa8 in G__getexpr () from /usr/local/lib/root/
#12 0x00002b6020cb2c13 in G__exec_statement () from /usr/local/lib/root/
#13 0x00002b6020c1b646 in G__exec_tempfile_core () from /usr/local/lib/root/
#14 0x00002b6020c1b8de in G__exec_tempfile_fp () from /usr/local/lib/root/
#15 0x00002b6020cbc1f8 in G__process_cmd () from /usr/local/lib/root/
#16 0x00002b60203c77ff in TCint::ProcessLine () from /usr/local/lib/root/
#17 0x00002b6020336277 in TApplication::ProcessLine () from /usr/local/lib/root/
#18 0x00002b60214e1daf in TRint::HandleTermInput () from /usr/local/lib/root/
#19 0x00002b60214e0677 in TTermInputHandler::Notify () from /usr/local/lib/root/
#20 0x00002b60214e257d in TTermInputHandler::ReadNotify () from /usr/local/lib/root/
#21 0x00002b60203f0273 in TUnixSystem::CheckDescriptors () from /usr/local/lib/root/
#22 0x00002b60203f0a96 in TUnixSystem::DispatchOneEvent () from /usr/local/lib/root/
#23 0x00002b60203877b6 in TSystem::InnerLoop () from /usr/local/lib/root/
#24 0x00002b602038a5f7 in TSystem::Run () from /usr/local/lib/root/
#25 0x00002b6020334d0f in TApplication::Run () from /usr/local/lib/root/
#26 0x00002b60214e21ad in TRint::Run () from /usr/local/lib/root/
#27 0x0000000000400fdd in main ()
The program is running. Quit anyway (and detach it)? (y or n) [answered Y; input not from terminal]
Detaching from program: /proc/17779/exe, process 17779[/code]

When line 26 is commented, it runs fine. Does anyone know what goes wrong? For your info, I’m using ROOT v.5.19/01 on a x86_64 Intel Core2 Duo.

Cheers, Pieter

Well you are telling the TTree to use memory that has gone out of scope:

void TTest::Init()
double x1=0.0;
double x2=0.0;

Branch(“x2”,&x2,“x2/D”); // line 26

}[/code]You either need to move x1 and x2 to be data members of your TTest or you need to call ResetBranchAdresses at the end of the function.


PS. On the first order it looks like you need (are implementing) TNtupleD

Thanks a lot!

My actual implementation will hold different types, so I need a TTree. TTest was just the smallest example that reproduced my problem.

Cheers, Pieter


In addition, you should review whether you should really derive from TTree instead of owning a TTree object (derivation vs containing).