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5.p1 ] [ 10.6 ] [ 10.6.p1 ] [ 10.6.p2 ] [ 10.6.p3 ] [ 10.7 ] [ 10.7.p1 ] [ 10.7.p2 ] [ 10.7.p3 ] [ 10.7.p4 ] [ 11.0 ] [ 11.0.p1 ] [ 11.0.p2 ] [ 11.0.p3, ] [ 11.0.p4 ] [ 11.1 ] [ 11.1.1 ] [ 11.1.2 ] [ 11.1.3 ] [ 11.2 ] [ 11.2.1 ] [ 11.2.2 ] [ 11.3.0 ]

Diff markup

Differences between /event/src/G4ParticleGun.cc (Version 11.3.0) and /event/src/G4ParticleGun.cc (Version 10.4.p2)


  1 //                                                  1 //
  2 // *******************************************      2 // ********************************************************************
  3 // * License and Disclaimer                         3 // * License and Disclaimer                                           *
  4 // *                                                4 // *                                                                  *
  5 // * The  Geant4 software  is  copyright of th      5 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
  6 // * the Geant4 Collaboration.  It is provided      6 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
  7 // * conditions of the Geant4 Software License      7 // * conditions of the Geant4 Software License,  included in the file *
  8 // * LICENSE and available at  http://cern.ch/      8 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
  9 // * include a list of copyright holders.           9 // * include a list of copyright holders.                             *
 10 // *                                               10 // *                                                                  *
 11 // * Neither the authors of this software syst     11 // * Neither the authors of this software system, nor their employing *
 12 // * institutes,nor the agencies providing fin     12 // * institutes,nor the agencies providing financial support for this *
 13 // * work  make  any representation or  warran     13 // * work  make  any representation or  warranty, express or implied, *
 14 // * regarding  this  software system or assum     14 // * regarding  this  software system or assume any liability for its *
 15 // * use.  Please see the license in the file      15 // * use.  Please see the license in the file  LICENSE  and URL above *
 16 // * for the full disclaimer and the limitatio     16 // * for the full disclaimer and the limitation of liability.         *
 17 // *                                               17 // *                                                                  *
 18 // * This  code  implementation is the result      18 // * This  code  implementation is the result of  the  scientific and *
 19 // * technical work of the GEANT4 collaboratio     19 // * technical work of the GEANT4 collaboration.                      *
 20 // * By using,  copying,  modifying or  distri     20 // * By using,  copying,  modifying or  distributing the software (or *
 21 // * any work based  on the software)  you  ag     21 // * any work based  on the software)  you  agree  to acknowledge its *
 22 // * use  in  resulting  scientific  publicati     22 // * use  in  resulting  scientific  publications,  and indicate your *
 23 // * acceptance of all terms of the Geant4 Sof     23 // * acceptance of all terms of the Geant4 Software license.          *
 24 // *******************************************     24 // ********************************************************************
 25 //                                                 25 //
 26 // G4ParticleGun class implementation          << 
 27 //                                                 26 //
 28 // Author: Makoto Asai, 1997                   <<  27 // $Id: G4ParticleGun.cc 94950 2016-01-07 11:53:14Z gcosmo $
 29 // ------------------------------------------- <<  28 //
 30                                                    29 
                                                   >>  30 // G4ParticleGun
 31 #include "G4ParticleGun.hh"                        31 #include "G4ParticleGun.hh"
 32 #include "G4SystemOfUnits.hh"                      32 #include "G4SystemOfUnits.hh"
 33 #include "G4PrimaryParticle.hh"                    33 #include "G4PrimaryParticle.hh"
 34 #include "G4ParticleGunMessenger.hh"               34 #include "G4ParticleGunMessenger.hh"
 35 #include "G4Event.hh"                              35 #include "G4Event.hh"
 36 #include "G4ios.hh"                                36 #include "G4ios.hh"
 37                                                    37 
 38 G4ParticleGun::G4ParticleGun()                     38 G4ParticleGun::G4ParticleGun()
 39 {                                                  39 {
 40   SetInitialValues();                              40   SetInitialValues();
 41 }                                                  41 }
 42                                                    42 
 43 G4ParticleGun::G4ParticleGun(G4int numberofpar     43 G4ParticleGun::G4ParticleGun(G4int numberofparticles)
 44 {                                                  44 {
 45   SetInitialValues();                              45   SetInitialValues();
 46   NumberOfParticlesToBeGenerated = numberofpar     46   NumberOfParticlesToBeGenerated = numberofparticles;
 47 }                                                  47 }
 48                                                    48 
 49 G4ParticleGun::G4ParticleGun(G4ParticleDefinit <<  49 G4ParticleGun::G4ParticleGun
 50                              G4int numberofpar <<  50     (G4ParticleDefinition * particleDef, G4int numberofparticles)
 51 {                                                  51 {
 52   SetInitialValues();                              52   SetInitialValues();
 53   NumberOfParticlesToBeGenerated = numberofpar     53   NumberOfParticlesToBeGenerated = numberofparticles;
 54   SetParticleDefinition( particleDef );            54   SetParticleDefinition( particleDef );
 55 }                                                  55 }
 56                                                    56 
 57 void G4ParticleGun::SetInitialValues()             57 void G4ParticleGun::SetInitialValues()
 58 {                                                  58 {
 59   NumberOfParticlesToBeGenerated = 1;              59   NumberOfParticlesToBeGenerated = 1;
 60   particle_definition = nullptr;                   60   particle_definition = nullptr;
 61   G4ThreeVector zero;                              61   G4ThreeVector zero;
 62   particle_momentum_direction = (G4ParticleMom     62   particle_momentum_direction = (G4ParticleMomentum)zero;
 63   particle_energy = 0.0;                           63   particle_energy = 0.0;
 64   particle_momentum = 0.0;                         64   particle_momentum = 0.0;
 65   particle_position = zero;                        65   particle_position = zero;
 66   particle_time = 0.0;                             66   particle_time = 0.0;
 67   particle_polarization = zero;                    67   particle_polarization = zero;
 68   particle_charge = 0.0;                           68   particle_charge = 0.0;
 69   theMessenger = new G4ParticleGunMessenger(th     69   theMessenger = new G4ParticleGunMessenger(this);
 70 }                                                  70 }
 71                                                    71 
 72 G4ParticleGun::~G4ParticleGun()                    72 G4ParticleGun::~G4ParticleGun()
 73 {                                                  73 {
 74   delete theMessenger;                             74   delete theMessenger;
 75 }                                                  75 }
 76                                                    76 
 77 void G4ParticleGun::                           <<  77 //G4ParticleGun::G4ParticleGun(const G4ParticleGun& /*right*/)
 78 SetParticleDefinition(G4ParticleDefinition* aP <<  78 //:G4VPrimaryGenerator()
                                                   >>  79 //{ G4Exception(
                                                   >>  80 //  "G4ParticleGun::G4ParticleGun","Event0191",FatalException,
                                                   >>  81 //  "G4ParticleGun : Copy constructor should not be used."); }
                                                   >>  82 //
                                                   >>  83 //const G4ParticleGun& G4ParticleGun::operator=(const G4ParticleGun& right)
                                                   >>  84 //{ G4Exception(
                                                   >>  85 //  "G4ParticleGun::operator=","Event0192",FatalException,
                                                   >>  86 //  "G4ParticleGun : Equal operator should not be used.");
                                                   >>  87 //  return right; }
                                                   >>  88 //
                                                   >>  89 //G4int G4ParticleGun::operator==(const G4ParticleGun& /*right*/) const
                                                   >>  90 //{ G4Exception(
                                                   >>  91 //  "G4ParticleGun::operator==","Event0193",FatalException,
                                                   >>  92 //  "G4ParticleGun : == operator should not be used.");
                                                   >>  93 //  return true; }
                                                   >>  94 //
                                                   >>  95 //G4int G4ParticleGun::operator!=(const G4ParticleGun& /*right*/) const
                                                   >>  96 //{ G4Exception(
                                                   >>  97 //  "G4ParticleGun::operator!=","Event0193",FatalException,
                                                   >>  98 //  "G4ParticleGun : != operator should not be used.");
                                                   >>  99 //  return false; }
                                                   >> 100 
                                                   >> 101 void G4ParticleGun::SetParticleDefinition
                                                   >> 102                  (G4ParticleDefinition * aParticleDefinition)
 79 {                                                 103 { 
 80   if(aParticleDefinition == nullptr)           << 104   if(!aParticleDefinition)
 81   {                                               105   {
 82     G4Exception("G4ParticleGun::SetParticleDef << 106     G4Exception("G4ParticleGun::SetParticleDefinition()","Event0101",
 83                 FatalException, "Null pointer  << 107      FatalException,"Null pointer is given.");
 84   }                                               108   }
 85   if(aParticleDefinition->IsShortLived())         109   if(aParticleDefinition->IsShortLived())
 86   {                                               110   {
 87     if(aParticleDefinition->GetDecayTable() == << 111     if(!(aParticleDefinition->GetDecayTable()))
 88     {                                             112     {
 89       G4ExceptionDescription ED;                  113       G4ExceptionDescription ED;
 90       ED << "G4ParticleGun does not support sh << 114       ED << "G4ParticleGun does not support shooting a short-lived particle without a valid decay table." << G4endl;
 91          << "particle without a valid decay ta << 
 92       ED << "G4ParticleGun::SetParticleDefinit    115       ED << "G4ParticleGun::SetParticleDefinition for "
 93          << aParticleDefinition->GetParticleNa    116          << aParticleDefinition->GetParticleName() << " is ignored." << G4endl;
 94       G4Exception("G4ParticleGun::SetParticleD << 117       G4Exception("G4ParticleGun::SetParticleDefinition()","Event0102",
 95                   JustWarning, ED);            << 118       JustWarning,ED);
 96       return;                                     119       return;
 97     }                                             120     }
 98   }                                               121   }
 99   particle_definition = aParticleDefinition;      122   particle_definition = aParticleDefinition; 
100   particle_charge = particle_definition->GetPD    123   particle_charge = particle_definition->GetPDGCharge();
101   if(particle_momentum>0.0)                       124   if(particle_momentum>0.0)
102   {                                               125   {
103     G4double mass = particle_definition->GetPD << 126     G4double mass =  particle_definition->GetPDGMass();
104     particle_energy =                             127     particle_energy =
105       std::sqrt(particle_momentum*particle_mom << 128                  std::sqrt(particle_momentum*particle_momentum+mass*mass)-mass;
106   }                                               129   }
107 }                                                 130 }
108                                                   131 
109 void G4ParticleGun::SetParticleEnergy(G4double    132 void G4ParticleGun::SetParticleEnergy(G4double aKineticEnergy)
110 {                                                 133 {
111   particle_energy = aKineticEnergy;               134   particle_energy = aKineticEnergy;
112   if(particle_momentum>0.0)                    << 135   if(particle_momentum>0.0){
113   {                                            << 136     if(particle_definition){
114     if(particle_definition != nullptr)         << 
115     {                                          << 
116       G4cout << "G4ParticleGun::" << particle_    137       G4cout << "G4ParticleGun::" << particle_definition->GetParticleName()
117              << G4endl;                           138              << G4endl;
118     }                                          << 139     }else{
119     else                                       << 
120     {                                          << 
121       G4cout << "G4ParticleGun::" << " " << G4    140       G4cout << "G4ParticleGun::" << " " << G4endl;
122     }                                             141     }
123     G4cout << " was defined in terms of Moment    142     G4cout << " was defined in terms of Momentum: " 
124            << particle_momentum/GeV << "GeV/c"    143            << particle_momentum/GeV << "GeV/c" << G4endl;
125     G4cout << " is now defined in terms of Kin    144     G4cout << " is now defined in terms of KineticEnergy: " 
126            << particle_energy/GeV   << "GeV"      145            << particle_energy/GeV   << "GeV"   << G4endl;
127     particle_momentum = 0.0;                      146     particle_momentum = 0.0;
128   }                                               147   }
129 }                                                 148 }
130                                                   149 
131 void G4ParticleGun::SetParticleMomentum(G4doub    150 void G4ParticleGun::SetParticleMomentum(G4double aMomentum)
132 {                                                 151 {
133   if(particle_energy>0.0)                      << 152   if(particle_energy>0.0){
134   {                                            << 153     if(particle_definition){
135     if(particle_definition != nullptr)         << 
136     {                                          << 
137       G4cout << "G4ParticleGun::" << particle_    154       G4cout << "G4ParticleGun::" << particle_definition->GetParticleName()
138              << G4endl;                           155              << G4endl;
139     }                                          << 156     }else{
140     else                                       << 
141     {                                          << 
142       G4cout << "G4ParticleGun::" << " " << G4    157       G4cout << "G4ParticleGun::" << " " << G4endl;
143     }                                             158     }
144     G4cout << " was defined in terms of Kineti    159     G4cout << " was defined in terms of KineticEnergy: "
145            << particle_energy/GeV << "GeV"   <    160            << particle_energy/GeV << "GeV"   << G4endl;
146     G4cout << " is now defined in terms Moment    161     G4cout << " is now defined in terms Momentum: "
147            << aMomentum/GeV       << "GeV/c" <    162            << aMomentum/GeV       << "GeV/c" << G4endl;
148   }                                               163   }
149   if(particle_definition == nullptr)           << 164   if(!particle_definition)
150   {                                               165   {
151     G4cout << "Particle Definition not defined << 166     G4cout <<"Particle Definition not defined yet for G4ParticleGun"<< G4endl;
152            << G4endl;                          << 167     G4cout <<"Zero Mass is assumed"<<G4endl;
153     G4cout << "Zero Mass is assumed" << G4endl << 
154     particle_momentum = aMomentum;                168     particle_momentum = aMomentum;
155     particle_energy = aMomentum;                  169     particle_energy = aMomentum;
156   }                                               170   }
157   else                                            171   else
158   {                                               172   {
159     G4double mass = particle_definition->GetPD << 173     G4double mass =  particle_definition->GetPDGMass();
160     particle_momentum = aMomentum;                174     particle_momentum = aMomentum;
161     particle_energy =                             175     particle_energy =
162       std::sqrt(particle_momentum*particle_mom << 176                  std::sqrt(particle_momentum*particle_momentum+mass*mass)-mass;
163   }                                               177   }
164 }                                                 178 }
165                                                   179  
166 void G4ParticleGun::SetParticleMomentum(G4Part    180 void G4ParticleGun::SetParticleMomentum(G4ParticleMomentum aMomentum)
167 {                                                 181 {
168   if(particle_energy>0.0)                      << 182   if(particle_energy>0.0){
169   {                                            << 183     if(particle_definition){
170     if(particle_definition != nullptr)         << 
171     {                                          << 
172       G4cout << "G4ParticleGun::" << particle_    184       G4cout << "G4ParticleGun::" << particle_definition->GetParticleName()
173              << G4endl;                           185              << G4endl;
174     }                                          << 186     }else{
175     else                                       << 
176     {                                          << 
177       G4cout << "G4ParticleGun::" << " " << G4    187       G4cout << "G4ParticleGun::" << " " << G4endl;
178     }                                             188     }
179     G4cout << " was defined in terms of Kineti    189     G4cout << " was defined in terms of KineticEnergy: "
180            << particle_energy/GeV << "GeV"   <    190            << particle_energy/GeV << "GeV"   << G4endl;
181     G4cout << " is now defined in terms Moment    191     G4cout << " is now defined in terms Momentum: "
182            << aMomentum.mag()/GeV << "GeV/c" <    192            << aMomentum.mag()/GeV << "GeV/c" << G4endl;
183   }                                               193   }
184   if(particle_definition == nullptr)           << 194   if(!particle_definition)
185   {                                               195   {
186     G4cout << "Particle Definition not defined << 196     G4cout <<"Particle Definition not defined yet for G4ParticleGun"<< G4endl;
187            << G4endl;                          << 197     G4cout <<"Zero Mass is assumed"<<G4endl;
188     G4cout << "Zero Mass is assumed" << G4endl << 198     particle_momentum_direction =  aMomentum.unit();
189     particle_momentum_direction = aMomentum.un << 
190     particle_momentum = aMomentum.mag();          199     particle_momentum = aMomentum.mag();
191     particle_energy = aMomentum.mag();            200     particle_energy = aMomentum.mag();
192   }                                               201   } 
193   else                                            202   else 
194   {                                               203   {
195     G4double mass =  particle_definition->GetP    204     G4double mass =  particle_definition->GetPDGMass();
196     particle_momentum = aMomentum.mag();          205     particle_momentum = aMomentum.mag();
197     particle_momentum_direction = aMomentum.un << 206     particle_momentum_direction =  aMomentum.unit();
198     particle_energy =                             207     particle_energy = 
199       std::sqrt(particle_momentum*particle_mom << 208                  std::sqrt(particle_momentum*particle_momentum+mass*mass)-mass;
200   }                                               209   }
201 }                                                 210 }
202                                                   211 
203 void G4ParticleGun::GeneratePrimaryVertex(G4Ev    212 void G4ParticleGun::GeneratePrimaryVertex(G4Event* evt)
204 {                                                 213 {
205   if(particle_definition == nullptr)           << 214   if(!particle_definition) 
206   {                                               215   {
207     G4ExceptionDescription ED;                    216     G4ExceptionDescription ED;
208     ED << "Particle definition is not defined.    217     ED << "Particle definition is not defined." << G4endl;
209     ED << "G4ParticleGun::SetParticleDefinitio << 218     ED << "G4ParticleGun::SetParticleDefinition() has to be invoked beforehand." << G4endl;
210        << G4endl;                              << 219     G4Exception("G4ParticleGun::GeneratePrimaryVertex()","Event0109",
211     G4Exception("G4ParticleGun::GeneratePrimar << 220     FatalException, ED);
212                 FatalException, ED);           << 
213     return;                                       221     return;
214   }                                               222   }
215                                                   223 
216   // Create a new vertex                       << 224   // create a new vertex
217   //                                           << 225   G4PrimaryVertex* vertex = 
218   auto* vertex =                               << 
219     new G4PrimaryVertex(particle_position,part    226     new G4PrimaryVertex(particle_position,particle_time);
220                                                   227 
221   // Create new primaries and set them to the  << 228   // create new primaries and set them to the vertex
222   //                                           << 
223   G4double mass =  particle_definition->GetPDG    229   G4double mass =  particle_definition->GetPDGMass();
224   for( G4int i=0; i<NumberOfParticlesToBeGener << 230   for( G4int i=0; i<NumberOfParticlesToBeGenerated; i++ ){
225   {                                            << 231     G4PrimaryParticle* particle =
226     auto* particle =                           << 
227       new G4PrimaryParticle(particle_definitio    232       new G4PrimaryParticle(particle_definition);
228     particle->SetKineticEnergy( particle_energ    233     particle->SetKineticEnergy( particle_energy );
229     particle->SetMass( mass );                    234     particle->SetMass( mass );
230     particle->SetMomentumDirection( particle_m    235     particle->SetMomentumDirection( particle_momentum_direction );
231     particle->SetCharge( particle_charge );       236     particle->SetCharge( particle_charge );
232     particle->SetPolarization(particle_polariz    237     particle->SetPolarization(particle_polarization.x(),
233                               particle_polariz << 238             particle_polarization.y(),
234                               particle_polariz << 239             particle_polarization.z());
235     particle->SetWeight( particle_weight );    << 
236     vertex->SetPrimary( particle );               240     vertex->SetPrimary( particle );
237   }                                               241   }
                                                   >> 242 
238   evt->AddPrimaryVertex( vertex );                243   evt->AddPrimaryVertex( vertex );
239 }                                                 244 }
                                                   >> 245 
                                                   >> 246 
240                                                   247