# Seg Fault when Reading Multiple Trees

**URL:** <https://root-forum.cern.ch/t/seg-fault-when-reading-multiple-trees/14739>\
**Category:** ROOT\
**Created:** [August 9, 2012, 7:21pm UTC](https://root-forum.cern.ch/t/seg-fault-when-reading-multiple-trees/14739 "2012-08-09T19:21:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jfcaron](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/7c8e57/32.png) [@jfcaron](https://root-forum.cern.ch/u/jfcaron)\
**Post date:** [August 9, 2012, 7:21pm UTC](https://root-forum.cern.ch/t/seg-fault-when-reading-multiple-trees/14739/1 "2012-08-09T19:21:22Z")

</div>

I am having trouble with the code pasted below. The code is a stripped-down version of the real analysis code that I am trying to develop. I run the code with “+” to compile it, and I pass as the argument a directory which contains C1.root, C2.root, C3.root files which have identical structure and the same number of entries each (I checked). The structure is that CN.root has a tree called CN, and this tree has a single TVectorD branch called “ampl\_tv”.

[code]#define debug 1  
#include “TVectorD.h”  
#include “TFile.h”  
#include “TTree.h”  
#include   
#include “TString.h”  
#include “TDirectory.h”

using namespace std;

void basic\_analysis(TString dir\_to\_plot)  
{  
// Open files  
TFile f1(dir\_to\_plot+"/C1.root");  
TFile f3(dir\_to\_plot+"/C3.root");  
TFile outfile(dir\_to\_plot+"/basic\_analysed.root",“RECREATE”);

// Get trees and set addresses for the relevant branch.  
[f1.cd](http://f1.cd)();  
TTree\* C1 = (TTree\*)gDirectory-\>Get(“C1”);  
TVectorD \*C1\_tv=0;  
C1-\>SetBranchAddress(“ampl\_tv”,&C1\_tv);

[f3.cd](http://f3.cd)();  
TTree\* C3 = (TTree\*)gDirectory-\>Get(“C3”);  
TVectorD \*C3\_tv;  
C3-\>SetBranchAddress(“ampl\_tv”,&C3\_tv);

// Now we loop over all the entries  
Int\_t n\_entries = C1-\>GetEntries();  
for(Int\_t i=0; i \< n\_entries; i++ )  
{  
if(debug == 1){cout \<\< “Reading C1.” \<\< endl;}  
C1-\>GetEntry(i);  
if(debug == 1){cout \<\< "Reading C3. Entry " \<\< i \<\< " Total entries: " \<\< C3-\>GetEntries() \<\< endl;}  
C3-\>GetEntry(i);  
if(debug == 1){cout \<\< “Done reading C3.” \<\< endl;}  
} // Ends loop over n\_entries

if(debug==1){cout \<\< “Done looping over n\_entries” \<\< endl;}

f1.Close();  
f3.Close();

[outfile.cd](http://outfile.cd)();  
gDirectory-\>Write();  
outfile.Close();  
}  
[/code]

My goal is to open the C1, C2, C3 files and do some processing, then write the processed variables to a new tree in a new output file. When I run the code however I get a segmentation fault, with the following printed lines from the cout statements:

[quote]root -l -b -q ‘basic\_analysis.C+("/Users/jfcaron/Projects/TRIUMFBeamTesrter/Run0012")’

- ROOT v5.31/01 \*

root [0]  
Processing basic\_analysis.C+("/Users/jfcaron/Projects/TRIUMFBeamTest/Converter/Run0012")…  
Info in TUnixSystem::ACLiC: creating shared library /Users/jfcaron/Projects/TRIUMFBeamTest/standard\_analysis/./basic\_analysis\_C.so  
Reading C1.  
Reading C3. Entry 0 Total entries: 1010

\*\*\* Break \*\*\* segmentation violation  
[/quote]

Surprisingly the problem only occurs on the C3-\>GetEntry(i) line on event 0, but from the interpreter I can easily Get() this entry from this tree. If I comment out the C3 parts and only run C1, it works fine. If I comment out the C3 parts and add C2 parts (along with C1), it also works fine. I manually checked that C3 is not special or corrupted, this occurs for multiple collections of CN.root files, always on C3.

I would appreciate appreciate ideas, explanations or solutions, thanks,  
Jean-François

---

<div class="post-metadata">

**Author:** ![Pepe\_Le\_Pew](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pepe_le_pew/32/12365_2.png) [@Pepe\_Le\_Pew](https://root-forum.cern.ch/u/Pepe_Le_Pew)\
**Post date:** [August 9, 2012, 7:34pm UTC](https://root-forum.cern.ch/t/seg-fault-when-reading-multiple-trees/14739/2 "2012-08-09T19:34:17Z")

</div>

TVectorD \*C3\_tv = 0;

---

<div class="post-metadata">

**Author:** ![jfcaron](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/7c8e57/32.png) [@jfcaron](https://root-forum.cern.ch/u/jfcaron)\
**Post date:** [August 9, 2012, 7:46pm UTC](https://root-forum.cern.ch/t/seg-fault-when-reading-multiple-trees/14739/3 "2012-08-09T19:46:58Z")

</div>

Thanks! I guess I thought I copied & pasted that code, so I didn’t notice that missing detail. The full analysis code now works properly.
