Geant4  9.6.p02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MedicalBeam.cc
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 // ********************************************************************
27 
28 #include "MedicalBeam.hh"
29 #include "Randomize.hh"
30 #include "G4Event.hh"
31 #include "G4ParticleTable.hh"
32 #include "G4PrimaryVertex.hh"
33 
34 using namespace CLHEP;
35 
36 #include <cmath>
37 
38 // --------------------------------------------------------------------------
40  : particle(0), kineticE(1.*MeV), sourcePosition(G4ThreeVector()),
41  SSD(1.*m), fieldShape(MedicalBeam::SQUARE), fieldR(10.*cm)
42 {
43 
45  particle = particleTable-> FindParticle("proton");
46 
47  kineticE = 200.*MeV;
48  sourcePosition = G4ThreeVector(0.,0.,-125.*cm);
49  SSD = 100.*cm;
50  fieldXY[0] = fieldXY[1] = 5.*cm;
51 }
52 
53 // --------------------------------------------------------------------------
55 {
56 }
57 
58 // --------------------------------------------------------------------------
60 {
61  // uniform distribution in a limitted solid angle
62  G4double dr;
63  if ( fieldShape == MedicalBeam::SQUARE ) {
64  dr = std::sqrt(sqr(fieldXY[0]/2.) + sqr(fieldXY[1]/2.));
65  } else {
66  dr = fieldR;
67  }
68 
69  G4double sin0 = dr/SSD;
70  G4double cos0 = std::sqrt(1.-sqr(sin0));
71 
72  G4double dcos = 0.;
73  G4double dsin, dphi, z;
74 
75  G4double x = DBL_MAX;
76  G4double y = DBL_MAX;
77 
78  G4double xmax, ymax;
79  if ( fieldShape == MedicalBeam::SQUARE ) {
80  xmax = fieldXY[0]/2./SSD;
81  ymax = fieldXY[1]/2./SSD;
82  } else {
83  xmax = ymax = DBL_MAX-1.;
84  }
85 
86  while(! (std::abs(x) < xmax && std::abs(y) < ymax) ) {
87  dcos = RandFlat::shoot(cos0, 1.);
88  dsin = std::sqrt(1.-sqr(dcos));
89  dphi = RandFlat::shoot(0., twopi);
90 
91  x = std::cos(dphi)*dsin*dcos;
92  y = std::sin(dphi)*dsin*dcos;
93  }
94  z = dcos;
95 
96  return G4ThreeVector(x,y,z);
97 }
98 
99 // --------------------------------------------------------------------------
101 {
102  if ( particle == 0 ) return;
103 
104  // create a new vertex
105  G4PrimaryVertex* vertex = new G4PrimaryVertex(sourcePosition, 0.*ns);
106 
107  // momentum
108  G4double mass = particle-> GetPDGMass();
109  G4double p = std::sqrt(sqr(mass+kineticE)-sqr(mass));
110  G4ThreeVector pmon = p*GenerateBeamDirection();
111  G4PrimaryParticle* primary =
112  new G4PrimaryParticle(particle, pmon.x(), pmon.y(), pmon.z());
113 
114  // set primary to vertex
115  vertex-> SetPrimary(primary);
116 
117  // set vertex to event
118  anEvent-> AddPrimaryVertex(vertex);
119 }