Error on compiled code on TTree->GetEntry(i)

Hello,
do you have any idea why the code below here works well when executed as script (say .L myScript.c then i call the function) BUT NOT when is compiled and executed as part of a program? The error happen on the GetEntry command with the following error

Cheers, Maurizio

*** Break *** segmentation violation
Generating stack trace…
/usr/bin/addr2line: lt-techmodelreader: No such file or directory
/usr/bin/addr2line: lt-techmodelreader: No such file or directory
0x0000002a95ce7a6d in TFile::MakeFree(long long, long long) + 0x2d from /usr/local/root/lib/libCore.so
0x0000002a95ce0f1c in TDirectory::WriteKeys() + 0xdc from /usr/local/root/lib/libCore.so
0x0000002a99932e6d in TBranchElement::ReadLeaves(TBuffer&) + 0x84d from /usr/local/root/lib/libTree.so
0x0000002a9992a4f0 in TBranch::GetEntry(int, int) + 0x130 from /usr/local/root/lib/libTree.so
0x0000002a99931abe in TBranchElement::GetEntry(int, int) + 0x1ee from /usr/local/root/lib/libTree.so
0x0000002a99931a5e in TBranchElement::GetEntry(int, int) + 0x18e from /usr/local/root/lib/libTree.so
0x0000002a99950d5a in TTree::GetEntry(int, int) + 0x16a from /usr/local/root/lib/libTree.so
0x0000002a99950d87 in TTree::GetEntry(int, int) + 0x197 from /usr/local/root/lib/libTree.so
0x0000002a99f4112d in pamela::YodaScan::NeutronDetectorScan(TString) at /home/kusanagi/mauProjects/yoda/techmodel/yodaScan.cpp:335 from /home/kusanagi/mauProjects/yoda/techmodel/.libs/libyodatechmodel.so.0
0x0000000000404b95 in pamela::YodaScan::NeutronDetectorScan(TString) + 0xc75 from lt-techmodelreader
0x0000002a9ce844ea in __libc_start_main + 0x11a from /lib64/tls/libc.so.6
0x0000000000403f6a in std::__default_alloc_template<true, 0>::deallocate(void*, unsigned long) + 0x3a from lt-techmodelreader
Aborted

/----------------------------------------------------------
void NeutronDetectorScan(TString base){
TFile *headerFile;
TFile *neutronFile;
Int_t tmpSize;

neutronFile = getFile(base, “Neutron”);
headerFile = getFile(base, “Physics”, “Header”);

//Takes the tree of the header file
TTree tr = (TTree)headerFile->Get(“Pscu”);
tr->AddFriend(“Neutron”, neutronFile);

TCanvas *finalCanv = new TCanvas(“NeutronDetector”, base, 1280, 1024);
finalCanv->Divide(2,4);

Int_t nevents = (Int_t)tr->GetEntries();
Int_t mintime = (Int_t)tr->GetMinimum(“Pscu.OrbitalTime”);
Int_t maxtime = (Int_t)tr->GetMaximum(“Pscu.OrbitalTime”);
Int_t nbintime = (Int_t)(maxtime-mintime)/1000;

const Int_t size = nevents;
Double_t x[size], yUpperTrig[size], yBottomTrig[size], yUpperBackground[size], yBottomBackground[size];
pamela::neutron::NeutronEvent *ne;
pamela::neutron::NeutronRecord *nr;

pamela::EventHeader *eh;
pamela::PscuHeader ph;
tr->SetBranchAddress(“Event”, &ne);
tr->SetBranchAddress(“Header”, &eh);
for (Int_t i = 0; i < nevents; i++){
tr->GetEntry(i); <--------------------------------here I get segmentation violation
tmpSize = ne->Records->GetEntries();
for (Int_t j = 0; j < tmpSize; j++){
nr = (pamela::neutron::NeutronRecord
)ne->Records->At(j);
yUpperTrig[i] = yUpperTrig[i] + nr->upperTrig;
yBottomTrig[i] = yBottomTrig[i] + nr->bottomTrig;
yUpperBackground[i] = yUpperBackground[i] + nr->upperBack;
yBottomBackground[i] = yBottomBackground[i] + nr->bottomBack;
}
ph = eh->GetPscuHeader();
x[i] = ph->GetOrbitalTime();
}


}

It looks like you have something wrong with your shared libs.
The traceback that you show cannot be true.

Rene

Hello Rene,
this mail is just to tell you that i have found the solution. I don’t know why but it seems that the scripts are more failure resistant than the compiled code; the problem was that the classes which where referred to into the SetBrachAddress were uninitialized so i modified the code from


pamela::neutron::NeutronEvent *ne;
pamela::neutron::NeutronRecord *nr;
pamela::EventHeader *eh;
pamela::PscuHeader *ph;
tr->SetBranchAddress(“Event”, &ne);
tr->SetBranchAddress(“Header”, &eh);
for (Int_t i = 0; i < nevents; i++){
tr->GetEntry(i);

to


pamela::neutron::NeutronEvent *ne = new pamela::neutron::NeutronEvent();
pamela::neutron::NeutronRecord *nr;
pamela::EventHeader *eh = new pamela::EventHeader();
pamela::PscuHeader *ph;
tr->SetBranchAddress(“Event”, &ne);
tr->SetBranchAddress(“Header”, &eh);
for (Int_t i = 0; i < nevents; i++){
tr->GetEntry(i);

and now it works (also the compiled one) and I am happy; but just to know there is any explaination?

Cheers
Maurizio

Maurizio,

The pointers given to SetBranchAddress must be set before calling the function.
You can simply initialize them to 0 (no need to create an object)

Rene