/*! @page  geometry The Detector Construction

<BR>
<B> Previous:</B> @ref runmgr
<B> Up:</B> @ref Mandatory
<B> Next:</B>  @ref geometry2 "Description of the geometry	"
<BR>

\section geo_implement Implementation of the geometry
The detector construction class is the virtual class for the geometry definition. To implement a geometry, one has to provide the dimensions, a material, a position and different attributes for each element of the detector. Practicaly, the implementation consists in the definition of three types of volumes, which are organized according to the following hierarchy:
* -# a <b>solid volume</b>: it is the definition of the shape of the volume, for example a cone, a sphere or a cylinder (etc.) together with its dimensions. The solid volume can be the result of a boolean operation (addition, subtraction).
* -# a <b>logical volume</b>: it is a solid volume plus a material and other attributes like
*    - a field manager e.g. for applying an electromagnetic field in the volume, 
*    - the visualization parameters as color, transparency etc. 
*    - a sensitive detector object for monitoring the analysis of flying through particles,
*    - some "user limit" parameters for controling the step size or the minimal energy of particles inside the volume. 
* The logical volume is indeed the volume which the simulation manager will be refering to for all logical operations during the run.
* -# a <b>physical volume</b>: it is a logical volume with its final position and orientation (spacial conformal transformation) in a so-called mother logical volume. That means, if we have for example a cylinder of the third lens, its position is defined in relation with the vacuum volume of the third chamber logical volume, which itself is converted into a physical volume by getting a position in the laboratory logical volume. It is then much more easier to handle relative positioning of the different detector elements. One would notice that only the laboratory volume (usually called World) is not given a position because it has no mother volume. Notice that a same logical volume can be used to define different physical volumes, as a same solid volume can be used to define different logical volumes.
*.
The following trsncript illustrates the construction of the scintillators.
\code
void LEMuSRDetectorConstruction::lemuSCINT()
{

  // solids 
  SCIS_tube= new G4Tubs("sSCIS",9.0*cm,9.5*cm, 13.0*cm, -45*deg, +90*deg);
  SCOS_tube= new G4Tubs("sSCOS",9.6*cm,10.1*cm, 13.0*cm, -45*deg, +90*deg);
  
  // materials
  SC_material=  G4Material::GetMaterial("scint");

  // logical
  lv_SCIS= new G4LogicalVolume(SCIS_tube,SC_material,"lv_SCIS",0,0,0);
  lv_SCOS= new G4LogicalVolume(SCOS_tube,SC_material,"lv_SCOS",0,0,0);
  
  // physical
  rotation1=G4RotateZ3D(90*deg) ;
  rotation2=G4RotateZ3D(180*deg) ;
  rotation3=G4RotateZ3D(270*deg) ;

  pv_SCISr = new G4PVPlacement( rotation2,lv_SCIS ,"pv_SCISr",lv_LABO, false, 0 ); 
  pv_SCOSr = new G4PVPlacement( rotation2,lv_SCOS ,"pv_SCOSr",lv_LABO, false, 0 ); 

  
(...)

      pv_SCISt = new G4PVPlacement( rotation1,lv_SCIS ,"pv_SCISt",lv_LABO, false, 0 ); 
      pv_SCOSt = new G4PVPlacement( rotation1,lv_SCOS ,"pv_SCOSt",lv_LABO, false, 0 ); 
      
      pv_SCISl = new G4PVPlacement( 0 , G4ThreeVector(0 *cm,0 *cm, 0*cm),lv_SCIS ,"pv_SCISl",lv_LABO, false, 0 );
      pv_SCOSl = new G4PVPlacement( 0 , G4ThreeVector(0 *cm,0 *cm, 0*cm),lv_SCOS ,"pv_SCOSl",lv_LABO, false, 0 ); 
      
      pv_SCISb = new G4PVPlacement( rotation3,lv_SCIS ,"pv_SCISb",lv_LABO, false, 0 ); 
      pv_SCOSb = new G4PVPlacement( rotation3,lv_SCOS ,"pv_SCOSb",lv_LABO, false, 0 ); 

(...)

  // vis attributes
  lv_SCIS->SetVisAttributes(SCINT_style);
  lv_SCOS->SetVisAttributes(dSCINT_style);
}
\endcode

Different types of interfaces has been implemented for the geometry definition, as for example an XML interface. Further development of \gf will include CAD/\gf geometry conversion. 

The code fo the detector construction was separate in three different files: 
* - LEMuSRDetectorConstruction.cc, which concerns all the geometry definition
* - LEMuSRMaterials.cc, which contains the definition of the materials and the attributes
* - LEMuSRdummydets.cc, which was used to draw dummy vacuum planes in the detector in order to get informations on particles at precise points. This file should be deleted in the future because one can simply use a limit on the step size (e.g. 1. cm) and use the stepping action to collect data at the end of each step.
*.
The advantage of writting the code in separate files is simply a gain of compilation time when a modification is done in one of the files.



\section geo_material The materials definition
The definition of the materials and elements is a powerful feature of \gf. It is possible to define all kind of elements and materials with specific parameter. The code for the material definition can be found in the LEMuSRMaterials.cc file, which is no more than an extension of the LEMuSRDetectorConstructor.cc file. 

The user can define elements giving a name, a symbol, an atomic number and an atomic mass. Then the defined elements can be used to define materials. In the following transcript we see how elements are defined:

\code 

void LEMuSRDetectorConstruction :: MaterialsDefinition ()
{
  
 
  
  G4double a; 		    //  atomic mass
  G4double z; 		    //  atomic number
  G4double density; 	    //
  G4String name, symbol;    // 
  G4int nbelements;         //  number of elements
  G4double fractionmass;    //  fractionnal mass - for mixtures
  
  
 
  // ELEMENTS
  
  // definition hydrogene
  a=1.01*g/mole;
  G4Element* H = new G4Element(name="hydrogen", symbol="H", z=1., a);

  // definition boron
  a=10.811*g/mole;
  G4Element* B = new G4Element(name="boron", symbol="B", z=5., a);

  (...)
 
\endcode
  Then using those elements it is possible to define material and gases:
\code
  // MATERIALS
  
  // definition : composition de Air
  density=1.290*mg/cm3;
  G4Material* Air= new G4Material(name="Air", density,nbelements=2);
  Air->AddElement (N, fractionmass=0.7);//fractionmass=G4double
  Air->AddElement (O, fractionmass=0.3);//fractionmass=G4double

     density= 2.376e-15*g/cm3; 
   G4double temperature= 300*kelvin;
   G4double pressure= 3.0e-9*pascal;
   G4Material* vacuum = new G4Material(name="vacuum", density, nbelements=1,
                                        kStateGas,temperature,pressure);
   vacuum-> AddMaterial(Air, fractionmass= 1.);
  
  //definition H2O=
  density = 1.000*g/cm3;
  G4Material* H2O = new G4Material(name="H2O", density, 2);
  H2O->AddElement (H, nbelements=2);//nbelements=G4int
  H2O->AddElement (O, nbelements=1);//nbelements=G4int
  
(...)
 
  // definition material 'tungsten'
  density =19.250*g/cm3;
  G4Material*  tungsten = new G4Material("tungsten",density,1);			
  tungsten->AddElement(W,1);


  // definition : composition of Graphite
  density=2.*g/cm3;
  G4Material* graphite= new G4Material(name="graphite",density,1);
  graphite->AddElement (C,1);

(...)
}
\endcode
 
<b> Important remark:</b>
* - The MaterialDefinition() method is called in the constructor of the LEMuSRDetectorConstruction class and not in the Contruct() method. The reason is that the materials table should be built only once; whereas the contructor is called only once, the Contruct() method is called at each modification of the detector.

* - Some dedicated pointers are declared in the @ref mat_def "header file" and instanciated in the method in charge to construct the different parts of the detector. 
\cd
  //====== MATERIAL DECLARATION  =========================
private:
  void MaterialsDefinition();
  //materials
  G4Material* SC_material;
  G4Material* SAH_material;
  G4Material* SAPH_material;
  G4Material* Scint_material;
  G4Material* DMCP_material;
  G4Material* Vacuum       ;
  G4Material* SSteel       ;
  G4Material* Copper       ;
  G4Material* Macor       ;
  G4Material* Carbon       ;
\ec
 The instanciation is done calling the GetMaterial() method. The most used elements (stainless steel and vacuum) are instanciated in the lemuDetector() method and the other in their respective methods.
 \cd
 G4VPhysicalVolume* LEMuSRDetectorConstruction::lemuDetector() // !attention au V dans G4VP...
{

(...) 
  // main materials

  Vacuum        = G4Material::GetMaterial("vacuum");
  SSteel        = G4Material::GetMaterial("stainless_steel");
  //-------------------------------
(...)
}


void LEMuSRDetectorConstruction::  lemuMCPdet()
{ 
 
(...)

  //materials
 
  Macor         = G4Material::GetMaterial("macor");
  DMCP_material = G4Material::GetMaterial("mcpglass");
   
  // logicals volumes
  lv_DMCP  = new G4LogicalVolume( DMCP_tube  , DMCP_material ,"lv_DMCP",0,0,0);
  lv_MCPM  = new G4LogicalVolume( MCPM_tube  , Macor         ,"lv_MCVR",0,0,0); 
  lv_MCPA  = new G4LogicalVolume( MCPA_box   , SSteel        ,"lv_MCPA",0,0,0); 
  lv_ANVA  = new G4LogicalVolume( ANVA_tube  , Vacuum        ,"lv_ANVA",0,0,0); 
(...)
} 
\ec
In this transcript we see that the vacuum and the stainless steel are not redefined in the submethods.


\section geo_elmag Managing electromagnetic fields
As stated previously, it is possible to assign a field to a logical volume. This is done in four steps:
*-# Creating a field object using a virtual class or a field map
*-# Instanciating a field manager
*-# Providing the field manager with the field, the motion equation and the integrator stepper (Runge-Kutta, Heum...), the minimal step increment etc.
*-# Assigning the field manager to the desired logical volume
*.
Different types of fields are virtually implemented in \gf, as well as the corresponding equations of motion. Anyway, for the \lemu simulation new classes were built in order to handle field maps generated by COMSOL multiphysics finite element solver.
\cd
 void LEMuSRDetectorConstruction::lemuLinse3()
{
(...)
 if(elfield==1)
    {
      
      G4ElectricField* L3Field = new LEMuSRElectricField(L3FieldVal,
							 FieldMapsDir+"/ThirdLense/L3b.map"
							 ,"dm",-567,60,60,100);
      
      //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
      
      L3FieldMgr =  new G4FieldManager();//1
      
      G4MagIntegratorStepper *pStepper;
      G4El_UsualEqRhs  *fEquation =  new G4El_UsualEqRhs(L3Field); 

      pStepper    = new G4ClassicalRK4(   fEquation, 12 );
       
      // equation considers spin or not
      G4ChordFinder*  pChordFinder = new G4ChordFinder( L3Field,
							1.e-10 * mm,      // Minimum step size: must be very small for a precise calculation of transport energy
							pStepper);

      L3FieldMgr->SetDetectorField(L3Field);
      L3FieldMgr->SetChordFinder(pChordFinder);
      
      lv_L3VA  = new G4LogicalVolume( L3VA_tube  , Vacuum        ,"lv_L3VA",L3FieldMgr,0,0);  

    }
(...)
}
\ec
This transcript shows how to declare the field map of the third lens. The directory of the field maps must be set via the environment variable <b><tt>LEMuSR_FIELDMAPS_DIR</tt></b>.


\section geo_sensi Managing the sensitive detection
\gf is well designed for modeling the sensitive detection. It features a virtual class for creating sensitive detectors. These are objects which are linked to a volume and which are called during the particle propagation to take specific actions implemented by the user. For example, the LEMuSRCryoSD class is responsible for monitoring the sensitive detection of particles entering the plates of the sample cryostat.

*/
//------------------------------------------------------------
/*! @page  geometry2 The Detector Construction

<BR>
<B> Previous:</B> @ref geo_implement
<B> Up:</B> @ref Mandatory
<B> Next:</B>  @ref physicsref
<BR>
\section geo_list Description of the geometry
The \lemu detector consists of the last part of the spectrometer, from the trigger detector to the sample cryostat.

The top volume is the laboratory volume (World), which is a cubic air volume (2mx2mx4m). 
\image html det2.gif XZ projection view of the detector in the World volume. 
*
\section geo_submethod The contruction methods
For clarity reasons, the different parts of the detector are built by different methods. This allows a better control of the geometry and a better modularity. We also had to build different vacuum volumes because it is not possible to assign more than one field per volume. Hence, in LEMuSRDetectorConstruction.cc, on can see that the detector construction method is call other methods to construct different parts of the detector according to the following repartition:

* \subsection geo_labo Laboratory
*- pv_LABO
*
* - <b><tt>lemuDetector()</tt></b>: constructs the Laboratory and calls the following methods.

* \subsection geo_trigger Trigger Detector and Chamber
*- pv_Trigger: steel cylinder
*- pv_TriggerV: vacuum
*- pv_TriggerF1: flange
*- pv_TriggerF2: flange
*- pv_CFOIL: carbon foil
*- pv_TriggerB: vacuum region with electric field
*- pv_TriggerB2: vacuum region with electric field
*- pv_TriggerB3: vacuum region with electric field
*
* - <b><tt>lemuTrigger_Detector()</tt></b>: builds the carbon foil, the vacuum volume, the steel cylinder and flanges around it and three vacuum volumes to introduce the electric fields. \image html trigger.gif Trigger detector carbon foil, electric field regions and  vacuum chamber.

* \subsection geo_cgate Compensation Gate Chamber
*- pv_CGateV: vacuum
*- pv_CGate: steel cylinder
*- pv_CGateF1: flange
*- pv_CGateF2: flange
*
* - <b><tt>lemuCGate()</tt></b>: builds the compensation gate steel tube and the vacuum volume.

* \subsection geo_l3 Third Lense and Chamber
*- pv_L3VA: vacuum
*- pv_L3ST: steel cylinder
*- pv_L3F1: flange
*- pv_L3F2: flange
*- pv_L3GP1: ground potential tube
*- pv_L3GP2: "
*- pv_L3GP3: "
*- pv_L3GP4: "
*- pv_L3GP5: "
*- pv_L3GP6: "
*- pv_L3GP7: "
*- pv_L3GP8: "
*- pv_L3HP: high potential tube
*- pv_L3HP4: "
*- pv_L3HP5: "
*- pv_L3HP7: "
*- pv_L3HP8: "
*
* - <b><tt>lemuLinse3()</tt></b>: builds the third lens, the vacuum volume and the tube around it. The electric field of the third lens is assigned to the vacuum volume. \im det_L3.gif Third lens and chamber.

* \subsection geo_mcp2 MCP2 and Gate Valve Chambers
*- pv_MCPV: vacuum volume of the MCP2 chamber	
*- pv_MCPS: steel cylinder of MCP2 chamber
*- pv_F100: end of the cylinder
*- pv_F160: flange
*- pv_F200: flange
*- pv_GATV: additional vacuum volume in gate valve chamber (indeed, the radii of the gate valve and the mcp chambers are different)
*- pv_GATS: steel cylinder for the gate valve chamber
*
* - <b><tt>lemuMCP2()</tt></b>: builds the gate valve tube and flanges, the cryostat chamber and flange and a vacuum volume for the gate valve and the sample cryostat region. Indeed, we have and overlap of the electric fields (ring anode, sample cryostat) and magnetic field (coils) in this region. For this volume we built a class for superimposing electric fields and a class for combining electric and magnetic fields. 

* \subsection geo_ringanode RING ANODE
*- pv_RA_E:  left front conical part 
*- pv_RA_M:  left rear conical part 
*- pv_RA_E2: right front conical part 
*- pv_RA_M2: right rear conical part 
*- pv_RA_G: cylinder part
*
* - <b><tt>lemuANODE()</tt></b>: builds the ring anode.\im det_mcpv.gif MCP2 and Gate Valve chamber, Ring Anode and MCP detector.

* \subsection geo_scint Inner and Outer Scintillators
*- pv_SCISt: inner top scintillator
*- pv_SCISb: inner bottom scintillator
*- pv_SCISr: inner right scintillator
*- pv_SCISl: inner left scintillator
*- pv_SCOSt: outer top  scintillator
*- pv_SCOSb: outer bottom scintillator
*- pv_SCOSr: ouer right scintillator
*- pv_SCOSl: outer left scintillator
*
* - <b><tt>lemuSCINT()</tt></b>: builds the inner and outer scintillators.

* \subsection geo_mcpdet MCP2 Detector
*- pv_DMPC: glass
*- pv_MCPA: steel
*- pv_MCPM: macor
*- pv_MCPM2: macor
*- pv_ANVA: vacuum
*- pv_MCSR: vacuum
*- pv_MCSR: steel
*- pv_MCVR: vacuum
*- pv_MCSS: steel
*
* - <b><tt>lemuMCPdet()</tt></b>: builds the MCP2 detector. \im det_mcp_scint.gif MCP2 detector and scintillators.

* \subsection geo_cryo CRYOSTAT
*- pv_SAH1: sample holder 1
*- pv_SAH2: sample holder 2
*- pv_SAH3: sample holder 3 (not used)
*- pv_SAPH: sapphire plate
*- pv_COFI: cold finger tube
*- pv_CRY1: cryostat
*- pv_CRY2: cryostat
*- pv_CRY3: cryostat
*- pv_CRY4: cryostat
*- pv_CRSH: He shield
*- pv_CRSH2: front shield
*- pv_Guard1: guard
*- pv_Guard2: guard
*
* - <b><tt>lemuCryo()</tt></b>: builds the sample cryostat. \im det_cryo.gif Sample Cryostat.
*.

* \subsection geo_emf Electromagnetic Fields
The electromagnetic fields of the trigger and the third lense are build in their respective contruction methods. For the MCP2 region, the field is build by the method <b><tt>BuildAnodeField()</tt></b>, which will construct the electromagnetic field according to the user settings. In this region, it is possible to enable or disable the magnetic field, the electric field at the sample and the electric field of the ring anode. Two special classes, LEMuSRElFieldMix and LEMuSRElMagField, were specialy implemented in order to handle the superposition of two electric fields for the first one, and the superposition of an electric field and a magnetic field for the second one.


\subsection geo_printlist Printing the Volumes List
The list of all the volumes and their densities and masses can be printed during the simulation by entering the commands 
*
* <b><tt>vis/ASCIITree/verbose x</tt></b> to set the level of details (\f$ 0 < x < 15 \f$ )
*
* <b><tt>vis/drawTree</tt></b>
*

The commands list can be found in section \ref commands.



*/