tree->SetBranchAddress() modifies tree?

Hi all,

I have a problem regarding tree->SetBranchAddress. It seems that this member function does not only set the address for the tree readout but also overwrites the data address in the tree. I have coded a small sample program which shows the problem.

main.cpp:

[code]#include “TDemo.h”
#include “TDemo2.h”

int main(int argc, char **argv) {
TDemo test;
TDemo2 test2;

test.configure();
test2.configure(test);

test.write();
test2.read();
return 0;

}[/code]

TDemo.h:

[code]#ifndef TDEMO_H
#define TDEMO_H

#include “TTree.h”

#include “UIntVector2D.h”

class TDemo
{
protected:
TTree* outputTree_;
std::string id_;

hat::UIntVector2D intvector2d;

public:
void configure();
void write();

TTree *getTree();

TDemo();    

};

#endif // TDEMO_H[/code]

TDemo.cpp:

[code]#include “TDemo.h”

#include “TFile.h”

#include

void TDemo::configure()
{
outputTree_ = new TTree(id_.c_str(),“Test Tree”);
outputTree_->Branch(“Test”, &intvector2d);
outputTree_->Fill();
}

void TDemo::write()
{
for (int i(0); i<2; i++) {
for (int j(0); j<16384; j++) {
intvector2d[i][j] = 3;
}
}
outputTree_->Branch(“Test”, &intvector2d);
outputTree_->Fill();
}

TTree* TDemo::getTree()
{
return outputTree_;
}

TDemo::TDemo() : id_(“test”), intvector2d(2, std::vector(16384,0))
{
}[/code]

TDemo2.h:

[code]#ifndef TDEMO2_H
#define TDEMO2_H

#include “TTree.h”

#include “UIntVector2D.h”

#include “TDemo.h”

class TDemo2
{
protected:

TTree* inputTree_;
std::string id_;

hat::UIntVector2D *intvector2d;

public:
void configure(TDemo &test);
void read();

TDemo2();

};

#endif // TDEMO2_H[/code]

TDemo2.cpp:

[code]#include “TDemo2.h”

#include

void TDemo2::configure(TDemo& test)
{
inputTree_ = test.getTree();
intvector2d = new hat::UIntVector2D;
inputTree_->SetBranchAddress(“Test”, &intvector2d);
}

void TDemo2::read()
{
inputTree_->GetEntry(inputTree_->GetEntries()-1);

std::cout << "intvector2d2[0][0] " << (*intvector2d)[0][0] << std::endl; // Here it is not accessible any more!!
}

TDemo2::TDemo2()
{
}[/code]

UIntVector2D.h:

[code]#ifndef UINTVECTOR2D_H
#define UINTVECTOR2D_H

#include

namespace hat {

typedef std::vector<std::vector > UIntVector2D;

}

#endif // UINTVECTOR2D_H[/code]

Error:

[code]
*** Break *** segmentation violation

===========================================================
There was a crash (#7 0x007cef7d in SigHandler(ESignals) () from /libs/root_5.28/lib/libCore.so).
This is the entire stack trace of all threads:

#0 0x00221422 in __kernel_vsyscall ()
#1 0x019957d3 in __waitpid_nocancel ()
at …/sysdeps/unix/syscall-template.S:82
#2 0x01936de3 in do_system (line=)
at …/sysdeps/posix/system.c:149
#3 0x0238627d in system (
line=0x89859a8 “/libs/root_5.28/etc/gdb-backtrace.sh 2653 1>&2”)
at pt-system.c:29
#4 0x007c8ccd in TUnixSystem::Exec(char const*) ()
from /libs/root_5.28/lib/libCore.so
#5 0x007cfae5 in TUnixSystem::StackTrace() ()
from /libs/root_5.28/lib/libCore.so
#6 0x007cee6f in TUnixSystem::DispatchSignals(ESignals) ()
from /libs/root_5.28/lib/libCore.so
#7 0x007cef7d in SigHandler(ESignals) () from /libs/root_5.28/lib/libCore.so
#8 0x007c5e62 in sighandler(int) () from /libs/root_5.28/lib/libCore.so
#9
#10 0x0805315e in std::vector<unsigned int, std::allocator >::operator[] (this=0x0, __n=0) at /usr/include/c++/4.4/bits/stl_vector.h:611
#11 0x08052f34 in TDemo2::read (this=0xbfe4ae00)
at /home/adonai/projects/TreeDemo/TDemo2.cpp:43
#12 0x0805d4c7 in main (argc=1, argv=0xbfe4aed4)
at /home/adonai/projects/TreeDemo/main.cpp:14

The lines below might hint at the cause of the crash.
If they do not help you then please submit a bug report at
http://root.cern.ch/bugs. Please post the ENTIRE stack trace
from above as an attachment in addition to anything else
that might help us fixing this issue.

#10 0x0805315e in std::vector<unsigned int, std::allocator >::operator[] (this=0x0, __n=0) at /usr/include/c++/4.4/bits/stl_vector.h:611
#11 0x08052f34 in TDemo2::read (this=0xbfe4ae00)
at /home/adonai/projects/TreeDemo/TDemo2.cpp:43
#12 0x0805d4c7 in main (argc=1, argv=0xbfe4aed4)
at /home/adonai/projects/TreeDemo/main.cpp:14
===========================================================[/code]

The structure of the program is like that because I like to have multiple objects which are configured once, using the member function configure(), and afterwards only the other member functions are used. To be able to declare the structure of the tree which is generated by a class from which the data is read I have to tell the configure function in which data address it should read the data. But by doing this the address is overwritten and I cannot access the data.

Is there a different way to realize the functionality I need?

Any information is very welcome.

BR

I have managed to simplify the source code and the problem I have. See Simultaneous writing and reading of a TTree .