Seg Fault when Reading Multiple Trees

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 “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.;
TTree* C1 = (TTree*)gDirectory->Get(“C1”);
TVectorD *C1_tv=0;
TTree* C3 = (TTree*)gDirectory->Get(“C3”);
TVectorD *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;}
if(debug == 1){cout << "Reading C3. Entry " << i << " Total entries: " << C3->GetEntries() << endl;}
if(debug == 1){cout << “Done reading C3.” << endl;}
} // Ends loop over n_entries

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


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/./
Reading C1.
Reading C3. Entry 0 Total entries: 1010

*** Break *** segmentation violation

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,

TVectorD *C3_tv = 0;

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.