double sigmoidPrime(double aVal){
  double sigOut;
  sigOut = 1.0/(1.0 + pow(aVal,2));
  return sigOut;
}
void nnTestManyNodes(){
  gROOT->Reset();
  gStyle->SetPalette(1);
  gROOT->SetStyle("Plain");
  gStyle->SetOptStat(0);
  char *s = new char[1];

  //GET RANDOM
  UInt_t mySeed = 3; 
  TRandom3 *myRandGen = new TRandom3(mySeed);
  Double_t myRandVal;
  
  int nToPlot = 100;
  int nToPlot1 = 0;
  int nToPlot2 = 0;

  double vecX[100];
  double vecY[100];
  double vecEX[100];
  double vecEY[100];

  double vecX1[100];
  double vecY1[100];
  double vecEX1[100];
  double vecEY1[100];

  double vecX2[100];
  double vecY2[100];
  double vecEX2[100];
  double vecEY2[100];

  int vecT[100];

  for (int myInt = 0; myInt < nToPlot; myInt++){
    double myXVal = myRandGen->Uniform(0.0,1.0);
    double myYVal = myRandGen->Uniform(0.0,1.0);
    int tVal = 0;
    vecX[myInt] = myXVal;       
    vecY[myInt] = myYVal;       
    vecEX[myInt] = 0.0; 
    vecEY[myInt] = 0.0; 

    if (myYVal > -myXVal + 1.0 && myYVal < myXVal) {
      vecX1[nToPlot1] = myXVal; 
      vecY1[nToPlot1] = myYVal; 
      vecEX1[nToPlot1] = 0.0;   
      vecEY1[nToPlot1] = 0.0;   
      tVal = 1;
      nToPlot1++;
    } else {
      vecX2[nToPlot2] = myXVal; 
      vecY2[nToPlot2] = myYVal; 
      vecEX2[nToPlot2] = 0.0;   
      vecEY2[nToPlot2] = 0.0;   
      nToPlot2++;
    }
    vecT[myInt] = tVal; 
  }

  double w1 = 1.0;
  double w2 = 0.3;
  double theta1 = 0.7;
  double theta2 = 0.5;
  double alpha = .00001;
  double lambda = .0;
  bool runMe = true;

  TGraphErrors *myGraph = new TGraphErrors(nToPlot,vecX,vecY,vecEX,vecEY);
  TGraphErrors *myGraph1 = new TGraphErrors(nToPlot1,vecX1,vecY1,vecEX1,vecEY1);
  TGraphErrors *myGraph2 = new TGraphErrors(nToPlot2,vecX2,vecY2,vecEX2,vecEY2);

  myGraph->Draw();

  myGraph->SetMarkerStyle(27);
  myGraph->SetMarkerColor(3);
  myGraph1->SetMarkerStyle(20);
  myGraph1->SetMarkerColor(2);
  myGraph2->SetMarkerStyle(21);
  myGraph2->SetMarkerColor(4);

  TLine *myTrueLine = new TLine(0,1,1,0);
  myTrueLine->SetLineWidth(2);
  myTrueLine->SetLineColor(1);

  double x1 = 0.0;
  double x2 = 1.0;
  double y1 = theta1/w2 - x1*w1/w2;
  double y2 = theta1/w2 - x2*w1/w2;
  TLine *InitialLine = new TLine(x1,y1,x2,y2);
  y1 = theta2/w2 - x1*w1/w2;
  y2 = theta2/w2 - x2*w1/w2;
  TLine *SecondInitialLine = new TLine(x1,y1,x2,y2);
  
  
  //training time

  //need a hidden node

  //needs pattern change
  int tmpCount = 0;
  double ErrBefore = 0;
  double WrongnessBefore = 0;
  double cut = .0001;
  while(runMe == true){
    double dEx = 0;
    double dEy = 0;
    double cut = .0001;
    double ErrAve = 0;
    double dw = 0;
    double WrongnessAve = 0;
    for(int i = 0; i < nToPlot; ++i)
      {
	double a = w1 * vecX[i] + w2 * vecY[i] - theta1;
	double tValTmp = vecT[i];
	double yVal = 0;
	double tValTmpOld = tValTmp;
	if (tValTmp < 0) tValTmpOld = 0; 
	if (a > 0) yVal = 1;

	double alphaPrime = alpha*sigmoidPrime(a);
	w1 = w1 + alphaPrime*(tValTmp-yVal)*vecX[i] + lambda*dw*vecX[i];
	w2 = w2 + alphaPrime*(tValTmp-yVal)*vecY[i] + lambda*dw*vecY[i];
	theta1 = theta1 - alpha*(tValTmp-yVal);
	dEx = -(tValTmp-a)*vecX[i];
	dEy = -(tValTmp-a)*vecY[i];
	ErrAve += pow(tValTmp-a,2);
	WrongnessAve += pow(tValTmpOld-yVal,2);
	dw = alphaPrime*(tValTmp-a);
    }  

    tmpCount++;
    ErrAve = ErrAve/nToPlot;
    WrongnessAve = WrongnessAve/nToPlot;
    cout<<"ErrAve = "<<ErrAve<<" Err Change = " << ErrAve-ErrBefore<<endl;
    cout<<"WrongnessAve = "<<WrongnessAve<<" Wrongness Change = " << WrongnessAve-WrongnessBefore<<endl;

    if (pow(ErrAve-ErrBefore,2) < pow(cut,2)) runMe = false;
    ErrBefore = ErrAve;
    WrongnessBefore = WrongnessAve;
  }
  
  y1 = theta1/w2 - x1*w1/w2;
  y2 = theta1/w2 - x2*w1/w2;
  cout<<"line = "<< (y2-y1)/(x2-x1)<<"x + " <<y1<<endl;
  TLine *betterLine = new TLine(x1,y1,x2,y2);

  y1 = theta2/w2 - x1*w1/w2;
  y2 = theta2/w2 - x2*w1/w2;  
  TLine *betterLine2 = new TLine(x1,y1,x2,y2);


  betterLine->SetLineWidth(2);
  betterLine->SetLineColor(7);
  betterLine2->SetLineWidth(2);
  betterLine2->SetLineColor(9);

  InitialLine->SetLineWidth(2);
  InitialLine->SetLineColor(3);
  SecondInitialLine->SetLineWidth(3);
  SecondInitialLine->SetLineColor(6);


  c1->Clear();
  c1->Divide(2,2);
  c1->cd(1); 
  myGraph->Draw("ap");
  c1->cd(2); 
  myGraph1->Draw("ap");
  c1->cd(3); 
  myGraph2->Draw("ap");
  c1->cd(4); 
  myGraph->Draw("ap");
  myGraph1->Draw("psame");  
  myGraph2->Draw("psame"); 
  //  myTrueLine->Draw();
  InitialLine->Draw();
  SecondInitialLine->Draw();
  betterLine->Draw();
  betterLine2->Draw();

}
