geant4-B1-example  1.0
 All Classes Files Functions Variables Pages
B1PrimaryGeneratorAction.cc
Go to the documentation of this file.
1 //file B1PrimaryGeneratorAction.cc
2 // brief Implementation of the B1PrimaryGeneratorAction class
3 
5 
6 #include "G4LogicalVolumeStore.hh"
7 #include "G4LogicalVolume.hh"
8 #include "G4Box.hh"
9 #include "G4RunManager.hh"
10 #include "G4ParticleGun.hh"
11 #include "G4ParticleTable.hh"
12 #include "G4ParticleDefinition.hh"
13 #include "G4SystemOfUnits.hh"
14 #include "Randomize.hh"
15 
16 /*! @brief ### set up our gamma beam
17  It is 6MeV gamma, moving toward (0,0,1) direction. <br>
18  gamma's location will be set in GeneratePrimaries() <br>
19 
20  + run the G4VUserPrimaryGeneratorAction() constructor.
21  + initial the fParticleGun = 0, fEnvelopeBox =0
22 */
24  : G4VUserPrimaryGeneratorAction(),
25  fParticleGun(0),
26  fEnvelopeBox(0)
27 {
28 
29  G4int n_particle = 1;
30 
31  fParticleGun = new G4ParticleGun(n_particle);
32 
33 
34  // set up a gamma particle.
35  G4ParticleTable* particleTable = G4ParticleTable::GetParticleTable();
36  G4String particleName;
37  G4ParticleDefinition* particle
38  = particleTable->FindParticle(particleName="gamma");
39 
40 
41  // put the gamma particle, momentum direction, energy
42  // into the fParticleGun obj.
43  fParticleGun->SetParticleDefinition(particle);
44  fParticleGun->SetParticleMomentumDirection(G4ThreeVector(0.,0.,1.));
45  fParticleGun->SetParticleEnergy(6.*MeV);
46 }
47 
48 
49 
50 
51 
52 
53 /// we must delete the fParticleGun when the B1PrimaryGeneratorAction obj is deleted.
55 {
56  delete fParticleGun;
57 }
58 
59 
60 
61 
62 /*!
63  @brief ###setup the gamma's location <br>
64  to get the width and height of the 'Envelope' phyisclogic volume. <br>
65  we set 0.8*width and 0.8*height as the area that the gamma will hit. <br>
66 */
68 {
69  /*! + this is called at the begining of ecah event
70  + In order to avoid dependence of DetectorConstruction class,
71  we get Envelope volume from G4LogicalVolumeStore.
72  */
73 
74 
75 
76  G4double envSizeXY = 0;
77  G4double envSizeZ = 0;
78 
79 
80  // to retrive the 'Envelope' G4Box obj to our fEnvelopeBox obj.
81 
82  if (!fEnvelopeBox) // note: initially fEnvelopeBox = 0 = false, so !false = true
83  {
84  G4LogicalVolume* envLV
85  = G4LogicalVolumeStore::GetInstance()->GetVolume("Envelope");
86 
87  if ( envLV ) fEnvelopeBox = dynamic_cast<G4Box*>(envLV->GetSolid());
88  }
89 
90 
91 
92  if ( fEnvelopeBox )
93  {
94  // if the retrival success, then assign the width and length to our envSizeXY and envSizeZ variables.
95 
96  envSizeXY = fEnvelopeBox->GetXHalfLength()*2.;
97 
98  envSizeZ = fEnvelopeBox->GetZHalfLength()*2.;
99  }
100  else
101  {
102  // However, if not success, then send out an error message. ( and an exception.)
103 
104  G4ExceptionDescription msg;
105  msg << "Envelope volume of box shape not found.\n";
106  msg << "Perhaps you have changed geometry.\n";
107  msg << "The gun will be place at the center.";
108  G4Exception("B1PrimaryGeneratorAction::GeneratePrimaries()",
109  "MyCode0002",JustWarning,msg);
110  }
111 
112 
113  /*
114  | set the active area = 0.8 width * 0.8 length
115  | and use G4UniformRand to control the gamma ray position.
116  */
117  G4double size = 0.8;
118  G4double x0 = size * envSizeXY * (G4UniformRand()-0.5);
119  G4double y0 = size * envSizeXY * (G4UniformRand()-0.5);
120  G4double z0 = -0.5 * envSizeZ;
121 
122  // set up the gamma ray position by G4ThreeVector obj.
123  fParticleGun->SetParticlePosition(G4ThreeVector(x0,y0,z0));
124 
125 
126 
127 
128  // anEvent is our input argument.
129  // data type note: G4ParticleGun* fParticleGun;
130  // think of this line is a routine.
131  fParticleGun->GeneratePrimaryVertex(anEvent);
132 
133 
134 
135 }
136 
137 
virtual void GeneratePrimaries(G4Event *)
setup the gamma's location to get the width and height of the 'Envelope' phyisclogic volume...
B1PrimaryGeneratorAction()
set up our gamma beamIt is 6MeV gamma, moving toward (0,0,1) direction. gamma's location will be se...
Definition of the B1PrimaryGeneratorAction class.
virtual ~B1PrimaryGeneratorAction()
we must delete the fParticleGun when the B1PrimaryGeneratorAction obj is deleted. ...