#include "TTree.h"
#include "TFile.h"
#include "TH1.h"
#include "TF1.h"
#include "TRandom3.h"
using namespace std;
#include "treeMaker.h"

void printUsage(); // print usage   

int main(int argc, char **argv){
  
  char  outFileName[150];
  char *argptr;
  
  //Set the default output file                                                 
  sprintf(outFileName,"./trainingTree.root");
  for (int i=1; i<argc; i++) {
    argptr = argv[i];
    if (*argptr == '-') {
      argptr++;
      switch (*argptr) {
      case 'h':
        printUsage();
        break;
      case 'o':
        strcpy(outFileName,++argptr);
        break;
      default:
        //fprintf(stderr,"\nUnrecognized argument: [-%s]\n",argptr);
        //printUsage();
        break;
      }
    } 
  }

  //DEFINE OUTPUT FILE
  TFile outFile(outFileName,"RECREATE");

  //DEFINE OUTPUT TREE
  TTree outTree("myTree","myTree");

  //DEFINE OUTPUT-TREE STRUCTURE AND SET BRANCHES                                         
  myTree_t myOutputTree;

  setBranchesHitToGo(&outTree,&myOutputTree);

  //Setup random generator
  UInt_t mySeed = 3;
  TRandom3 *myRandGen = new TRandom3(mySeed);


  for (int i = 0; i<10000; i++) {
    double x1Val = myRandGen->Uniform(0.0,1.0);
    double x2Val = myRandGen->Uniform(0.0,1.0);
    int tVal1 = 0;
    int tVal2 = 0;

    if (x2Val > -x1Val + 1.0) tVal1 = 1; 
    if (x2Val >  x1Val) tVal2 = 1;
    
    myOutputTree.x1 = x1Val;
    myOutputTree.x2 = x2Val;
    myOutputTree.tVal1 = tVal1;
    myOutputTree.tVal2 = tVal2;
    
    outTree.Fill();
  }
  outFile.Write();
  return 0;
}


void printUsage(){
  fprintf(stderr,"\nUsage:");
  fprintf(stderr,"\nmakeTree [-switches]\n");
  fprintf(stderr,"\nSWITCHES:\n");
  fprintf(stderr,"-h\tPrint this message\n");
  fprintf(stderr,"-o<arg>\tOutFileName. Default is tpolOutFile.root\n");
  
  std::cout<<"The current default operation is equivalent to the command:"<<std::endl;
  std::cout<<"makeTree -ooutFile.root"<<std::endl;
  
  exit(0);
}



