Array index out of range

Very long source code. Please help me. In this source code, I get an error saying “Array index out of range a1x” and “Array index out of range a1y”. Since cnt1 and cnt3 in this source code have exactly the same conditions, they should be the same number. And a1x [cntt1], which is cnt1 * 3. Since the number of 0 → (cnt3-1) * 3 + 2 is assigned to the array a1x, I don’t think it will exceed the range of the array. Is there any mistake? I’m new to programming, so the basics may be wrong.

{
  gStyle->SetMarkerStyle(20);
  gStyle->SetMarkerSize(0.7);
  double R=1.18*pow(197.0,(1.0/3.0))-0.48;
  double a=0.54;
int n=197;
double x11[197];
double y11[197];
 double x22[197];
 double y22[197];
 double cntk[10000];
 double ep[10000];
 int total=10000;
 double R1=1.2*pow(197,1.0/3.0);
double c=sqrt(4.2/3.14);
 double c11=sqrt(0.81/3.14);
gRandom->SetSeed();
 TGraphErrors *g1=new TGraphErrors;
 g1->SetMarkerColor(kBlack);
 for(int k=0.0;k<total;){
   
   for(int l=0.0;l<1;){
 double b=gRandom->Rndm()*22.0-11.0;
 double b5=gRandom->Rndm()*22.0-11.0;
 if(b*b+b5*b5<(R1+2.0)*(R1+2.0)){
 
 l+=1;
  

  for(int i=0.0;i<n;){
    double x1=gRandom->Rndm()*16.0-8.0;
    double x01=x1-b;
    double y1=gRandom->Rndm()*16.0-8.0;
    double y01=y1-b5;
    double z1=gRandom->Rndm()*16.0-8.0;
    double r1=sqrt(x1*x1+y1*y1+z1*z1);
    double r3=gRandom->Rndm();
    double rho1=1.0/(1+exp((r1-R)/a));
    if(r3<rho1){
    
    x11[i]=x01;
    y11[i]=y01;
    i+=1;
    }
  }
for(int j=0.0;j<n;){
   double x2=gRandom->Rndm()*16.0-8.0;
   double x02=x2+b;
    double y2=gRandom->Rndm()*16.0-8.0;
    double y02=y2+b5;
    double z2=gRandom->Rndm()*16.0-8.0;
    double r2=sqrt(x2*x2+y2*y2+z2*z2);
    double r4=gRandom->Rndm();
    double rho2=1.0/(1+exp((r2-R)/a));
    if(r4<rho2){
    
    x22[j]=x02;
    y22[j]=y02;
    j+=1;

    }
 }
 int cnt1=0;
for(int i=0.0;i<197;i++){
  for(int j=0.0;j<197;j++){
    double r1111=(x11[i]-x22[j]);
    double r1112=(y11[i]-y22[j]);
    double r1113=sqrt(r1111*r1111+r1112*r1112);
    if(r1113<c){
      cnt1+=1.0;
      break;
    }
  }
 }
 int cntt1=cnt1*3;
 
  double a1x[cntt1];
  double a1y[cntt1];
  int cnt3=0.0;
      for(int i=0.0;i<197;i++){
  for(int j=0.0;j<197;j++){
    double r1111=(x11[i]-x22[j]);
    double r1112=(y11[i]-y22[j]);
    double r1113=sqrt(r1111*r1111+r1112*r1112);
    if(r1113<c){
      cnt3+=1;
      for(int l1=0.0;l1<1;){
      double a01x=gRandom->Rndm()*1.752-0.876;
      double a01y=gRandom->Rndm()*1.752-0.876;
      double a01z=gRandom->Rndm()*1.752-0.876;
      double sr11=a01x*a01x+a01y*a01y+a01z*a01z;
      if(sqrt(sr11)<0.876){
	l1+=1;
	int i01=(cnt3-1)*3;
	a1x[i01]=a01x;
	a1y[i01]=a01y;
	for(int l2=0.0;l2<1;){
      double b01x=gRandom->Rndm()*1.752-0.876;
      double b01y=gRandom->Rndm()*1.752-0.876;
      double b01z=gRandom->Rndm()*1.752-0.876;
      double sr12=b01x*b01x+b01y*b01y+b01z*b01z;
      if(sqrt(sr12)<0.876){
	l2+=1;
	int i02=(cnt3-1)*3+1;
	a1x[i02]=b01x;
	a1y[i02]=b01y;
	for(int l3=0.0;l3<1;){
      double c01x=gRandom->Rndm()*1.752-0.876;
      double c01y=gRandom->Rndm()*1.752-0.876;
      double c01z=gRandom->Rndm()*1.752-0.876;
      double sr13=c01x*c01x+c01y*c01y+c01z*c01z;
      if(sqrt(sr13)<0.876){
	l3+=1;
	int i03=(cnt3-1)*3+2;
	a1x[i03]=c01x;
	a1y[i03]=c01y;
      }
      }
      }
      }
      }
      }
    }
    }
  }
      int cnt2=0;
  for(int j=0.0;j<197;j++){
   for(int i=0.0;i<197;i++){
     double r01111=(x22[j]-x11[i]);
     double r01112=(y22[j]-y11[i]);
     double r01113=sqrt(r01111*r01111+r01112*r01112);
     if(r01113<c){
       cnt2+=1.0;
       break;
     }
   }
  }
  int cntt2=cnt2*3;
  int cnt4=0;
  double a2x[cntt2];
  double a2y[cntt2];
      for(int j=0.0;j<197;j++){
  for(int i=0.0;i<197;i++){
    double r1111=(x22[j]-x11[i]);
    double r1112=(y22[j]-y11[i]);
    double r1113=sqrt(r1111*r1111+r1112*r1112);
    if(r1113<c){
      cnt4+=1;
      for(int l4=0.0;l4<1;){
      double a02x=gRandom->Rndm()*1.752-0.876;
      double a02y=gRandom->Rndm()*1.752-0.876;
      double a02z=gRandom->Rndm()*1.752-0.876;
      double sr21=a02x*a02x+a02y*a02y+a02z*a02z;
      if(sqrt(sr11)<0.876){
	l4+=1;
	int j01=(cnt4-1)*3;
	a2x[j01]=a02x;
	a2y[j01]=a02y;
	for(int l5=0.0;l5<1;){
      double b02x=gRandom->Rndm()*1.752-0.876;
      double b02y=gRandom->Rndm()*1.752-0.876;
      double b02z=gRandom->Rndm()*1.752-0.876;
      double sr22=b02x*b02x+b02y*b02y+b02z*b02z;
      if(sqrt(sr22)<0.876){
	l5+=1;
	int j02=(cnt4-1)*3+1;
	a2x[j02]=b02x;
	a2y[j02]=b02y;
	for(int l6=0.0;l6<1;){
      double c02x=gRandom->Rndm()*1.752-0.876;
      double c02y=gRandom->Rndm()*1.752-0.876;
      double c02z=gRandom->Rndm()*1.752-0.876;
      double sr23=c02x*c02x+c02y*c02y+c02z*c02z;
      if(sqrt(sr23)<0.876){
	int j03=(cnt4-1)*3+2;
	a2x[j03]=c02x;
	a2y[j03]=c02y;
      }
      }
      }
	}
      }
      }
      
    }
  }
      }
      double sumxs1=0.0;
      double sumxxs1=0.0;
      double sumys1=0.0;
      double sumyys1=0.0;
      double sumxys1=0.0;

      for(int i=0.0;i<cntt1;i++){
	for(int j=0.0;j<cntt2;j++){
	  double sr01=(a1x[i]-a2x[j]);
	  double sr02=(a1y[i]-a2y[j]);
	  double sr03=sqrt(sr01*sr01+sr02*sr02);
	  if(sr03<c11){
	    double xs11=a1x[i];
	    sumxs1+=xs11;
	    double xxs11=(a1x[i])*(a1x[i]);
	    sumxxs1+=xxs11;
	    double ys11=a1y[i];
	    sumys1+=ys11;
	    double yys11=(a1y[i])*(a1y[i]);
	    sumyys1+=yys11;
	    double xys11=(a1x[i])*(a1y[i]);
	    sumxys1+=xys11;
	    break;
	  }
	}
      }
      double sumxs2=0.0;
      double sumxxs2=0.0;
      double sumys2=0.0;
      double sumyys2=0.0;
      double sumxys2=0.0;
      for(int j=0.0;j<cntt2;j++){
	for(int i=0.0;i<cntt1;i++){
	  double sr04=(a2x[j]-a1x[i]);
	  double sr05=(a2y[j]-a1y[i]);
	  double sr06=sqrt(sr04*sr04+sr05*sr05);
	  if(sr06<c11){
	    double xs21=a2x[j];
	    sumxs2+=xs21;
	    double xxs21=(a2x[j])*(a2x[j]);
	    sumxxs2+=xxs21;
	    double ys21=a2y[j];
	    sumys2+=ys21;
	    double yys21=(a2y[j])*(a2y[j]);
	    sumyys2+=yys21;
	    double xys21=(a2x[j])*(a2y[j]);
	    sumxys2+=xys21;
	    break;
	  }
	}
      }
      
       
	  if((cnt1+cnt2)>0.0){
   
  cntk[k]=cnt1+cnt2;
  double xxmean=(sumxxs1+sumxxs2)/cntt1+cntt2;
  double xmean=(sumxs1+sumxs2)/cntt1+cntt2;
  double yymean=(sumyys1+sumyys2)/cntt1+cntt2;
  double ymean=(sumys1+sumys2)/cntt1+cntt2;
  double xymean=(sumxys1+sumxys2)/cntt1+cntt2;
  double sigx=xxmean-(xmean)*(xmean);
  double sigy=yymean-(ymean)*(ymean);
  double sigxy=xymean-(xmean)*(ymean);
  double x011=(sigy-sigx)*(sigy-sigx)+4.0*sigxy*sigxy;
  double x01=sqrt(x011);
  double y01=sigy+sigx;
   double ip1=x01/y01;
  ep[k]=ip1;
  k+=1;
  }  
  }
   }
 }

 double dum1;
 double dum2;
 for(int k=0.0;k<total;++k){
   for(int l=k+1;l<total;++l){
     if(cntk[k]>cntk[l]){
       dum1=cntk[k];
       cntk[k]=cntk[l];
       cntk[l]=dum1;

       dum2=ep[k];
       ep[k]=ep[l];
       ep[l]=dum2;
     }
   }
 }
 double cntsepa[20];
 for(int m=0.0;m<20;m++){
   double sumcnt=0.0;
   for(int n=500*m;n<500*(m+1);n++){
     sumcnt+=cntk[n];
   }
   cntsepa[m]=sumcnt/500.0;
 }
 double epsepa[20];
 for(int m=0.0;m<20;m++){
   double sumep=0.0;
   for(int n=500*m;n<500*(m+1);n++){
     sumep+=ep[n];
   }
   epsepa[m]=sumep/500.0;
   double sum11=0.0;
   for(int o=500*m;o<500*(m+1);o++){
     sum11+=(ep[o]-epsepa[m])*(ep[o]-epsepa[m]);
   }
   double sd=sqrt(sum11/(500.0-1.0)/500.0);
   g1->SetPointError(19-m,0,sd);
   g1->SetPoint(m,cntsepa[m],epsepa[m]);
 }

 
 
 
 g1->Draw("AP");
}

Try printing the value of i03 (in fact, also cnt3) to see where it fails (you can also print the loop variable values if you can’t tell why cnt3 has the value that breaks it, and so on):

//...
  int i03=(cnt3-1)*3+2;
  cout << "i03 / cnt3:  " << i03 << "   " << cnt3 << endl;
//...

General note: don’t put decimals into integers (you have int something=0.0 in all your loops).

Thank you very much. It was certainly out of range. After making the correction, the error disappeared, but the program result did not appear either. I thought it was just taking time, but is it still something wrong because it takes so long?
If you have time, I would be grateful if you could take a look.

{
  gStyle->SetMarkerStyle(20);
  gStyle->SetMarkerSize(0.7);
  double R=1.18*pow(197.0,(1.0/3.0))-0.48;
  double a=0.54;
int n=197;
double x11[197];
double y11[197];
 double x22[197];
 double y22[197];
 double cntk[10000];
 double ep[10000];
 int total=10000;
 double R1=1.2*pow(197,1.0/3.0);
double c=sqrt(4.2/3.14);
 double c11=sqrt(0.81/3.14);
gRandom->SetSeed();
 TGraphErrors *g1=new TGraphErrors;
 g1->SetMarkerColor(kBlack);
 for(int k=0;k<total;){
   
   for(int l=0;l<1;){
 double b=gRandom->Rndm()*22.0-11.0;
 double b5=gRandom->Rndm()*22.0-11.0;
 if(b*b+b5*b5<(R1+2.0)*(R1+2.0)){
 
 l+=1;
  

  for(int i=0;i<n;){
    double x1=gRandom->Rndm()*16.0-8.0;
    double x01=x1-b;
    double y1=gRandom->Rndm()*16.0-8.0;
    double y01=y1-b5;
    double z1=gRandom->Rndm()*16.0-8.0;
    double r1=sqrt(x1*x1+y1*y1+z1*z1);
    double r3=gRandom->Rndm();
    double rho1=1.0/(1+exp((r1-R)/a));
    if(r3<rho1){
    
    x11[i]=x01;
    y11[i]=y01;
    i+=1;
    }
  }
for(int j=0;j<n;){
   double x2=gRandom->Rndm()*16.0-8.0;
   double x02=x2+b;
    double y2=gRandom->Rndm()*16.0-8.0;
    double y02=y2+b5;
    double z2=gRandom->Rndm()*16.0-8.0;
    double r2=sqrt(x2*x2+y2*y2+z2*z2);
    double r4=gRandom->Rndm();
    double rho2=1.0/(1+exp((r2-R)/a));
    if(r4<rho2){
    
    x22[j]=x02;
    y22[j]=y02;
    j+=1;

    }
 }
 int cnt1=0;
for(int i=0;i<197;i++){
  for(int j=0;j<197;j++){
    double r1111=(x11[i]-x22[j]);
    double r1112=(y11[i]-y22[j]);
    double r1113=sqrt(r1111*r1111+r1112*r1112);
    if(r1113<c){
      cnt1+=1;
      break;
    }
  }
 }
 int cntt1=cnt1*3;
 if(cntt1>0){
  double a1x[cntt1];
  double a1y[cntt1];
  int cnt3=0.0;
      for(int i=0;i<197;i++){
  for(int j=0.0;j<197;j++){
    double r1111=(x11[i]-x22[j]);
    double r1112=(y11[i]-y22[j]);
    double r1113=sqrt(r1111*r1111+r1112*r1112);
    if(r1113<c){
      cnt3+=1;
      for(int l1=0;l1<1;){
      double a01x=gRandom->Rndm()*1.752-0.876;
      double a01y=gRandom->Rndm()*1.752-0.876;
      double a01z=gRandom->Rndm()*1.752-0.876;
      double sr11=a01x*a01x+a01y*a01y+a01z*a01z;
      if((sqrt(sr11))<0.876){
	l1+=1;
	int i01=(cnt3-1)*3;
	a1x[i01]=a01x;
	a1y[i01]=a01y;
	for(int l2=0.0;l2<1;){
      double b01x=gRandom->Rndm()*1.752-0.876;
      double b01y=gRandom->Rndm()*1.752-0.876;
      double b01z=gRandom->Rndm()*1.752-0.876;
      double sr12=b01x*b01x+b01y*b01y+b01z*b01z;
      if(sqrt(sr12)<0.876){
	l2+=1;
	int i02=(cnt3-1)*3+1;
	a1x[i02]=b01x;
	a1y[i02]=b01y;
	for(int l3=0;l3<1;){
      double c01x=gRandom->Rndm()*1.752-0.876;
      double c01y=gRandom->Rndm()*1.752-0.876;
      double c01z=gRandom->Rndm()*1.752-0.876;
      double sr13=c01x*c01x+c01y*c01y+c01z*c01z;
      if(sqrt(sr13)<0.876){
	l3+=1;
	int i03=(cnt3-1)*3+2;
	a1x[i03]=c01x;
	a1y[i03]=c01y;
      }
      }
      }
      }
      }
      }
      break;
    }
    }
 
  }
 }
      int cnt2=0;
  for(int j=0;j<197;j++){
   for(int i=0;i<197;i++){
     double r01111=(x22[j]-x11[i]);
     double r01112=(y22[j]-y11[i]);
     double r01113=sqrt(r01111*r01111+r01112*r01112);
     if(r01113<c){
       cnt2+=1;
       break;
     }
   }
  }
  int cntt2=cnt2*3;
  if(cntt2>0){
  int cnt4=0;
  double a2x[cntt2];
  double a2y[cntt2];
      for(int j=0;j<197;j++){
  for(int i=0.0;i<197;i++){
    double r1111=(x22[j]-x11[i]);
    double r1112=(y22[j]-y11[i]);
    double r1113=sqrt(r1111*r1111+r1112*r1112);
    if(r1113<c){
      cnt4+=1;
      for(int l4=0;l4<1;){
      double a02x=gRandom->Rndm()*1.752-0.876;
      double a02y=gRandom->Rndm()*1.752-0.876;
      double a02z=gRandom->Rndm()*1.752-0.876;
      double sr21=a02x*a02x+a02y*a02y+a02z*a02z;
      if(sqrt(sr21)<0.876){
	l4+=1;
	int j01=(cnt4-1)*3;
	a2x[j01]=a02x;
	a2y[j01]=a02y;
	for(int l5=0;l5<1;){
      double b02x=gRandom->Rndm()*1.752-0.876;
      double b02y=gRandom->Rndm()*1.752-0.876;
      double b02z=gRandom->Rndm()*1.752-0.876;
      double sr22=b02x*b02x+b02y*b02y+b02z*b02z;
      if(sqrt(sr22)<0.876){
	l5+=1;
	int j02=(cnt4-1)*3+1;
	a2x[j02]=b02x;
	a2y[j02]=b02y;
	for(int l6=0;l6<1;){
      double c02x=gRandom->Rndm()*1.752-0.876;
      double c02y=gRandom->Rndm()*1.752-0.876;
      double c02z=gRandom->Rndm()*1.752-0.876;
      double sr23=c02x*c02x+c02y*c02y+c02z*c02z;
      if(sqrt(sr23)<0.876){
	int j03=(cnt4-1)*3+2;
	a2x[j03]=c02x;
	a2y[j03]=c02y;
      }
      }
      }
	}
      }
      }
      break;
    }
  }
      }
  }
      double sumxs1=0.0;
      double sumxxs1=0.0;
      double sumys1=0.0;
      double sumyys1=0.0;
      double sumxys1=0.0;

      for(int i=0;i<cntt1;i++){
	for(int j=0;j<cntt2;j++){
	  double sr01=(a1x[i]-a2x[j]);
	  double sr02=(a1y[i]-a2y[j]);
	  double sr03=sqrt(sr01*sr01+sr02*sr02);
	  if(sr03<c11){
	    double xs11=a1x[i];
	    sumxs1+=xs11;
	    double xxs11=(a1x[i])*(a1x[i]);
	    sumxxs1+=xxs11;
	    double ys11=a1y[i];
	    sumys1+=ys11;
	    double yys11=(a1y[i])*(a1y[i]);
	    sumyys1+=yys11;
	    double xys11=(a1x[i])*(a1y[i]);
	    sumxys1+=xys11;
	    break;
	  }
	}
      }
      double sumxs2=0.0;
      double sumxxs2=0.0;
      double sumys2=0.0;
      double sumyys2=0.0;
      double sumxys2=0.0;
      for(int j=0;j<cntt2;j++){
	for(int i=0;i<cntt1;i++){
	  double sr04=(a2x[j]-a1x[i]);
	  double sr05=(a2y[j]-a1y[i]);
	  double sr06=sqrt(sr04*sr04+sr05*sr05);
	  if(sr06<c11){
	    double xs21=a2x[j];
	    sumxs2+=xs21;
	    double xxs21=(a2x[j])*(a2x[j]);
	    sumxxs2+=xxs21;
	    double ys21=a2y[j];
	    sumys2+=ys21;
	    double yys21=(a2y[j])*(a2y[j]);
	    sumyys2+=yys21;
	    double xys21=(a2x[j])*(a2y[j]);
	    sumxys2+=xys21;
	    break;
	  }
	}
      }
      
       
	  if((cnt1+cnt2)>0){
   
  cntk[k]=cnt1+cnt2;
  double xxmean=(sumxxs1+sumxxs2)/((double)cntt1+(double)cntt2);
  double xmean=(sumxs1+sumxs2)/((double)cntt1+(double)cntt2);
  double yymean=(sumyys1+sumyys2)/((double)cntt1+(double)cntt2);
  double ymean=(sumys1+sumys2)/((double)cntt1+(double)cntt2);
  double xymean=(sumxys1+sumxys2)/((double)cntt1+(double)cntt2);
  double sigx=xxmean-(xmean)*(xmean);
  double sigy=yymean-(ymean)*(ymean);
  double sigxy=xymean-(xmean)*(ymean);
  double x011=(sigy-sigx)*(sigy-sigx)+4.0*sigxy*sigxy;
  double x01=sqrt(x011);
  double y01=sigy+sigx;
   double ip1=x01/y01;
  ep[k]=ip1;
  k+=1;
  }  
  }
   }
 }

 double dum1;
 double dum2;
 for(int k=0;k<total;++k){
   for(int l=k+1;l<total;++l){
     if(cntk[k]>cntk[l]){
       dum1=cntk[k];
       cntk[k]=cntk[l];
       cntk[l]=dum1;

       dum2=ep[k];
       ep[k]=ep[l];
       ep[l]=dum2;
     }
   }
 }
 double cntsepa[20];
 for(int m=0;m<20;m++){
   double sumcnt=0.0;
   for(int n=500*m;n<500*(m+1);n++){
     sumcnt+=cntk[n];
   }
   cntsepa[m]=sumcnt/500.0;
 }
 double epsepa[20];
 for(int m=0;m<20;m++){
   double sumep=0.0;
   for(int n=500*m;n<500*(m+1);n++){
     sumep+=ep[n];
   }
   epsepa[m]=sumep/500.0;
   double sum11=0.0;
   for(int o=500*m;o<500*(m+1);o++){
     sum11+=(ep[o]-epsepa[m])*(ep[o]-epsepa[m]);
   }
   double sd=sqrt(sum11/(500.0-1.0)/500.0);
   g1->SetPointError(19-m,0,sd);
   g1->SetPoint(m,cntsepa[m],epsepa[m]);
 }

 
 
 
 g1->Draw("AP");
}

I don’t know, you’ll have to take a closer look at your code, but on the surface it seems that there are lots of nested loops, maybe that’s what’s taking so long, and if it finishes without errors then there’s probably something wrong in the logic. You can try printing more messages at some key places to check where the code is while it runs.

You are stuck in that infinite loop:

                           for(int l6=0;l6<1;){
                              double c02x=gRandom->Rndm()*1.752-0.876;
                              double c02y=gRandom->Rndm()*1.752-0.876;
                              double c02z=gRandom->Rndm()*1.752-0.876;
                              double sr23=c02x*c02x+c02y*c02y+c02z*c02z;
                              if(sqrt(sr23)<0.876){
                                 int j03=(cnt4-1)*3+2;
                                 a2x[j03]=c02x;
                                 a2y[j03]=c02y;
                              }
                           }

l6 is always 0.

thank you very much.
I made some other corrections, but "Warning in : Inf / NaN propagated to the pad. Check drawn objects.
Warning in : c1 height changed from 0 to 10 "is displayed and the graph cannot be drawn.
I am very sorry for all the questions.

{
  gStyle->SetMarkerStyle(20);
  gStyle->SetMarkerSize(0.7);
  double R=1.18*pow(197.0,(1.0/3.0))-0.48;
  double a=0.54;
int n=197;
double x11[197];
double y11[197];
 double x22[197];
 double y22[197];
 double cntk[100];
 double ep[100];
 int total=100;
 double R1=1.2*pow(197,1.0/3.0);
double c=sqrt(4.2/3.14);
 double c11=sqrt(0.81/3.14);
gRandom->SetSeed();
 TGraphErrors *g1=new TGraphErrors;
 g1->SetMarkerColor(kBlack);
 for(int k=0;k<total;){
   
   for(int l=0;l<1;){
 double b=gRandom->Rndm()*22.0-11.0;
 double b5=gRandom->Rndm()*22.0-11.0;
 if(b*b+b5*b5<(R1+2.0)*(R1+2.0)){
 
 l+=1;
  

  for(int i=0;i<n;){
    double x1=gRandom->Rndm()*16.0-8.0;
    double x01=x1-b;
    double y1=gRandom->Rndm()*16.0-8.0;
    double y01=y1-b5;
    double z1=gRandom->Rndm()*16.0-8.0;
    double r1=sqrt(x1*x1+y1*y1+z1*z1);
    double r3=gRandom->Rndm();
    double rho1=1.0/(1+exp((r1-R)/a));
    if(r3<rho1){
    
    x11[i]=x01;
    y11[i]=y01;
    i+=1;
    }
  }
for(int j=0;j<n;){
   double x2=gRandom->Rndm()*16.0-8.0;
   double x02=x2+b;
    double y2=gRandom->Rndm()*16.0-8.0;
    double y02=y2+b5;
    double z2=gRandom->Rndm()*16.0-8.0;
    double r2=sqrt(x2*x2+y2*y2+z2*z2);
    double r4=gRandom->Rndm();
    double rho2=1.0/(1+exp((r2-R)/a));
    if(r4<rho2){
    
    x22[j]=x02;
    y22[j]=y02;
    j+=1;

    }
 }
 int cnt1=0;
for(int i=0;i<197;i++){
  for(int j=0;j<197;j++){
    double r1111=(x11[i]-x22[j]);
    double r1112=(y11[i]-y22[j]);
    double r1113=sqrt(r1111*r1111+r1112*r1112);
    if(r1113<c){
      cnt1+=1;
      break;
    }
  }
 }

      int cnt2=0;
  for(int j=0;j<197;j++){
   for(int i=0;i<197;i++){
     double r01111=(x22[j]-x11[i]);
     double r01112=(y22[j]-y11[i]);
     double r01113=sqrt(r01111*r01111+r01112*r01112);
     if(r01113<c){
       cnt2+=1;
       break;
     }
   }
  }


  if((cnt1+cnt2)>0){ 
 int cntt1=cnt1*3;
  double a1x[cntt1];
  double a1y[cntt1];
  int cnt3=0.0;
      for(int i=0;i<197;i++){
  for(int j=0.0;j<197;j++){
    double r1111=(x11[i]-x22[j]);
    double r1112=(y11[i]-y22[j]);
    double r1113=sqrt(r1111*r1111+r1112*r1112);
    if(r1113<c){
      cnt3+=1;
      for(int l1=0;l1<1;){
      double a01x=gRandom->Rndm()*1.752-0.876;
      double a01y=gRandom->Rndm()*1.752-0.876;
      double a01z=gRandom->Rndm()*1.752-0.876;
      double sr11=a01x*a01x+a01y*a01y+a01z*a01z;
      if((sqrt(sr11))<0.876){
	l1+=1;
	int i01=(cnt3-1)*3;
	a1x[i01]=x11[i]+a01x;
	a1y[i01]=y11[i]+a01y;
	for(int l2=0.0;l2<1;){
      double b01x=gRandom->Rndm()*1.752-0.876;
      double b01y=gRandom->Rndm()*1.752-0.876;
      double b01z=gRandom->Rndm()*1.752-0.876;
      double sr12=b01x*b01x+b01y*b01y+b01z*b01z;
      if(sqrt(sr12)<0.876){
	l2+=1;
	int i02=(cnt3-1)*3+1;
	a1x[i02]=x11[i]+b01x;
	a1y[i02]=y11[i]+b01y;
	for(int l3=0;l3<1;){
      double c01x=gRandom->Rndm()*1.752-0.876;
      double c01y=gRandom->Rndm()*1.752-0.876;
      double c01z=gRandom->Rndm()*1.752-0.876;
      double sr13=c01x*c01x+c01y*c01y+c01z*c01z;
      if(sqrt(sr13)<0.876){
	l3+=1;
	int i03=(cnt3-1)*3+2;
	a1x[i03]=x11[i]+c01x;
	a1y[i03]=y11[i]+c01y;
      }
      }
      }
      }
      }
      }
      break;
    }
    }
 
  
 }


  int cntt2=cnt2*3;
  int cnt4=0;
  double a2x[cntt2];
  double a2y[cntt2];
      for(int j=0;j<197;j++){
  for(int i=0.0;i<197;i++){
    double r1111=(x22[j]-x11[i]);
    double r1112=(y22[j]-y11[i]);
    double r1113=sqrt(r1111*r1111+r1112*r1112);
    if(r1113<c){
      cnt4+=1;
      for(int l4=0;l4<1;){
      double a02x=gRandom->Rndm()*1.752-0.876;
      double a02y=gRandom->Rndm()*1.752-0.876;
      double a02z=gRandom->Rndm()*1.752-0.876;
      double sr21=a02x*a02x+a02y*a02y+a02z*a02z;
      if(sqrt(sr21)<0.876){
	l4+=1;
	int j01=(cnt4-1)*3;
	a2x[j01]=x22[j]+a02x;
	a2y[j01]=y22[j]+a02y;
	for(int l5=0;l5<1;){
      double b02x=gRandom->Rndm()*1.752-0.876;
      double b02y=gRandom->Rndm()*1.752-0.876;
      double b02z=gRandom->Rndm()*1.752-0.876;
      double sr22=b02x*b02x+b02y*b02y+b02z*b02z;
      if(sqrt(sr22)<0.876){
	l5+=1;
	int j02=(cnt4-1)*3+1;
	a2x[j02]=x22[j]+b02x;
	a2y[j02]=y22[j]+b02y;
	for(int l6=0;l6<1;){
      double c02x=gRandom->Rndm()*1.752-0.876;
      double c02y=gRandom->Rndm()*1.752-0.876;
      double c02z=gRandom->Rndm()*1.752-0.876;
      double sr23=c02x*c02x+c02y*c02y+c02z*c02z;
      if(sqrt(sr23)<0.876){
	l6+=1;
	int j03=(cnt4-1)*3+2;
	a2x[j03]=x22[j]+c02x;
	a2y[j03]=y22[j]+c02y;
      }
      }
      }
	}
      }
      }
      break;
    }
  
      }
  }
      double sumxs1=0.0;
      double sumxxs1=0.0;
      double sumys1=0.0;
      double sumyys1=0.0;
      double sumxys1=0.0;

      for(int i=0;i<cntt1;i++){
	for(int j=0;j<cntt2;j++){
	  double sr01=(a1x[i]-a2x[j]);
	  double sr02=(a1y[i]-a2y[j]);
	  double sr03=sqrt(sr01*sr01+sr02*sr02);
	  if(sr03<c11){
	    double xs11=a1x[i];
	    sumxs1+=xs11;
	    double xxs11=(a1x[i])*(a1x[i]);
	    sumxxs1+=xxs11;
	    double ys11=a1y[i];
	    sumys1+=ys11;
	    double yys11=(a1y[i])*(a1y[i]);
	    sumyys1+=yys11;
	    double xys11=(a1x[i])*(a1y[i]);
	    sumxys1+=xys11;
	    break;
	  }
	}
      }
      double sumxs2=0.0;
      double sumxxs2=0.0;
      double sumys2=0.0;
      double sumyys2=0.0;
      double sumxys2=0.0;
      for(int j=0;j<cntt2;j++){
	for(int i=0;i<cntt1;i++){
	  double sr04=(a2x[j]-a1x[i]);
	  double sr05=(a2y[j]-a1y[i]);
	  double sr06=sqrt(sr04*sr04+sr05*sr05);
	  if(sr06<c11){
	    double xs21=a2x[j];
	    sumxs2+=xs21;
	    double xxs21=(a2x[j])*(a2x[j]);
	    sumxxs2+=xxs21;
	    double ys21=a2y[j];
	    sumys2+=ys21;
	    double yys21=(a2y[j])*(a2y[j]);
	    sumyys2+=yys21;
	    double xys21=(a2x[j])*(a2y[j]);
	    sumxys2+=xys21;
	    break;
	  }
	}
      }
      
       
   
  cntk[k]=cnt1+cnt2;
  double xxmean=(sumxxs1+sumxxs2)/((double)cntt1+(double)cntt2);
  double xmean=(sumxs1+sumxs2)/((double)cntt1+(double)cntt2);
  double yymean=(sumyys1+sumyys2)/((double)cntt1+(double)cntt2);
  double ymean=(sumys1+sumys2)/((double)cntt1+(double)cntt2);
  double xymean=(sumxys1+sumxys2)/((double)cntt1+(double)cntt2);
  double sigx=xxmean-(xmean)*(xmean);
  double sigy=yymean-(ymean)*(ymean);
  double sigxy=xymean-(xmean)*(ymean);
  double x011=(sigy-sigx)*(sigy-sigx)+4.0*sigxy*sigxy;
  double x01=sqrt(x011);
  double y01=sigy+sigx;
   double ip1=x01/y01;
  ep[k]=ip1;
  k+=1;
  }  
  }
   }
 }

 double dum1;
 double dum2;
 for(int k=0;k<total;++k){
   for(int l=k+1;l<total;++l){
     if(cntk[k]>cntk[l]){
       dum1=cntk[k];
       cntk[k]=cntk[l];
       cntk[l]=dum1;

       dum2=ep[k];
       ep[k]=ep[l];
       ep[l]=dum2;
     }
   }
 }
 double cntsepa[20];
 for(int m=0;m<20;m++){
   double sumcnt=0.0;
   for(int n=5*m;n<5*(m+1);n++){
     sumcnt+=cntk[n];
   }
   cntsepa[m]=sumcnt/5.0;
 }
 double epsepa[20];
 for(int m=0;m<20;m++){
   double sumep=0.0;
   for(int n=5*m;n<5*(m+1);n++){
     sumep+=ep[n];
   }
   epsepa[m]=sumep/5.0;
   double sum11=0.0;
   for(int o=5*m;o<5*(m+1);o++){
     sum11+=(ep[o]-epsepa[m])*(ep[o]-epsepa[m]);
   }
   double sd=sqrt(sum11/(5.0-1.0)/5.0);
   g1->SetPointError(19-m,0,sd);
   g1->SetPoint(m,cntsepa[m],epsepa[m]);
 }

 
 
 
 g1->Draw("AP");
}

I’ll look …
Note that your code is not intended at all. That makes it very hard to read.

I put g1->Print() before g1->Draw. and I get:

x[0]=2, y[0]=nan, ex[0]=0, ey[0]=0.0297169
x[1]=2.8, y[1]=nan, ex[1]=0, ey[1]=0.0471107
x[2]=3.4, y[2]=nan, ex[2]=0, ey[2]=0.0257794
x[3]=6.6, y[3]=0.907549, ex[3]=0, ey[3]=0.0375041
x[4]=11.2, y[4]=0.784905, ex[4]=0, ey[4]=0.059923
x[5]=15.2, y[5]=0.743175, ex[5]=0, ey[5]=0.0458565
x[6]=20.8, y[6]=0.849444, ex[6]=0, ey[6]=0.0659703
x[7]=24.6, y[7]=0.583883, ex[7]=0, ey[7]=0.0592383
x[8]=29.4, y[8]=0.542479, ex[8]=0, ey[8]=0.0669605
x[9]=45.4, y[9]=0.536931, ex[9]=0, ey[9]=0.0524587
x[10]=66.4, y[10]=0.610833, ex[10]=0, ey[10]=0.0881171
x[11]=94.2, y[11]=0.459016, ex[11]=0, ey[11]=0.040139
x[12]=106.2, y[12]=0.464858, ex[12]=0, ey[12]=0.106752
x[13]=128.2, y[13]=0.40539, ex[13]=0, ey[13]=0.0402122
x[14]=151.2, y[14]=0.349312, ex[14]=0, ey[14]=0.0928197
x[15]=182.2, y[15]=0.332079, ex[15]=0, ey[15]=0.0657537
x[16]=209.4, y[16]=0.313629, ex[16]=0, ey[16]=0.0332497
x[17]=244.4, y[17]=0.233602, ex[17]=0, ey[17]=nan
x[18]=273.8, y[18]=0.226763, ex[18]=0, ey[18]=nan
x[19]=334.6, y[19]=0.15374, ex[19]=0, ey[19]=nan

As you can see your vectors are full of nan … you should debug and fix it.

solved!
I’m really thankful to you!!
That was a really big help!!

1 Like

Good ! Do not forget to indent your code, that will simplify your life :wink: !

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.