Segmentation violation when reading 2d array

Dear ROOTers,
I was trying to read entry from root file.

  1. I can see the histogram when doing T->Draw("photon_y_at_gc_mirror_reflected")

  2. Then I try to read event by event [color=#FF0000](This one gives me error.)[/color]
void read() {
	TChain* T=new TChain("T","T");
	T->Add("BGC_9999.root");
	Int_t i,j,k,l;

	//T->Print();
	
	//Double_t x[411][4];
	Double_t** x= new Double_t*[411];
	for ( i = 0; i < 411; ++i ) {
		x[i]=new Double_t[4];
	}
	T->SetBranchAddress("photon_y_at_gc_mirror_reflected",x);
	//for ( k = 0; k < T->GetEntries(); ++k ) {
	for ( k = 0; k < 1; ++k ) {
		printf("----Event %d----\n",k+1);
		T->GetEntry(k);
		for ( i = 0; i < 411; ++i ) {
			for ( j = 0; j < 4; ++j ) {
				if ( x[i][j]>-1e8 ) {
					printf("Line %d:x[%d][%d]=%g\n",__LINE__,i,j,x[i][j]);
				}
			}
		}
	}
}
  1. So I print out the tree
*Br   66 :photon_y_at_gc_mirror_reflected :                                  *
*         | Photon_Double_Info[Total_Num_Of_Cher_Photons][4]/D               *
*Entries :        2 : Total  Size=      27235 bytes  File Size  =       3829 *
*Baskets :        1 : Basket Size=      32000 bytes  Compression=   6.92     *
  1. I also try to use (this works)
Double_t x[411][4];

But you can see the array has variable size.

I’m just asking how to read it correctly?

Thanks a lot.

You must know what the maximum allowed value of the “Total_Num_Of_Cher_Photons” was.
Then, use “Double_t x[(MAX_ALLOWED_TOTAL_NUMBER)][4];”.
Moreover, you MUST make sure that you read the “Total_Num_Of_Cher_Photons” branch first, i.e. that the “GetEntry” call retrieves it (before reading the “photon_y_at_gc_mirror_reflected” branch, as it depends on the “Total_Num_Of_Cher_Photons”).

BTW.“Double_t** x= new Double_t*[411]; … x[i]=new Double_t[4]; …” is completely wrong in this case.

See also “Adding a Branch with a Fixed Length Array” and “Adding a Branch with a Variable Length Array” in http://root.cern.ch/download/doc/ROOTUsersGuideHTML/ch12s15.html
Search also for “array” in http://root.cern.ch/root/html/TTree.html#TTree:Branch@3
See also http://root.cern.ch/download/doc/ROOTUsersGuideHTML/ch11s03.html#d5e12548