// This code implementation is the intellectual property of
// the GEANT4 collaboration.
//
// By copying, distributing or modifying the Program (or any work
// based on the Program) you indicate your acceptance of this statement,
// and all its terms.
//
// $Id: ExN03DetectorConstruction.cc,v 1.4 1999/12/15 14:49:25 gunter Exp $
// GEANT4 tag $Name: geant4-02-00 $
//
// 
/////////////////////////////////////////////////////////////////////////
//
//     Modified: 31-01-2006  J.L. Tain --> testDetectorConstruction.cc
//
//     Simple test detector geometry
//

#include "testDetectorConstruction.hh"

#include "G4Material.hh"
#include "G4MaterialTable.hh"
#include "G4Element.hh"
#include "G4ElementTable.hh"
#include "G4Box.hh"
#include "G4Tubs.hh"
#include "G4Sphere.hh"
#include "G4LogicalVolume.hh"
#include "G4ThreeVector.hh"
#include "G4PVPlacement.hh"
#include "G4VisAttributes.hh"

testDetectorConstruction::testDetectorConstruction()
{ }

testDetectorConstruction::~testDetectorConstruction()
{ }

G4VPhysicalVolume* testDetectorConstruction::Construct()
{

  //----------------------------------------------------
  // Materials definitions
  //----------------------------------------------------
 
  G4String name, symbol;             //a=mass of a mole;
  G4double a, z, density;            //z=mean number of protons;  

  G4int ncomponents, natoms;         
  G4double fractionmass;

//   G4int iz, n;                       //iz=number of protons  in an isotope; 
//                                      // n=number of nucleons in an isotope;


  //
  //  define Vacuum
  //

  G4double pressure    = 3.e-18*pascal;
  G4double temperature = 2.73*kelvin;
  density     = 1.e-25*g/cm3;

  G4Material* Vacuum = new G4Material(name="Galactic", z=1., a=1.01*g/mole, 
				      density,kStateGas,temperature,pressure);

  //
  // define simple materials
  //

  // Al
  density = 2.700*g/cm3;
  a = 26.98*g/mole;
  G4Material* Al = new G4Material(name="Aluminium", z=13., a, density);

  //
  // define Elements
  //

  // H
  a = 1.00794*g/mole;
  G4Element* H  = new G4Element(name="Hydrogen"  ,symbol="H" , z= 1., a);

  // C
  a = 12.011*g/mole;
  G4Element* C  = new G4Element(name="Carbon"  ,symbol="C" , z= 6., a);

  // O
  a = 16.000*g/mole;
  G4Element* O  = new G4Element(name="Oxygen"  ,symbol="O" , z= 8., a);

  // Na
  a = 22.990*g/mole;
  G4Element* Na  = new G4Element(name="Sodium"  ,symbol="Na" , z= 11., a);

  // Ge
  a = 72.61*g/mole;
  G4Element* Ge  = new G4Element(name="Germanium"  ,symbol="Ge" , z= 32., a);

  // I
  a = 126.904*g/mole;
  G4Element* I = new G4Element(name="Iodine",symbol="I" , z= 53., a);

  // Bi
  a = 208.980*g/mole;
  G4Element* Bi = new G4Element(name="Bismuth",symbol="Bi" , z= 83., a);

  //
  // define a material from elements.   case 1: chemical molecule
  //
 
  // NaI
  density = 3.67*g/cm3;
  G4Material* NaI = new G4Material(name="NaI", density, ncomponents=2);
  NaI->AddElement(Na, natoms=1);
  NaI->AddElement(I, natoms=1);
 
  //
  // define a material from elements.   case 2: mixture by fractional mass
  //

  //BC-400
  density = 1.032*g/cm3;
  G4Material* BC400 = new G4Material(name="BC400", density, ncomponents=2);
  BC400->AddElement(H, fractionmass=0.0847);
  BC400->AddElement(C, fractionmass=0.9153);




  //------------------------------------------------------
  // Detector geometry
  //------------------------------------------------------
   
  //     
  // World as a box
  //
 
  G4double WorldSize= 100.*cm;
 
  G4Box* 
    solidWorld = new G4Box("World",		       	       //its name
			WorldSize/2,WorldSize/2,WorldSize/2);  //its size
 
  G4LogicalVolume* 
    logicWorld = new G4LogicalVolume(solidWorld,      	//its solid
				     Vacuum,	        //its material
				     "World");		//its name
  
  G4VPhysicalVolume* 
    physiWorld = new G4PVPlacement(0,			//no rotation
				   G4ThreeVector(),	//at (0,0,0)
				   "World",		//its name
				   logicWorld,		//its logical volume
				   NULL,		//its mother  volume
				   false,	       	//no boolean operation
				   0);			//copy number
  

  //
  // Detector as a cylinder
  //

  G4double minRadius = 0.0*cm;
  G4double maxRadius = 2.5*cm;
  G4double startPhi = 0.*deg;
  G4double deltaPhi = 360.*deg;
  G4double halfLength = 2.5*cm;


  G4Tubs* solidDetector = new G4Tubs("Detector",minRadius,maxRadius,
					halfLength,startPhi,deltaPhi);

  G4LogicalVolume* logicDetector = new G4LogicalVolume(solidDetector,NaI,
						      "Detector");

  G4double distance = 10*cm+halfLength;

  G4ThreeVector positionDetector = G4ThreeVector(0.*cm,0.*cm,distance);

  G4VPhysicalVolume* physiDetector = new G4PVPlacement(0,positionDetector,
						      "Detector",logicDetector,
						      physiWorld,false,0);



  //
  // visualization attributes
  //

  logicWorld->SetVisAttributes(G4VisAttributes::Invisible);


  //
  // always return the physical World
  //

  return physiWorld;
}
