Bool_t gApacheExec=1; Bool_t gApacheNoCache=kTrue;

// HistogramSelect.C
// This file provides a drop down list of all the histogram for a particular test for
// a particular version which the user has chosen in the last page. the options which 
// are available to the user now are:
// Draw Histogram
// Overplot Histogram with Standard
// KS test for chosen histogram
// KS test for all histograms in current file
// Below these choices is a drop down list which specifies the various histogram files
// against which the chosen histogram can be compared. The default standard is the one
// specified in the information for each test
 
#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>
#include <TKey.h>
#include <string.h>



void HistogramSelect()
{

  TApache ap;

  // Get the systest_id, standardtest_id and the histogram_file
  TString histfile_T = gSystem->Getenv("hf");
  TString systest_id_T = gSystem->Getenv("systestid");
  TString stdtest_id_T = gSystem->Getenv("stdtestid");

  // Setting up the HTML form
  gApache->Puts("
<html>
 <head>
   <meta HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=iso8859-1\">
   <meta NAME=\"hsimple7.C\" Content=\"Display histograms on a web page\">

   <title>Histogram Display</title>
 </head>

 <body bgcolor=\"darkolivegreen\" topmargin=\"0\" leftmargin=\"0\" marginwidth=\"5\" marginheight=\"5\">
");


  // converting the TString values to strings
  char *hist_file = histfile_T.Data();
  char *systest_id = systest_id_T.Data();
  char *stdtest_id = stdtest_id_T.Data();

  // This is the histogram file of the test
  TFile *histfile = new TFile("");
  TList *pKeyList = gDirectory->GetListOfKeys();
  TObject *pObj;
  TKey *pKey ;
  TIter it(pKeyList);
  
  gApache->Puts("<h4>Display histograms stored in a root file</h4>");
  gApache->Puts("<center><form method=\"get\" action=\"HistogramDisplay.C\" name=\"histo4form\">");  
  gApache->Puts("<P>Choose the histogram <BR><SELECT NAME=\"filename\">");
  //gApache->Puts(hist_file);
  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"))
	    {
	      gApache->Puts("<OPTION>");
	      ap.Printf(((TH1*)pObj)->GetName());
	   
	    }// end if
	  
	} //end if
      else if (pClassName == "TDirectory")
	{
	  TDirectory *pSubDir = (TDirectory*)pKey->ReadObj();
	  fillListFromDir(pList, pSubDir);
	  
	}// end else
    }// end while
  
 
  gApache->Puts("</SELECT></P>");
  gApache->Puts("<BR><BR>");

  // These are the radio buttons to determine the following
  // Draw Histogram
  // Overplot Histogram with Standard
  // KS test for chosen histogram
  // KS test for all histograms in current file
  
  gApache->Puts("<INPUT TYPE=\"radio\" NAME=\"histover\" VALUE=\"1\" CHECKED>");
  gApache->Puts("Draw Histogram");
  gApache->Puts("<BR>");
  gApache->Puts("<INPUT TYPE=\"radio\" NAME=\"histover\" VALUE=\"2\" UNCHECKED>");
  gApache->Puts("Compare to Standard");
  gApache->Puts("<BR>");
  gApache->Puts("<INPUT TYPE=\"radio\" NAME=\"histover\" VALUE=\"3\" UNCHECKED>");
  gApache->Puts("Kolmogorov Test for this histogram");
  gApache->Puts("<BR>");
  gApache->Puts("<INPUT TYPE=\"radio\" NAME=\"histover\" VALUE=\"4\" UNCHECKED>");
  gApache->Puts("Kolmogorov Test for all histograms in the current file");

  gApache->Puts("<BR><BR>");

  // This is a drop down list of the versions which are present as standard and one
  // value in the default which is the one specified as the standardtest_id
  // for that test

  gApache->Puts("<P>Choose a Standard <BR><SELECT NAME=\"standard\">");
  gApache->Puts("<OPTION>");
  ap.Printf("default");

  // Now for the set of standards

  char *connection="oracle://sage/SLAC_TCP";
  char *user="GLAST_DATA";
  char *pass="FLIGHT06";

  char *sql = "select stdt.standardtest_id from system_tests syst, standard_tests stdt where stdt.systest_id = syst.systest_id";

  // connect to oracle server
  TSQLServer *db=TSQLServer::Connect(connection, user, pass);
  TSQLResult *res=db->Query(sql);   
  TSQLRow *row1=res->Next();

  // Display the list of standard tests available
  do{
    
    gApache->Puts("<OPTION>");
    ap.Printf(row1->GetField(0));
    
  }while(res->Next());

  gApache->Puts("</SELECT></P>");
  gApache->Puts("<BR><BR>");


  // This is to pass the name of the histogram file as a hidden value
  char *hiddenvalue1 = "<INPUT TYPE = \"hidden\" NAME = \"histfilename\" VALUE = \""; 
  char *hiddenvalue2 ="\">";
  
  char *histfilename = new char[200];
  std::strcpy(histfilename, hiddenvalue1);
  std::strcat(histfilename, hist_file);
  std::strcat(histfilename, hiddenvalue2);
 
  // This is to pass the id of the standard test as a hidden value
  gApache->Puts(histfilename);
 
  char *hiddenvalue3 = "<INPUT TYPE = \"hidden\" NAME = \"stdtestid\" VALUE = \""; 
  char *hiddenvalue2 ="\">";
  char *standard = new char[200];
  std::strcpy(standard, hiddenvalue3);
  std::strcat(standard, stdtest_id);
  std::strcat(standard, hiddenvalue2);
  
  gApache->Puts(standard);
     
  gApache->Puts("<input type=\"submit\" value=\"Display Result\"><BR></center></form></body></html>");
  

  delete[] histfilename;
  delete[] standard;


}// end HistogramSelect

