Geant4  v4-10.4-release
 모두 클래스 네임스페이스들 파일들 함수 변수 타입정의 열거형 타입 열거형 멤버 Friends 매크로 그룹들 페이지들
G4RandomTools.hh
이 파일의 문서화 페이지로 가기
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 105368 2017-07-24 09:44:20Z gcosmo $
28 //
29 //
30 // ---------------------------------------------------------------------------
31 // GEANT 4 class header file
32 // ---------------------------------------------------------------------------
33 // Class description:
34 //
35 // Utility functions
36 
37 // History:
38 //
39 // 24.08.17 - E.Tcherniaev, added G4RandomRadiusInRing, G4RandomPointInEllipse
40 // G4RandomPointOnEllipse, G4RandomPointOnEllipsoid
41 // 07.11.08 - P.Gumplinger, based on implementation in G4OpBoundaryProcess
42 //
43 // ---------------------------------------------------------------------------
44 
45 #ifndef G4RANDOMTOOLS_HH
46 #define G4RANDOMTOOLS_HH
47 
49 
50 #include "globals.hh"
51 #include "Randomize.hh"
52 #include "G4TwoVector.hh"
53 #include "G4ThreeVector.hh"
54 #include "G4RandomDirection.hh"
55 
56 // ---------------------------------------------------------------------------
57 // Returns a random lambertian unit vector (rejection sampling)
58 //
60 {
61  G4ThreeVector vect;
62  G4double ndotv;
63  G4int count=0;
64  const G4int max_trials = 1024;
65 
66  do
67  {
68  ++count;
69  vect = G4RandomDirection();
70  ndotv = normal * vect;
71 
72  if (ndotv < 0.0)
73  {
74  vect = -vect;
75  ndotv = -ndotv;
76  }
77 
78  } while (!(G4UniformRand() < ndotv) && (count < max_trials));
79 
80  return vect;
81 }
82 
83 // ---------------------------------------------------------------------------
84 // Chooses a random vector within a plane given by the unit normal
85 //
87 {
88  G4ThreeVector vec1 = normal.orthogonal();
89  G4ThreeVector vec2 = vec1.cross(normal);
90 
92  G4double cosphi = std::cos(phi);
93  G4double sinphi = std::sin(phi);
94 
95  return cosphi * vec1 + sinphi * vec2;
96 }
97 
98 // ---------------------------------------------------------------------------
99 // Returns a random radius in annular ring
100 //
102 {
103  if (rmin == rmax)
104  {
105  return rmin;
106  }
107  G4double k = G4UniformRand();
108  return (rmin <= 0) ? rmax*std::sqrt(k)
109  : std::sqrt(k*rmax*rmax + (1.-k)*rmin*rmin);
110 }
111 
112 // ---------------------------------------------------------------------------
113 // Returns a random point in ellipse (x/a)^2 + (y/b)^2 = 1
114 // (rejection sampling)
115 //
117 {
118  G4double aa = (a*a == 0) ? 0 : 1/(a*a);
119  G4double bb = (b*b == 0) ? 0 : 1/(b*b);
120  for (G4int i=0; i<1000; ++i)
121  {
122  G4double x = a*(2*G4UniformRand() - 1);
123  G4double y = b*(2*G4UniformRand() - 1);
124  if (x*x*aa + y*y*bb <= 1) return G4TwoVector(x,y);
125  }
126  return G4TwoVector(0,0);
127 }
128 
129 // ---------------------------------------------------------------------------
130 // Returns a random point on ellipse (x/a)^2 + (y/b)^2 = 1
131 // (rejection sampling)
132 //
134 {
135  G4double A = std::abs(a);
136  G4double B = std::abs(b);
137  G4double mu_max = std::max(A,B);
138 
139  G4double x,y;
140  for (G4int i=0; i<1000; ++i)
141  {
143  x = std::cos(phi);
144  y = std::sin(phi);
145  G4double mu = std::sqrt((B*x)*(B*x) + (A*y)*(A*y));
146  if (mu_max*G4UniformRand() <= mu) break;
147  }
148  return G4TwoVector(A*x,B*y);
149 }
150 
151 // ---------------------------------------------------------------------------
152 // Returns a random point on ellipsoid (x/a)^2 + (y/b)^2 + (z/c)^2 = 1
153 // (rejection sampling)
154 //
155 inline
157 {
158  G4double A = std::abs(a);
159  G4double B = std::abs(b);
160  G4double C = std::abs(c);
161  G4double mu_max = std::max(std::max(A*B,A*C),B*C);
162 
164  for (G4int i=0; i<1000; ++i)
165  {
166  p = G4RandomDirection();
167  G4double xbc = p.x()*B*C;
168  G4double yac = p.y()*A*C;
169  G4double zab = p.z()*A*B;
170  G4double mu = std::sqrt(xbc*xbc + yac*yac + zab*zab);
171  if (mu_max*G4UniformRand() <= mu) break;
172  }
173  return G4ThreeVector(A*p.x(),B*p.y(),C*p.z());
174 }
175 
176 #endif /* G4RANDOMTOOLS_HH */
Float_t x
Definition: compare.C:6
T max(const T t1, const T t2)
brief Return the largest of the two arguments
CLHEP::Hep3Vector G4ThreeVector
std::vector< ExP01TrackerHit * > a
Definition: ExP01Classes.hh:33
G4TwoVector G4RandomPointInEllipse(G4double a, G4double b)
Float_t y
Definition: compare.C:6
const char * p
Definition: xmltok.h:285
CLHEP::Hep2Vector G4TwoVector
Definition: G4TwoVector.hh:42
G4double G4RandomRadiusInRing(G4double rmin, G4double rmax)
double z() const
static double normal(HepRandomEngine *eptr)
Definition: RandPoisson.cc:77
G4ThreeVector G4RandomDirection()
G4TwoVector G4RandomPointOnEllipse(G4double a, G4double b)
static ulg bb
Definition: csz_inflate.cc:344
double G4double
Definition: G4Types.hh:76
G4ThreeVector G4LambertianRand(const G4ThreeVector &normal)
#define G4UniformRand()
Definition: Randomize.hh:53
double A(double temperature)
G4ThreeVector G4PlaneVectorRand(const G4ThreeVector &normal)
Hep3Vector cross(const Hep3Vector &) const
static constexpr double twopi
Definition: SystemOfUnits.h:55
int G4int
Definition: G4Types.hh:78
double C(double temp)
double x() const
G4ThreeVector G4RandomPointOnEllipsoid(G4double a, G4double b, G4double c)
Hep3Vector orthogonal() const
double y() const
double B(double temperature)