/********************************************************************************
 *    Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH    *
 *                                                                              *
 *              This software is distributed under the terms of the             * 
 *         GNU Lesser General Public Licence version 3 (LGPL) version 3,        *  
 *                  copied verbatim in the file "LICENSE"                       *
 ********************************************************************************/
Int_t  sql_params_read()
{
  // Create a Runtime Database singleton.
  FairRuntimeDb* db = FairRuntimeDb::instance();

  // Set the SQL IO as first input
  FairParTSQLIo* inp = new FairParTSQLIo();
  
 
  // Verbosity level
  inp->SetVerbosity(1);

  inp->open();
  db->setFirstInput(inp);

  // Create the container via the factory if not already created
  FairDbTutPar* p1 = (FairDbTutPar*)(db->getContainer("TUTParDefault"));
  FairDbTutPar* p2 = (FairDbTutPar*)(db->getContainer("TUTParAlternative"));


  // Create a dummy runID using date in UTC from which
  // corresponding  parameters will be initialised

  ValTimeStamp tStamp(2015,02,20,10,10,10);
  UInt_t runId = tStamp.GetSec();
  cout << "-I- looking for parameters at runID# " << runId << endl;
  cout << "-I- corresponding  time in runID (UTC) " << tStamp.Format("iso") << endl;

  // Use the generated RunID to initialised the parameter
  // using the SQL-based IO input
  db->initContainers(runId);


  // Get the container after initialisation
  // from the RuntimeDB
  FairDbTutPar* pp1 = (FairDbTutPar*)(db->getContainer("TUTParDefault"));
  FairDbTutPar* pp2 = (FairDbTutPar*)(db->getContainer("TUTParAlternative"));


  cout << endl;
  cout << "-I- Reading Parameter data from SQL Database: \n" << endl;
  cout << endl;

  pp1->Print();
  pp2->Print();

  cout << endl;
 


  if (db) delete db;
  return 0;
}
Int_t  calgeo_read()
{
  // Create a Runtime Database singleton.
  FairRuntimeDb* db = FairRuntimeDb::instance();

  // Set the SQL IO as first input
  FairParTSQLIo* inp = new FairParTSQLIo();
  
  // Verbosity level
  //inp->SetVerbosity(1);

  inp->open();
  db->setFirstInput(inp);

   // Create in memory the relevant container
  R3BCaloGeometryPar* par = (R3BCaloGeometryPar*)(db->getContainer("CaloGeometryPar"));

  // Create a dummy runID using date in UTC from which
  // corresponding  parameters will be initialised

  ValTimeStamp tStamp(2014,03,21,18,00,00);
  UInt_t runId = tStamp.GetSec();
  cout << "-I- looking for parameters at runID# " << runId << endl;
  cout << "-I- corresponding  time in runID (UTC) " << tStamp.Format("iso") << endl;

  // Use the generated RunID to initialised the parameter
  // using the SQL-based IO input
  db->initContainers(runId);


  // Get the container after initialisation
  // from the RuntimeDB
  R3BCaloGeometryPar* par = (R3BCaloGeometryPar*)(db->getContainer("CaloGeometryPar"));


  cout << endl;
  cout << "-I- Reading Parameter data from SQL Database: \n" << endl;
  cout << endl;

  par->Print();

  cout << endl;
 


  if (db) delete db;
  return 0;
}
Int_t  califa_test()
{
  // Create a Runtime Database singleton.
  FairRuntimeDb* db = FairRuntimeDb::instance();

   // Create in memory the relevant container
  R3BCaloCalPar* par = (R3BCaloCalPar*)(db->getContainer("CaloCalPar"));  

  // Set the SQL IO as first input
  FairParAsciiFileIo* inp = new FairParAsciiFileIo();

  TString filename ="ducals.par";
  inp->open(filename.Data(),"in");

  db->setFirstInput(inp);

// Generate a unique RunID
  FairRunIdGenerator runID;
  UInt_t runId =  runID.generateId();
  db->initContainers(runId);


  // Get the container after initialisation
  // from the RuntimeDB
  R3BCaloCalPar* par = (R3BCaloCalPar*)(db->getContainer("CaloCalPar"));

  // Dump the Parameters
  cout << endl;
  cout << "-I- Reading Parameter data from Ascii File: \n" << filename.Data() << endl;
  cout << endl;

  par->Print();
  cout << endl;

  // Convert in ROOT format 
  par->setChanged();
  Bool_t kParameterMerged = kTRUE;
  FairParRootFileIo* parOut = new FairParRootFileIo(kParameterMerged);
  parOut->open("califa_cal_par.root");
  db->setOutput(parOut);
  db->saveOutput();
  db->print();
  // ------------------------------------------------------------------------

  if (db) delete db;
  return 0;
}
/********************************************************************************
 *    Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH    *
 *                                                                              *
 *              This software is distributed under the terms of the             * 
 *         GNU Lesser General Public Licence version 3 (LGPL) version 3,        *  
 *                  copied verbatim in the file "LICENSE"                       *
 ********************************************************************************/
Int_t  sql_params_read_bin()
{
  // ----  Load libraries   -------------------------------------------------
  gROOT->LoadMacro("$VMCWORKDIR/gconfig/basiclibs.C");
  basiclibs();
  gSystem->Load("libGenVector");
  gSystem->Load("libGeoBase");
  gSystem->Load("libFairDB");
  gSystem->Load("libParBase");
  gSystem->Load("libBase");
  gSystem->Load("libMCStack");
  gSystem->Load("libField");
  gSystem->Load("libTutorial5");


  // Create a Runtime Database singleton.
  FairRuntimeDb* db = FairRuntimeDb::instance();

  // Set the SQL IO as first input
  FairParTSQLIo* inp = new FairParTSQLIo();
  // Verbosity level
  inp->SetVerbosity(1);

  inp->open();
  db->setFirstInput(inp);

  // Create the container via the factory if not already created
  FairDbTutParBin* p1 = (FairDbTutParBin*)(db->getContainer("TUTParBin"));

  p1->Print();

  // Create a dummy runID using date in UTC from which
  // corresponding  parameters will be initialised

  ValTimeStamp tStamp(2015,02,20,10,07,48);
  UInt_t runId = tStamp.GetSec();
  cout << "-I- looking for parameters at runID# " << runId << endl;
  cout << "-I- corresponding  time in runID (UTC) " << tStamp.AsString("c") << endl;

  // Use the generated RunID to initialised the parameter
  // using the SQL-based IO input
  db->initContainers(runId);


 

  cout << endl;
  cout << "-I- Initialisation from SQL Database:" << endl;
  cout << endl;

 // Get the container after initialisation
  // from the RuntimeDB
  FairDbTutParBin* pp1 = (FairDbTutParBin*)(db->getContainer("TUTParBin"));
  pp1->Print();

  cout << endl;


  if (db) delete db;
  return 0;
}
/********************************************************************************
 *    Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH    *
 *                                                                              *
 *              This software is distributed under the terms of the             * 
 *         GNU Lesser General Public Licence version 3 (LGPL) version 3,        *  
 *                  copied verbatim in the file "LICENSE"                       *
 ********************************************************************************/
Int_t  sql_params_write_bin()
{
  // ----  Load libraries   -------------------------------------------------
  gROOT->LoadMacro("$VMCWORKDIR/gconfig/basiclibs.C");
  basiclibs();
  gSystem->Load("libGenVector");
  gSystem->Load("libGeoBase");
  gSystem->Load("libFairDB");
  gSystem->Load("libParBase");
  gSystem->Load("libBase");
  gSystem->Load("libMCStack");
  gSystem->Load("libTutorial5");

  // Generate a unique RunID
  FairRunIdGenerator runID;
  UInt_t runId =  runID.generateId();

  FairRuntimeDb* db = FairRuntimeDb::instance();
  cout << "-I- FairRuntimeDb created ----> " << db << endl;

  // Create in memory the relevant container
  FairDbTutParBin* p1 = (FairDbTutParBin*)(db->getContainer("TUTParBin"));

  // Set the Ascii IO as first input
  FairParAsciiFileIo* inp1 = new FairParAsciiFileIo();

  //TString work = getenv("VMCWORKDIR");
  TString filename = "ascii-example_bin.par";
  inp1->open(filename.Data(),"in");
  db->setFirstInput(inp1);

  // Set the SQL based IO as second input
  FairParTSQLIo* inp2 = new FairParTSQLIo();
  inp2->open();
  db->setSecondInput(inp2);


  // <INIT> containers from Ascii input
  // with assigned  RunId
  db->initContainers(runId);

  // Additionnally  prime some dummy values to the data members that are not
  // initialized using the ascii input file .
 
  p1->FillDummy();

  cout << endl;
  cout << "\n  -I- FAIRDB: RuntimeDB::init from Ascii File done using RunID# " <<  runId << endl;
  cout << endl;

  // <WRITE> back containers to the user-defined
  // Database using the Sql based IO of the
  // second input.

  db->setOutput(inp2);
  db->writeContainers();

  cout << endl;
  cout << "-I-  FAIRDB: RuntimeDB Parameters successfully written to DB with RunID# " << runId << endl;
  cout << endl;

  if (db) delete db;
  return 0;
}