Segmentation fault after TTree::GetEntry

ROOT Version: 6.22/06
Platform: Ubuntu 18
Compiler: gcc 7.5.0


I am running into a segmentation fault at the line p16sim->GetEntry(i); The error happens at the second iteration of the loop (at i=1). Below is my code:

   `TFile *f = new TFile("root_files/hor_scan_2.5_0.0_2GeV.root");
   TTree *p16sim = (TTree*)f->Get("p16sim");
   Double_t edepTrue=0;
   Double_t edep;
   Int_t crystal_xindex, crystal_yindex, i, j, k=0;
   const Int_t NX=5, NY=5;
   char hname[20];
   p16sim->SetBranchAddress("edepTrue",&edepTrue);
   //   p16sim->SetBranchAddress("EP16",&EP16);                                                                                                     

   // create histograms                                                                                                                             
   TH1F *hedep1d[NX][NY];
   for (j=0;j<NX;j++) {
     for (i=0;i<NY;i++) {
       sprintf(hname,"h%d_%d",i,j);
       hedep1d[i][j] = new TH1F(hname,"edep",200,0,2000);
     }
   }

   //read all entries and fill the histograms                                                                                                       
   Int_t nentries = (Int_t)p16sim->GetEntries();
   for (i=0; i<nentries; i++) {
     p16sim->GetEntry(i);
     crystal_xindex = floor(i/2);
     crystal_yindex = i%5;
     hedep1d[crystal_xindex][crystal_yindex]->Fill(edepTrue);
   }`

And here is the first snippet of the error message I am getting:

`#0  0x00007f72f927c457 in __GI___waitpid (pid=4925, stat_loc=stat_loc
entry=0x7fff43ccba58, options=options
entry=0) at ../sysdeps/unix/sysv/linux/waitpid.c:30
#1  0x00007f72f91e7177 in do_system (line=<optimized out>) at ../sysdeps/posix/system.c:149
#2  0x00007f72f9e21e83 in TUnixSystem::StackTrace() () from /home/patrick/root/lib/libCore.so
#3  0x00007f72f45564a8 in cling::MultiplexInterpreterCallbacks::PrintStackTrace() () from /home/patrick/root/lib/libCling.so
#4  0x00007f72f4555df3 in cling_runtime_internal_throwIfInvalidPointer () from /home/patrick/root/lib/libCling.so
#5  0x00007f72fa6b94ae in ?? ()
#6  0x0000000000000000 in ?? ()

 *** Break *** segmentation violation

Right before the “Fill” statement, try to add:
if ((crystal_xindex >= NX) || (crystal_yindex >= NY)) continue; // just a precaution

Thanks but I’m still getting a segmentation fault when I add that line.

Attach the output of: p16sim->Print();

******************************************************************************
*Tree    :p16sim    : Proto16 simulation                                     *
*Entries :    30000 : Total =         6743409 bytes  File  Size =     888397 *
*        :          : Tree compression factor =   7.62                       *
******************************************************************************
*Br    0 :TrigE     : [1]/D                                                  *
*Entries :    30000 : Total  Size=     241194 bytes  File Size  =       1745 *
*Baskets :        7 : Basket Size=      32000 bytes  Compression= 128.34     *
*............................................................................*
*Br    1 :ColliE    : [1]/D                                                  *
*Entries :    30000 : Total  Size=     241204 bytes  File Size  =      42480 *
*Baskets :        7 : Basket Size=      32000 bytes  Compression=   5.27     *
*............................................................................*
*Br    2 :edepTrue  : [25]/D                                                 *
*Entries :    30000 : Total  Size=    6019467 bytes  File Size  =     772574 *
*Baskets :      188 : Basket Size=      32000 bytes  Compression=   7.76     *
*............................................................................*
*Br    3 :EP16      : [1]/D                                                  *
*Entries :    30000 : Total  Size=     241184 bytes  File Size  =      57784 *
*Baskets :        7 : Basket Size=      32000 bytes  Compression=   3.88     *
*............................................................................*

******************************************************************************
*Tree    :p16sim    : Proto16 simulation                                     *
*Entries :    30000 : Total =         6743409 bytes  File  Size =     888397 *
*        :          : Tree compression factor =   7.62                       *
******************************************************************************
*Br    0 :TrigE     : [1]/D                                                  *
*Entries :    30000 : Total  Size=     241194 bytes  File Size  =       1745 *
*Baskets :        7 : Basket Size=      32000 bytes  Compression= 128.34     *
*............................................................................*
*Br    1 :ColliE    : [1]/D                                                  *
*Entries :    30000 : Total  Size=     241204 bytes  File Size  =      42480 *
*Baskets :        7 : Basket Size=      32000 bytes  Compression=   5.27     *
*............................................................................*
*Br    2 :edepTrue  : [25]/D                                                 *
*Entries :    30000 : Total  Size=    6019467 bytes  File Size  =     772574 *
*Baskets :      188 : Basket Size=      32000 bytes  Compression=   7.76     *
*............................................................................*
*Br    3 :EP16      : [1]/D                                                  *
*Entries :    30000 : Total  Size=     241184 bytes  File Size  =      57784 *
*Baskets :        7 : Basket Size=      32000 bytes  Compression=   3.88     *
*............................................................................*

This output looks crazy.
Maybe you need: Double_t edepTrue[25];

Hi @phm5024 ,
I can’t say what the problem is either. Can you maybe share a complete (but minimal) reproducer and an example input file so that we can debug on our side?

Cheers,
Enrico