#include <G4Cons.hh>

#include <RAT/Cone.hh>
#include <RAT/Log.hh>
using namespace RAT;
using namespace RAT::geo;

#include <CLHEP/Units/SystemOfUnits.h>
#include <CLHEP/Units/PhysicalConstants.h>
#include <string>
using namespace CLHEP;
using namespace std;

G4VSolid*
Cone::Construct( const std::string& name,
                 DBLinkPtr table ) const
{
  const double rMax1 = table->GetD( "r_max1" ) * mm;
  const double rMax2 = table->GetD( "r_max2" ) * mm;
  const double sizeZ = table->GetD( "half_z" ) * mm; // This is the half height

  /// Optional parameters
  double rMin1 = 0.0;
  try { rMin1 = table->GetD( "r_min1" ) * mm; }
  catch( DBNotFoundError& e ) { };

  double rMin2 = 0.0;
  try { rMin2 = table->GetD( "r_min1" ) * mm; }
  catch( DBNotFoundError& e ) { };

  double phiStart = 0.0;
  try { phiStart = table->GetD( "phi_start" ) * deg; }
  catch( DBNotFoundError& e ) { };

  double phiDelta = twopi;
  try { phiDelta = table->GetD( "phi_delta" ) * deg; }
  catch( DBNotFoundError& e ) { };

  return new G4Cons( name + "_solid", rMin1, rMax1, rMin2, rMax2, sizeZ, phiStart, phiDelta );
}