Error while using TTree->GetEntry()

I have a ROOT file which contains a tree as shown above. I tried to print the entries of a variable (for example, Px) using the following code.

root[0] TFile f("ampt_afterART.root");
root[1] TTree* t;
root[2] f.GetObject("AMPT;1",t);
root[3] float px;
root[4] t->SetBranchAddress("Px",&px);
root[5] int k;
root[6] for (k=0; k<t->GetEntries(); {t->GetEntry(k); cout<<px<<endl;}

However I got the following error:

*** Break *** segmentation violation



===========================================================
There was a crash.
This is the entire stack trace of all threads:
===========================================================
#0  0x00007f96a1b08687 in __GI___waitpid (pid=3950, stat_loc=stat_loc
entry=0x7ffd260618e8, options=options
entry=0) at ../sysdeps/unix/sysv/linux/waitpid.c:30
#1  0x00007f96a1a73067 in do_system (line=<optimized out>) at ../sysdeps/posix/system.c:149
#2  0x00007f96a2717cc3 in TUnixSystem::StackTrace() () from /home/recaptcha19/Downloads/ROOT6/root/lib/libCore.so.6.18
#3  0x00007f96a271a6b4 in TUnixSystem::DispatchSignals(ESignals) () from /home/recaptcha19/Downloads/ROOT6/root/lib/libCore.so.6.18
#4  <signal handler called>
#5  0x00007f96a053a971 in TBufferFile::ReadFastArray(float*, int) () from /home/recaptcha19/Downloads/ROOT6/root/lib/libRIO.so
#6  0x00007f9694185345 in TBranch::GetEntry(long long, int) () from /home/recaptcha19/Downloads/ROOT6/root/lib/libTree.so.6.18.02
#7  0x00007f96941f9e88 in TTree::GetEntry(long long, int) () from /home/recaptcha19/Downloads/ROOT6/root/lib/libTree.so.6.18.02
#8  0x00007f96a2ef6101 in ?? ()
#9  0x0000000000000015 in ?? ()
#10 0x0a660d27f4bb4100 in ?? ()
#11 0x0000561c0fffbd30 in ?? ()
#12 0x00007f96941f9dc0 in ?? () from /home/recaptcha19/Downloads/ROOT6/root/lib/libTree.so.6.18.02
#13 0x0000561c0ffd8078 in ?? ()
#14 0x0000561c0ebb88d0 in ?? ()
#15 0x0000000000000000 in ?? ()
===========================================================


The lines below might hint at the cause of the crash.
You may get help by asking at the ROOT forum http://root.cern.ch/forum
Only if you are really convinced it is a bug in ROOT then please submit a
report at http://root.cern.ch/bugs Please post the ENTIRE stack trace
from above as an attachment in addition to anything else
that might help us fixing this issue.
===========================================================
#5  0x00007f96a053a971 in TBufferFile::ReadFastArray(float*, int) () from /home/recaptcha19/Downloads/ROOT6/root/lib/libRIO.so
#6  0x00007f9694185345 in TBranch::GetEntry(long long, int) () from /home/recaptcha19/Downloads/ROOT6/root/lib/libTree.so.6.18.02
#7  0x00007f96941f9e88 in TTree::GetEntry(long long, int) () from /home/recaptcha19/Downloads/ROOT6/root/lib/libTree.so.6.18.02
#8  0x00007f96a2ef6101 in ?? ()
#9  0x0000000000000015 in ?? ()
#10 0x0a660d27f4bb4100 in ?? ()
#11 0x0000561c0fffbd30 in ?? ()
#12 0x00007f96941f9dc0 in ?? () from /home/recaptcha19/Downloads/ROOT6/root/lib/libTree.so.6.18.02
#13 0x0000561c0ffd8078 in ?? ()
#14 0x0000561c0ebb88d0 in ?? ()
#15 0x0000000000000000 in ?? ()
===========================================================


Root > 

How do I rectify this?

Try:

for (k=0; k<t->GetEntries(); ++k) {t->GetEntry(k); cout<<px<<endl;}

I forgot to include k++ in the code in the question, sorry. But I tried ++k and I’m getting the same error.

What is the result of t->Print()?

This is the output.

******************************************************************************
*Tree    :AMPT      : AMPT DST Tree                                          *
*Entries :      100 : Total =        31965541 bytes  File  Size =   17572407 *
*        :          : Tree compression factor =   1.82                       *
******************************************************************************
*Br    0 :Event     : nevent/I:nrun:multi:impactpar/F:NpartP/I:NpartT:NELP:  *
*         | NINP:NELT:NINT:zero                                              *
*Entries :      100 : Total  Size=       5771 bytes  File Size  =       2242 *
*Baskets :        1 : Basket Size=      32000 bytes  Compression=   1.99     *
*............................................................................*
*Br    1 :ID        : ID[multi]/I                                            *
*Entries :      100 : Total  Size=    3551097 bytes  File Size  =     844600 *
*Baskets :       68 : Basket Size=      32000 bytes  Compression=   4.20     *
*............................................................................*
*Br    2 :Px        : fPx[multi]/F                                           *
*Entries :      100 : Total  Size=    3551107 bytes  File Size  =    2076513 *
*Baskets :       68 : Basket Size=      32000 bytes  Compression=   1.71     *
*............................................................................*
*Br    3 :Py        : fPy[multi]/F                                           *
*Entries :      100 : Total  Size=    3551107 bytes  File Size  =    2057080 *
*Baskets :       68 : Basket Size=      32000 bytes  Compression=   1.73     *
*............................................................................*
*Br    4 :Pz        : fPz[multi]/F                                           *
*Entries :      100 : Total  Size=    3551107 bytes  File Size  =    3041678 *
*Baskets :       68 : Basket Size=      32000 bytes  Compression=   1.17     *
*............................................................................*
*Br    5 :Mass      : fMass[multi]/F                                         *
*Entries :      100 : Total  Size=    3551251 bytes  File Size  =     682882 *
*Baskets :       68 : Basket Size=      32000 bytes  Compression=   5.20     *
*............................................................................*
*Br    6 :X         : fX[multi]/F                                            *
*Entries :      100 : Total  Size=    3551035 bytes  File Size  =    2243958 *
*Baskets :       68 : Basket Size=      32000 bytes  Compression=   1.58     *
*............................................................................*
*Br    7 :Y         : fY[multi]/F                                            *
*Entries :      100 : Total  Size=    3551035 bytes  File Size  =    2237222 *
*Baskets :       68 : Basket Size=      32000 bytes  Compression=   1.59     *
*............................................................................*
*Br    8 :Z         : fZ[multi]/F                                            *
*Entries :      100 : Total  Size=    3551035 bytes  File Size  =    2409227 *
*Baskets :       68 : Basket Size=      32000 bytes  Compression=   1.47     *
*............................................................................*
*Br    9 :Time      : fT[multi]/F                                            *
*Entries :      100 : Total  Size=    3551242 bytes  File Size  =    1970435 *
*Baskets :       68 : Basket Size=      32000 bytes  Compression=   1.80     *
*............................................................................*