/**
 * \file RunGlue.cpp
 * \author Emiliano Mocchiutti
 * 
 * The file contains implementation of the methods to query the DB.
 */
//
#include <sstream>
#include <iostream>
//
#include <TFile.h>
#include <TTree.h>
#include <TTimeStamp.h>
#include <RunGlue.h>
//
ClassImp(RunGlue);
//
using namespace std;

RunGlue::RunGlue() {
  this->Clear();
}

RunGlue::RunGlue(TSQLServer *da, UInt_t ru, TString di, TString wrkdi) {
  fDBG = false;
  li = new PamLevel2();
  this->Clear();
  dbc = da;
  run = ru;
  dir = di;
  //
  TString wd = gSystem->WorkingDirectory();  
  //
  outdir = wrkdi;
  if ( !strcmp(outdir.Data(),"") ) outdir = wd;
  fList = 0;
  if ( run ){
    runmode = true;
  } else {
    runmode = false;
    createlist = true;
  };
  //
  //  lList = new TList();
  //
};


void RunGlue::Clear() {
  fEnd = false;
  run = 0;
  dir = "";
  outdir = "";
  fFilename = "";
};

void RunGlue::Clean() {
  if ( !merged ) gSystem->Unlink(fFilename);
  lList->Delete();
  delete Target;
};

void RunGlue::SetDebug(Bool_t dbg) {
  fDBG = dbg;
};

void RunGlue::SetDList(TString list) {
  //
  RUN = true;
  //
  if (list.Contains("-RUN", TString::kIgnoreCase)) RUN = false;
  if (list.Contains("-ALL", TString::kIgnoreCase)) RUN = false;
  if (list.Contains("+RUN", TString::kIgnoreCase)) RUN = true;
  if (list.Contains("+ALL", TString::kIgnoreCase)) RUN = true;
  //
  fDList = list;
  li->SetWhichTrees(fDList);
  //
  if ( DebugMode() ) printf(" Detector list is %s \n",fDList.Data());
  //
};

TList *RunGlue::GetRunList(){
  //
  lList = new TList();
  lList->Clear();
  TString thisrun;
  TFile *su;
  //
  TSQLResult *pResult;
  TSQLRow *Row;
  //
  if ( run && runmode ){
    //
    // Qurey the GL_RUN table to obtain RUN infos
    //
    GL_RUN *glrun = new GL_RUN();
    glrun->Query_GL_RUN(run,dbc);
    //
    // convert RUNHEADER_TIME into a UTC date
    //
    GL_TIMESYNC *dbtime = new GL_TIMESYNC();
    //
    TString UTC="UTC";
    TString rhdate=dbtime->ConvertTime(UTC,glrun->RUNHEADER_TIME);
    //
    TDatime ti = TDatime(rhdate.Data());
    //
    YY=(UInt_t)ti.GetYear();
    MM=(UInt_t)ti.GetMonth();
    DD=(UInt_t)ti.GetDay();
    //
    TTimeStamp *llim = new TTimeStamp(YY,MM,DD,0,0,0,0,true,0);
    //
    UInt_t lowerlimit = llim->GetSec();
    UInt_t upperlimit = lowerlimit + 86401;
    //
    stringstream myquery;
    myquery.str("");
    myquery << "SELECT ID FROM GL_RUN WHERE RUNHEADER_TIME>=" << (UInt_t)lowerlimit
	    << " AND RUNHEADER_TIME<" << (UInt_t)upperlimit << " ORDER BY RUNHEADER_TIME ASC;";
    //
    pResult = dbc->Query(myquery.str().c_str());
    for( UInt_t r=0; r < 1000; r++){ 
      Row = pResult->Next();      
      if( Row == NULL ) break;
      if ( DebugMode() ) printf(" %u RUN %s \n",r,Row->GetField(0));
      //
      thisrun=dir+(TString)Row->GetField(0)+".Level2.root";      
      //
      if ( DebugMode() ) printf(" Filename is %s \n",thisrun.Data());
      //
      su = TFile::Open(thisrun);
      if ( li->CheckLevel2File(thisrun) ){
	lList->Add(su);
      } else {
	if ( DebugMode() ) printf(" RUN %s DISCARDED \n",Row->GetField(0));
      };
      //
    };
    //
    if ( DebugMode() ) printf(" YY %u MM %u DD %u ll %u ul %u \n",YY,MM,DD,lowerlimit,upperlimit);
    //
    YY -= 2000;
    stringstream tmpf;
    tmpf.str("");
    tmpf << "L2PAM";
    if ( YY < 10 ){
      tmpf<< "0" << YY;
    } else {
      tmpf<< YY;
    };
    if ( MM < 10 ){
      tmpf<< "0" << MM;
    } else {
      tmpf<< MM;
    };
    if ( DD < 10 ){
      tmpf << "0" << DD << ".root";
    } else {
      tmpf << DD << ".root";
    };
    TString fpath = "";
    if ( !strcmp(outdir,"") ){
      fpath="./";
    } else {
      fpath += outdir;
      fpath += "/";
    };
    //
    fFilename = fpath + tmpf.str().c_str(); 
    //
    printf(" Output filename is %s \n",fFilename.Data());
    //
    delete glrun;
    delete dbtime;
    //
  };
  //
  if ( strcmp(dir.Data(),"" )){
    //
    if ( createlist ){
      fList = new TList();
      fDoneList = new TList();
      fNlist = 0;
      fNlistdone = 0;
      //
      // read files in the directory
      //
      if ( DebugMode() ) printf("\n No input run given\n Check for existing root files. \n");
      //    
      TSystemDirectory *datadir = new TSystemDirectory(gSystem->BaseName(dir),dir);
      TList *temp = datadir->GetListOfFiles();
      //
      TIter next(temp);
      TSystemFile *questo = 0;
      //
      while ( (questo = (TSystemFile*)next()) ) {
	TString name =  questo->GetName();
	if( name.EndsWith(".Level2.root") ){
	  char *fullpath0;
	  gSystem->IsFileInIncludePath(name,&fullpath0);
	  TString fullpath = fullpath0;
	  //
	  if ( DebugMode() ) printf(" fullpath = %s     name %s \n",fullpath.Data(),name.Data());
	  //
	  //
	  su = TFile::Open(fullpath);
	  if ( li->CheckLevel2File((TString)fullpath) ){
	    fList->Add(su);
	    fNlist++;
	  } else {
	    if ( DebugMode() ) printf(" RUN %s DISCARDED \n",fullpath.Data());
	  };
	  //
	};
      };
      createlist = false;
      nrun = 0;
    };
    //
    //
    //
    Bool_t stop = true;
    UInt_t drun = 0;
    while ( nrun < fNlist && stop ){
      TString myrun = (TString)((TString)(gSystem->BaseName(((TFile*)fList->At(nrun))->GetName()))).ReplaceAll(".Level2.root",12,"",0);
      if ( DebugMode() ) printf(" nrun %u myrun is %s \n",nrun,(myrun.ReplaceAll(".Level2.root",12,"",0)).Data());
      run = (UInt_t)(myrun.Atoi());
      nrun++;
      //
      TString donerun = "";
      for (UInt_t ll=0; ll<fNlistdone; ll++){	
	donerun = "";
	donerun = (TString)((TString)(gSystem->BaseName(((TFile*)fDoneList->At(ll))->GetName()))).ReplaceAll(".Level2.root",12,"",0);
	if ( DebugMode() ) printf(" donerun is %s \n",(donerun.ReplaceAll(".Level2.root",12,"",0)).Data());	
	drun = (UInt_t)(donerun.Atoi());
	//
	if ( run == drun ) goto jump;
	//
      };
      //
      stop = false;
      //
    jump:
      drun = 0;
      //
    };
    //
    if ( nrun >= fNlist && stop ){
      fEnd = true;
      return(0);
    };
    //
    // Qurey the GL_RUN table to obtain RUN infos
    //
    printf(" PROCESSING RUN %u AND ITS FRIENDS\n",run);
    GL_RUN *glrun = new GL_RUN();
    glrun->Query_GL_RUN(run,dbc);
    //
    // convert RUNHEADER_TIME into a UTC date
    //
    GL_TIMESYNC *dbtime = new GL_TIMESYNC();
    //
    TString UTC="UTC";
    TString rhdate=dbtime->ConvertTime(UTC,glrun->RUNHEADER_TIME);
    //
    TDatime ti = TDatime(rhdate.Data());
    //
    YY=(UInt_t)ti.GetYear();
    MM=(UInt_t)ti.GetMonth();
    DD=(UInt_t)ti.GetDay();
    //
    TTimeStamp *llim = new TTimeStamp(YY,MM,DD,0,0,0,0,true,0);
    //
    UInt_t lowerlimit = llim->GetSec();
    UInt_t upperlimit = lowerlimit + 86401;
    //
    stringstream myquery;
    myquery.str("");
    myquery << "SELECT ID FROM GL_RUN WHERE RUNHEADER_TIME>=" << (UInt_t)lowerlimit
	    << " AND RUNHEADER_TIME<" << (UInt_t)upperlimit << " ORDER BY RUNHEADER_TIME ASC;";
    //
    pResult = dbc->Query(myquery.str().c_str());
    for( UInt_t r=0; r < 1000; r++){ 
      Row = pResult->Next();      
      if( Row == NULL ) break;
      if ( DebugMode() ) printf(" %u RUN %s \n",r,Row->GetField(0));
      //
      thisrun=dir+(TString)Row->GetField(0)+".Level2.root";      
      //
      if ( DebugMode() ) printf(" Filename is %s \n",thisrun.Data());
      //
      su = TFile::Open(thisrun);
      if ( su ){
	TFile *su0 = TFile::Open(thisrun);
	fDoneList->Add(su0);
	fNlistdone++;
      };
      if ( li->CheckLevel2File(thisrun) ){
	lList->Add(su);
	if ( DebugMode() ) printf(" RUN %s ADDED \n",Row->GetField(0));
      } else {
	if ( DebugMode() ) printf(" RUN %s DISCARDED \n",Row->GetField(0));
      };
      //
    };
    //
    if ( DebugMode() ) printf(" YY %u MM %u DD %u ll %u ul %u \n",YY,MM,DD,lowerlimit,upperlimit);
    //
    YY -= 2000;
    stringstream tmpf;
    tmpf.str("");
    tmpf << "L2PAM";
    if ( YY < 10 ){
      tmpf<< "0" << YY;
    } else {
      tmpf<< YY;
    };
    if ( MM < 10 ){
      tmpf<< "0" << MM;
    } else {
      tmpf<< MM;
    };
    if ( DD < 10 ){
      tmpf << "0" << DD << ".root";
    } else {
      tmpf << DD << ".root";
    };
    TString fpath = "";
    if ( !strcmp(outdir,"") ){
      fpath="./";
    } else {
      fpath += outdir;
      fpath += "/";
    };
    //
    fFilename = fpath + tmpf.str().c_str(); 
    //
    printf(" Output filename is %s \n",fFilename.Data());
    //
    if ( DebugMode() ){
      UInt_t ll = 0;
      while ( (TFile*)lList->At(ll) ){
	TString donerun = "";
	donerun = (TString)((TString)(gSystem->BaseName(((TFile*)lList->At(ll))->GetName()))).ReplaceAll(".Level2.root",12,"",0);
	printf(" XXX file is %s \n",donerun.Data());      
	ll++;
	//
      };
    };
    //
    delete glrun;
    delete dbtime;
    //
  };
  //
  // is it the end?
  //
  if ( runmode ){
    fEnd = true;
  } else {
    //
    // did we use all the run in the directory? if so exit
    //
    if ( nrun >= fNlist ) fEnd = true;
  };
  //
  return(lList);
};

Bool_t RunGlue::OpenFile(){
  //
  printf(" Check if output file already exists \n");
  Target = TFile::Open((this->GetFilename()).Data(), "READ" );
  //
  if ( Target ){ 
    Target->Close();
    printf("Error opening target file, %s  already exist!\n",(this->GetFilename()).Data());
    return(false);
  } else {
    //
    printf(" Output file does not exist, creating it\n");
    //
    Long64_t maxsize = 10000000000LL;  
    //
    Target = TFile::Open((this->GetFilename()).Data(), "RECREATE" );
    fastMethod = kTRUE;
    //fastMethod = kFALSE;
    //    
    //    
    if ( !Target || Target->IsZombie()) {
      printf("Error opening target file (does %s exist?)\n",(this->GetFilename()).Data());
      exit(1);
    }
    //
    TTree::SetMaxTreeSize(maxsize);
    //
    return(true);
    //
  };
  //
  return(true);
  //
};


void RunGlue::DeleteRunFiles(TList *dlist){
  //
  TString donerun = "";
  //
  printf(" Deleting runs: \n");
  //
  UInt_t ll = 0;
  //
  while ( (TFile*)dlist->At(ll) ){
    donerun = (TString)(((TFile*)dlist->At(ll))->GetName());
    gSystem->Unlink(donerun);
    printf(" Deleted file is %s \n",donerun.Data());      
    ll++;
    //
  };
};

void RunGlue::UpdateDB(TList *dlist){
  //
  TSQLResult *pResult;
  TSQLRow *Row;
  stringstream myquery;
  //
  TString donerun = "";
  //
  if ( DebugMode() ) printf(" Updating GL_RUN: \n");
  //
  UInt_t ll = 0;
  UInt_t idl2 = 0;
  //
  //
  myquery.str("");
  myquery << "insert into GL_ROOT (ID_RAW,PATH,NAME) values (4294967295,'" << outdir.Data() << "','" << ((TString)gSystem->BaseName(this->GetFilename())).Data() << "');";
  if ( DebugMode() ) printf(" query is %s \n",myquery.str().c_str());
  //
  pResult = dbc->Query(myquery.str().c_str());
  if ( !pResult ){
    printf(" ERROR WRITING ON DB!\n");
    return;
  };
  myquery.str("");
  myquery << "select ID from GL_ROOT where NAME='" << ((TString)gSystem->BaseName(this->GetFilename())).Data() << "';";
  if ( DebugMode() ) printf(" query is %s \n",myquery.str().c_str());
  //
  pResult = dbc->Query(myquery.str().c_str());
  if ( !pResult ){
    printf(" ERROR WRITING ON DB!\n");
    return;
  };
  Row = pResult->Next();      
  if( Row == NULL ){
    printf(" ERROR QUERYING THE DB!\n");
    return;
  } else {
    idl2 = (UInt_t)atoll(Row->GetField(0));
  };
  //
  //
  //
  UInt_t drun = 0;
  //
  while ( (TFile*)dlist->At(ll) ){
    donerun = "";
    donerun = (TString)((TString)(gSystem->BaseName(((TFile*)dlist->At(ll))->GetName()))).ReplaceAll(".Level2.root",12,"",0);
    drun = (UInt_t)(donerun.Atoi());
    //
    if ( DebugMode() ) printf(" Run is %s \n",donerun.Data());      
    //
    myquery.str("");
    myquery << "update GL_RUN set ID_ROOT_L2=" << idl2 << " where ID=" << drun << ";";
    if ( DebugMode() ) printf(" query is %s \n",myquery.str().c_str());
    //
    pResult = dbc->Query(myquery.str().c_str());
    if ( !pResult ){
    printf(" ERROR WRITING ON DB!\n");
    return;
    };    
    //
    ll++;
    //
  };
};

//
// method stolen to ROOT's "hadd" and modified
//
void RunGlue::MergeRootfile(TList *sourcelist) {
  //
  merged = false;
  //
  TDirectory *target = Target;
  //
  if ( DebugMode() ) printf("\nTarget path is: %s \n",target->GetPath());
  //
  TString path( (char*)strstr( target->GetPath(), ":" ) );
  path.Remove(0,2);
  //
  TDirectory *first_source = (TDirectory*)sourcelist->First();
  THashList allNames;
  //
  while ( first_source ){
    //
    TDirectory *current_sourcedir = first_source->GetDirectory(path);
    //
    if ( !current_sourcedir ){
      first_source = (TDirectory*)sourcelist->After(first_source);
      continue;
    }
    //
    // loop over all keys in this directory
    //
    TChain *globChain = 0;
    TIter nextkey( current_sourcedir->GetListOfKeys() );
    TKey *key, *oldkey=0;
    TH1::AddDirectory(kFALSE); // gain time, do not add the objects in the list in memory
    //
    while ( (key = (TKey*)nextkey()) ) {
      //
      if ( current_sourcedir == target ) break;
      //
      if (oldkey && !strcmp(oldkey->GetName(),key->GetName())) continue; //keep only the highest cycle number for each key
      //
      if (allNames.FindObject(key->GetName())) continue;
      //
      if ( DebugMode() ) printf(" Key name is -%s- \n",key->GetName());
      //
      //
      //
      if ( !strcmp(key->GetName(),"Tracker") && !li->TRK2 && !li->TRK1 && !li->TRKh ) continue;
      //
      if ( !strcmp(key->GetName(),"Calorimeter") && !li->CAL2 && !li->CAL1 ) continue;
      //
      if ( !strcmp(key->GetName(),"ToF") && !li->TOF ) continue;
      //
      if ( !strcmp(key->GetName(),"Trigger") && !li->TRG ) continue;
      //
      if ( !strcmp(key->GetName(),"Anticounter") && !li->AC ) continue;
      //
      if ( !strcmp(key->GetName(),"S4") && !li->S4 ) continue;
      //
      if ( !strcmp(key->GetName(),"NeutronD") && !li->ND ) continue;
      //
      if ( !strcmp(key->GetName(),"OrbitalInfo") && !li->ORB ) continue;
      //
      if ( !strcmp(key->GetName(),"Run") && !RUN ) continue;
      //
      if ( strcmp(key->GetName(),"Calorimeter") && strcmp(key->GetName(),"Tracker") && strcmp(key->GetName(),"ToF") && strcmp(key->GetName(),"Trigger") && strcmp(key->GetName(),"Anticounter") && strcmp(key->GetName(),"S4") && strcmp(key->GetName(),"NeutronD") && strcmp(key->GetName(),"OrbitalInfo") && strcmp(key->GetName(),"Run") && strcmp(key->GetName(),"ProcessID0") ){
	if ( DebugMode() ) printf(" ERROR UNKNOWN KEY %s !\n",key->GetName());
	continue;
      };
      //
      allNames.Add(new TObjString(key->GetName()));
      //
      // read object from first source file
      //
      current_sourcedir->cd();
      TObject *obj = key->ReadObj();
      //
      if ( obj->IsA()->InheritsFrom("TTree") ){
	//
      	// loop over all source files create a chain of Trees "globChain"
	//
	TString obj_name;
	//
	if ( path.Length() ) {
	  obj_name = path + "/" + obj->GetName();
	} else {
	  obj_name = obj->GetName();
	};
	//
	globChain = new TChain(obj_name);
	//
	globChain->Add(first_source->GetName());
	//
	TFile *nextsource = (TFile*)sourcelist->After( first_source );
	//
	while ( nextsource ) {     	  
	  //
	  //do not add to the list a file that does not contain this Tree
	  //
	  TFile *curf = TFile::Open(nextsource->GetName());
	  //
	  if ( curf ) {
	    //
	    Bool_t mustAdd = kFALSE;
	    //
	    if (curf->FindKey(obj_name)) {
	      //
	      mustAdd = kTRUE;
	      //
	    } else {
	      //
	      //we could be more clever here. No need to import the object
	      //we are missing a function in TDirectory
	      //
	      TObject *aobj = curf->Get(obj_name);
	      //
	      if ( aobj ){
		mustAdd = kTRUE;
		delete aobj;
	      };
	    };
	    if (mustAdd) {
	      globChain->Add(nextsource->GetName());
	    };
	  };
	  delete curf;
	  nextsource = (TFile*)sourcelist->After(nextsource);
	};
	//
      } else {
	//
	// object is of no type that we know or can handle
	//
	if ( DebugMode() ) cout << "Unknown object type, name: " 
	     << obj->GetName() << " title: " << obj->GetTitle() << endl;
      };
      //
      // now write the merged histogram (which is "in" obj) to the target file
      // note that this will just store obj in the current directory level,
      // which is not persistent until the complete directory itself is stored
      // by "target->Write()" below
      //
      if ( obj ){
	//
	target->cd();
	//
	if( obj->IsA()->InheritsFrom("TTree") ) {
	  //
	  //
	  if ( fastMethod ){
	    globChain->Merge(target->GetFile(),0,"keep fast");
	  } else {
            globChain->Merge(target->GetFile(),0,"keep");
	  };
	  //
	  merged = true;
	  //
	  if ( DebugMode() ) globChain->ls();
	  //
	  delete globChain;
	  //
	} else {
	  //
	  //	  obj->Write( key->GetName() ); // <==================================================================================
	  //
	};
      };
      oldkey = key;
    };
    //
    first_source = (TDirectory*)sourcelist->After(first_source);
    //
  };
  // save modifications to target file
  target->SaveSelf(kTRUE);
  //
}
