#include <TH1.h>
#include <TSQLServer.h>
#include <TSQLRow.h>
#include <TSQLResult.h>
#include <TROOT.h>
#include <TObject.h>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>

#include "SqlRoot.h"

ClassImp(SqlRoot)
  
using namespace std;

//-----------------------------------------------------------
SqlRoot::SqlRoot(char* db, char* uid, char* pw)  
{
  nrows=0;
  srv = TSQLServer::Connect(db, uid, pw);
  histo=new TH1F("histo", "histogram", 3000, 0, 3000);
}

//-----------------------------------------------------------
char * SqlRoot::itoa(int x){ 
  char * ret = new char[128];
  sprintf(ret, "%d", x);
  return ret;
}

//-----------------------------------------------------------
TH1F * SqlRoot::Getxy_histo(char *x, char *y)
{
  const char *field=new char[10];
  float emin, emax, fvalue;
  char *query = new char[100];
  
  strcpy(query, "select * from test where x=");
  strcat(query, x);
  strcat(query, " and y=");
  strcat(query, y);
  
  result = srv->TSQLServer::Query(query);
  nrows = result->TSQLResult::GetRowCount();
  
  emax = 0;
  emin = 999999;
  for(int i=0; i<nrows; i++)
    {
      row = result->TSQLResult::Next();
      field = row->TSQLRow::GetField(3);
      fvalue = atoi(field);
      
      if( fvalue>emax ) emax=fvalue;
      if( fvalue<emin ) emin=fvalue;
      histo->Fill(fvalue);
    }
  return histo;
}

//-----------------------------------------------------------
TH1F * SqlRoot::Getdxdy_histo(char *xmin, char *xmax, char *ymin, char *ymax)
{
  histo=(TH1F*)gROOT->FindObject("histo");
  if(histo)delete histo;
  histo=new TH1F("histo", "histogram", 300, 0, 3000);
  const char *field=new char[10];
  float emin, emax, fvalue;
  char *query = new char[100];
  char * xt = NULL;
  char * yt = NULL;
  
  int xmint, xmaxt, ymint, ymaxt;
  xmint = atoi(xmin);
  xmaxt = atoi(xmax);
  ymint = atoi(ymin);
  ymaxt = atoi(ymax);
  
  for(int i = 0; i <= (xmaxt-xmint);i++){
    
    xt = "";
    xt = itoa(xmint + i);
    for(int j = 0; j <= (ymaxt-ymint);j++){
      query = "";
      yt = "";
      yt = itoa(ymint + j);
      
      strcpy(query, "select * from casa_4634 where x=");
      strcat(query, xt);
      strcat(query, " and y=");
      strcat(query, yt);
      std::cout << query << std::endl;
      result = srv->TSQLServer::Query(query);
      nrows = result->TSQLResult::GetRowCount();
      
      emax = 0;
      emin = 999999;
      for(int k=0; k<nrows; k++)
	{
	  row = result->TSQLResult::Next();
	  field = row->TSQLRow::GetField(3);
	  fvalue = atoi(field);
	  
	  if( fvalue>emax ) emax=fvalue;
	  if( fvalue<emin ) emin=fvalue;
	  histo->Fill(fvalue);
	}
      
    }
  }
  histo->SetBins((int)(emax - emin), emin, emax);
  delete xt;
  delete yt;
  return histo;
  
}

//-----------------------------------------------------------
TH1F * SqlRoot::Getdxdy_subhisto(char *xmin, char *xmax, char *ymin, char *ymax, float emin, float emax){
  histo=(TH1F*)gROOT->FindObject("histo");
  if(histo)delete histo;
  histo=new TH1F("histo", "histogram", 300, 0, 3000);
  const char *field=new char[10];
  float fvalue;
  char *query = new char[100];
  char * xt = NULL;
  char * yt = NULL;
  
  int xmint, xmaxt, ymint, ymaxt;
  xmint = atoi(xmin);
  xmaxt = atoi(xmax);
  ymint = atoi(ymin);
  ymaxt = atoi(ymax);
  
  for(int i = 0; i <= (xmaxt-xmint);i++){
    
    xt = "";
    xt = itoa(xmint + i);
    for(int j = 0; j <= (ymaxt-ymint);j++){
      query = "";
      yt = "";
      yt = itoa(ymint + j);
      
      strcpy(query, "select * from casa_4634 where x=");
      strcat(query, xt);
      strcat(query, " and y=");
      strcat(query, yt);
      std::cout << query << std::endl;
      result = srv->TSQLServer::Query(query);
      nrows = result->TSQLResult::GetRowCount();
      
      emax = 0;
      emin = 999999;
      for(int k=0; k<nrows; k++)
	{
	  row = result->TSQLResult::Next();
	  field = row->TSQLRow::GetField(3);
	  fvalue = atoi(field);
	  
	  if((fvalue > emin) && (fvalue < emax)){ 
	    histo->Fill(fvalue);
	  }
	}
      
    }
  }
  
  histo->SetBins((int)(emax - emin), emin, emax);
  delete xt;
  delete yt;
  return histo;
}

