Bool_t gApacheExec=1; Bool_t gApacheNoCache=kTrue;

// HistWithinThreshold.C
// Using the systest_id for that system test's histogram file we index
// into the metadata table and get the KS values of all the histograms
// in the file. The standard for every histogram is got from the
// standard_test_thresholds table and is compared with the KS value.
// Here in this file we display all the histograms which have their 
// KS values above the standard

#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 HistOutsideThreshold(){

  // connect to oracle server
  char *connection="oracle://sage/SLAC_TCP";
 //  char *user="LAT";
//   char *pass="LATREAD";
  char *user="glast_data";
  char *pass="flight06";
  TSQLServer *db=TSQLServer::Connect(connection, user, pass);

  // Get the system test id of the histogram root file
  TString systest_id = gSystem->Getenv("systestid");
  int systestid = std::atoi(systest_id.Data());

  // Get the name of the histogram file
  const char *temp_histfile_sql = "select histogram_file, standardtest_id from glast_data.system_tests where systest_id = %d";
  char histfile_sql[4096];
  std::sprintf(histfile_sql, temp_histfile_sql, systestid);


  // Query the database
  TSQLResult *res=db->Query(histfile_sql);   
  TSQLRow *row=res->Next();
  
  TString  hist_file = row->GetField(0);
  TString  stdtest_id = row->GetField(1);
  delete row;
  delete res;

  stdtestid = std::atoi(stdtest_id.Data());

  // Get the name of the standards file with which the comparison is done
  
  const char *temp_stdfile_sql = "select histogram_file from glast_data.system_tests syst, glast_data.standard_tests stdt where stdt.systest_id = syst.systest_id and stdt.standardtest_id = %d";
  char stdfile_sql[4096];
  std::sprintf(stdfile_sql, temp_stdfile_sql, stdtestid);
  
  // Query the database
  TSQLResult *res=db->Query(stdfile_sql);   
  TSQLRow *row=res->Next();
  TString  std_file = row->GetField(0);
  delete res;
  delete row;


  // Define a structure to hold the information of all the histograms
  // Later scan it over twice to first get histograms within their thresholds
  // and second time for the ones that fell outside their thresholds
  
  struct Aaa{
    float info;
    float info_value;
    char *histname;
    float thresh_info_value;
  };

  
  // Get the number of elements in this array of structures

  const char *temp_number_sql = "select count(meta_id) from glast_data.metadata m, glast_data.info_labels i where systest_id = %d and m.info = i.info and i.label like \'%KS\'";
  char number_sql[4096];
  std::sprintf(number_sql, temp_number_sql, systestid);

  TSQLResult *s = db->Query(number_sql);    
  TSQLRow *rows = s->Next();
  int num_meta = std::atoi(rows->GetField(0));  
  delete s;
  delete rows;
  
  struct Aaa *hist;
  
  // assign array size
  hist = (struct Aaa *)malloc(num_meta * sizeof(struct Aaa));

  // Now to get the info, info_value, label, thresh_info_value

  const char *temp_sql = "select m.info, m.info_value, i.label, s.info_value from glast_data.metadata m, glast_data.info_labels i, glast_data.standard_test_thresholds s where m.systest_id = %d and m.info = i.info and i.label like \'%KS\' and s.info=i.info and s.systest_id = m.systest_id and s.standardtest_id = %d";

  char sql[4096];
  
  std::sprintf(sql, temp_sql, systestid, stdtestid);
  TSQLResult *res = db->Query(sql);    
  TSQLRow *row = res->Next();
  i = 0;
  do{
    hist[i].info = std::atof(row->GetField(0));
    hist[i].info_value = std::atof(row->GetField(1));
    hist[i].thresh_info_value = std::atof(row->GetField(3));
    hist[i].histname = new char[30];
    TString hist1_file = row->GetField(2);
    TString hf = hist1_file.Strip();
    strcpy(hist[i].histname,hf.Data());
    i++;
  }
  while(res->Next());


  // Display the name of the histogram file and the standard file used

  gApache->Puts("<HTML><BODY><CENTER>");
  gApache->Puts("<BR><BR>");
  gApache->Puts("<h3> Histogram File :");
  gApache->Puts(hist_file.Data());
  gApache->Puts("<BR> Standard File :");  
  gApache->Puts(std_file.Data());
  gApache->Puts("</h3><BR><BR>");

  // Display the tables

  
  // Display the table for histograms with KS values within the threshold

  gApache->Puts("<BR><BR><BR><TABLE>");
  gApache->Puts("<CAPTION><h3>Histograms with KS values within the threshold</h3></CAPTION>");
  gApache->Puts("<TR>");
  gApache->Puts("<TH>Histogram   </TH>");
  gApache->Puts("<TH>KS Value    </TH>");
  gApache->Puts("<TH>   KS Threshold Value</TH>");
  gApache->Puts("</TR>");
 
  char ks[10];
  char thresh_ks[10];
  gApache->Puts("<TR>");

  for(i=0; i<num_meta; i++){
    
    if(hist[i].info_value < hist[i].thresh_info_value){
      
      gApache->Puts("<TD>");
      gApache->Puts(hist[i].histname);
      gApache->Puts("</TD>");
      std::sprintf(ks, "%f", hist[i].info_value);
      std::sprintf(thresh_ks, "%f", hist[i].thresh_info_value);
      gApache->Puts("<TD>");
      gApache->Puts(ks);
      gApache->Puts("</TD>"); 
      gApache->Puts("<TD>");
      gApache->Puts(thresh_ks);  
      gApache->Puts("</TD>");
      gApache->Puts("</TR>");
      std::strcpy(ks,"");
      std::strcpy(thresh_ks,"");
     
     
    }
  }// end for

  gApache->Puts("</TABLE>");

}// end of HistOutsideThreshold







