/**PosGen_UFO
 * contact person:  Mark Stringer <ms711@sussex.ac.uk>
 */

#include <Randomize.hh>

#include <RAT/GLG4StringUtil.hh>

#include <sstream>
#include <string>
using namespace std;
#include <RAT/DB.hh>
#include <RAT/PosGen_UFO.hh>
#include <RAT/Log.hh>
using namespace RAT;

PosGen_UFO::PosGen_UFO(const std::string& dbname) : GLG4PosGen( dbname )
{
    // Not yet initialised - call the Init function to setup on first event
    fInitDB=false;
}

void PosGen_UFO::Init()
{
    // This function should be run once on the first event (once DB all set up)
    warn << "PosGen_UFO::Init: Initialising simulation settings \n";

    // Load database values
    DBLinkPtr lUFO = DB::Get()->GetLink("UFOGEO");


    fPosition.setX(lUFO->GetD("position_x"));
    fPosition.setY(lUFO->GetD("position_y"));
    fPosition.setZ(lUFO->GetD("position_z"));
    info <<  "PosGen_UFO::Init: Setting position from db to "<< (float) fPosition.x() <<" "
        << (float) fPosition.y() << " "<< (float) fPosition.z()<<"\n";

    fInitDB = true;


}

void PosGen_UFO::BeginOfRun() {
  Init();
}

void PosGen_UFO::GeneratePosition( G4ThreeVector& argResult )
{
    if(!fInitDB)Init();
    argResult = fPosition;
}

void PosGen_UFO::SetState( G4String newValues )
{
    Log::Die("SetState is deprecated, do not call it. Set all variables with /rat/db/set.");
    return;

}

G4String PosGen_UFO::GetState() const
{

  stringstream result;
  result << "UFO at (" << fPosition.x() << ", " << fPosition.y() << ", " << fPosition.z() << ")";
  return result.str();
}