SetBranchAddress Seg Fault

Hi,

I am facing a seg fault when using SetBranchAddress:

TFile file = new TFile("…");
TTree
tree = (TTree*)file->Get(Event/Rec/CalibReadout);

UInt_t eventNum;
tree->SetBranchAddress(“eventNum”, &eventNum);

Int_t nentries = tree->GetEntries();
for (int jentry = 0; jentry < nentries ; jentry++){

 tree->GetEntry(jentry);
 cout << EventNum << endl;

}

Please help. Thanks.

Right after “Get”, try to add: if (!tree) { std::cout << "Error: tree not found." << std::endl; delete file; return; }

[quote=“Wile E. Coyote”]Right after “Get”, try to add: if (!tree) { std::cout << "Error: tree not found." << std::endl; delete file; return; }[/quote]

It doesn’t seg fault now but it gives 0 when I cout << eventNum << endl; which I know is not the case.

Post here the output of:
tree->Print();

[quote=“Wile E. Coyote”]Post here the output of:
tree->Print();[/quote]

Attached is the script and the log output after doing tree->Print(). I have also initialized the variable to the value 1 and upon cout still gives the value 1. It appears that the SetBranchAddress is not reading the value in the tree.
log.txt (15.2 KB)
problem.C (902 Bytes)

Try the attached macro.
From your attached “log.txt”, it is not clear that “triggerTimeSec” is an “UInt_t”. Maybe now the new output of “tree->Print();” will confirm this but if not then you should make sure that this type is correct, otherwise the macro will not work (actually none of branches “reports” its type, which seems a bit strange to me).
problem.C (975 Bytes)

[quote=“Wile E. Coyote”]Try the attached macro.
From your attached “log.txt”, it is not clear that “triggerTimeSec” is an “UInt_t”. Maybe now the new output of “tree->Print();” will confirm this but if not then you should make sure that this type is correct, otherwise the macro will not work (actually none of branches “reports” its type, which seems a bit strange to me).[/quote]

It worked. Thanks!!