Geant4 10.7.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4ParticleChange.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// ********************************************************************
25//
26// G4ParticleChange class implementation
27//
28// Author: Hisaya Kurashige, 23 March 1998
29// --------------------------------------------------------------------
30
31#include "G4ParticleChange.hh"
33#include "G4SystemOfUnits.hh"
34#include "G4Track.hh"
35#include "G4Step.hh"
36#include "G4TrackFastVector.hh"
37#include "G4DynamicParticle.hh"
39
40// --------------------------------------------------------------------
43{
44}
45
46// --------------------------------------------------------------------
48{
49}
50
51// --------------------------------------------------------------------
53 : G4VParticleChange(right)
54{
56
66 isVelocityChanged = true;
70}
71
72// --------------------------------------------------------------------
74{
75 if(this != &right)
76 {
78 {
79 for(G4int index = 0; index < theNumberOfSecondaries; ++index)
80 {
81 if((*theListOfSecondaries)[index])
82 delete(*theListOfSecondaries)[index];
83 }
84 }
86
89 for(G4int index = 0; index < theNumberOfSecondaries; ++index)
90 {
91 G4Track* newTrack = new G4Track(*((*right.theListOfSecondaries)[index]));
92 theListOfSecondaries->SetElement(index, newTrack);
93 }
94
97
107 isVelocityChanged = true;
111
115 }
116 return *this;
117}
118
119// --------------------------------------------------------------------
121{
122 return ((G4VParticleChange*) this == (G4VParticleChange*) &right);
123}
124
125// --------------------------------------------------------------------
127{
128 return ((G4VParticleChange*) this != (G4VParticleChange*) &right);
129}
130
131// --------------------------------------------------------------------
133 G4bool IsGoodForTracking)
134{
135 // create track
136 G4Track* aTrack = new G4Track(aParticle, GetGlobalTime(), thePositionChange);
137
138 // set IsGoodGorTrackingFlag
139 if(IsGoodForTracking)
140 aTrack->SetGoodForTrackingFlag();
141
142 // touchable handle is copied to keep the pointer
144
145 // add a secondary
147}
148
149// --------------------------------------------------------------------
151 G4ThreeVector newPosition,
152 G4bool IsGoodForTracking)
153{
154 // create track
155 G4Track* aTrack = new G4Track(aParticle, GetGlobalTime(), newPosition);
156
157 // set IsGoodGorTrackingFlag
158 if(IsGoodForTracking)
159 aTrack->SetGoodForTrackingFlag();
160
161 // touchable is a temporary object, so you cannot keep the pointer
162 aTrack->SetTouchableHandle(nullptr);
163
164 // add a secondary
166}
167
168// --------------------------------------------------------------------
170 G4double newTime, G4bool IsGoodForTracking)
171{
172 // create track
173 G4Track* aTrack = new G4Track(aParticle, newTime, thePositionChange);
174
175 // set IsGoodGorTrackingFlag
176 if(IsGoodForTracking)
177 aTrack->SetGoodForTrackingFlag();
178
179 // touchable handle is copied to keep the pointer
181
182 // add a secondary
184}
185
186// --------------------------------------------------------------------
188{
189 // add a secondary
191}
192
193// --------------------------------------------------------------------
195{
196 // use base class's method at first
198 theCurrentTrack = &track;
199
200 // set Energy/Momentum etc. equal to those of the parent particle
201 const G4DynamicParticle* pParticle = track.GetDynamicParticle();
202 theEnergyChange = pParticle->GetKineticEnergy();
204 isVelocityChanged = false;
207 theProperTimeChange = pParticle->GetProperTime();
208
209 // set mass/charge/MagneticMoment of DynamicParticle
210 theMassChange = pParticle->GetMass();
211 theChargeChange = pParticle->GetCharge();
213
214 // set Position equal to those of the parent track
216
217 // set TimeChange equal to local time of the parent track
218 theTimeChange = track.GetLocalTime();
219
220 // set initial Local/Global time of the parent track
221 theLocalTime0 = track.GetLocalTime();
223}
224
225// --------------------------------------------------------------------
227{
228 // A physics process always calculates the final state of the
229 // particle relative to the initial state at the beginning
230 // of the Step, i.e., based on information of G4Track (or
231 // equivalently the PreStepPoint).
232 // So, the differences (delta) between these two states have to be
233 // calculated and be accumulated in PostStepPoint
234
235 // Take note that the return type of GetMomentumDirectionChange() is a
236 // pointer to G4ParticleMometum. Also it is a normalized
237 // momentum vector
238
239 G4StepPoint* pPreStepPoint = pStep->GetPreStepPoint();
240 G4StepPoint* pPostStepPoint = pStep->GetPostStepPoint();
241 G4Track* pTrack = pStep->GetTrack();
242 G4double mass = theMassChange;
243
244 // set Mass/Charge/MagneticMoment
245 pPostStepPoint->SetMass(theMassChange);
246 pPostStepPoint->SetCharge(theChargeChange);
248
249 // calculate new kinetic energy
250 G4double preEnergy = pPreStepPoint->GetKineticEnergy();
251 G4double energy =
252 pPostStepPoint->GetKineticEnergy() + (theEnergyChange - preEnergy);
253
254 // update kinetic energy and momentum direction
255 if(energy > 0.0)
256 {
257 // calculate new momentum
258 G4ThreeVector pMomentum = pPostStepPoint->GetMomentum()
260 - pPreStepPoint->GetMomentum());
261 G4double tMomentum = pMomentum.mag();
262 G4ThreeVector direction(1.0, 0.0, 0.0);
263 if(tMomentum > 0.)
264 {
265 G4double inv_Momentum = 1.0 / tMomentum;
266 direction = pMomentum * inv_Momentum;
267 }
268 pPostStepPoint->SetMomentumDirection(direction);
269 pPostStepPoint->SetKineticEnergy(energy);
270 }
271 else
272 {
273 // stop case
274 // pPostStepPoint->SetMomentumDirection(G4ThreeVector(1., 0., 0.));
275 pPostStepPoint->SetKineticEnergy(0.0);
276 }
277 // calculate velocity
279 {
280 if(energy > 0.0)
281 {
282 pTrack->SetKineticEnergy(energy);
284 pTrack->SetKineticEnergy(preEnergy);
285 }
286 else if(theMassChange > 0.0)
287 {
288 theVelocityChange = 0.0;
289 }
290 }
291 pPostStepPoint->SetVelocity(theVelocityChange);
292
293 // update polarization
294 pPostStepPoint->AddPolarization(thePolarizationChange -
295 pPreStepPoint->GetPolarization());
296
297 // update position and time
298 pPostStepPoint->AddPosition(thePositionChange - pPreStepPoint->GetPosition());
299 pPostStepPoint->AddGlobalTime(theTimeChange - theLocalTime0);
300 pPostStepPoint->AddLocalTime(theTimeChange - theLocalTime0);
301 pPostStepPoint->AddProperTime(theProperTimeChange -
302 pPreStepPoint->GetProperTime());
303
305 {
306 pPostStepPoint->SetWeight(theParentWeight);
307 }
308
309#ifdef G4VERBOSE
310 G4Track* aTrack = pStep->GetTrack();
311 if(debugFlag) { CheckIt(*aTrack); }
312#endif
313
314 // update the G4Step specific attributes
315 return UpdateStepInfo(pStep);
316}
317
318// --------------------------------------------------------------------
320{
321 // A physics process always calculates the final state of the particle
322
323 // Take note that the return type of GetMomentumChange() is a
324 // pointer to G4ParticleMometum. Also it is a normalized
325 // momentum vector
326
327 G4StepPoint* pPostStepPoint = pStep->GetPostStepPoint();
328 G4Track* pTrack = pStep->GetTrack();
329
330 // set Mass/Charge
331 pPostStepPoint->SetMass(theMassChange);
332 pPostStepPoint->SetCharge(theChargeChange);
334
335 // update kinetic energy and momentum direction
337 pPostStepPoint->SetKineticEnergy(theEnergyChange);
338
339 // calculate velocity
342 {
343 if(theEnergyChange > 0.0)
344 {
346 }
347 else if(theMassChange > 0.0)
348 {
349 theVelocityChange = 0.0;
350 }
351 }
352 pPostStepPoint->SetVelocity(theVelocityChange);
353
354 // update polarization
355 pPostStepPoint->SetPolarization(thePolarizationChange);
356
357 // update position and time
358 pPostStepPoint->SetPosition(thePositionChange);
359 pPostStepPoint->AddGlobalTime(theTimeChange - theLocalTime0);
360 pPostStepPoint->SetLocalTime(theTimeChange);
361 pPostStepPoint->SetProperTime(theProperTimeChange);
362
364 {
365 pPostStepPoint->SetWeight(theParentWeight);
366 }
367
368#ifdef G4VERBOSE
369 G4Track* aTrack = pStep->GetTrack();
370 if(debugFlag) { CheckIt(*aTrack); }
371#endif
372
373 // update the G4Step specific attributes
374 return UpdateStepInfo(pStep);
375}
376
377// --------------------------------------------------------------------
379{
380 // A physics process always calculates the final state of the particle
381
382 G4StepPoint* pPostStepPoint = pStep->GetPostStepPoint();
383
384 // set Mass/Charge
385 pPostStepPoint->SetMass(theMassChange);
386 pPostStepPoint->SetCharge(theChargeChange);
388
389 // update kinetic energy and momentum direction
391 pPostStepPoint->SetKineticEnergy(theEnergyChange);
394 pPostStepPoint->SetVelocity(theVelocityChange);
395
396 // update polarization
397 pPostStepPoint->SetPolarization(thePolarizationChange);
398
399 // update position and time
400 pPostStepPoint->SetPosition(thePositionChange);
401 pPostStepPoint->AddGlobalTime(theTimeChange - theLocalTime0);
402 pPostStepPoint->SetLocalTime(theTimeChange);
403 pPostStepPoint->SetProperTime(theProperTimeChange);
404
406 {
407 pPostStepPoint->SetWeight(theParentWeight);
408 }
409
410#ifdef G4VERBOSE
411 G4Track* aTrack = pStep->GetTrack();
412 if(debugFlag) { CheckIt(*aTrack); }
413#endif
414
415 // update the G4Step specific attributes
416 return UpdateStepInfo(pStep);
417}
418
419// --------------------------------------------------------------------
421{
422 // use base-class DumpInfo
424
425 G4int oldprc = G4cout.precision(3);
426
427 G4cout << " Mass (GeV) : " << std::setw(20) << theMassChange / GeV
428 << G4endl;
429 G4cout << " Charge (eplus) : " << std::setw(20)
430 << theChargeChange / eplus << G4endl;
431 G4cout << " MagneticMoment : " << std::setw(20)
433 G4cout << " : = " << std::setw(20)
435 * 2. * theMassChange / c_squared / eplus / hbar_Planck
436 << "*[e hbar]/[2 m]" << G4endl;
437 G4cout << " Position - x (mm) : " << std::setw(20)
438 << thePositionChange.x() / mm << G4endl;
439 G4cout << " Position - y (mm) : " << std::setw(20)
440 << thePositionChange.y() / mm << G4endl;
441 G4cout << " Position - z (mm) : " << std::setw(20)
442 << thePositionChange.z() / mm << G4endl;
443 G4cout << " Time (ns) : " << std::setw(20)
444 << theTimeChange / ns << G4endl;
445 G4cout << " Proper Time (ns) : " << std::setw(20)
447 G4cout << " Momentum Direct - x : " << std::setw(20)
449 G4cout << " Momentum Direct - y : " << std::setw(20)
451 G4cout << " Momentum Direct - z : " << std::setw(20)
453 G4cout << " Kinetic Energy (MeV): " << std::setw(20)
454 << theEnergyChange / MeV << G4endl;
455 G4cout << " Velocity (/c): " << std::setw(20)
456 << theVelocityChange / c_light << G4endl;
457 G4cout << " Polarization - x : " << std::setw(20)
459 G4cout << " Polarization - y : " << std::setw(20)
461 G4cout << " Polarization - z : " << std::setw(20)
463 G4cout.precision(oldprc);
464}
465
466// --------------------------------------------------------------------
468{
469 G4bool exitWithError = false;
470 G4double accuracy;
471 static G4ThreadLocal G4int nError = 0;
472#ifdef G4VERBOSE
473 const G4int maxError = 30;
474#endif
475
476 // no check in case of "fStopAndKill"
478 return G4VParticleChange::CheckIt(aTrack);
479
480 // MomentumDirection should be unit vector
481 G4bool itsOKforMomentum = true;
482 if(theEnergyChange > 0.)
483 {
484 accuracy = std::fabs(theMomentumDirectionChange.mag2() - 1.0);
485 if(accuracy > accuracyForWarning)
486 {
487 itsOKforMomentum = false;
488 nError += 1;
489 exitWithError = exitWithError || (accuracy > accuracyForException);
490#ifdef G4VERBOSE
491 if(nError < maxError)
492 {
493 G4cout << " G4ParticleChange::CheckIt : ";
494 G4cout << "the Momentum Change is not unit vector !!"
495 << " Difference: " << accuracy << G4endl;
497 << " E=" << aTrack.GetKineticEnergy() / MeV
498 << " pos=" << aTrack.GetPosition().x() / m << ", "
499 << aTrack.GetPosition().y() / m << ", "
500 << aTrack.GetPosition().z() / m << G4endl;
501 }
502#endif
503 }
504 }
505
506 // both global and proper time should not go back
507 G4bool itsOKforGlobalTime = true;
508 accuracy = (aTrack.GetLocalTime() - theTimeChange) / ns;
509 if(accuracy > accuracyForWarning)
510 {
511 itsOKforGlobalTime = false;
512 nError += 1;
513 exitWithError = exitWithError || (accuracy > accuracyForException);
514#ifdef G4VERBOSE
515 if(nError < maxError)
516 {
517 G4cout << " G4ParticleChange::CheckIt : ";
518 G4cout << "the local time goes back !!"
519 << " Difference: " << accuracy << "[ns] " << G4endl;
521 << " E=" << aTrack.GetKineticEnergy() / MeV
522 << " pos=" << aTrack.GetPosition().x() / m << ", "
523 << aTrack.GetPosition().y() / m << ", "
524 << aTrack.GetPosition().z() / m
525 << " global time=" << aTrack.GetGlobalTime() / ns
526 << " local time=" << aTrack.GetLocalTime() / ns
527 << " proper time=" << aTrack.GetProperTime() / ns << G4endl;
528 }
529#endif
530 }
531
532 G4bool itsOKforProperTime = true;
533 accuracy = (aTrack.GetProperTime() - theProperTimeChange) / ns;
534 if(accuracy > accuracyForWarning)
535 {
536 itsOKforProperTime = false;
537 nError += 1;
538 exitWithError = exitWithError || (accuracy > accuracyForException);
539#ifdef G4VERBOSE
540 if(nError < maxError)
541 {
542 G4cout << " G4ParticleChange::CheckIt : ";
543 G4cout << "the proper time goes back !!"
544 << " Difference: " << accuracy << "[ns] " << G4endl;
546 << " E=" << aTrack.GetKineticEnergy() / MeV
547 << " pos=" << aTrack.GetPosition().x() / m << ", "
548 << aTrack.GetPosition().y() / m << ", "
549 << aTrack.GetPosition().z() / m
550 << " global time=" << aTrack.GetGlobalTime() / ns
551 << " local time=" << aTrack.GetLocalTime() / ns
552 << " proper time=" << aTrack.GetProperTime() / ns << G4endl;
553 }
554#endif
555 }
556
557 // kinetic Energy should not be negative
558 G4bool itsOKforEnergy = true;
559 accuracy = -1.0 * theEnergyChange / MeV;
560 if(accuracy > accuracyForWarning)
561 {
562 itsOKforEnergy = false;
563 nError += 1;
564 exitWithError = exitWithError || (accuracy > accuracyForException);
565#ifdef G4VERBOSE
566 if(nError < maxError)
567 {
568 G4cout << " G4ParticleChange::CheckIt : ";
569 G4cout << "the kinetic energy is negative !!"
570 << " Difference: " << accuracy << "[MeV] " << G4endl;
572 << " E=" << aTrack.GetKineticEnergy() / MeV
573 << " pos=" << aTrack.GetPosition().x() / m << ", "
574 << aTrack.GetPosition().y() / m << ", "
575 << aTrack.GetPosition().z() / m << G4endl;
576 }
577#endif
578 }
579
580 // velocity should not be less than c_light
581 G4bool itsOKforVelocity = true;
582 if(theVelocityChange < 0.)
583 {
584 itsOKforVelocity = false;
585 nError += 1;
586 exitWithError = true;
587#ifdef G4VERBOSE
588 if(nError < maxError)
589 {
590 G4cout << " G4ParticleChange::CheckIt : ";
591 G4cout << "the velocity is negative !!"
592 << " Velocity: " << theVelocityChange / c_light << G4endl;
594 << " E=" << aTrack.GetKineticEnergy() / MeV
595 << " pos=" << aTrack.GetPosition().x() / m << ", "
596 << aTrack.GetPosition().y() / m << ", "
597 << aTrack.GetPosition().z() / m << G4endl;
598 }
599#endif
600 }
601
602 accuracy = theVelocityChange / c_light - 1.0;
603 if(accuracy > accuracyForWarning)
604 {
605 itsOKforVelocity = false;
606 nError += 1;
607 exitWithError = exitWithError || (accuracy > accuracyForException);
608#ifdef G4VERBOSE
609 if(nError < maxError)
610 {
611 G4cout << " G4ParticleChange::CheckIt : ";
612 G4cout << "the velocity is greater than c_light !!" << G4endl;
613 G4cout << " Velocity: " << theVelocityChange / c_light << G4endl;
615 << " E=" << aTrack.GetKineticEnergy() / MeV
616 << " pos=" << aTrack.GetPosition().x() / m << ", "
617 << aTrack.GetPosition().y() / m << ", "
618 << aTrack.GetPosition().z() / m << G4endl;
619 }
620#endif
621 }
622
623 G4bool itsOK = itsOKforMomentum && itsOKforEnergy && itsOKforVelocity &&
624 itsOKforProperTime && itsOKforGlobalTime;
625 // dump out information of this particle change
626#ifdef G4VERBOSE
627 if(!itsOK)
628 {
629 DumpInfo();
630 }
631#endif
632
633 // Exit with error
634 if(exitWithError)
635 {
636 G4Exception("G4ParticleChange::CheckIt()", "TRACK003", EventMustBeAborted,
637 "momentum, energy, and/or time was illegal");
638 }
639 // correction
640 if(!itsOKforMomentum)
641 {
644 }
645 if(!itsOKforGlobalTime)
646 {
647 theTimeChange = aTrack.GetLocalTime();
648 }
649 if(!itsOKforProperTime)
650 {
652 }
653 if(!itsOKforEnergy)
654 {
655 theEnergyChange = 0.0;
656 }
657 if(!itsOKforVelocity)
658 {
659 theVelocityChange = c_light;
660 }
661
662 itsOK = (itsOK) && G4VParticleChange::CheckIt(aTrack);
663 return itsOK;
664}
@ EventMustBeAborted
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
Definition: G4Exception.cc:35
G4FastVector< G4Track, G4TrackFastVectorSize > G4TrackFastVector
@ fStopAndKill
double G4double
Definition: G4Types.hh:83
bool G4bool
Definition: G4Types.hh:86
int G4int
Definition: G4Types.hh:85
#define G4endl
Definition: G4ios.hh:57
G4GLOB_DLL std::ostream G4cout
double z() const
double x() const
double mag2() const
double y() const
double mag() const
G4double GetMass() const
const G4ThreeVector & GetMomentumDirection() const
G4double GetCharge() const
G4double GetKineticEnergy() const
G4double GetProperTime() const
G4double GetMagneticMoment() const
const G4ThreeVector & GetPolarization() const
void SetElement(G4int anIndex, Type *anElement)
Definition: G4FastVector.hh:72
virtual G4Step * UpdateStepForAtRest(G4Step *Step)
void AddSecondary(G4Track *aSecondary)
G4bool operator==(const G4ParticleChange &right) const
G4ThreeVector CalcMomentum(G4double energy, G4ThreeVector direction, G4double mass) const
virtual void DumpInfo() const
virtual ~G4ParticleChange()
G4ThreeVector thePositionChange
G4ThreeVector theMomentumDirectionChange
G4Step * UpdateStepInfo(G4Step *Step)
G4double theProperTimeChange
virtual G4Step * UpdateStepForPostStep(G4Step *Step)
virtual G4bool CheckIt(const G4Track &)
G4ThreeVector thePolarizationChange
virtual G4Step * UpdateStepForAlongStep(G4Step *Step)
G4double theMagneticMomentChange
G4bool operator!=(const G4ParticleChange &right) const
virtual void Initialize(const G4Track &)
const G4Track * theCurrentTrack
G4ParticleChange & operator=(const G4ParticleChange &right)
G4double GetGlobalTime(G4double timeDelay=0.0) const
const G4String & GetParticleName() const
void AddPolarization(const G4ThreeVector &aValue)
void SetLocalTime(const G4double aValue)
void SetMagneticMoment(G4double value)
void SetKineticEnergy(const G4double aValue)
void SetWeight(G4double aValue)
void SetMass(G4double value)
void SetCharge(G4double value)
G4double GetProperTime() const
void AddProperTime(const G4double aValue)
void SetVelocity(G4double v)
void AddPosition(const G4ThreeVector &aValue)
G4ThreeVector GetMomentum() const
const G4ThreeVector & GetPosition() const
void SetProperTime(const G4double aValue)
void AddGlobalTime(const G4double aValue)
void SetPosition(const G4ThreeVector &aValue)
const G4ThreeVector & GetPolarization() const
G4double GetKineticEnergy() const
void AddLocalTime(const G4double aValue)
void SetMomentumDirection(const G4ThreeVector &aValue)
void SetPolarization(const G4ThreeVector &aValue)
Definition: G4Step.hh:62
G4Track * GetTrack() const
G4StepPoint * GetPreStepPoint() const
G4StepPoint * GetPostStepPoint() const
G4double GetVelocity() const
const G4ThreeVector & GetPosition() const
void SetTouchableHandle(const G4TouchableHandle &apValue)
G4double GetGlobalTime() const
G4double GetProperTime() const
G4double GetLocalTime() const
G4ParticleDefinition * GetDefinition() const
const G4DynamicParticle * GetDynamicParticle() const
const G4TouchableHandle & GetTouchableHandle() const
G4double GetKineticEnergy() const
G4double CalculateVelocity() const
void SetKineticEnergy(const G4double aValue)
void SetGoodForTrackingFlag(G4bool value=true)
virtual G4bool CheckIt(const G4Track &)
static const G4double accuracyForException
G4TrackStatus theStatusChange
G4TrackFastVector * theListOfSecondaries
G4SteppingControl theSteppingControlFlag
static const G4double accuracyForWarning
virtual void Initialize(const G4Track &)
void AddSecondary(G4Track *aSecondary)
virtual void DumpInfo() const
G4TrackStatus GetTrackStatus() const
#define G4ThreadLocal
Definition: tls.hh:77
#define ns
Definition: xmlparse.cc:614