SegFault for TTree GetEvent or SetBranchAddress

Hi everyone,

I’m trying to make my macro script into a standalone program and possibly use it on the grid. The problem here is that I kept on getting a seg fault for the compiled program. Here is how it happened:

The program runs fine with SetBranchAddress function alone and the GetEvent section commented out:
int cppTest2();

#[code]
TFile f("/scruser/3/yjf/group10.perf-jets.data10_7TeV.00155228.physics_L1Calo.recon.ESD.f259_JetEtMissDPDModifier000025.v1.EXT0._00001.qcd.root");
TTree* qcd = (TTree*)f.Get(“qcd”);
Int_t nEntriesNtuple = (Int_t)qcd->GetEntries();
std::cout << "Number of entries in the n-tuple is " << nEntriesNtuple << std::endl;

// Declare variables to plot, remember to check the class of the variables
std::vector<Float_t>* m_phi;

//NOTICE THE COMMENTED OUT SECTION
qcd->SetBranchAddress(“jet_AntiKt4H1TopoNew_phi”, &m_phi);
for(Long64_t k=0; k<nEntriesNtuple; ++k) {
// qcd->GetEvent(k);
}
return 0;
}[/code]

or alternatively if I comment out the SetBranchAddress function and uncomment the GetEvent function.

//NOTICE THE COMMENTED OUT SECTION //qcd->SetBranchAddress("jet_AntiKt4H1TopoNew_phi", &m_phi); for(Long64_t k=0; k<nEntriesNtuple; ++k) { qcd->GetEvent(k);

But not when both codes are present, i.e.

[b]qcd->SetBranchAddress("jet_AntiKt4H1TopoNew_phi", &m_phi);[/b] for(Long64_t k=0; k<nEntriesNtuple; ++k) { [b]qcd->GetEvent(k); [/b]

I don’t understand what’s going on…can someone please help me? Thanks!

This worked fine in a macro script in interactive ROOT…

Hi,

you MUST initialize the pointer you pass to SetBranchAddress! ROOT cannot know whether it points to a std::vector<Float_t> or not if its value is != 0! So please do

// Declare variables to plot, remember to check the class of the variables
  std::vector<Float_t>* m_phi = 0;

Axel.

[quote=“Axel”]Hi,

you MUST initialize the pointer you pass to SetBranchAddress! ROOT cannot know whether it points to a std::vector<Float_t> or not if its value is != 0! So please do

// Declare variables to plot, remember to check the class of the variables
  std::vector<Float_t>* m_phi = 0;

Axel.[/quote]

Thank you Axel! It worked! But I’m wondering why no initialization worked in an interactive ROOT session?

Hi,

CINT does it for you.

Cheers, Axel.