TGraph2D SetPoint problems inside a non-for loop

Thanks Wile. I put here the essentials of my code. Basically I am building 2 canvases: one for a TGraph and another for a TGraph2D. TGraph here is a movie. TGraph works well and I fail in TGraph2D.

// reads a data file fname with nch channels and return an 1D histogram
TH1F *readDataFile(char* fname, int nch) {
  int ch;
  float fc;
  TH1F *h = new TH1F("h1","",nch,-0.5,nch-0.5);
  h->Sumw2();
  
  FILE* fp=fopen(fname,"r");
  if (fp==NULL) {
    printf("Could not open file %s\n",fname);
    return h;
  }
  for (int j=0; j<nch; j++) {  
      fscanf(fp,"%d %f",&ch,&fc);      
      h->SetBinContent(j+1,fc);
  }
  fclose(fp); 
  return h;
}

// Creates a 2D histogram from a set of files (fn is the format of the file name)
TH2F* createScan(char* fn, int mi, int ma, int step, int nch) {    
  char fname[1000];
  TH1F *hdum;
  TH2F *h2 = new TH2F("h2","",nch,-0.5,nch-0.5,((ma-mi)/step)+1,mi-(Double_t)step/2.,ma+(Double_t)step/2.);
  h2->SetStats(kFALSE); 
  for (int i=mi; i<ma+1; i=i+step) {
    sprintf(fname,fn,i);   
    hdum=readDataFile(fname, nch);
    for (int j=0; j<nch; j++)
      h2->Fill(j,i,hdum->GetBinContent(j+1));
    delete hdum;   
  }
  return h2; 
}

float pixelAveCount(TH2F *h2, int ichan) {
		float pixAveCount;
		int nsteps = h2->GetYaxis()->GetNbins();	
		float pixCount=0.;			
		for (int istep=1; istep<nsteps+1; istep++) {			
			pixCount +=h2->GetBinContent (ichan+1, istep);				
		}
		pixAveCount = pixCount/nsteps;		
		return pixAveCount;
}

//scans through bad channels
bool item_exists(int item, int *array, int arrSize) {
  for(int k=0; k < arrSize; k++) {     
     if(array[k] == item)  {
       return true;
     }
  }
  return false;
}

void storeToVector(char *fpath0, int chMin, int chMax, int sensor) {
	gStyle->SetCanvasDefH(500);
       gStyle->SetCanvasDefW(560);
	gStyle->SetPalette(1);
	
	char fname[256];
	char fn[6] = "/run_";
	char ext[5] = ".raw";
	
	int begin = 0;
	int end = 59;
	char specifier[3] = "%d";	
	float pixelAveCnts;		
	
	int filter = 20;	
	vector<float> avCnt[filter];
	int size_goodChan;	
	Double_t avPixCount;	
	vector<float> ioc;	
	
	char *fbadname ="/home/results/layer_2_2011May/badChan_AllMods.raw";		
	char line_bad[80];
	int badChan;
	vector<int> vec_bad;
	int size;	
		
	FILE *fbad = fopen(fbadname,"r");
	if(fbad==NULL) {
		printf("Could not open file %s\n", fbadname);
		return;
	}		
	while (fgets(line_bad, 256, fbad) != NULL)  {
		sscanf(line_bad, "%d",&badChan);
                vec_bad.push_back(badChan);
	}
	size=vec_bad.size(); 
	int *bad=new int[size*sizeof(int)];
  for (int ibad =0; ibad<size; ibad++) {
  	bad[ibad] = vec_bad[ibad];    
  }	
	fclose(fbad);	
		
		
	for (int ifilter = 0; ifilter<filter; ifilter++) {							
		sprintf(fname, "%s%d%s%s%s", fpath0, ifilter,fn, specifier, ext);				
		TH2F *h2 = createScan(fname, begin,end, 1,128*12*3);		
		for (int ichan=chMin; ichan<chMax+1; ichan++) {	
			if(!item_exists(ichan, bad, size)) {
				pixelAveCnts = pixelAveCount(h2, ichan);																	
				avCnt[ifilter].push_back(pixelAveCnts);								
			}else{
				;
			}			
		}		
		delete h2;				
	}
	
	
	vector<float> iocData() ;
	int igoodChan;
	char channel[]= "Good Channel";
       char chanNum[100];     
       vector<float> iocVector = iocData() ;
         float iocData_elem;   
      Double_t mypar[2], emypar[2];   
      vector<float> fitElements; 
  
  char *fitFile0 = "/home/res1/filElements.sensor0";
	char fitFile[256];
	sprintf(fitFile,"%s%d",fitFile0, sensor);
	FILE *ftfile = fopen(fitFile, "w");
	if(ftfile==NULL) {
		printf("Could not open file %s\n", fitFile);
		return;	
	}

 	size_goodChan = avCnt[0].size();
 	
 	char *hist = " Sensor";
	char histName[256];
	sprintf(histName,"%s%d", hist, sensor); 		
	
	TCanvas *c1 = new TCanvas("c1");
	//c1=c1;
	TCanvas *c2 = new TCanvas("c2");
	
   TGraph2D *g2Deff = new TGraph2D(filter);
  
	M1:
	if(igoodChan>=0 && igoodChan< size_goodChan){
		sprintf(chanNum, "%s %d", channel,igoodChan );		

		TGraph *geff = new TGraph();				
		geff ->SetLineColor(2);
		geff ->SetMarkerStyle(20);
		geff ->SetMarkerSize(2);
		geff ->SetMarkerColor(2);
		geff ->SetLineWidth(8);	
		geff ->SetTitle(chanNum);			
		for (int ifilter =0; ifilter<filter; ifilter++) {			
			avPixCount = avCnt[ifilter][igoodChan];			
			iocData_elem = iocVector[ifilter];			
			geff->SetPoint(ifilter, iocData_elem, avPixCount);
			g2Deff->SetPoint(ifilter,igoodChan, iocData_elem, avPixCount);				
		}		
		printf("g2Deff Xmin %f Ymin %f Zmin %f\n", g2Deff->GetXmin(), g2Deff->GetYmin(), g2Deff->GetZmin());
	       printf("g2Deff Xmax %f Ymax %f Zmax %f\n", g2Deff->GetXmax(), g2Deff->GetYmax(), g2Deff->GetZmax());
	  
		c1->cd();
		geff->Draw("ALP");			
				
		c1->Modified();
		usleep(500000);				
		c1->Update();	
			
		delete geff;
		igoodChan++;	
		
		if (igoodChan<=size_goodChan)
			goto M1;			
	}	
		
	c2->cd();
	g2Deff->Draw("surf4");
	
	fclose(ftfile);
	return;
}


//read ioc data
vector <float> iocData() {
	vector<float> ioc;
	int nreadings =20;
	float iocReading;
	char *f_ioc = "/home/l_common/Frances/results/efficiencyMay2011/iocReadings.log";
	FILE * fioc = fopen(f_ioc, "r");
	if(fioc==NULL) {
		printf("Could not open file %s'\n", f_ioc);
		return ioc;
	}
	for(int iioc=0; iioc<nreadings; iioc++) {
		fscanf(fioc,"%f", &iocReading);	
		ioc.push_back(iocReading);
	}	
	return ioc;
}

it seems the problematic part is this one

g2Deff->SetPoint(ifilter,igoodChan, iocData_elem, avPixCount)

It outputs

printf("g2Deff Xmin %f Ymin %f Zmin %f\n", g2Deff->GetXmin(), g2Deff->GetYmin(),g2Deff->GetZmin());
printf("g2Deff Xmax %f Ymax %f Zmax %f\n", g2Deff->GetXmax(), g2Deff->GetYmax(),g2Deff->GetZmax());

//g2Deff Xmin 2.000000 Ymin 1365.420044 Zmin 1013.833313
//g2Deff Xmax 2.000000 Ymax 3877350.000000 Zmax 854429.437500

You l->SetPoint(i, x, y, z) and g2->SetPoint(i, x, y, z) in a for loop while I do if(igoodChan>=0 && igoodChan< size_goodChan){ …if (igoodChan<=size_goodChan) goto M1; } for

geff->SetPoint(ifilter, iocData_elem, avPixCount);
g2Deff->SetPoint(ifilter,igoodChan, iocData_elem, avPixCount);  

because of the issue described in

https://root-forum.cern.ch/t/calling-th1f-function-in-a-loop-segmentation-violoation/14992/1

So anyway , here are the error logs for the code above

Error in <TCanvas::Range>: illegal world coordinates range: x1=2.000000, y1=-483132.675012, x2=2.000000, y2=4361848.095056
Error in <TCanvas::RangeAxis>: illegal axis coordinates range: xmin=2.000000, ymin=1365.420044, xmax=2.000000, ymax=3877350.000000
Error in <TView3D::ResetView>: Error in min-max scope
Error in <TView3D::ResetView>: Error in min-max scope
Error in <TView3D::ResetView>: Error in min-max scope
Error in <TView3D::SetRange>: problem setting view
Error in <TView3D::ResetView>: Error in min-max scope
Error in <TCanvas::Range>: illegal world coordinates range: x1=-0.000000, y1=-0.000000, x2=0.000000, y2=0.000000
Error in <TCanvas::RangeAxis>: illegal axis coordinates range: xmin=-0.000000, ymin=-0.000000, xmax=0.000000, ymax=0.000000
root [2] Error in <TCanvas::Range>: illegal world coordinates range: x1=2.000000, y1=-483132.675012, x2=2.000000, y2=4361848.095056
Error in <TCanvas::RangeAxis>: illegal axis coordinates range: xmin=2.000000, ymin=1365.420044, xmax=2.000000, ymax=3877350.000000
Error in <TView3D::ResetView>: Error in min-max scope
Error in <TView3D::ResetView>: Error in min-max scope
Error in <TView3D::SetRange>: problem setting view
Error in <TView3D::ResetView>: Error in min-max scope
Error in <TCanvas::Range>: illegal world coordinates range: x1=-0.000000, y1=-0.000000, x2=0.000000, y2=0.000000
Error in <TCanvas::RangeAxis>: illegal axis coordinates range: xmin=-0.000000, ymin=-0.000000, xmax=0.000000, ymax=0.000000
Error in <TCanvas::Range>: illegal world coordinates range: x1=2.000000, y1=-483132.675012, x2=2.000000, y2=4361848.095056
Error in <TCanvas::RangeAxis>: illegal axis coordinates range: xmin=2.000000, ymin=1365.420044, xmax=2.000000, ymax=3877350.000000
Error in <TView3D::ResetView>: Error in min-max scope
Error in <TView3D::ResetView>: Error in min-max scope
Error in <TView3D::SetRange>: problem setting view
Error in <TView3D::ResetView>: Error in min-max scope
Error in <TCanvas::Range>: illegal world coordinates range: x1=-0.000000, y1=-0.000000, x2=0.000000, y2=0.000000
Error in <TCanvas::RangeAxis>: illegal axis coordinates range: xmin=-0.000000, ymin=-0.000000, xmax=0.000000, ymax=0.000000

and if i

myFilename.C++                    //ACLiC 

my issues are

In file included from /tmp/rootcint_YrT848.h:3,
                 from /tmp/W3YpNL_cint.cxx:1:
/home/l_common/Frances/macros/efficiencyMay2011/./effMay2011.C:9:17: error: array: No such file or directory
Error: external preprocessing failed. :0:
!!!Removing /home/l_common/Frances/macros/efficiencyMay2011/./file0CwgbU.cxx /home/l_common/Frances/macros/efficiencyMay2011/./file0CwgbU.h !!!
Error: /users/root_v5.20/bin/rootcint: error loading headers...
Error in <ACLiC>: Dictionary generation failed!
Info in <ACLiC>: Invoking compiler to check macro's validity
/home/l_common/Frances/macros/efficiencyMay2011/./effMay2011.C:9:17: error: array: No such file or directory
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/sstream.tcc: In member function ‘virtual typename std::basic_stringbuf<_CharT, _Traits, _Alloc>::int_type std::basic_stringbuf<_CharT, _Traits, _Alloc>::overflow(typename _Traits::int_type)’:
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/sstream.tcc:112: error: expected unqualified-id before ‘(’ token
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/sstream.tcc:114: error: expected unqualified-id before ‘(’ token

In my .bashrc I have

export ROOTSYS=/users/root_v5.20
export PATH=$ROOTSYS/bin
export LD_LIBRARY_PATH=$ROOTSYS/lib

and

 gcc -v

Using built-in specs.
Target: i386-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-libgcj-multifile --enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --disable-dssi --disable-plugin --with-java-home=/usr/lib/jvm/java-1.4.2-gcj-1.4.2.0/jre --with-cpu=generic --host=i386-redhat-linux
Thread model: posix
gcc version 4.1.2 20080704 (Red Hat 4.1.2-50)