//$Id$

///\file "optical/LXe/.README.txt"
///\brief Example LXe README page

/*! \page ExampleLXe Example LXe

\section LXe_s1 Geometry

The way the geometry is constructed is an experiment for a new, more object
oriented, way to construct geometry. It seperates the concept of how a volume
is built from where it is placed. Each major volume in the geometry is defined
as a class derived from G4PVPlacement. In this example, just the main LXe
volume, the WLS scintillator slab, and the WLS fibers were chosen. To place
one of these volumes, simply create an instance of it with the appropriate
rotation, translation, and mother volumes.

\verbatim
LXeMainVolume(G4RotationMatrix *pRot,
              const G4ThreeVector &tlate,
              G4LogicalVolume *pMotherLogical,
              G4bool pMany,
              G4int pCopyNo,
              LXeDetectorConstruction* c);
\endverbatim

Also necessary are the pMany and pCopyNo variables with the same usage as in 
G4PVPlacement. Additionally, the detector construction must be passed to the
main volume as a way to communicate the many parameters to the volume and its
sub-volumes. The communication is done from the CopyValues() function which
retrieves the information from the detector constructor.

Notably, the name and logical volume parameters are no longer part of the
constructor. This is because they are both to be decided by the volume itself.
The volume must specify its own name and a temporary logical volume. The
constructor will then procede to define its logical volume in the normal way.
Once complete, the logical volume can be assigned to the physical volume using
the SetLogicalVolume() function.

To handle instances of the same type of volume, a new logical volume should not
be defined for each one. Instead, the logical volume is kept as a static member
and defined only once. 
\verbatim
if(!fHousing_log || fUpdated){
//...
//Define logical volume
//...
}
SetLogicalVolume(fHousing_log);
\endverbatim

\subsection LXe_subs10 Modifying the geometry at runtime

This example allows the user to modify the geometry definition at runtime. This
is accomplished through LXeDetectorMessenger, a derived class of G4UImessenger.
The commands it adds change variables stored in LXeDetectorConstructor that
are used when constructing the geometry. After changing these variables
the /LXe/detector/update command must be issued to reconstruct the geometry
with the new values.

\verbatim
void LXeDetectorConstruction::UpdateGeometry(){
  // clean-up previous geometry
  G4SolidStore::GetInstance()->Clean();
  G4LogicalVolumeStore::GetInstance()->Clean();
  G4PhysicalVolumeStore::GetInstance()->Clean();

  //define new one
  G4RunManager::GetRunManager()->DefineWorldVolume(ConstructDetector());
  G4RunManager::GetRunManager()->GeometryHasBeenModified();
}
\endverbatim

\section LXe_s2 PMT sensitive detector

The PMT sensitive detector cannot be triggered like a normal sensitive detector
because the sensitive volume does not allow photons to pass through it. Rather,
it detects them in the OpBoundary process based on an efficiency set on the
skin of the volume.
\verbatim
  G4OpticalSurface* photocath_opsurf=
     new G4OpticalSurface("photocath_opsurf",glisur,polished,
                           dielectric_metal);
  G4double photocath_EFF[num]={1.,1.};
  G4double photocath_REFL[num]={0.,0.};
  G4MaterialPropertiesTable* photocath_mt = new G4MaterialPropertiesTable();
  photocath_mt->AddProperty("EFFICIENCY",Ephoton,photocath_EFF,num);
  photocath_mt->AddProperty("REFLECTIVITY",Ephoton,photocath_REFL,num);
  photocath_opsurf->SetMaterialPropertiesTable(photocath_mt);
  new G4LogicalSkinSurface("photocath_surf",photocath_log,photocath_opsurf);
\endverbatim
A normal sensitive detector would have its ProcessHits 
function called for each step by a particle inside the volume. So, to record
these hits with a sensitive detector we watched the status of the OpBoundary
process from the stepping manager whenever a photon hit the sensitive volume
of the pmt. If the status was 'Detection', we retrieve the sensitive detector
from G4SDManager and call its ProcessHits function.
\verbatim
  boundaryStatus=boundary->GetStatus();
  //Check to see if the particle was actually at a boundary
  //Otherwise the boundary status may not be valid
  //Prior to Geant4.6.0-p1 this would not have been enough to check
  if(thePostPoint->GetStepStatus()==fGeomBoundary){
    switch(boundaryStatus){
    //...    
      case Detection: //Note, this assumes that the volume causing detection
                      //is the photocathode because it is the only one with
	              //non-zero efficiency
	{
	  //Trigger sensitive detector manually since photon is
	  //absorbed but status was Detection
	  G4SDManager* SDman = G4SDManager::GetSDMpointer();
	  G4String sdName="/LXeDet/pmtSD";
	  LXePMTSD* pmtSD = (LXePMTSD*)SDman
	    ->FindSensitiveDetector(sdName);
	  if(pmtSD)
	    pmtSD->ProcessHits_constStep(theStep,NULL);
	  break;
	}
      //...
    }
\endverbatim

\section LXe_s3 Modular Physics List

Using a modular physics list is an easy way to organize the physics list into
categories for easier maintenance. It can also assist with testing code
by making it easy to disable an entire category of physics at once if
necessary. The physics list instantiated in main() is a derived class of
G4VModularPhysics list rather than the usual G4VUserPhysicsList. The only
function aside from the constructor that is necessary in this class is
SetCuts(). The constructor must register the other physics lists individually.

\verbatim
RegisterPhysics( new LXeGeneralPhysics("general") );
\endverbatim

The other physics lists (the modules) are derived from G4VPhysicsConstructor 
and it is necessary to write the ConstructParticle() and ConstructProcess() 
functions for each list. They work in the same way as in G4VUserPhysicsList.

Do not create instances of the individual physics processes as members of the
modules. Instead, use pointers to the processes and create the instances
in the ConstructProcess() function. The reason for this is that the materials
needed to build physics tables for the processes will not have been created
at the time that the modules are created but will have been created before the
ConstructProcess() function is called.

\section LXe_s4 Selectively drawing trajectories or highlighting volumes

In a simulation such as this one, where an average of 6000 trajectories are
generated in a small space, there is little use in drawing all of them. There
are two ways to select which ones to draw. The first of which is to decide
while looping through the trajectory container which ones to draw and only call
DrawTrajectory on the important ones. However, trajectories only contain a
small portion of the information from the track it represents. This may not
be enough to decide if a trajectory is worth drawing.

The alternative is to define your own trajectory class to store additional
information to help decide if it should be drawn. To use your custom trajectory
you must create it in the PreUserTrackingAction:

\verbatim
fpTrackingManager->SetTrajectory(new LXeTrajectory(aTrack));
\endverbatim

Then at any point you can get access to the trajectory you can update the extra
information within it. When it comes to drawing, you can then use this to
decide if you want to call DrawTrajectory. Or you can call DrawTrajectory for
all trajectories and have the logic decide how and if a trajectory should
be drawn inside the DrawTrajectory function itself.

Selectively highlighting volumes is useful to show which volumes were hit. To
do this, you simply need a pointer to the physical volume. With that, you can
modify its vis attributes and instruct the vis manager to redraw the volume
with the new vis attributes.
\verbatim
  G4VisAttributes attribs(G4Colour(1.,0.,0.));
  attribs.SetForceSolid(true);
  G4RotationMatrix rot;
  if(physVol->GetRotation())//If a rotation is defined use it
    rot=*(physVol->GetRotation());
  G4Transform3D trans(rot,physVol->GetTranslation());//Create transform
  pVVisManager->Draw(*fPhysVol,attribs,trans);//Draw it
\endverbatim
In this case, it is done in Draw function of a PMT hit but it can be placed
anywhere. The logic to decide if it should be drawn or not may be similar to
the logic used in choosing which trajectories to draw.

See /LXe/detector/volumes/sphere in "UI commands" below for info on what 
trajectories are drawn in this simulation.

\section LXe_s5 Saving random engine seeds

At times it may be necessary to review a particular event of interest. To do
this without redoing an entire run, which may take a long time, you must store
the random engine seed from the beginning of the event. The run manager
has some functions that help in this task.

\verbatim
G4RunManager::SetRandomNumberStore(G4bool)
\endverbatim

When set to true, this causes the run manager to write the seed for the
beginning of the current run to CurrentRun.rndm and the current event to
CurrentEvent.rndm. However, at the beginning of each event this file will be 
overwritten with the new event. To keep a copy for a particular event there is
a function to copy this file to run###evt###.rndm.

\verbatim
G4RunManager::rndmSaveThisEvent()
\endverbatim

This can be done for every event so you can review any event you like but this
may be awkward for runs with very large numbers of events. Instead, implement
some form of logic in EndOfEventAction to decide if the event is worth saving.
If it is, then call rndmSaveThisEvent(). By default, these files are stored in
the current working directory. There is a function to change this as well.
Typically you would call that at the same time SetRandomNumberStore. The
directory to save in must exist first. GEANT4 will not create it for you.

\verbatim
G4RunManager::SetRandomNumberStoreDir(G4String)
\endverbatim

\section LXe_s6 LXeRecorderBase

LXeRecorderBase is a virtual class to serve as a template for how to add
histogram functionality to a GEANT4 application. To use it, derive a
class from it and instantiate that in main(). Each of your user action classes
to do any recording must have a pointer to this instance. Then at the end of 
the critical functions in each user action, call the appropriate recorder
function. The recorder functions and the functions to call them from are listed
here:
\verbatim
  RecordBeginOfRun(const G4Run*)
	-Call from BeginOfRunAction()
  RecordEndOfRun(const G4Run*)
	-Call from EndOfRunAction()
  RecordBeginOfEvent(const G4Event*)
	-Call from BeginOfEventAction()
  RecordEndOfEvent(const G4Event*)
	-Call from EndOfEventAction()
  RecordTrack(const G4Track*)
	-Call from PostUserTrackingAction()
  RecordStep(const G4Step*)
	-Call from UserSteppingAction()
\endverbatim
For the reasoning behind why it is done this way see LXeRecorderBase.hh

\section LXe_s7 UI commands

The method to define UI commands is well documented in the GEANT4 documentation
so will not be discussed here. This is a description of the commands added to
this example.

Directories:
- /LXe/ - All custom commands belong below this directory
- /LXe/detector/ - Geometry related commands
- /LXe/detector/volumes/ - Commands to enable/disable volumes in the geometry

Commands:
\verbatim
/LXe/saveThreshold <int, default = 4500>
\endverbatim 

- Specifies a threshold for saving the random seed for an event. If the number
of photons generated in an event is below this number then the random seed is
saved to ./random/run###evt###.rndm. See "Saving random engine seeds".

\verbatim
/LXe/eventVerbose <int, default = 1>
\endverbatim

- Enables end of event verbose data to be printed. This includes information
counted and calculated by the user action classes.

\verbatim
/LXe/pmtThreshold <int, default = 1>
\endverbatim

- Sets the PMT threshold in # of photons being detected by the PMT. PMTs below
with fewer hits than the threshold will not count as being hit and will also
not be highlighted at the end of the event.

\verbatim
/LXe/oneStepPrimaries <bool>
\endverbatim

- This causes primary particles to be killed after going only one step inside
the scintillator volume. This is useful to view the photons generated during 
the initial conversion of the primary particle.

\verbatim
/LXe/forceDrawPhotons <bool>
\endverbatim

- Forces all optical photon trajectories to be drawn at the end of the event 
regardless of the scheme mentioned in /LXe/detector/volumes/sphere below.

\verbatim
/LXe/forceDrawNoPhotons <bool>
\endverbatim

- Forces all optical photon trajectories to NOT be drawn at the end of the 
event regardless of the scheme mentioned in /LXe/detector/volumes/sphere below.
- If /LXe/forceDrawPhotons is set to true, this has no effect.

\verbatim
/LXe/detector/dimensions <double x y z> <unit, default = cm>
\endverbatim

- Sets the dimensions of the main scintillator volume.

\verbatim
/LXe/detector/housingThickness <double>
\endverbatim

- Sets the thickness of the housing surrounding the main detector volume.

\verbatim
/LXe/detector/pmtRadius <double> <unit, default = cm>
\endverbatim

- Sets the radius of the PMTs

\verbatim
/LXe/detector/nx
/LXe/detector/ny
/LXe/detector/nz
\endverbatim

- Sets the number of PMTs placed in a row along each axis.

\verbatim
/LXe/detector/reflectivity <double>
\endverbatim

- Sets the reflectivity of the inside of the aluminum housing. The geometry
uses a default value of 1.00 for a fully reflective surface.

\verbatim
/LXe/detector/nfibers <int>
\endverbatim

- Sets the number of WLS fibers placed in the WLS scintillator slab. The
geometry uses a default value of 15 fibers.

\verbatim
/LXe/detector/scintYieldFactor <double>
\endverbatim

- Sets the yield factor for the scintillation process. This is cumulative with
the yield factor set on individual materials. Set to 0 to produce no 
scintillation photons.

\verbatim
/LXe/detector/update
\endverbatim

- Builds the new geometry based on any parameters that have been updated with
the other UI commands. ***This must be called for the changes to take effect***

\verbatim
/LXe/detector/defaults
\endverbatim

- Resets all detector values customizable with commands above to their defaults.

\verbatim
/LXe/detector/volumes/sphere <bool>
\endverbatim

- Enables/disables the sphere placed inside the main scintillator volume. When
the sphere is enabled, only photons that hit the sphere and hit a PMT are
drawn. If it is disabled, then all photons that hit PMTs are drawn.

\verbatim
/LXe/detector/volumes/wls <bool>
\endverbatim

- Enables/disables the WLS scintillator slab containing WLS fibers. By default
this is not part of the geometry. Enabling it will place it behind the LXe
scintillator volume.

\verbatim
/LXe/detector/volumes/lxe <bool>
\endverbatim

- Enables/disables the main LXe scintillator volume. By default this is part of
the geometry.

\section LXe_s8 Macro files

The following are the macro files included in this example and what they do.

- LXe.in \n
This produces a standard event with a 511 keV gamma fired into the LXe volume.
All values are left at their default states but verbose output has been
enabled.

- cerenkov.mac \n
This is to demonstrate the cerenkov process. It disables the scintillation
process and uses a 200MeV mu+ to produce cerenkov photons. The volume has
been resized and the number of pmts has been increased to more accurately
show the cone. OneStepPrimaries has been enabled so that the cone does not fill
itself in as the muon slows down.

- wls.mac \n
This disables the main volume and enables the WLS slab volume. It sets the
particle gun to use an e- to produce scintillation in the slab which will be
absorbed by the WLS fibers and re-emited at a different wavelength.

- vis.mac \n
This is a standard vis.mac file to tell the vis manager how to visualize the
simulation.

- photon.mac \n
A very simple test in which the gun is set to produce a single photon inside 
the main scintillator volume.

- reviewEvent.mac \n
This is to review an event by loading in a random seed and running the event
with verbose output. Modify the file to specify the filename of the random 
seed.

- defaults.mac \n
This resets all values that can be changed with the /LXe/ commands back to
their initial configuration including those that are not reset with
\verbatim
/LXe/detector/defaults
\endverbatim

<hr>

\section LXe_s9 Classes Used

\subsection LXe_subs11 main ()

 See LXe.cc.

 - Use G4UItcsh if available
 - Provide interactive and macro mode

\subsection LXe_subs12 G4VModularPhysicsList

Class: LXePhysicsList

 - Registers General, EM, Muon, and Optical physics lists

 - define particles; including 
     - G4OpticalPhoton     

 - define processes; including 
     - G4Cerenkov          
     - G4Scintillation     
     - G4OpAbsorption      
     - G4OpRayleigh        
     - G4OpBoundaryProcess 
     - G4OpWLS             

\subsection LXe_subs13 G4VUserDetectorConstruction

Class: LXeDetectorConstruction

 - define material: LXe (liquid xenon), Aluminum, Air, Vacuum, Glass,...
 - define G4Box geometry with aluminum housing and LXe volume inside
 - define G4Tubs placed around the housing walls
 - define G4Sphere to demonstrate skin surfaces inside volumes:
    - add G4MaterialPropertiesTable to G4Material         
    - define G4OpticalSurface(s)                          
    - define G4LogicalBorderSurface(s)                    
    - define G4LogicalSkinSurface(s)                      
    - add G4MaterialPropertiesTable to G4OpticalSurface(s)

 - Mesenger to change many of the dectector geometry properties

 - Uses a alternative style of geometry definition. See "Geometry" section.

\subsection LXe_subs14 G4VUserPrimaryGeneratorAction

Class: LXePrimaryGeneratorAction

 - Use G4ParticleGun to shoot a 511 keV gamma through the housing into
     liquid xenon scintillator

\subsection LXe_subs15 G4UserStackingAction

Class: LXeStackingAction

 - show how to count the number of secondary particles in an event
     differentiates between different creator processes

\subsection LXe_subs16 G4UserRunAction

Class: LXeRunAction

 - Call recorder class for begin and end of run

\subsection LXe_subs17 G4UserSteppingAction

Class: LXeSteppingAction

 - Identify which secondaries were generated during a particular step

 - Count reflections/absorptions/detections due to G4OpBoundaryProcess \n
   Count absorptions due to G4OpAbsorption \n
   Manually trigger a sensitive detector when a boundary process detects

 - Call recorder class at end of step

\subsection LXe_subs18 G4UserTrackingAction

Class: LXeTrackingAction

 - Determine if the trajectory should be drawn by checking if it hit the
     sphere(if enabled) and a pmt.
 - Call recorder class at end of track

\subsection LXe_subs19 G4UserEventAction

Class: LXeEventAction

 - Triggers drawing of trajectories

 - Calculates and stores data in a G4VUserEventInformation object

 - Outputs basic event data at end of event

 - Decides if the random seed should be saved for this event

 - Call recorder class at begin and end of event

\subsection LXe_subs110 G4VSensitiveDetector

Classes: LXePMTSD, LXeScintSD

 - Basic sensitive detectors keeping hit collections
   -  Keep one G4VHit object per hit \n
      or                             \n
      Keep one G4VHit object per volume containing hits

 - LXePMTSD decides if the hits it is creating should be redrawn

\subsection LXe_subs111 G4VHit

Classes: LXePMTHit, LXeScintHIT

 - Store individual hit positions \n
   or                             \n
   Store a count of hits in a particular volume

 - Selectively redraw volumes containing hits at the end of event

\subsection LXe_subs112 G4VUserEventInformation & G4VUserTrackInformation

Classes: LXeUserEventInformation, LXeUserTrackInformation

 - Store aditional information along with the G4Event/G4Track objects

\subsection LXe_subs113 G4VSteppingVerbose

Classes: LXeSteppingVerbose
 
 - Custom verbose stepping output to use G4BestUnit and print current volume
      rather than next volume
 - Same as ExN03SteppingVerbose but output reformated to fit nicer into 
      tables.

\subsection LXe_subs114 G4UImessenger

Classes: LXeDetectorMessenger, LXeEventMessenger, LXeSteppingMessenger

 - Create /LXe and /LXe/detector interactive command folders

 - Create new commands

 - See interactive help when running the example for descriptions of commands

\subsection LXe_subs115 G4Trajectory

Class: LXeTrajectory

 - Derived from G4Trajectory to use most of the basic trajectory functions
     already defined

 - Uses a coppied and modified version of DrawTrajectory from G4VTrajectory
     to enable/disable drawing of individual trajectories and to redefine
     the colours used

\subsection LXe_subs116 LXeRecorderBase

Class LXeRecorderBase

 - Virtual class provided for recording of simulation data

 - Derive your own implementation from it and instantiate the recorder
     object in main () (see LXe.cc)

 - For full description see LXeRecorderBase.hh

*/
