# Reading in custom class stored in tree

**URL:** <https://root-forum.cern.ch/t/reading-in-custom-class-stored-in-tree/15767>\
**Category:** ROOT\
**Created:** [March 6, 2013, 12:28am UTC](https://root-forum.cern.ch/t/reading-in-custom-class-stored-in-tree/15767 "2013-03-06T00:28:20Z")\
**Posts on this page:** 5\
**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:** [March 6, 2013, 12:28am UTC](https://root-forum.cern.ch/t/reading-in-custom-class-stored-in-tree/15767/1 "2013-03-06T00:28:20Z")

</div>

Hello,

I have made a stand alone executable that writes an event class, that I wrote, to a tree. When writing the class to the tree it has no problem. I can open the root file and see that the information was stored correctly. However, When I try to read the event from the tree it seg faults (that is again in a stand alone executable).

I have tried with and without creating a shared object with no success.  
Below is the back trace of the error. It happens when I call tree-\>GetEntry(0);

I have followed [https://root-forum.cern.ch/t/simple-way-to-create-and-merge-shared-libraries/11652/1  
 ](https://root-forum.cern.ch/t/simple-way-to-create-and-merge-shared-libraries/11652/1) in making the associated dictionary’s and linking everything together.  
Below below is my make file

Thanks in advance

`#0 0x080633b3 in std::__fill_n_a<double*, unsigned int, double> (__first=0x0, __n=3,__ value=@0xbfffc8d8) at /usr/include/c++/4.4/bits/stl_algobase.h:758
#1 0x08063363 in std::fill_n<double*, unsigned int, double> ( __first=0x0,__ n=3, __value=@0xbfffc8d8) at /usr/include/c++/4.4/bits/stl_algobase.h:791
#2 0x080632cc in std::__uninitialized_fill_n<true>::uninitialized_fill_n<double*, unsigned int, double> (__first=0x0, __n=3,__ x=@0xbfffc8d8) at /usr/include/c++/4.4/bits/stl_uninitialized.h:204
#3 0x08063223 in std::uninitialized_fill_n<double*, unsigned int, double> ( __first=0x0,__ n=3, __x=@0xbfffc8d8) at /usr/include/c++/4.4/bits/stl_uninitialized.h:223
#4 0x080630d6 in std::__uninitialized_fill_n_a<double*, unsigned int, double, double> (__first=0x0, __n=3,__ x=@0xbfffc8d8) at /usr/include/c++/4.4/bits/stl_uninitialized.h:318
#5 0x08062d71 in std::vector<double, std::allocator<double> >::_M_fill_insert (this=0x8453f40, __position=...,__ n=3, __x=@0xbfffc948) at /usr/include/c++/4.4/bits/vector.tcc:397
#6 0x080628be in std::vector<double, std::allocator<double> >::insert (this=0x8453f40, __position=...,__ n=3, __x=@0xbfffc948) at /usr/include/c++/4.4/bits/stl_vector.h:851
#7 0x0806243a in std::vector<double, std::allocator<double> >::resize (this=0x8453f40, __new_size=3,__ x=0) at /usr/include/c++/4.4/bits/stl_vector.h:557
#8 0x0806e27a in ROOT::TCollectionProxyInfo::Pushback<std::vector<double, std::allocator<double> > >::resize(void*) ()
#9 0x00f17ac3 in TGenCollectionStreamer::ReadPrimitives(int, TBuffer&) () from /soft/intel/lucid/root/5.26.00/lib/root/libRIO.so.5.26
#10 0x00f1ae03 in TGenCollectionStreamer::Streamer(TBuffer&) () from /soft/intel/lucid/root/5.26.00/lib/root/libRIO.so.5.26
#11 0x00ef35c7 in TCollectionStreamer::Streamer(TBuffer&, void*, int, TClass*) () from /soft/intel/lucid/root/5.26.00/lib/root/libRIO.so.5.26
#12 0x00ef4076 in TCollectionClassStreamer::operator()(TBuffer&, void*) () from /soft/intel/lucid/root/5.26.00/lib/root/libRIO.so.5.26
#13 0x0031a016 in TClass::Streamer(void*, TBuffer&, TClass const*) const () from /soft/intel/lucid/root/5.26.00/lib/root/libCore.so.5.26
#14 0x00eef1d7 in TBufferFile::ReadFastArray(void*, TClass const*, int, TMemberStreamer*, TClass const*) () from /soft/intel/lucid/root/5.26.00/lib/root/libRIO.so.5.26
#15 0x00fa9d54 in int TStreamerInfo::ReadBuffer<char**>(TBuffer&, char** const&, int, int, int, int) () from /soft/intel/lucid/root/5.26.00/lib/root/libRIO.so.5.26
#16 0x019b0f22 in TBranchElement::ReadLeaves(TBuffer&) () from /soft/intel/lucid/root/5.26.00/lib/root/libTree.so.5.26
#17 0x019a1442 in TBranch::GetEntry(long long, int) () from /soft/intel/lucid/root/5.26.00/lib/root/libTree.so.5.26
#18 0x019a7be2 in TBranchElement::GetEntry(long long, int) () from /soft/intel/lucid/root/5.26.00/lib/root/libTree.so.5.26
#19 0x019a7b75 in TBranchElement::GetEntry(long long, int) () from /soft/intel/lucid/root/5.26.00/lib/root/libTree.so.5.26
#20 0x019f13b4 in TTree::GetEntry(long long, int) () from /soft/intel/lucid/root/5.26.00/lib/root/libTree.so.5.26
#21 0x019be0bc in TChain::GetEntry(long long, int) () from /soft/intel/lucid/root/5.26.00/lib/root/libTree.so.5.26
#22 0x080739ab in main (argc=4, argv=0xbffff474) at NeutronBuilder.C:225`

[code]CXX=$(shell root-config --cxx)  
CFLAGS=-c -g -Wall $(shell root-config --cflags) -I./src -I ./include  
LDLIBS=$(shell root-config --glibs)  
LDFLAGS=$(shell root-config --ldflags)  
#SOURCES=./src/SL\_Event.cc ./src/FileManager.cc ./src/Filter.cc  
SOURCES=$(shell ls ./src/_.cc)  
TEMP=$(shell ls ./src/_.cc~)  
TEMP2=$(shell ls ./include/\*.hh~)  
OBJECTS=$(SOURCES:.cc=.o)  
MAIN=NeutronBuilder.C  
MAINO=./src/NeutronBuilder.o

EXECUTABLE=NeutronBuilder

INCLUDEPATH=include  
SRCPATH=src  
ROOTCINT=rootcint  
DICTNAME = SL\_Event  
DICTOBJ = $(addsuffix Dictionary.o, $(DICTNAME))

LIBNAME =SL\_Event  
LIBOBJ = $(addsuffix [Lib.so](http://Lib.so), $(LIBNAME))

.PHONY: clean get put all test sclean

all: $(EXECUTABLE)

$(EXECUTABLE) : $(OBJECTS) $(MAINO) $(LIBOBJ) $(DICTOBJ)  
@echo “Building target” $@ “…”  
$(CXX) $(LDFLAGS) -o $@ $(OBJECTS)$(DICTOBJ) $(MAINO) $(LDLIBS)  
@echo  
@echo “Build succeed”

.cc.o:  
@echo “Compiling” $\< “…”  
@$(CXX) $(CFLAGS) $\< -o $@

$(MAINO): $(MAIN)  
@echo “Compiling” $\< “…”  
@$(CXX) $(CFLAGS) $\< -o $@

%Dictionary.o: include/%.hh src/%LinkDef.h  
$(ROOTCINT) -f $(patsubst %.o,%.cc,$@) -c $^;  
$(CXX) -p -fPIC $(CFLAGS) -c $(patsubst %.o,%.cc,$@) $(patsubst %.o,%.h,$@)

%[Lib.so](http://Lib.so): %Dictionary.o src/%.o  
$(CXX) `root-config --cflags` -fPIC -shared -Wl -o $@ $^;  
[/code]

---

<div class="post-metadata">

**Author:** ![MicahFolsom](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/91b2a8/32.png) [@MicahFolsom](https://root-forum.cern.ch/u/MicahFolsom)\
**Post date:** [March 6, 2013, 10:06pm UTC](https://root-forum.cern.ch/t/reading-in-custom-class-stored-in-tree/15767/2 "2013-03-06T22:06:44Z")

</div>

Can you please post the code demonstrating how you’re reading the event out (from the TFile step to GetEntry()), and your class declaration? I assume you’re doing it correctly, but just to be safe!

---

<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:** [March 7, 2013, 2:54pm UTC](https://root-forum.cern.ch/t/reading-in-custom-class-stored-in-tree/15767/3 "2013-03-07T14:54:52Z")

</div>

I found my problem. I had been reading the tree with a Tchain like

```auto

```

That had worked when the tree in “file” had only simple type branches.  
Switching to

```auto
TFile *inFile =new TFile("file");
TTree *inT = (TTree*) inFile->Get("nameOfTree");
```

Fixed the problem.

Thank you for the reply.

---

<div class="post-metadata">

**Author:** ![fedino](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/f/82dd89/32.png) [@fedino](https://root-forum.cern.ch/u/fedino)\
**Post date:** [June 5, 2013, 4:41pm UTC](https://root-forum.cern.ch/t/reading-in-custom-class-stored-in-tree/15767/4 "2013-06-05T16:41:27Z")

</div>

Hi all,

I had the same problem recently.  
I solved it by using:

```auto

```

and this let me still use TChain’s.  
Could please any expert comment?

thanks,  
federico

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [July 26, 2013, 8:07pm UTC](https://root-forum.cern.ch/t/reading-in-custom-class-stored-in-tree/15767/5 "2013-07-26T20:07:23Z")

</div>

Hi,

The two problem mentioned here seems unrelated. For both of them I would need more input (more information about the original code) to decipher what might be wrong.

Philippe.
