Geant4  10.03.p03
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4RandomTools.hh
Go to the documentation of this file.
1 //
2 // ********************************************************************
3 // * License and Disclaimer *
4 // * *
5 // * The Geant4 software is copyright of the Copyright Holders of *
6 // * the Geant4 Collaboration. It is provided under the terms and *
7 // * conditions of the Geant4 Software License, included in the file *
8 // * LICENSE and available at http://cern.ch/geant4/license . These *
9 // * include a list of copyright holders. *
10 // * *
11 // * Neither the authors of this software system, nor their employing *
12 // * institutes,nor the agencies providing financial support for this *
13 // * work make any representation or warranty, express or implied, *
14 // * regarding this software system or assume any liability for its *
15 // * use. Please see the license in the file LICENSE and URL above *
16 // * for the full disclaimer and the limitation of liability. *
17 // * *
18 // * This code implementation is the result of the scientific and *
19 // * technical work of the GEANT4 collaboration. *
20 // * By using, copying, modifying or distributing the software (or *
21 // * any work based on the software) you agree to acknowledge its *
22 // * use in resulting scientific publications, and indicate your *
23 // * acceptance of all terms of the Geant4 Software license. *
24 // ********************************************************************
25 //
26 //
27 // $Id: G4RandomTools.hh 90092 2015-05-13 11:53:25Z gcosmo $
28 //
29 //
30 // ---------------------------------------------------------------------------
31 // GEANT 4 class header file
32 // ---------------------------------------------------------------------------
33 // Class description:
34 //
35 // Utility functions
36 
37 // History:
38 //
39 // 07.11.08 - P.Gumplinger, based on implementation in G4OpBoundaryProcess
40 //
41 // ---------------------------------------------------------------------------
42 
43 #ifndef G4RANDOMTOOLS_HH
44 #define G4RANDOMTOOLS_HH
45 
47 
48 #include "globals.hh"
49 #include "Randomize.hh"
50 #include "G4RandomDirection.hh"
51 
52 // ---------------------------------------------------------------------------
53 // Returns a random lambertian unit vector
54 //
56 {
57  G4ThreeVector vect;
58  G4double ndotv;
59  G4int count=0;
60  const G4int max_trials = 1024;
61 
62  do
63  {
64  ++count;
65  vect = G4RandomDirection();
66  ndotv = normal * vect;
67 
68  if (ndotv < 0.0)
69  {
70  vect = -vect;
71  ndotv = -ndotv;
72  }
73 
74  } while (!(G4UniformRand() < ndotv) && (count < max_trials));
75 
76  return vect;
77 }
78 
79 // ---------------------------------------------------------------------------
80 // Chooses a random vector within a plane given by the unit normal
81 //
83 {
84  G4ThreeVector vec1 = normal.orthogonal();
85  G4ThreeVector vec2 = vec1.cross(normal);
86 
88  G4double cosphi = std::cos(phi);
89  G4double sinphi = std::sin(phi);
90 
91  return cosphi * vec1 + sinphi * vec2;
92 }
93 
94 #endif /* G4RANDOMTOOLS_HH */
G4ThreeVector G4LambertianRand(const G4ThreeVector &normal)
G4ThreeVector G4RandomDirection()
int G4int
Definition: G4Types.hh:78
static double normal(HepRandomEngine *eptr)
Definition: RandPoisson.cc:77
G4ThreeVector G4PlaneVectorRand(const G4ThreeVector &normal)
#define G4UniformRand()
Definition: Randomize.hh:97
Hep3Vector orthogonal() const
Hep3Vector cross(const Hep3Vector &) const
double G4double
Definition: G4Types.hh:76
static constexpr double twopi
Definition: SystemOfUnits.h:55