Bool_t gApacheExec=1; Bool_t gApacheNoCache=kTrue;

// HistogramDisplay.C
// Depending on what choice the user had made the appropriate action is taken
// 1. Draw the histogram
//    The histogram selected by the user is plotted on a canvas
// 2. Comapre the histogram to the standard 
//    The user would have chosen whether he wants to comapre the histogram
//    to the default standard or some other standard and accordingly the original
//    histogram will be overplotted with the standard. The standard histogram will
//    be displayed in red
// 3. KS test for this histogram
//    Again the the KS results can be obtained by comparing to the default or
//    user chosen standard. The result only for this histogram is displayed
// 4. KS test for all histograms in this file
//    Same as Choice 3 except that the results are diaplayed for all the histograms
//    in the file 


#include <TApache.h>
#include <TCanvas.h>
#include <TH1.h>
#include <THashList.h>
#include <TIterator.h>
#include <TList.h>
#include <TObject.h>
#include <TFile.h>
#include <TRandom.h>
#include <TSystem.h>
#include <TFrame.h>
#include <TString.h>


void HistogramDiaplay()
{
  TApache ap;

  // Connect to the Oracle database
  char *connection="oracle://sage/SLAC_TCP";
  char *user="GLAST_DATA";
  char *pass="FLIGHT06";

  // This gets the name of the histogram object which has to be displayed
  TString qstr = gSystem->Getenv("filename");
  // Whether plain draw or comparison or KS results
  TString choice = gSystem->Getenv("histover");
  // Name of the histogram file 
  TString histstr = gSystem->Getenv("histfilename");
  // index of standard which is the default
  TString stdtest_id = gSystem->Getenv("stdtestid");  
  // get the standard version to be plotted
  TString std = gSystem->Getenv("standard");

  // The name of the canvas, title, top left coordinates and length (x and y)
  TCanvas* myCanvas = new TCanvas("myCanvas","A Simple Graph Example",300,20,800,700); 
  myCanvas->SetFillColor(11);
  myCanvas->GetFrame()->SetFillColor(30);
  myCanvas->GetFrame()->SetBorderSize(6);
  myCanvas->GetFrame()->SetBorderMode(-1);

  // This gets the name of the histogram root file in which we have to search for 
  // the histogram specified by filename
  
  char *histname= histstr->Data();
  TFile *histfile = new TFile(histname);

  // This reads the choice of the user
  // 1. Draw the histogram
  // 2. Draw the histogram overplotting with the standard chosen
  // 3. Give the results of the KS test of chosen histogram
  // 4. Give the results of the KS tests for all histograms in that file
  
  char *ch = choice.Data();
  int ch_int = std::atoi(ch);
  
  // Get the object with the name from the histogram file
  // Cast it to a histogram object
  TH1 *hpx= (TH1 *)histfile->Get(qstr);


  // These choices are to indicate that the user has chosen to either
  // display only the histogram or the histogram along with the standard
  if(ch_int == 1 || ch_int ==2){

    // Draw the original histogram
    hpx->Draw();
    
    // In order to overplot with histogram and the standard
    // First connect to database to get the filename of standard
  
    // Overplotting
    if(ch_int == 2){
          

      // Here the version to be compared with can either be the default standard 
      // or it can be any other file chosen from the set of standards

      // To compare to the default standard
      if((std.CompareTo("default"))==0){
	
	char *stdteststring = stdtest_id.Data();        
	char *sql1 = "select histogram_file from system_tests syst, standard_tests stdt where syst.systest_id = stdt.systest_id and stdt.standardtest_id =";
      
	char sql[200];
	
	// Generate the sql query
	std::strcpy(sql, sql1);
	std::strcat(sql, stdteststring);
      
	// connect to oracle server
	TSQLServer *db=TSQLServer::Connect(connection, user, pass);
	TSQLResult *res=db->Query(sql);   
	TSQLRow *row1=res->Next();
      
	// There is just one value returned
	TString  std_file = row1->GetField(0);
	// To strip off zeroes
	TString std_file_strip = std_file.Strip();
	char *std_hist_file_name = std_file_strip.Data();
	TFile *histfiledefault = new TFile(std_hist_file_name);
	
	// Draw the standard histogram on the same canvas
	TH1 *hpx = (TH1 *)histfiledefault->Get(qstr);
	hpx->SetLineColor(kRed);
	hpx->Draw("same");
	delete res;
	delete row1;
      
      }// end if the version for overplotting is the default 
      // we know the standardtest_id
      else{
	
	char *sql1 = "select histogram_file from system_tests syst, standard_tests stdt where stdt.systest_id = syst.systest_id and stdt.standardtest_id = ";
      
	char *sql2 = std.Data();
      	char sql[200];
      
	// Generate the sql query
	std::strcpy(sql, sql1);
	std::strcat(sql, sql2);
	
	// connect to oracle server
	TSQLServer *db=TSQLServer::Connect(connection, user, pass);
	TSQLResult *res=db->Query(sql);   
	TSQLRow *row1=res->Next();
	
	// There is just one value returned
	TString  std_file = row1->GetField(0);
	
	// To strip off zeroes
	TString std_file_strip = std_file.Strip();
	char *std_hist_file_name = std_file_strip.Data();
	TFile *histfilestd = new TFile(std_hist_file_name);
	
	// Draw the standard histogram on the same canvas
	TH1 *hpx = (TH1 *)histfilestd->Get(qstr);
	hpx->Draw("same");
	delete res;
	delete row1;
            
      }// end else
    }// end if
    ap.Put(myCanvas);
  }// end if ch_int == 1 || 2
  
  // This choice shows the KS test for this particular histogram
  // Again this could be the default standard or any other standard
  // the user chooses
  else if(ch_int == 3){

    // Here we have one histogram for the comparison and we need to determine
    // the other one--that could be either the default or some specified one

    if((std.CompareTo("default"))==0){

      char *stdteststring = stdtest_id.Data();        
      char *sql1 = "select histogram_file from system_tests syst, standard_tests stdt where syst.systest_id = stdt.systest_id and stdt.standardtest_id =";
      
      char sql[200];
      
      // Generate the sql query
      std::strcpy(sql, sql1);
      std::strcat(sql, stdteststring);
           
      // connect to oracle server
      TSQLServer *db=TSQLServer::Connect(connection, user, pass);
      TSQLResult *res_sql=db->Query(sql);   
      TSQLRow *row_sql=res_sql->Next();

      // There is just one value returned
      TString  std_file = row_sql->GetField(0);
      // To strip off zeroes
      TString std_file_strip = std_file.Strip();
      char *std_hist_file_name = std_file_strip.Data();
      

      TFile *histfilestd = new TFile(std_hist_file_name);
      
      TH1 *hpy = (TH1 *)histfilestd->Get(qstr);
      
      gApache->Puts("<h2><center>Kolmogorov Test</h2>");
      gApache->Puts("<br><br>");
      gApache->Puts("Histogram File: ");
      gApache->Puts(histname);
      gApache->Puts("<br>");
      gApache->Puts("Standard File: ");
      gApache->Puts(std_hist_file_name);
      gApache->Puts("<br>");
      gApache->Puts("Histogram Name: "); 
      gApache->Puts(qstr);
      gApache->Puts("<br><br>");
      gApache->Puts("Compatibility: ");
            
      double KT = hpx->KolmogorovTest((TH1 *)histfilestd->Get(qstr));
      char kt_char[10];
      std::sprintf(kt_char, "%f",KT);
      gApache->Puts(kt_char);     
    }// if we want to compare to the default histogram 
    else{
	
      char *sql1 = "select histogram_file from system_tests syst, standard_tests stdt where stdt.systest_id = syst.systest_id and stdt.standardtest_id = ";
      
      char *sql2 = std.Data();
      
      char sql[200];

      // Generate sql query
      std::strcpy(sql, sql1);
      std::strcat(sql, sql2);
	
      // connect to oracle server
      TSQLServer *db=TSQLServer::Connect(connection, user, pass);
      TSQLResult *res=db->Query(sql);   
      TSQLRow *row1=res->Next();

      // There is just one value returned
      TString  std_file = row1->GetField(0);
      // To strip off zeroes
      TString std_file_strip = std_file.Strip();
      char *std_hist_file_name = std_file_strip.Data();
      TFile *histfilestd = new TFile(std_hist_file_name);

      TH1 *hpy = (TH1 *)histfilestd->Get(qstr);

      gApache->Puts("<h2><center>Kolmogorov Test</h2>");
      gApache->Puts("<br><br>");
      gApache->Puts("Histogram File: ");
      gApache->Puts(histname);
      gApache->Puts("<br>");
      gApache->Puts("Standard File: ");
      gApache->Puts(std_hist_file_name);
      gApache->Puts("<br>");
      gApache->Puts("Histogram Name: "); 
      gApache->Puts(qstr);
      gApache->Puts("<br><br>");
      gApache->Puts("Compatibility: ");
            
      double KT = hpx->KolmogorovTest((TH1 *)histfilestd->Get(qstr));
      char kt_char[10];
      std::sprintf(kt_char, "%f",KT);
      gApache->Puts(kt_char);

    }// end else
  }// end of ch_int == 3 Kolmogorov Test
  
  // This choice is to display the KS test results for all the histograms in this file
  // The user has a choice between using the default or some other standard to compare with
  if(ch_int == 4){
    char *std_hist_file_name;
    std_hist_file_name = new char[250];
    
    if((std.CompareTo("default"))==0){
      
      char *stdteststring = stdtest_id.Data();        
      char *sql1 = "select histogram_file from system_tests syst, standard_tests stdt where syst.systest_id = stdt.systest_id and stdt.standardtest_id =";
      
      char sql[200];
	
      // Generate the sql query
      std::strcpy(sql, sql1);
      std::strcat(sql, stdteststring);
      
      // connect to oracle server
      TSQLServer *db=TSQLServer::Connect(connection, user, pass);
      TSQLResult *res=db->Query(sql);   
      TSQLRow *row1=res->Next();
      
      // There is just one value returned
      TString  std_file = row1->GetField(0);
      
      // To strip off zeroes
      TString std_file_strip = std_file.Strip();
      std_hist_file_name = std_file_strip.Data();
            
    }// If we want to compare it to default we have got the
    // name of the standard in std_hist_file_name
    else{

      char *sql1 = "select histogram_file from system_tests syst, standard_tests stdt where stdt.systest_id = syst.systest_id and stdt.standardtest_id = ";
      
      char *sql2 = std.Data();
      
      char sql[200];
      
      std::strcpy(sql, sql1);
      std::strcat(sql, sql2);
	
      // connect to oracle server
      TSQLServer *db=TSQLServer::Connect(connection, user, pass);
      TSQLResult *res=db->Query(sql);   
      TSQLRow *row1=res->Next();
	
      // There is just one value returned
      TString  std_file = row1->GetField(0);
	
      // To strip off zeroes
      TString std_file_strip = std_file.Strip();
      std_hist_file_name = std_file_strip.Data();
      
    }// We want another histogram for comparison
    
    gApache->Puts("<center><h2>Kolmogorov Test for all histograms</h2>");
    gApache->Puts("<br><br>");
    gApache->Puts("Histogram File: ");
    gApache->Puts(histname);
    gApache->Puts("<br>");
    gApache->Puts("Standard File: ");
    gApache->Puts(std_hist_file_name);
    gApache->Puts("<br>");

    // Here we have the name of the original histogram file in histname 
    // and the standard in std_hist_file_name

    // Iterate through all the histograms in the root file
        
    TFile *standardhistfile = new TFile(std_hist_file_name);
    TFile *histfile = new TFile(histname);
    TList *pKeyList = gDirectory->GetListOfKeys();
    TObject *pObj;
    TKey *pKey ;
    TIter it(pKeyList);

    while (pKey = (TKey*)it())
      {
	  
	TString pClassName(pKey->GetClassName());
	if(pClassName.BeginsWith("TH1") ||    // Cheap way of 
	   pClassName.BeginsWith("TH2") ||    // not reading objects 
	   pClassName.BeginsWith("TH3"))      // of types other than histos 
	    
	  { 
	    // object associated with the key is read into pObj 
	    pObj = pKey->ReadObj();
	    // if it inherits from TH1 it is a histogram
	    if (pObj->InheritsFrom("TH1"))
	      {

		char *histoname  =((TH1*)pObj)->GetName();
		gApache->Puts("<br><center>");
		gApache->Puts(histoname);
		gApache->Puts(": ");
		TH1 *hpx = (TH1 *)histfile->Get(histoname);
		double KT = hpx->KolmogorovTest((TH1 *)standardhistfile->Get(histoname));
		char kt_char[10];
		std::sprintf(kt_char, "%f",KT);
		gApache->Puts(kt_char);     
	      } 
	      
	  }  
	else if (pClassName == "TDirectory")
	  { 
	    TDirectory *pSubDir = (TDirectory*)pKey->ReadObj();
	    fillListFromDir(pList, pSubDir);
	  
	  }// end else
      }//  end while
      
    
  }// end of ch_int == 4

}// end of HistogramDiaplay


