/** University College London Dept of P&A Course in OO 3C59 | all rights reserved 1999 | | class: RanCom | | | This class provides a method to generates an angle in the range 0-180 degrees | which has the correct probability distribution for Compton scattering. | | The method is: | float scatter( ) | | | | NOTE: In this class the angles are in degrees ! | | Author: P.Clarke */ import java.lang.Math ; import java.util.Random ; public class RanCom { // Member variable to store electron mass in KeV private double electronMass ; // Member variable to store the incident photon energy in KeV private double incidentEnergy ; // Random number generator private Random rnums ; //-------------------------- //Constructor which takes two parameters //- the electron mass is in KeV //- the incident photon energy in KeV public RanCom( double emass, double incident ) { // Set the electron mass electronMass = emass ; // Set the incident photon energy incidentEnergy = incident ; // get a randome generator rnums = new Random() ; } //------------------------ //This is the method the user needs. //It returns an angle picked randomly using the Compton scattering //probability distribution. public double scatter( ) { boolean notdone = true ; double phi = 0 ; double Pmax, Pphi, Ptest ; // The maximum of the compton distribution is at phi = 0 // We need this value Pmax = this.probability( 0.) ; while( notdone ) { // Generate a random x value in range in [0,180] phi = this.uniform() * 180.0 ; //Calc the probability function at at phi Pphi = this.probability(phi) ; //Generate a random number in the range [0,Pmax] Ptest = this.uniform() * Pmax ; //Test if Ptest < Pphi //If it is then we keep this value of phi and so can stop, //otherwise continue in the loop if( Ptest < Pphi ) notdone = false ; //System.out.println( " phi Ptest Pmax/ "+phi+" / "+Ptest+" / "+Pmax+" \n " ); } return phi ; } //-------------- // Method to calculate the Compton probability functin for a givven // scattering angle private double probability( double phiDeg ) { double alpha = incidentEnergy/electronMass ; double prob, exp1, exp2, exp3, exp4 ; double phi = phiDeg / 360.0 * 2.0 * Math.PI ; /* Parts of expression */ exp1 = alpha * ( 1 - Math.cos(phi) ) ; exp2 = Math.pow( 1 / ( 1 + exp1 ), 2 ) ; exp3 = Math.pow(exp1, 2) / ( 1 + exp1 ) ; exp4 = 1 + Math.pow(Math.cos(phi),2.) + exp3 ; prob = exp2 * exp4 ; return prob ; } //--------------- // To get a uniform random number in range 0-1 private double uniform( ) { double x = rnums.nextDouble() ; //x += 1. ; //x /= 2. ; //System.out.println( " uniform x= "+x+" \n " ); return x ; } }